<jupyter_start><jupyter_text>Transfer Learning<jupyter_code>import matplotlib.pyplot as plt

# This is a bit of magic to make matplotlib figures appear inline in the
# notebook rather than in a new window.
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
# import the necessary packages
from keras.applications import VGG16
from keras.layers import Input

import argparse

# load the VGG16 network
print("[INFO] loading network...")
model = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))
print("[INFO] showing layers...")

# loop over the layers in the network and display them to the console
for (i, layer) in enumerate(model.layers):
    print("[INFO] {}\t{}".format(i, layer.__class__.__name__))
model.summary()
# import the necessary packages
from keras.layers.core import Dropout
from keras.layers.core import Flatten
from keras.layers.core import Dense
class FCHeadNet:
    
    def build(self, baseModel, classes, D):
        # initialize the head model that will be placed on top of
        # the base, then add a FC layer
        headModel = baseModel.output
        headModel = Flatten(name="flatten")(headModel)
        headModel = Dense(D, activation="relu")(headModel)
        # headModel = Dropout(0.5)(headModel)

        # add a softmax layer
        headModel = Dense(classes, activation="softmax")(headModel)

        # return the model
        return headModel<jupyter_output><empty_output><jupyter_text>Fine Tuning<jupyter_code># import the necessary packages
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

from utils.image_preprocessor import ImagePreprocessor
from utils.image_preprocessor import AspectAwarePreprocessor
from utils.image_preprocessor import ImageToArrayPreprocessor

from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import RMSprop
from keras.optimizers import SGD
from keras.applications import VGG16
from keras.layers import Input
from keras.models import Model

import numpy as np
import os
# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,
                         height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
                         horizontal_flip=True, fill_mode="nearest")
from keras.datasets import cifar10
((trainX, trainY), (testX, testY)) = cifar10.load_data()
labelNames = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
train_sample_num = 1000
test_sample_num = 200
trainX = trainX[:train_sample_num]
trainY = trainY[:train_sample_num]
print(trainX.shape)
print(trainY.shape)
testX = testX[:test_sample_num]
testY = testY[:test_sample_num]
print(testX.shape)
print(testY.shape)
aap = AspectAwarePreprocessor(224, 224)
iap = ImageToArrayPreprocessor()
ip = ImagePreprocessor(preprocessors=[aap, iap])
trainX = ip.preprocess(trainX)
testX = ip.preprocess(testX)
trainX = trainX / 255.0
testX = testX / 255.0
trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)
print(trainX.shape)
print(trainY.shape)
print(testX.shape)
print(testY.shape)
# path to output model
model_path = "./output/model_persistence/vgg_cifar10_transfer_learning.hdf5"
# load the VGG16 network, ensuring the head FC layer sets are left
# off
baseModel = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))

# initialize the new head of the network, a set of FC layers
# followed by a softmax classifier
headModel = FCHeadNet().build(baseModel, len(labelNames), 256)

# place the head FC model on top of the base model -- this will
# become the actual model we will train
model = Model(inputs=baseModel.input, outputs=headModel)

# loop over all layers in the base model and freeze them so they
# will *not* be updated during the training process
for layer in baseModel.layers:
    layer.trainable = False

# compile our model (this needs to be done after our setting our
# layers to being non-trainable
print("[INFO] compiling model...")

opt = RMSprop(lr=0.001)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])

# train the head of the network for a few epochs (all other
# layers are frozen) -- this will allow the new FC layers to
# start to become initialized with actual "learned" values
# versus pure random
print("[INFO] training head...")
model.fit_generator(aug.flow(trainX, trainY, batch_size=32), 
                    validation_data=(testX, testY), epochs=25, 
                    steps_per_epoch=len(trainX) // 32, verbose=1)

# evaluate the network after initialization
print("[INFO] evaluating after initialization...")
predictions = model.predict(testX, batch_size=32)
print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=labelNames))
# now that the head FC layers have been trained/initialized, lets
# unfreeze the final set of CONV layers and make them trainable
for layer in baseModel.layers[15:]:
    layer.trainable = True

# for the changes to the model to take affect we need to recompile
# the model, this time using SGD with a *very* small learning rate
print("[INFO] re-compiling model...")
opt = SGD(lr=0.001)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])

# train the model again, this time fine-tuning *both* the final set
# of CONV layers along with our set of FC layers
print("[INFO] fine-tuning model...")
model.fit_generator(aug.flow(trainX, trainY, batch_size=32), 
                    validation_data=(testX, testY), epochs=100, 
                    steps_per_epoch=len(trainX) // 32, verbose=1)

# evaluate the network on the fine-tuned model
print("[INFO] evaluating after fine-tuning...")
predictions = model.predict(testX, batch_size=32)
print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=labelNames))
# save the model to disk
print("[INFO] serializing model...")
model.save(model_path)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10180572851805729, 'fraction_numerical': 0.017901618929016188, 'mean_word_length': 4.927121771217712, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '637062', 'license': 'Apache-2.0', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1952, 'n_words': 608, 'path': 'notebooks/deep-learning-for-computer-vision-with-python/205_Transfer_Learning.ipynb', 'repo_name': 'pedro-abundio-wang/computer-vision', 'size': '6424'}