Hi @Alireza_Saei ! I’ve been working around with this for a while without success… My shame.
I had also reshape the images sets:
Reshaping the images
training_images = training_images.reshape(60000, 28, 28, 1)
test_images = test_images.reshape(10000, 28, 28, 1)
My model was:
Define the model
model = tf.keras.models.Sequential([
Adding the shape of the input
tf.keras.Input((1, 28, 28, 1)),
Add convolutions and max pooling
tf.keras.layers.Conv2D(32, (3,3), activation=‘relu’),
tf.keras.layers.MaxPooling2D(2, 2),
…
But it rises the error:
ValueError: Kernel shape must have the same length as input, but received kernel of shape (3, 3, 1, 32) and input of shape (None, 1, 28, 28, 1).
When I change the the line tf.keras.Input((1, 28, 28, 1)) to tf.keras.Input((28, 28, 1)) the model.summary() is:
Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ conv2d (Conv2D) │ (None, 26, 26, 32) │ 320 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ max_pooling2d (MaxPooling2D) │ (None, 13, 13, 32) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_1 (Conv2D) │ (None, 11, 11, 32) │ 9,248 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ max_pooling2d_1 (MaxPooling2D) │ (None, 5, 5, 32) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ flatten (Flatten) │ (None, 800) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense (Dense) │ (None, 128) │ 102,528 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_1 (Dense) │ (None, 10) │ 1,290 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 113,386 (442.91 KB)
Trainable params: 113,386 (442.91 KB)
Non-trainable params: 0 (0.00 B)
At line 64, as you sad I coded:
model(tf.keras.Input((1, 28, 28, 1)))
But this rises the following error at this line:
ValueError: Exception encountered when calling Sequential.call().
Input 0 of layer “functional” is incompatible with the layer: expected shape=(None, 28, 28, 1), found shape=(None, 1, 28, 28)
Arguments received by Sequential.call():
• args=(‘<KerasTensor shape=(None, 1, 28, 28, 1), dtype=float32, sparse=None, name=keras_tensor_8>’,)
• kwargs={‘mask’: ‘None’}
When I comment this line it comes to an error at:
activation_model = tf.keras.models.Model(inputs = model.input, outputs = layer_outputs)
^^^^^^^^^^^
File “C:\Users\cesar\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\ops\operation.py”, line 228, in input
return self._get_node_attribute_at_index(0, “input_tensors”, “input”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
…
ValueError: The layer sequential has never been called and thus has no defined input.
Ok, next I decided to adjust the call to:
model(tf.keras.Input((28, 28, 1)))
It pass through the activation.model(). My code follows:
for x in range(0,4):
my_image=test_images[FIRST_IMAGE]
print(f’\nmy_image.shape: {my_image.shape}')
f1 = activation_model.predict(my_image.reshape(1, 28, 28, 1))
At terminal it outputs:
my_image.shape: (28, 28, 1)
…
f1 = activation_model.predict(my_image.reshape(1, 28, 28, 1))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
output_tensors.append(tensor_dict[id(x)])
~~~~~~~~~~~^^^^^^^
KeyError: ‘Exception encountered when calling Functional.call().\n\n\x1b[1m2619439529424\x1b[0m\n\nArguments received by Functional.call():\n • inputs=tf.Tensor(shape=(1, 28, 28, 1), dtype=float32)\n • training=False\n • mask=None’
No matter I change my my_image.reshape(1, 28, 28, 1)) to my_image.reshape(28, 28, 1)) as a parameter the error persists the same.
By the way, I’m using Python version: 2.16.1 and Tensorflow version: 2.16.1
The complete code is:
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import models
# import numpy as np
# Printing the versions of python and tensorflow:
print(f'Python version: {tf.__version__}')
print(f'Tensorflow version: {tf.__version__}')
# Load the Fashion MNIST dataset
fmnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = fmnist.load_data()
# Reshaping the images
training_images = training_images.reshape(60000, 28, 28, 1)
test_images = test_images.reshape(10000, 28, 28, 1)
# Normalize the pixel values
training_images = training_images / 255.0
test_images = test_images / 255.0
# Training images shape
print(f'Training images shape: {training_images.shape}')
# Define the model
model = tf.keras.models.Sequential([
# Adding the shape of the input
tf.keras.Input((28, 28, 1)),
# Add convolutions and max pooling
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),#, input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# Add the same layers as before
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
# Print the model summary
model.summary()
# Use same settings
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
print(f'\nMODEL TRAINING:')
model.fit(training_images, training_labels, epochs=1)
# Evaluate on the test set
print(f'\nMODEL EVALUATION:')
test_loss = model.evaluate(test_images, test_labels)
print(test_labels[:100])
model(tf.keras.Input((28, 28, 1)))
# depth = model.layers[0].output.shape
f, axarr = plt.subplots(3,4)
FIRST_IMAGE = 0
SECOND_IMAGE = 23
THIRD_IMAGE = 28
CONVOLUTION_NUMBER = 1
layer_outputs = [layer.output for layer in model.layers]
activation_model = tf.keras.models.Model(inputs = model.input, outputs = layer_outputs)
for x in range(0,4):
my_image=test_images[FIRST_IMAGE]
print(f'\nmy_image.shape: {my_image.shape}')
f1 = activation_model.predict(my_image.reshape(28, 28, 1))[x]
axarr[0,x].imshow(f1[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
axarr[0,x].grid(False)
f2 = activation_model.predict(test_images[SECOND_IMAGE].reshape(1, 28, 28, 1))[x]
axarr[1,x].imshow(f2[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
axarr[1,x].grid(False)
f3 = activation_model.predict(test_images[THIRD_IMAGE].reshape(1, 28, 28, 1))[x]
axarr[2,x].imshow(f3[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
axarr[2,x].grid(False)