Convolution layer visualisation error (missing previous layer metadata).]

I am trying to visualize the convolution layer output to see that how the model is learning from the image. But during visualization, it shows the error as follows. The model trained perfectly, also return a true value for the testing data but could not visualize the convolution layer.

The Model

model = tf.keras.models.Sequential([
    # first convolution
    tf.keras.layers.Conv2D(16, (3, 3), activation=tf.nn.relu, input_shape=(300, 300, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # second convolution
    tf.keras.layers.Conv2D(32, (3, 3), activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(2, 2),
    # third convolution
    tf.keras.layers.Conv2D(64, (3, 3), activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(2, 2),
    # fourth convolution
    tf.keras.layers.Conv2D(64, (3, 3), activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(2, 2),
    # fifth convolution
    tf.keras.layers.Conv2D(64, (3, 3), activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(2, 2),
    # flatten the results to feed into a DNN
    tf.keras.layers.Flatten(),
    # hidden layers
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    # output layer
    tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
])

The visualization code

for layer_name, feature_map in zip(layer_names, successive_feature_maps):
if len(feature_map.shape) == 4:
    # Just do this for the conv / max pool layers, not the fully-connected layers
    n_features = feature_map.shape[-1]  # number of features in feature map
    # The feature map has shape (1, size, size, n_features)
    size = feature_map.shape[1]
    # We will tile our images in this matrix
    display_grid = np.zeros((size, size * n_features))
    for i in range(n_features):
        # Postprocessor the feature to make it visually palatable
        x = feature_map[0, :, :, i]
        x -= x.mean()
        x /= x.std()
        x *= 64
        x += 128
        x = np.clip(x, 0, 255).astype('uint8')
        # We'll tile each filter into this big horizontal grid
        display_grid[:, i * size: (i + 1) * size] = x
        # Display the grid
    scale = 20. / n_features
    plt.figure(figsize=(scale * n_features, scale))
    plt.title(layer_name)
    plt.grid(False)
    plt.imshow(display_grid, aspect='auto', cmap='viridis')

The Error is as follow:

Traceback (most recent call last): File “dataVisualization.py”, line 51, in visualization_model = tf.keras.models.Model(inputs=model.layers, outputs=successive_outputs) File “/home/vedantdave77/PycharmProjects/HorseVsHuman/venv/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py”, line 517, in _method_wrapper result = method(self, *args, **kwargs) File “/home/vedantdave77/PycharmProjects/HorseVsHuman/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py”, line 120, in init self._init_graph_network(inputs, outputs) File “/home/vedantdave77/PycharmProjects/HorseVsHuman/venv/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py”, line 517, in _method_wrapper result = method(self, *args, **kwargs) File “/home/vedantdave77/PycharmProjects/HorseVsHuman/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py”, line 157, in _init_graph_network self._validate_graph_inputs_and_outputs() File “/home/vedantdave77/PycharmProjects/HorseVsHuman/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py”, line 688, in _validate_graph_inputs_and_outputs raise ValueError(‘Input tensors to a ‘ + cls_name + ‘ ‘ + ValueError: Input tensors to a Functional must come from tf.keras.Input. Received: <tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7f3db89184c0> (missing previous layer metadata).

How to solve it? Thank you in advance!

Answer

In your code:

tf.keras.models.Model(inputs=model.layers, outputs=successive_outputs)

as the error says, you have to pass tf.keras.Input to the keras Model inputs parameter, not model.layers.

Try something like:

tf.keras.models.Model(inputs=model.inputs, outputs=model.layers)

Leave a Reply

Your email address will not be published. Required fields are marked *