<jupyter_start><jupyter_text>Applying knowledge to other fields: Transfer learningTransfer learning is a very convenient technique consisting in using a previously trained model, reusing the weights adjusted for a benchmark (in the case of image classification problems it is Imagenet), In this section we will gather images from two different flower types, coming from the flower17 dataset. It is a 17 category flower dataset with 80 images for each class. The flowers chosen are some common flowers in the UK. The images have large scale, pose and light variations and there are also classes with large varations of images within the class and close similarity to other classes.In this case we will gather the first 2 classes (Daffodil and Coltsfoot), and build a classifier on top of the pretrained VGG16 network.Previously, we will will do image data aughmentation, because the images quantity could be not enough to abstract all the elements of any species.Let's start by importing all the needed libraries, including applications, preprocession, the checkpoint model and associated object, to allow saving the intermediate steps, and the cv2 and Numpy ones, for image processing and numerical base operation.<jupyter_code>from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential, Model 
from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D
from keras import backend as k 
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
from keras.models import load_model
from keras.applications.vgg16 import VGG16, decode_predictions,preprocess_input
import cv2
import numpy as np<jupyter_output>Using TensorFlow backend.<jupyter_text>In this section we will define all the variables affectin the input, data sources, and training parameters.<jupyter_code>img_width, img_height = 224, 224
train_data_dir = "train"
validation_data_dir = "validation"
nb_train_samples = 300
nb_validation_samples = 100 
batch_size = 16
epochs = 50<jupyter_output><empty_output><jupyter_text>Now we will invoke the VGG16 pretrained model, not including the top flattening layers.<jupyter_code>model = applications.VGG16(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3))

# Freeze the layers which you don't want to train. Here I am freezing the first 5 layers.
for layer in model.layers[:5]:
    layer.trainable = False

#Adding custom Layers 
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(2, activation="softmax")(x)

# creating the final model 
model_final = Model(input = model.input, output = predictions)<jupyter_output>Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5
32194560/58889256 [===============>..............] - ETA: 1206s<jupyter_text>Now its time to compile the model, and create the  image data authmentation object for the training and testing dataset<jupyter_code># compile the model 
model_final.compile(loss = "categorical_crossentropy", optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"])

# Initiate the train and test generators with data Augumentation 
train_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.3,
width_shift_range = 0.3,
height_shift_range=0.3,
rotation_range=30)

test_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.3,
width_shift_range = 0.3,
height_shift_range=0.3,
rotation_range=30)<jupyter_output><empty_output><jupyter_text>Now we will properly generate the new augmented data<jupyter_code>train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size, 
class_mode = "categorical")

validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size = (img_height, img_width),
class_mode = "categorical")

# Save the model according to the conditions  
checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto')<jupyter_output><empty_output><jupyter_text>It's time to fit the new final layers for the model<jupyter_code>model_final.fit_generator(
train_generator,
samples_per_epoch = nb_train_samples,
nb_epoch = epochs,
validation_data = validation_generator,
nb_val_samples = nb_validation_samples,
callbacks = [checkpoint, early])<jupyter_output><empty_output><jupyter_text>Then let's try with a daffoil image, testing the output of the classifier, which should output an array close to the [1.,0.] value, indicating that the probability for the first option is very high.<jupyter_code>im = cv2.resize(cv2.imread('test/gaff2.jpg'), (img_width, img_height))
im = np.expand_dims(im, axis=0).astype(np.float32)
im=preprocess_input(im)
print (im.shape)

out = model_final.predict(im)

print (out)
print (np.argmax(out))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08472327520849128, 'fraction_numerical': 0.02350265352539803, 'mean_word_length': 6.6037463976945245, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '141010', 'license': 'MIT', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1580, 'n_words': 527, 'path': 'BookCode/Chapter06/Transfer.ipynb', 'repo_name': 'irmoralesb/MLForDevsBook', 'size': '5276'}