<jupyter_start><jupyter_text>Dogs vs Cats Keras code for the Kaggle dogs and cats classification problem.  Imports<jupyter_code>from keras import layers
from keras import models
from keras import optimizers
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator<jupyter_output>Using TensorFlow backend.<jupyter_text>CNN models<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation = 'relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation = 'relu'))
model.add(layers.Dense(1, activation = 'sigmoid'))

model.summary()
model.compile(loss = 'binary_crossentropy', optimizer = optimizers.Adam(), metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>train_dir = 'sample_data'
validation_dir = 'sample_data'
train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)
train_generator = train_datagen.flow_from_directory(
    #(target directory use your own), resizing the images, Binary labelling
    train_dir,
    target_size = (150,150),
    batch_size = 20,
    class_mode = 'binary'
)

validation_generator = test_datagen.flow_from_directory(
    #(target directory use your own), resizing the images, Binary labelling
    validation_dir,
    target_size = (150,150),
    batch_size = 20,
    class_mode = 'binary'
)<jupyter_output>Found 0 images belonging to 0 classes.<jupyter_text>Fitting and saving the model<jupyter_code>history = model.fit_generator(
    train_generator,
    steps_per_epoch = 100,
    epochs = 10,
    validation_data = validation_generator,
    validation_steps = 50.
)
model.save('cats_and_dogs_small_1.h5')
print("Model saved to disk")<jupyter_output>Model saved to disk<jupyter_text>Plotting model<jupyter_code>acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(acc) + 1)

plt.plot(epochs, acc, 'g', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()

plt.plot(epochs, loss, 'g', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Data Augementation and Training a new model<jupyter_code>datagen = ImageDataGenerator(
    rotation_range = 40,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True,
    fill_mode = 'nearest'
)
# New convonet that includes dropout
model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation = 'relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.4))
model.add(layers.Dense(512, activation = 'relu'))
model.add(layers.Dense(1, activation = 'relu'))

model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])
train_datagen = ImageDataGenerator(
    rescale = 1./255,
    rotation_range = 40,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True
)
# Note validation data should not be augmented
test_datagen = ImageDataGenerator(
    rescale = 1./255
)
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size = (150,150),
    batch_size = 32,
    class_mode = 'binary'
)
validation_generator = test_datagen.flow_from_directory(
    validation_dir,
    target_size = (150,150),
    batch_size = 32,
    class_mode = 'binary'
)
history = model.fit_generator(
    train_generator,
    steps_per_epoch = 100,
    epochs = 100,
    validation_data = validation_generator,
    validation_steps = 50
)
model.save('cats_and_dogs_small_2.h5')
print("Model saved to disk")<jupyter_output><empty_output><jupyter_text>Using Pretrained Convo-net  Basic Transfer Learning<jupyter_code>from keras.applications import VGG16
conv_base = VGG16(weights = 'imagenet', include_top = False, input_shape = (150,150,3))
conv_base.summary()
# Adding a Densely connected network on top of VGG16
model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation = 'relu'))
model.add(layers.Dense(1, activation = 'sigmoid'))
model.summary()
# Freezing some of the parameters of the layers
print("Number of trainable weights before freezing VGG ",len(model.trainable_weights))
conv_base.trainable = False
print("Number of trainable weights after freezing VGG16 ",len(model.trainable_weights))<jupyter_output>Number of trainable weights before freezing VGG  30
Number of trainable weights after freezing VGG16  4<jupyter_text>* With this set up only the Densely conncected Layers will get updated.* There are 4 parameteres 2 weights and 2 biases* In order for the effect to take place we need to compile the model again* We must compile the model everytime we make changes or else changes will be ignored.<jupyter_code>train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest' )
# Do not augument the test data
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size = (150,150),
    batch_size = 20,
    class_mode = 'binary'
)
validation_generator = test_datagen.flow_from_directory(
    validation_dir,
    target_size=(150, 150),
    batch_size=20,
    class_mode='binary'
)
model.compile(loss = "binary_crossentropy", optimizer = optimizers.RMSprop(), metrics = ['accuracy'])
history = model.fit_generator(
    train_generator,
    steps_per_epoch=100,
    epochs=30,
    validation_data=validation_generator,
    validation_steps=50
)<jupyter_output><empty_output><jupyter_text>Fine tuning Transfer Learning<jupyter_code>conv_base.summary()
conv_base.trainable = True
set_trainable = False
for layer in conv_base.layers:
    if layer.name == 'block5_conv1':
        set_trainable = True
    if set_trainable:
        layer.trainable = True
    else:
        layer.trainable = False

conv_base.trainable_weights
# Now train at very low learning rate
model.compile(loss = "binary_crossentropy", optimizer = optimizers.Adam(lr = 1e-5), metrices = ['accuracy'])
history = model.fit_generator(
    train_generator,
    steps_per_epoch=100,
    epochs=100,
    validation_data=validation_generator,
    validation_steps=50
)<jupyter_output><empty_output><jupyter_text>Finally Testing the model<jupyter_code>test_generator = test_datagen.flow_from_directory(
    test_dir,
    target_size=(150, 150),
    batch_size=20,
    class_mode='binary'
)
test_loss, test_acc = model.evaluate_generator(test_generator, steps=50)
print('test acc:', test_acc)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11978623566214808, 'fraction_numerical': 0.03584462982273201, 'mean_word_length': 5.603270223752151, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '488468', 'license': 'MIT', 'n_tokens_mistral': 2792, 'n_tokens_neox': 2619, 'n_words': 553, 'path': 'Learning_Keras/Convolutional_Networks/Dogs vs Cat CNN.ipynb', 'repo_name': 'oke-aditya/Machine_Learning', 'size': '7672'}