Transfer network architecture

We will be replacing the final two layers with fully connected layers that are more appropriate for our use case. Since our problem is binary classification, we will replace the output layer with a single neuron with sigmoid activation, as shown in the following code:

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer
predictions = Dense(1, activation='sigmoid')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

Note that we are using a GlobalAveragePooling2D layer here. This layer flattens the 4D output of the previous layer into a 2D layer, suitable for our fully connected layer by averaging. It's also possible to accomplish this when you load the base model by specifying pooling='avg' or 'max'. It's your call on how you'd like to handle this.

At this point we have a network that is almost ready to train. However, before we do so, we need to remember to freeze the layers in the base model so their weights don't change as the new fully connected layers go crazy trying to learn. To do that, we can just iterate through the layers and set them to be not trainable, using the following code:

for layer in base_model.layers:
layer.trainable = False

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232