<jupyter_start><jupyter_text>This notebooks finetunes VGG16 by adding one new Dense layer and trains it to classify between cats and dogs.The prediction on this case is not the best (~71% on the validation dataset) becuase the network is not the best.This example just shows whats possible by just reusing `keras.applications.vgg16`.<jupyter_code>import numpy as np

import tensorflow as tf

from tensorflow.contrib.keras import layers
from tensorflow.contrib.keras import models
from tensorflow.contrib.keras import optimizers
from tensorflow.contrib.keras import applications
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications import imagenet_utils
def get_batches(dirpath, gen=image.ImageDataGenerator(), shuffle=True, batch_size=64, class_mode='categorical'):
    return gen.flow_from_directory(dirpath, target_size=(224,224), class_mode=class_mode, shuffle=shuffle, batch_size=batch_size)
batch_size = 64
train_batches = get_batches('./data/train', batch_size=batch_size)
val_batches = get_batches('./data/valid', batch_size=batch_size)<jupyter_output>Found 2203 images belonging to 2 classes.<jupyter_text>Model creation<jupyter_code>vgg16 = applications.VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
vgg16.summary()
finetune_in = vgg16.output
flat = layers.Flatten(name='flatten')(finetune_in)
predictions = layers.Dense(train_batches.num_class, activation='softmax', name='predictions')(flat)
model = models.Model(inputs=vgg16.input, outputs=predictions)
for layer in model.layers[:-1]:
    layer.trainable = False
model.summary()
lr = 0.01
model.compile(optimizer=optimizers.Adam(lr=lr), loss='categorical_crossentropy', metrics=['accuracy'])
epochs = 1<jupyter_output><empty_output><jupyter_text>Run enough steps per epoch to go throught the entire training set<jupyter_code>steps_per_epoch = train_batches.samples // train_batches.batch_size
validation_steps = val_batches.samples // val_batches.batch_size
steps_per_epoch
(steps_per_epoch * batch_size), train_batches.samples
model.fit_generator(train_batches, validation_data=val_batches, epochs=epochs,
                    steps_per_epoch=steps_per_epoch,validation_steps=validation_steps)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09413309982486866, 'fraction_numerical': 0.01882661996497373, 'mean_word_length': 7.65530303030303, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '293061', 'license': 'Apache-2.0', 'n_tokens_mistral': 753, 'n_tokens_neox': 722, 'n_words': 161, 'path': 'image/dogs-vs-cats/vgg16-retrain.ipynb', 'repo_name': 'danielfrg/deep-learning', 'size': '2284'}