09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Advanced Convolutional Neural Networks

Now, we can use GlobalAveragePooling2D() to average over the spatial

5×5 spatial locations and obtain a size of (32, 1280):

global_average_layer = tf.keras.layers.GlobalAveragePooling2D()

feature_batch_average = global_average_layer(feature_batch)

print(feature_batch_average.shape)

The last layer is dense with linear activation: if the prediction is positive, the class

is 1; if the prediction is negative, the class is 0:

prediction_layer = tf.keras.layers.Dense(1)

prediction_batch = prediction_layer(feature_batch_average)

print(prediction_batch.shape)

Our model is ready to be composed by combining the base_model (pretrained

MobileNetv2) with global_average_layer to get the correct shape output given as

an input to the final prediction_layer:

model = tf.keras.Sequential([

base_model,

global_average_layer,

prediction_layer

])

Now let's compile the model with an RMSProp() optimizer:

base_learning_rate = 0.0001

model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=base_learning_

rate), loss='binary_crossentropy',

metrics=['accuracy'])

If we display the composed model, we notice that there are more than 2 million

frozen parameters, and only about 1,000 trainable parameters:

Figure 18: Displaying the composed model; notice that we only have 1,281 trainable parameters

[ 156 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!