Hi guys,
I already completed the hw of Transfer_learning_with_MobileNet_v1
. But I still have some questions. Thanks for your time in advance.
My understanding: to use transfer learning, I download a premade NN work (assume 10 layers total), I just modify the last 2 layers (fully connected layers) for my new target. This also means, I only train the last 2 modified layers. The original first 8 layers are “frozen”. If my understanding is correct. My questions are below:
Q1. In the section of Exercise 2 - alpaca_model
, since we have
def alpaca_model(image_shape=IMG_SIZE, data_augmentation=data_augmenter()):
input_shape = image_shape + (3,)
### START CODE HERE
base_model = tf.keras.applications.MobileNetV2(input_shape=,
include_top=, # <== Important!!!!
weights='') # 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 = Input...
# apply data augmentation to the inputs
x = 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
x = base_model...
# add the new Binary classification layers
# use global avg pooling to summarize the info in each channel
x = GlobalAveragePooling2D...
# include dropout with probability of 0.2 to avoid overfitting
x = Dropout ...
# use a prediction layer with one neuron (as a binary classifier only needs one)
outputs = Dense...
### END CODE HERE
model = tf.keras.Model(inputs, outputs)
return model
Then we use this model to train
initial_epochs = 5
history = model2.fit(train_dataset, validation_data=validation_dataset, epochs=initial_epochs)
My question is, you already have base_model.trainable = False.
What are we training here? My understanding here is, this whole NN is NOT trainable, even the newly added layers. Is this right or wrong?
After this section, we start to do Fine-tuning. My understanding here is, I need to unfreeze only the newly added fully connected layers, and train them only (layers before those newly added layers are still frozen) .
I want to double-check my understanding of the code below:
base_model = model2.layers[4]
base_model.trainable = True
Q2. what’s meaning of trainable = True for layers[4] ?
# Freeze all the layers before the `fine_tune_at` layer
for layer in base_model.layers[:fine_tune_at]:
layer.trainable = True
Q3. Freeze or un freeze layers ? The running index is layer, and it runs from 1st to fine_tune_at, and the trainable = True. my understanding of this code is, un-freeze the layers from 1st to fine_tune_at layers. But I need to un freeze the layers after fine_tune_at, right?
I do aware I need to improve my python skills Thanks for your time again.