Coursera

Week 2

TF Hub

Website: tfhub.dev (soon to be replaced with Kaggle models)

Transfer Learning with TF Hub

Problem domains

Installation

pip install tensorflow_hub

And load

import tensorflow_hub as hub


MODULE_HANDLE = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4"

module = hub.load(MODULE_HANDLE) # Load the MobileNet image classification module

Inference

images = ... # batches of images

predictions = tf.nn.softmax(module(images))

Using a module with Keras

MODULE_HANDLE = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4"

OUTPUT_SHAPE = 1001
IMAGE_SIZE = (224, 224)

model = tf.keras.Sequential([
	hub.KerasLayer(MODULE_HANDLE, 
				output_shape=[OUTPUT_SHAPE],
				input_shape=IMAGE_SIZE + (3,)),
	tf.keras.layers.Activation("softmax"),
])

images = ... # batches of images
predictions = model.predict(images)

Using a feature vector

MODULE_HANDLE = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4"

FV_SIZE = 1280
IMAGE_SIZE = (224, 224)
            

model = tf.keras.Sequential([
	hub.KerasLayer(MODULE_HANDLE, 
				output_shape=[FV_SIZE],
				input_shape=IMAGE_SIZE + (3,)),
	tf.keras.layers.Dense(NUM_CLASSES, activation="softmax"),
])

images = ... # batches of images
predictions = model.predict(images)

Module storage

Initialy download and save in a temp folder.

Saving a module for local use:

MODULE_HANDLE = "https://tfhub.dev/...?tf-hub-format=compressed"

!wget $MODULE_HANDLE

# Untar the tarball and load it with hub
hub_module = hub.load("path/to/saved_model")

Inspecting the hub module

SAVED_MODEL_DIR = ...
model = tf.saved_model.load(SAVED_MODEL_DIR, tags="serve")

images = ... # batch of images
predictions = model.predict(images)

Relocating TF Hub modules

import os

os.environ["TFHUB_CACHE_DIR"] = "/home/hub_cache_dir"

# Or

export TFHUB_CACHE_DIR="/home/hub_cache_dir"

Text-based models

Dataset: IMDB Reviews

Word embeddings:

Image classification

Dataset: cats and dogs Model: MobileNet

Quiz

Question Answer
1. What’s the URL of the TensorFlow Hub site containing lots of models? Tfhub.dev
2. What are the primary problem domains for which you can find models on hub? All of the above
3. How do you install the Hub API in Python? pip install tensorflow_hub
4. When I have the URL of a model in MODULE_HANDLE, what’s the API to load it? model = hub.load(MODULE_HANDLE)
5. In a transfer learning scenario, and a model was created using keras, how can you get the layer that you can freeze, and retrain everything beneath? hub.KerasLayer(...)
6. You’ve taken a keras layer from a hosted model in hub and called it ‘foo’. What’s the syntax to then build a DNN with foo as the top layer(s)? model = tf.keras.Sequential([foo, Dense(2, activation='softmax')])
7. If you want to use a model in TensorFlow Lite, how can you do it with Hub? All of the above
8. You download an embedding from tensorflow hub and want to retrain it, what do you do? Use the trainable=true parameter in the KerasLayer call
9. If you want to get a JavaScript model from Hub, what’s the easiest way to do it? In TF.js use the loadGraphModel method and pass it the model url
10. You load a layer from hub using the KerasLayers method, and then add layers beneath it. When you do model.summary(), what will you see? A KerasLayer followed by your layers