daveboat
daveboat

Reputation: 187

Accessing intermediate layers from a loaded saved_model in Tensorflow 2.0

When using SavedModels in Tensorflow 2.0, is it possible to access activations from intermediate layers? For example, with one of the models here: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md, I can run, for example,

model = tf.saved_model.load('faster_rcnn_inception_v2_coco_2018_01_28/saved_model').signatures['serving_default']
outputs = model(input_tensor)

to get output predictions and bounding boxes. I would like to be able to access layers other than the outputs, but there doesn't seem to be any documentation for Tensorflow 2.0 on how to do this. The downloaded models also include checkpoint files, but there doesn't seem to be very good documentation for how to load those with Tensorflow 2.0 either...

Upvotes: 6

Views: 1843

Answers (1)

Prasad
Prasad

Reputation: 6034

If you are generating saved models using TensorFlow 2.0, it is possible to extract individual layers. But the model which you are referring to has been saved in TensorFlow 1.x. With TF 1.x saved models, you cannot individually extract layers.

Here is an example on how you can extract layers from a saved model in TensorFlow 2.0

import tensorflow as tf
import numpy as np

model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(100,)),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

# Compile and fit the model

model.save('save_model', save_format='tf')

Then load the model as shown.

model = tf.keras.models.load_model('save_model')
layer1 = model.get_layer(index=1)

Upvotes: 2

Related Questions