DLS 4, Week 2, Exercise 2. Can't get the functional layer dimensions right

Hi,
I’ll really appreciate your help, I’ve been working on this for days.
I can’t seem to find my error. In the alpaca_model method, I get an error in the test that my functional layer is (None, None, None, 1280) instead of (None, 5, 5, 1280).

I can’t seem to understand what I’m doing wrong. The shape of all the other layers seems to be OK.

(I wasn’t sure if I’m supposed to write my code or not so I didn’t. Happy to share it if it can help with the troubleshooting)

Hi, Fabian.

You’re right that we’re not supposed to post source code for the solutions (even if it’s not correct :smiley:). Usually we can figure out how to help without seeing the code, but there is a private way to share code (by DM) if we fail on the public help methods.

The first thing to do would be to show us the full error output that you get, not just that one line. Maybe that will shed more light.

Regards,
Paul

Ok, I just looked at that assignment again to refresh my memory. That functional layer there corresponds to the invocation of the base_model, right? So my guess is that you are passing the wrong input when you invoke the base_model. It should be the output of the previous layer, which is the “preprocess data” layer, right?

Yes.
I think that the code to write in the base_model line came written already with the initial code, the only thing we needed to do is set training to False.
In terms of the preprocessing step, the guideline include the following “data preprocessing using the same weights the model was trained on”. Does it mean I need to run preprocessing_input with more than x as a parameter?

I’m happy to share with you my code in a DM if that can be helpful

I forget what the template code looked like for the base_model step, but what you describe sounds right. You don’t need to do anything fancy with the preprocess step: it takes x as input and gives the processed x as output.

We can go to source code, but you skipped the step of showing us the full output. E.g. here’s what I get with correct code:

All tests passed!
['InputLayer', [(None, 160, 160, 3)], 0]
['Sequential', (None, 160, 160, 3), 0]
['TensorFlowOpLayer', [(None, 160, 160, 3)], 0]
['TensorFlowOpLayer', [(None, 160, 160, 3)], 0]
['Functional', (None, 5, 5, 1280), 2257984]
['GlobalAveragePooling2D', (None, 1280), 0]
['Dropout', (None, 1280), 0, 0.2]
['Dense', (None, 1), 1281, 'linear']

The point of that is I’m hoping we’ll see some clue in how your earlier layers look. If that doesn’t do it, then we can graduate to the DM method.

Thanks Paul!
Here’s the output I get for model2.summary()

Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 160, 160, 3)]     0         
_________________________________________________________________
sequential_1 (Sequential)    (None, 160, 160, 3)       0         
_________________________________________________________________
tf_op_layer_RealDiv (TensorF [(None, 160, 160, 3)]     0         
_________________________________________________________________
tf_op_layer_Sub (TensorFlowO [(None, 160, 160, 3)]     0         
_________________________________________________________________
mobilenetv2_1.00_None (Funct (None, None, None, 1280)  2257984   
_________________________________________________________________
global_average_pooling2d_1 ( (None, 1280)              0         
_________________________________________________________________
dropout (Dropout)            (None, 1280)              0         
_________________________________________________________________
dense (Dense)                (None, 1)                 1281      
=================================================================
Total params: 2,259,265
Trainable params: 1,281
Non-trainable params: 2,257,984


This is the error I get:

Test failed 
 Expected value 

 ['Functional', (None, 5, 5, 1280), 2257984] 

 does not match the input value: 

 ['Functional', (None, None, None, 1280), 2257984]

I edited your post to use the </> format to make your summary clearer to read. Here is what I get when I do

model2.summary()

with correct code:

Model: "functional_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 160, 160, 3)]     0         
_________________________________________________________________
sequential_1 (Sequential)    (None, 160, 160, 3)       0         
_________________________________________________________________
tf_op_layer_RealDiv (TensorF [(None, 160, 160, 3)]     0         
_________________________________________________________________
tf_op_layer_Sub (TensorFlowO [(None, 160, 160, 3)]     0         
_________________________________________________________________
mobilenetv2_1.00_160 (Functi (None, 5, 5, 1280)        2257984   
_________________________________________________________________
global_average_pooling2d_1 ( (None, 1280)              0         
_________________________________________________________________
dropout (Dropout)            (None, 1280)              0         
_________________________________________________________________
dense (Dense)                (None, 1)                 1281      
=================================================================
Total params: 2,259,265
Trainable params: 1,281
Non-trainable params: 2,257,984

Notice that what it says as the name of the functional layer is different than what it says in your case. Mine ends in _160 and yours ends in _None. Hmmmmm. Seems like that is a pretty good match for the differences in the output dimensions. So there must be either something wrong in how you defined base_model or how you invoked it. Note that the code that defines the base_model was just given to you in the template code. Are you sure you didn’t accidentally modify that code? Here’s what that cell looks like in my notebook:

IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=True,
                                               weights='imagenet')

OK, I found the mistake. Thanks Paul! input_shape was set to None :blush:

1 Like

I’m glad you found the issue! Note that the code I showed was part of the template and there should have been no need to modify it. It’s not against the rules to modify things outside the “YOUR CODE HERE” sections, meaning that the grader only cares about your answers not what the code looks like, but some care is required when “drawing outside the lines”. :nerd_face: