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

This skeleton example is inspired by a scheme available online (https://keras.io/

applications/). Let's suppose we have a training dataset D in a different domain

from ImageNet. D has 1024 features in input and 200 categories in output. Let's look

at a code fragment:

import tensorflow as tf

from tensorflow.keras.applications.inception_v3 import InceptionV3

from tensorflow.keras.preprocessing import image

from tensorflow.keras import layers, models

# create the base pre-trained model

base_model = InceptionV3(weights='imagenet', include_top=False)

We use a trained Inception-v3: we do not include the fully connected layer – dense

layer with 1024 inputs – because we want to fine-tune on D. The preceding code

fragment will download the pretrained weights on our behalf:

Figure 17: Pretrained weights being downloaded from GitHub

So if you look at the last four layers (where include_top=True), you see these

shapes:

# layer.name, layer.input_shape, layer.output_shape

('mixed10', [(None, 8, 8, 320), (None, 8, 8, 768), (None, 8, 8, 768),

(None, 8, 8, 192)], (None, 8, 8, 2048))

('avg_pool', (None, 8, 8, 2048), (None, 1, 1, 2048))

('flatten', (None, 1, 1, 2048), (None, 2048))

('predictions', (None, 2048), (None, 1000))

When you include_top=False, you are removing the last three layers and exposing

the mixed_10 layer. The GlobalAveragePooling2D layer converts the (None, 8,

8, 2048) to (None, 2048), where each element in the (None, 2048) tensor is

the average value for each corresponding (8,8) subtensor in the (None, 8, 8,

2048) tensor. None means an unspecified dimension, which is useful if you define

a placeholder:

x = base_model.output

# let's add a fully connected layer as first layer

x = layers.Dense(1024, activation='relu')(x)

# and a logistic layer with 200 classes as last layer

predictions = layers.Dense(200, activation='softmax')(x)

# model to train

model = models.Model(inputs=base_model.input, outputs=predictions)

[ 152 ]

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

Saved successfully!

Ooh no, something went wrong!