How to do it...

We can proceed with the recipe as follows:

  1. Import the pre-built models and additional modules needed for processing:
from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)
  1.  We use a trained Inception-v3 but we do not include the top model because we want to fine-tune on D. The top level is a Dense layer with 1,024 inputs and the last output level is a softmax Dense layer with 200 classes of output. x = GlobalAveragePooling2D()(x) is used to convert the input to the correct shape for the Dense layer to handle. In fact, base_model.output tensor has the shape (samples, channels, rows, cols) for dim_ordering="th" or (samples, rows, cols, channels) for dim_ordering="tf", but Dense needs them as (samples, channels) GlobalAveragePooling2D averages across (rows, cols). 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))
  1. When you include _top=False, you are removing the last three layers and exposing the mixed_10 layer, so 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:
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer as first layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer with 200 classes as last layer
predictions = Dense(200, activation='softmax')(x)
# model to train
model = Model(input=base_model.input, output=predictions)
  1. All the convolutional levels are pre-trained so we freeze them during the training of the full model.
# i.e. freeze all convolutional Inception-v3 layers
for layer in base_model.layers:
layer.trainable = False
  1. The model is then compiled and trained for a few epochs so that the top layers are trained:
# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
# train the model on the new data for a few epochs
model.fit_generator(...)
  1. Then we freeze the top layers in Inception and fine-tune the Inception layer. In this example, we freeze the first 172 layers (a hyperparameter to tune):
# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 172 layers and unfreeze the rest:
for layer in model.layers[:172]:
layer.trainable = False
for layer in model.layers[172:]:
layer.trainable = True
  1.  The model is then recompiled for fine-tune optimization. We need to recompile the model for these modifications to take effect:
# we use SGD with a low learning rate
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers
model.fit_generator(...)
..................Content has been hidden....................

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