'Value Error: No such layer - Extracting the output of a tensorflow keras layer
I'm trying to extract the output of thelayer
in my autoencoder and have referenced this Keras documentation and this stackoverflow post so far. When I try to extract the output, I get the following error:
Traceback (most recent call last):
File "train.py", line 36, in <module>
outputs=autoencoder.get_layer(layer_name).output)
File "..Traceback (most recent call last):
File "train.py", line 36, in <module>
outputs=autoencoder.get_layer(layer_name).output)
File "..python3.6/site packages/tensorflow/python/keras/engine/network.py", line 567, in get_layer
raise ValueError('No such layer: ' + name)
ValueError: No such layer: thelayer
", line 567, in get_layer
raise ValueError('No such layer: ' + name)
ValueError: No such layer: thelayer
Code:
encoder_img = tf.keras.layers.Input(shape=(16,16,1), name="input")
x = tf.keras.layers.Conv2D(1024, 1, activation='relu',kernel_initializer=keras.initializers.RandomUniform)(encoder_img)
x = tf.keras.layers.MaxPooling2D(1)(x)
inputtothelayer = tf.keras.layers.Conv2D(512, 1, activation='relu')(x)
thelayer = tf.keras.layers.MaxPooling2D(1)(inputtothelayer)
bottleneck = tf.keras.layers.Conv2D(256, 3, activation='relu')(thelayer)
x = tf.keras.layers.Conv2DTranspose(512, 1, activation='relu')(bottleneck)
x = tf.keras.layers.UpSampling2D(1)(x)
x = tf.keras.layers.Conv2DTranspose(1024, 1, activation='relu')(x)
x = tf.keras.layers.UpSampling2D(1)(x)
decoder_output = tf.keras.layers.Conv2DTranspose(1, 3, activation='relu')(x)
autoencoder = tf.keras.Model(inputs=encoder_img,outputs=decoder_output, name='autoencoder')
autoencoder.fit(data, data,
epochs=1,
batch_size=512,
shuffle=True,)
layer_name = 'thelayer'
intermediate_layer_model = autoencoder(inputs=inputtothelayer, outputs=autoencoder.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)
print(intermediate_layer_model)
Solution 1:[1]
Change the following lines from:
thelayer = tf.keras.layers.MaxPooling2D(1)(inputtothelayer)
bottleneck = tf.keras.layers.Conv2D(256, 3, activation='relu')(thelayer)
to:
pool = tf.keras.layers.MaxPooling2D(1, name="thelayer")(inputtothelayer)
bottleneck = tf.keras.layers.Conv2D(256, 3, activation='relu')(pool)
If you want to retrieve the layer by name model.get_layer(layer_name)
, you should include the layer name in the name
attribute. Furthermore, if you want to obtain the output from the intermediate layer, instead of doing:
layer_name = 'thelayer'
intermediate_layer_model = autoencoder(inputs=inputtothelayer, outputs=autoencoder.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)
print(intermediate_layer_model)
Do the following:
layer_name = 'thelayer'
intermediate_layer_model = tf.keras.Model(inputs=encoder_img, outputs=autoencoder.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(np.random.rand(10,16,16,1))
print(intermediate_layer_model)
Note that I am creating a new tf.keras.Model
with the same tf.keras.layers.Input
, where the output is the intermediate_output
.
Solution 2:[2]
I had the same problem since I had changed the machine. it means the code was correct in the old machine but gave this error in the new machine.
solution> I have installed>
pip install tf-nightly
and
pip install keras
Then it worked properly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Enric Grau-Luque |
Solution 2 | Dharman |