'Trying to get output of each layer while predicting test image, however getting an error Input tensors to a Functional must come from `tf.keras.Input`

I am trying to do Image Recognition in Python with TensorFlow and Keras. Please look at my code in the link below which I provided as I was facing another issue for the same code which is fixed now.

getting error while predicting a test image - cannot reshape array of size

I followed the post Keras, How to get the output of each layer? to get output of each layer and used code below

from keras import backend as K 
inp = model.input                                           # input placeholder
outputs = [layer.output for layer in model.layers[:12]]        # all layer outputs except first (input) layer
functor = K.function([inp, K.learning_phase()], outputs )   # evaluation function

# Testing
test = numpy.random.random(input_shape)[np.newaxis,...]
layer_outs = functor([test, 1.])
print (layer_outs)

However, I am getting below error:

ValueError: Input tensors to a Functional must come from `tf.keras.Input`. Received: 0 (missing previous layer metadata).

Can someone please help to get output of each layer for the image that I am doing the prediction against that is a new image and not part of the images the network was trained on?



Solution 1:[1]

If you could define your model as below, then you will not get the above specific error:

model = Sequential([
                    tf.keras.Input(shape=(X_train.shape[1:])),
                    Conv2D(32, (3, 3), padding='same', activation='relu'),
                    Conv2D(32, (3, 3),  activation='relu', padding='same'),
                    Dropout(0.2),
                    BatchNormalization(),
                    Conv2D(64, (3, 3), padding='same', name='test1', activation='relu'),
                    MaxPooling2D(pool_size=(2, 2)),
                    Dropout(0.2),
                    BatchNormalization(),
                    Conv2D(64, (3, 3), padding='same', name='test2', activation='relu'),
                    MaxPooling2D(pool_size=(2, 2)),
                    Dropout(0.2),
                    BatchNormalization(),
                    Conv2D(128, (3, 3), padding='same', name='test3',activation='relu'),
                    Dropout(0.2),
                    BatchNormalization(),
                    Flatten(),
                    Dropout(0.2),
                    Dense(256, kernel_constraint=maxnorm(3),activation='relu'),
                    Dropout(0.2),
                    BatchNormalization(),
                    Dense(128, kernel_constraint=maxnorm(3),activation='relu'),
                    Dropout(0.2),
                    BatchNormalization(),
                    Dense(class_num,activation='softmax')
])

Now, to get output values of each defined layer from the model, Please check this:

from tensorflow.keras import backend as K

for index, layer in enumerate(model.layers):
    func = K.function([model.get_layer(index=0).input], layer.output)
    layerOutput = func([X_test]) 
    print(layerOutput.shape) # to check each layer output, remove .shape

Output:

(10000, 28, 28, 32)
(10000, 28, 28, 32)
(10000, 28, 28, 32)
(10000, 28, 28, 32)
(10000, 28, 28, 64)
(10000, 14, 14, 64)
(10000, 14, 14, 64)
(10000, 14, 14, 64)
(10000, 14, 14, 64)
(10000, 7, 7, 64)
(10000, 7, 7, 64)
(10000, 7, 7, 64)
(10000, 7, 7, 128)
(10000, 7, 7, 128)
(10000, 7, 7, 128)
(10000, 6272)
(10000, 6272)
(10000, 256)
(10000, 256)
(10000, 256)
(10000, 128)
(10000, 128)
(10000, 128)
(10000, 10)

To extract one specific layer's output value, you can use below code:

feature_extractor = keras.Model(
    inputs=model.inputs,
    outputs=model.get_layer(name="test1").output)

features = feature_extractor(X_test)
features.shape
features

Output:

TensorShape([10000, 28, 28, 64])

<tf.Tensor: shape=(10000, 28, 28, 64), dtype=float32, numpy=
array([[[[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ..
.
.
.

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 TFer2