Week 2: Transfer Learning with MobileNet (alpaca)

Hi,

I am stuck on creating the alpaca classifier and would appreciate any help.

Here is my error:

AttributeError Traceback (most recent call last)
in
----> 1 model2 = alpaca_model(IMG_SIZE, data_augmentation)

in alpaca_model(image_shape, data_augmentation)
43 prediction_layer = tf.keras.layers.Dense(1, activation=‘sigmoid’)
44
—> 45 outputs = prediction_layer(x)
46 model = tf.keras.Model(inputs, outputs)
47

AttributeError: ‘Dropout’ object has no attribute ‘shape’

Here is a snippet of my code:

# data preprocessing using the same weights the model was trained on
x = tf.keras.applications.mobilenet_v2.preprocess_input

# set training to False to avoid keeping track of statistics in the batch norm layer
base_model.training = False

# Add the new Binary classification layers
# use global avg pooling to summarize the info in each channel
x = tf.keras.layers.GlobalAveragePooling2D()
x = tf.keras.layers.Dropout(rate = 0.2)

prediction_layer = tf.keras.layers.Dense(1, activation='sigmoid')

outputs = prediction_layer(x)
model = tf.keras.Model(inputs, outputs)

I am not sure how the global average and dropout lines are working, since I am not explicitly telling Python to use x (the previous layer) as an input.

I believe the error has to do with the GlobalAveragePooling2D and the Dropout lines, but everytime I try to put an input in either of those 2 layers, I get an error on that respective line. In the way it is now, I get an error on the “outputs” line.

I would appreciate any advice you can give me.

Thanks!

Hi,

You need to pass in a parameter/input for the GlobalAveragePooling2D as such:
x = tf.keras.layers.GlobalAveragePooling2D()(x) . FYI, Same error for the Dropout layer.

Also, the choice of activation should be linear.

Hi,

Thank you for your quick reply.

Yes, I have made the adjustments, but as I pointed out, now the error points to the GlobalAveragePooling2D line:

AttributeError Traceback (most recent call last)
in
----> 1 model2 = alpaca_model(IMG_SIZE, data_augmentation)

in alpaca_model(image_shape, data_augmentation)
36 # Add the new Binary classification layers
37 # use global avg pooling to summarize the info in each channel
—> 38 x = tf.keras.layers.GlobalAveragePooling2D()(x)
39 x = tf.keras.layers.Dropout(rate = 0.2)(x)
40

AttributeError: ‘function’ object has no attribute ‘shape’

Also, I changed the activation to ‘linear’.
Could the issue be how I am setting ‘training’ in the base_model to False?

I think you might missed passing in the preprocessed input to the model. As a result, the object has a type issue.

Yes, I corrected the preprocessing and the base model training lines and it works now!

Thanks you very much!

2 Likes

Error- ValueError: Input 0 of layer sequential_7 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 160, 160]

I am trying to figure out the error from long time, but I couldn’t. Can you please help me with the error? @DongdongSun

Test failed
Expected value

[‘Functional’, (None, 5, 5, 1280), 2257984]

does not match the input value:

[‘Functional’, (None, None, None, 1280), 2257984]

AssertionError Traceback (most recent call last)
in
10 [‘Dense’, (None, 1), 1281, ‘linear’]] #linear is the default activation
11
—> 12 comparator(summary(model2), alpaca_summary)
13
14 for layer in summary(model2):

~/work/release/W2A2/test_utils.py in comparator(learner, instructor)
19 “\n\n does not match the input value: \n\n”,
20 colored(f"{a}", “red”))
—> 21 raise AssertionError(“Error in test”)
22 print(colored(“All tests passed!”, “green”))
23

AssertionError: Error in test

@Sumanth Same problem! how can you fix it??

1 Like

same problem, I’m not quite sure where exactly it is going wrong

The problem is with the inputs, it should be
inputs = tf.keras.Input(shape=input_shape)

3 Likes

I figured it out, I didn’t update input_shape in tf.keras.applications.MobileNetV2

Hey everyone, just a reminder, sharing any snippet of solution code publicly is against the code of honour. Kindly refrain from doing so. Thanks.

Sorry, I forgot this detail u want me to delete my comment above?

Yes please. Thank you.

Hi, I had a same problem, but when I changed “outputs” description, it worked, even this is out of the coding area by students…

Means instead of using “outputs = prediction_layer(x)”, I changed it to “outputs = tf.keras.layers.Dense(1)(x)”, then worked.

hey can you tell me the changes you made in.

Same error. Spent hours looking for this bug :rofl: