Back to Models

Fix Colab default opencv problem

tensorflow_models/tensorflow_models_pypi.ipynb

2.20.02.2 KB
Original Source

Install Tensorflow-Models packages

The notebook is tested with Google Colab sandbox.

python
!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
python
import numpy as np
import tensorflow as tf
print(np.__version__)
print(tf.__version__)

import tensorflow_models as tfm

Check out modules

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.

python
print("Top-level modules: ", dir(tfm))
print("NLP modules: ", dir(tfm.nlp))
print("Vision modules: ", dir(tfm.vision))

Quick Examples

1. Use a tfm.nlp Keras layer

python
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)

2. Use a tfm.vision Backbone models

python
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)

3. Use Orbit package for advanced training loops

python
import orbit
print("Orbit modules: ", dir(orbit))