Home > Software design >  How to chang the output of the concrete function in Tensorflow 2.x?
How to chang the output of the concrete function in Tensorflow 2.x?

Time:06-10

I have trained a TensorFlow model and saved it to a local disk. when I loaded it and do inference, how can I get the output of the intermediate layer?

I use the example in the tutorial as a demo.

The model is:


class MyModel(Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.conv1 = Conv2D(32, 3, activation='relu')
    self.flatten = Flatten()
    self.d1 = Dense(128, activation='relu')
    self.d2 = Dense(10)

  def call(self, x):
    x = self.conv1(x)
    x = self.flatten(x)
    x = self.d1(x)
    return self.d2(x)

# Create an instance of the model
model = MyModel()

I save the model to local and load it in another place.

# save model to local
tf.saved_model.save(model, export_dir="./saved_model")

# load model from local
loaded = tf.saved_model.load("./saved_model")
concrete_fun = loaded.signatures["serving_default"]
# do reference
out = concrete_fun(tf.zeros((2, 28, 28, 1)))
out["output_1"].shape

As I know, the concrete function is unique to the input and output.

How can I get the output, weights and bias of intermediate layers, for example self.conv1?

CodePudding user response:

You can try running:

print([var for var in concrete_fun.trainable_variables])

to get your each layer's weights and biases. To access the graph of your model, you can run concrete_fun.graph. See here for more details.

To access the output of intermediate layers, it would be easiest to save the model like this:

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

and then load it:

model.save('your_model', save_format='tf')
model = tf.keras.models.load_model('your_model')
conv_layer = model.get_layer(index=0)
print(conv_layer(tf.random.normal((1, 28, 28, 1))).shape)

CodePudding user response:

You can access any layer in your model with the .get_layer() method (Model class). You just need the name of the layer:

conv1_layer = model.get_layer("name_of_the_layer")

From this you can access the weights (Layer class ):

conv1_layer.get_weights()

the output:

conv1_layer.output

and the bias (https://stackoverflow.com/a/42053507/13469674):

conv1_layer.get_weights()[1]
  • Related