The notebook is tested with Google Colab sandbox.
!pip3 install -q tf-models-nightly
# Fix Colab default opencv problem
!pip3 install -q opencv-python-headless==4.1.2.30
## Colab environment setup. To use a stable TF release version
## because of the possible breakage in tf-nightly.
# !pip3 install -U numpy>=1.20
# !pip3 install -q tensorflow==2.8.0
|████████████████████████████████| 21.8 MB 1.6 MB/s
import numpy as np
import tensorflow as tf
print(np.__version__)
print(tf.__version__)
import tensorflow_models as tfm
1.21.6 2.10.0-dev20220420
Note: As the TensorFlow Models (NLP + Vision) 2.9 release which is tested for this notebook, we partially exported selected modules but the APIs are not stable. Also be aware that, the modeling libraries are advancing very fast, so we generally don't guarantee compatibility between versions.
print("Top-level modules: ", dir(tfm))
print("NLP modules: ", dir(tfm.nlp))
print("Vision modules: ", dir(tfm.vision))
Top-level modules: ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'core', 'hyperparams', 'nlp', 'optimization', 'utils', 'vision'] NLP modules: ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'encoders', 'layers', 'losses', 'models', 'networks', 'ops', 'serving_modules', 'tasks'] Vision modules: ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'anchor', 'anchor_generator', 'augment', 'backbones', 'box_matcher', 'box_ops', 'classification_model', 'configs', 'decoders', 'factory', 'factory_3d', 'heads', 'iou_similarity', 'layers', 'mask_ops', 'maskrcnn_model', 'nms', 'preprocess_ops', 'preprocess_ops_3d', 'retinanet_model', 'sampling_ops', 'segmentation_model', 'spatial_transform_ops', 'target_gather', 'video_classification_model']
encoder_block = tfm.nlp.layers.TransformerEncoderBlock(
num_attention_heads=2, inner_dim=10, inner_activation='relu')
batch, length, hidden_size = 2, 3, 4
qkv_inputs = tf.ones((batch, length, hidden_size), tf.float32)
attention_mask = None
outputs = encoder_block([qkv_inputs, attention_mask])
print(encoder_block.name)
print(outputs)
transformer_encoder_block_1 tf.Tensor( [[[-1.063648 1.4375787 -0.79198956 0.4180589 ] [-1.063648 1.4375787 -0.79198956 0.4180589 ] [-1.063648 1.4375787 -0.79198956 0.4180589 ]] [[-1.063648 1.4375787 -0.79198956 0.4180589 ] [-1.063648 1.4375787 -0.79198956 0.4180589 ] [-1.063648 1.4375787 -0.7919895 0.41805887]]], shape=(2, 3, 4), dtype=float32)
input_size = 128
filter_size_scale, block_repeats, resample_alpha, endpoints_num_filters, min_level, max_level = 0.65, 1, 0.5, 128, 4, 6
input_specs = tf.keras.layers.InputSpec(
shape=[None, input_size, input_size, 3])
model = tfm.vision.backbones.SpineNet(
input_specs=input_specs,
min_level=min_level,
max_level=max_level,
endpoints_num_filters=endpoints_num_filters,
resample_alpha=resample_alpha,
block_repeats=block_repeats,
filter_size_scale=filter_size_scale,
init_stochastic_depth_rate=0.2,
)
inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)
endpoints = model(inputs)
print(model.name)
print(endpoints)
WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing. WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing. WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing.
spine_net {'4': <KerasTensor: shape=(1, 8, 8, 128) dtype=float32 (created by layer 'spine_net')>, '5': <KerasTensor: shape=(1, 4, 4, 128) dtype=float32 (created by layer 'spine_net')>, '6': <KerasTensor: shape=(1, 2, 2, 128) dtype=float32 (created by layer 'spine_net')>}
import orbit
print("Orbit modules: ", dir(orbit))
Orbit modules: ['AbstractEvaluator', 'AbstractTrainer', 'Action', 'Controller', 'StandardEvaluator', 'StandardEvaluatorOptions', 'StandardTrainer', 'StandardTrainerOptions', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'actions', 'controller', 'runner', 'standard_runner', 'utils']