Exercise 2 - convolutional_model - Error

Hi,
I can’t fix this error: “Input 0 of layer max_pooling2d_28 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [1, None, 64, 64, 8]”

Not sure what I have to change - can you give me a hint please?

This is how my code looks like:

GRADED FUNCTION: convolutional_model

def convolutional_model(input_shape):
“”"
Implements the forward propagation for the model:
CONV2D → RELU → MAXPOOL → CONV2D → RELU → MAXPOOL → FLATTEN → DENSE

Note that for simplicity and grading purposes, you'll hard-code some values
such as the stride and kernel (filter) sizes. 
Normally, functions should take these values as function parameters.

Arguments:
input_img -- input dataset, of shape (input_shape)

Returns:
model -- TF Keras model (object containing the information for the entire training process) 
"""

input_img = tf.keras.Input(shape=input_shape)
## CONV2D: 8 filters 4x4, stride of 1, padding 'SAME'
Z1 = tf.keras.layers.Conv2D(filters = 8, kernel_size = (4,4), strides=(1,1), padding = 'SAME')(input_img),

## RELU
A1 = tf.keras.layers.ReLU()(Z1),

## MAXPOOL: window 8x8, stride 8, padding 'SAME'
P1 = tf.keras.layers.MaxPool2D(pool_size=(8,8), strides=(8,8), padding='same')(A1),

## CONV2D: 16 filters 2x2, stride 1, padding 'SAME'
Z2 = tf.keras.layers.Conv2D(filters = 16, kernel_size = (2,2), strides=(1,1), padding = 'SAME')(P1),

## RELU
A2 = tf.keras.layers.ReLU()(Z2),

## MAXPOOL: window 4x4, stride 4, padding 'SAME'
P2 = tf.keras.layers.MaxPool2D(pool_size=(4, 4), strides=(4,4), padding='same')(A2),

## FLATTEN
F = tf.keras.layers.Flatten()(P2),

## Dense layer
## 6 neurons in output layer. Hint: one of the arguments should be "activation='softmax'" 
outputs = tf.keras.layers.Dense(units = 6, activation='softmax', name='fc')(F)


model = tf.keras.Model(inputs=input_img, outputs=outputs)
return model

conv_model = convolutional_model((64, 64, 3))
conv_model.compile(optimizer=‘adam’,
loss=‘categorical_crossentropy’,
metrics=[‘accuracy’])
conv_model.summary()

output = [[‘InputLayer’, [(None, 64, 64, 3)], 0],
[‘Conv2D’, (None, 64, 64, 8), 392, ‘same’, ‘linear’, ‘GlorotUniform’],
[‘ReLU’, (None, 64, 64, 8), 0],
[‘MaxPooling2D’, (None, 8, 8, 8), 0, (8, 8), (8, 8), ‘same’],
[‘Conv2D’, (None, 8, 8, 16), 528, ‘same’, ‘linear’, ‘GlorotUniform’],
[‘ReLU’, (None, 8, 8, 16), 0],
[‘MaxPooling2D’, (None, 2, 2, 16), 0, (4, 4), (4, 4), ‘same’],
[‘Flatten’, (None, 64), 0],
[‘Dense’, (None, 6), 390, ‘softmax’]]

comparator(summary(conv_model), output)

Hi man,

I think you need to remove the commas at the end of each command.

Good luck!

3 Likes

I agree with @Fouksi. The Functional model does not require commas at the ends of the lines of code.

The commas are only used when you’re using the Sequential model and you’re defining the layers as elements in a list.

1 Like