Question for week2 assignment: alpaca_model

I don’t know where is incorrect. below is my code.


def alpaca_model(image_shape=IMG_SIZE, data_augmentation=data_augmenter()):
‘’’ Define a tf.keras model for binary classification out of the MobileNetV2 model
image_shape – Image width and height
data_augmentation – data augmentation function

input_shape = image_shape + (3,)

base_model = tf.keras.applications.MobileNetV2(input_shape=input_shape,
                                               include_top=False, # <== Important!!!!
                                               weights='imagenet') # From imageNet

# Freeze the base model by making it non trainable
base_model.trainable = False

# create the input layer (Same as the imageNetv2 input size)
inputs = tf.keras.Input(shape=input_shape) 

# apply data augmentation to the inputs
x = data_augmentation

# data preprocessing using the same weights the model was trained on
x = preprocess_input()

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

# Add the new Binary classification layers
# use global avg pooling to summarize the info in each channel
x = tf.keras.layers.GlobalAveragePooling2D()
#include dropout with probability of 0.2 to avoid overfitting
x = tf.keras.layers.Dropout(rate = 0.2)
# create a prediction layer with one neuron (as a classifier only needs one)
prediction_layer = tf.keras.layers.Dense(1, activation='linear')

prediction_layer = tf.keras.layers.Dense(1, activation=‘linear’)


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

return model

Hey, you’ll need to follow the functional API for this exercise, as you used in the Week 1’s second assignment once, and within the resnet assignment. So, you’ll need to pass the tensors through the layers as you initialise them.

Remember that data_augmentation here will need to have the inputs tensor pass through it, same for preprocess_input. Though you will not need to initialise them here as they have already been initialised before.

Keeping these things in mind should be enough to get your function working.

Could you please delete your code once you fix your issue? :slight_smile:

1 Like

Thank you for reply. However, the answer is a little ambiguous.
Is it correct to use “x = preprocess_input()” ?
There is not enough explanation.

Yeah I meant for the answer to be ambiguous. It’s much easier to remember things when we figure them out ourselves :slightly_smiling_face:. I’ll help you through this though if you need clarifications, just want you to experiment things out as you go.

In your case here, if you’ve not passed any inputs to the preprocess_input function, it won’t really preprocess anything, will it? Same for data_augmentation function, it will need input to produce augmented inputs, won’t it?

1 Like

I passed the exam. Thank you very much.

Glad you were able to pass it. Good luck for the rest of the course!

1 Like