Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all layers' activations for a specific input for Tensorflow Hub modules?

I am new to Tensorflow Hub. I want to use I3D module and finetune this network to another dataset and I need to get the last hidden layer as well as some other layers' outputs. I was wondering if there is a way to get the other layers' activations. The only signature provided for I3D is just "default". I think there should be a way to get the output of all layers easily with Tensorflow Hub modules.

import tensorflow_hub as hub
module = hub.Module("https://tfhub.dev/deepmind/i3d-kinetics-600/1", trainable=False)
logits = module(inp)

This will give me the final layer output. How can I get other layer's outputs, for example, the second convolution layer's output?

like image 870
Siavash Avatar asked Sep 12 '18 22:09

Siavash


People also ask

What does the hub Keraslayer do?

This layer wraps a callable object for use as a Keras layer. The callable object can be passed directly, or be specified by a Python string with a handle that gets passed to hub. load() . This is the preferred API to load a TF2-style SavedModel from TF Hub into a Keras model.

What is hub layer in TensorFlow?

TensorFlow Hub is a repository of trained machine learning models ready for fine-tuning and deployable anywhere. Reuse trained models like BERT and Faster R-CNN with just a few lines of code.

What's the URL of the TensorFlow hub site containing lots of models?

TF Hub models have versioned url https://tfhub.dev/<publisher>/<model_name>/<version> and unversioned url https://tfhub.dev/<publisher>/<model_name> that resolves to the latest version of the model.


2 Answers

https://tfhub.dev/deepmind/i3d-kinetics-400/1 (and also the *-600 version) happen to export only the final layer, so there is no properly supported way to get the other layers. (That said, you can always experiment by inspecting the graph and selecting tensors by name, but this has a real risk to stop working with newer module or library versions.)

like image 94
arnoegw Avatar answered Oct 21 '22 14:10

arnoegw


You can get the other layers by name. Using Inception-v3 as an example:

import tensorflow_hub as hub

module = hub.Module("https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1")
logits = module(inp)

logits contains all the models layers. You can view them by calling items():

print(logits.items())

This outputs a dictionary containing all the layers in the graph, a few of which are shown below:

dict_items([
('InceptionV3/Mixed_6c', <tf.Tensor 'module_2_apply_image_feature_vector/InceptionV3/InceptionV3/Mixed_6c/concat:0' shape=(1, 17, 17, 768) dtype=float32>), 
('InceptionV3/Mixed_6d', <tf.Tensor 'module_2_apply_image_feature_vector/InceptionV3/InceptionV3/Mixed_6d/concat:0' shape=(1, 17, 17, 768) dtype=float32>), 
('InceptionV3/Mixed_6e', <tf.Tensor 'module_2_apply_image_feature_vector/InceptionV3/InceptionV3/Mixed_6e/concat:0' shape=(1, 17, 17, 768) dtype=float32>),
('default', <tf.Tensor 'module_2_apply_image_feature_vector/hub_output/feature_vector/SpatialSqueeze:0' shape=(1, 2048) dtype=float32>),     
('InceptionV3/MaxPool_5a_3x3', <tf.Tensor 'module_2_apply_image_feature_vector/InceptionV3/InceptionV3/MaxPool_5a_3x3/MaxPool:0' shape=(1, 35, 35, 192) dtype=float32>)])

Usually to get the last layer, you would use default:

sess.run(logits['default'])

But you can just as easily get other layers using their name:

sess.run(logits['InceptionV3/MaxPool_5a_3x3'])
like image 26
ysbecca Avatar answered Oct 21 '22 15:10

ysbecca