<jupyter_start><jupyter_text>Image Preparation<jupyter_code>import numpy as np
import keras
from keras import backend as K
from keras.models import Sequential
from keras.layers import Activation
from keras.layers.core import Dense, Flatten
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import *
from sklearn.metrics import confusion_matrix
import itertools
import matplotlib.pyplot as plt
%matplotlib inline
train_path = 'cats-and-dogs/train'
valid_path = 'cats-and-dogs/valid'
test_path = 'cats-and-dogs/test'
train_batches = ImageDataGenerator().flow_from_directory(train_path, target_size=(224,224), classes=['dog', 'cat'], batch_size=10)
valid_batches = ImageDataGenerator().flow_from_directory(valid_path, target_size=(224,224), classes=['dog', 'cat'], batch_size=4)
test_batches = ImageDataGenerator().flow_from_directory(test_path, target_size=(224,224), classes=['dog', 'cat'], batch_size=10)
# plots images with labels within jupyter notebook
def plots(ims, figsize=(12,6), rows=1, interp=False, titles=None):
    if type(ims[0]) is np.ndarray:
        ims = np.array(ims).astype(np.uint8)
        if (ims.shape[-1] != 3):
            ims = ims.transpose((0,2,3,1))
        f = plt.figure(figsize=figsize)
        cols = len(ims)//rows if len(ims) % 2 == 0 else len(ims)//rows + 1
        for i in range(len(ims)):
            sp = f.add_subplot(rows, cols, i+1)
            sp.axis('Off')
            if titles is not None:
                sp.set_title(titles[i], fontsize=16)
            plt.imshow(ims[i], interpolation=None if interp else 'none')
imgs, labels = next(train_batches)
plots(imgs, titles=labels)<jupyter_output><empty_output><jupyter_text>Build and Train CNN<jupyter_code>model = Sequential([
    Conv2D(32, (3,3), activation='relu', input_shape=(224,224,3)),
    Flatten(),
    Dense(2, activation='softmax')
])
model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit_generator(train_batches, steps_per_epoch=4, validation_data=valid_batches, validation_steps=4, epochs=5, verbose=2)<jupyter_output>Epoch 1/5
 - 3s - loss: 6.5506 - acc: 0.5500 - val_loss: 9.0664 - val_acc: 0.4375
Epoch 2/5
 - 3s - loss: 6.8502 - acc: 0.5750 - val_loss: 9.0664 - val_acc: 0.4375
Epoch 3/5
 - 3s - loss: 6.8502 - acc: 0.5750 - val_loss: 9.0664 - val_acc: 0.4375
Epoch 4/5
 - 3s - loss: 6.8502 - acc: 0.5750 - val_loss: 9.0664 - val_acc: 0.4375
Epoch 5/5
 - 3s - loss: 6.8502 - acc: 0.5750 - val_loss: 9.0664 - val_acc: 0.4375<jupyter_text>Predict<jupyter_code>test_imgs, test_labels = next(test_batches)
plots(test_imgs, titles=test_labels)
test_labels = test_labels[:,0]
test_labels
predictions = model.predict_generator(test_batches, steps=1, verbose=0)
predictions
cm = confusion_matrix(test_labels, predictions[:,0])
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
cm_plot_labels = ['cat', 'dog']
plot_confusion_matrix(cm, cm_plot_labels, title='Confusion Matrix')<jupyter_output>Confusion matrix, without normalization
[[5 0]
 [4 1]]<jupyter_text>Build Fine-Tuned VGG16 Model<jupyter_code>vgg16_model = keras.applications.vgg16.VGG16()
vgg16_model.summary()
type(vgg16_model)
model = Sequential()
for layer in vgg16_model.layers:
    model.add(layer)
model.summary()
model.layers.pop()
model.summary()
for layer in model.layers:
    # do not update these layer's weights during training
    layer.trainable = False
model.add(Dense(2, activation='softmax'))
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_poo[...]<jupyter_text>Train Fine-Tuned VGG16 Model<jupyter_code>model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit_generator(train_batches, steps_per_epoch=4, validation_data=valid_batches, validation_steps=4, epochs=5, verbose=2)<jupyter_output>Epoch 1/5
 - 42s - loss: 0.6928 - acc: 0.5000 - val_loss: 0.6939 - val_acc: 0.5000
Epoch 2/5
 - 48s - loss: 0.6856 - acc: 0.7500 - val_loss: 0.6894 - val_acc: 0.6875
Epoch 3/5
 - 42s - loss: 0.6796 - acc: 0.8500 - val_loss: 0.6853 - val_acc: 0.8125
Epoch 4/5
 - 50s - loss: 0.6737 - acc: 0.9000 - val_loss: 0.6812 - val_acc: 0.8750
Epoch 5/5
 - 51s - loss: 0.6691 - acc: 0.9000 - val_loss: 0.6774 - val_acc: 0.8750<jupyter_text>Predict Using Fine-Tuned VGG16 Model<jupyter_code>test_imgs, test_labels = next(test_batches)
plots(test_imgs, titles=test_labels)
test_labels = test_labels[:,0]
test_labels
predictions = model.predict_generator(test_batches, steps=1, verbose=0)
cm = confusion_matrix(test_labels, np.round(predictions[:,0]))
cm_plot_labels = ['cat', 'dog']
plot_confusion_matrix(cm, cm_plot_labels, title='Confusion Matrix')<jupyter_output>Confusion matrix, without normalization
[[4 1]
 [3 2]]
{'fraction_non_alphanumeric': 0.13061106284390386, 'fraction_numerical': 0.06284390385172314, 'mean_word_length': 4.232575757575757, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '477765', 'license': 'MIT', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2303, 'n_words': 506, 'path': 'deeplizard-keras-tutorial/deeplizard_kerastutorial_createcnn.ipynb', 'repo_name': 'sem-onyalo/ml-sandbox', 'size': '6906'}