Dogs vs Cats Image Classification Without Image Augmentation

In this tutorial, we will discuss how to classify images into pictures of cats or pictures of dogs. We'll build an image classifier using tf.keras.Sequential model and load data using tf.keras.preprocessing.image.ImageDataGenerator.

Specific concepts that will be covered:

In the process, we will build practical experience and develop intuition around the following concepts

  • Building data input pipelines using the tf.keras.preprocessing.image.ImageDataGenerator class — How can we efficiently work with data on disk to interface with our model?
  • Overfitting - what is it, how to identify it?

Before you begin

Before running the code in this notebook, reset the runtime by going to Runtime -> Reset all runtimes in the menu above. If you have been working through several notebooks, this will help you avoid reaching Colab's memory limits.

Importing packages

Let's start by importing required packages:

  • os — to read files and directory structure
  • numpy — for some matrix math outside of TensorFlow
  • matplotlib.pyplot — to plot the graph and display images in our training and validation data
In [1]:
import tensorflow as tf
In [2]:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
In [3]:
import os
import matplotlib.pyplot as plt
import numpy as np
In [4]:
import logging
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

Data Loading

To build our image classifier, we begin by downloading the dataset. The dataset we are using is a filtered version of Dogs vs. Cats dataset from Kaggle (ultimately, this dataset is provided by Microsoft Research).

In previous Colabs, we've used TensorFlow Datasets, which is a very easy and convenient way to use datasets. In this Colab however, we will make use of the class tf.keras.preprocessing.image.ImageDataGenerator which will read data from disk. We therefore need to directly download Dogs vs. Cats from a URL and unzip it to the Colab filesystem.

In [5]:
_URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
zip_dir = tf.keras.utils.get_file('cats_and_dogs_filterted.zip', origin=_URL, extract=True)
Downloading data from https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip
68608000/68606236 [==============================] - 0s 0us/step

The dataset we have downloaded has the following directory structure.

cats_and_dogs_filtered
|__ train
    |______ cats: [cat.0.jpg, cat.1.jpg, cat.2.jpg ...]
    |______ dogs: [dog.0.jpg, dog.1.jpg, dog.2.jpg ...]
|__ validation
    |______ cats: [cat.2000.jpg, cat.2001.jpg, cat.2002.jpg ...]
    |______ dogs: [dog.2000.jpg, dog.2001.jpg, dog.2002.jpg ...]

We can list the directories with the following terminal command:

In [6]:
zip_dir_base = os.path.dirname(zip_dir)
!find $zip_dir_base -type d -print
/root/.keras/datasets
/root/.keras/datasets/cats_and_dogs_filtered
/root/.keras/datasets/cats_and_dogs_filtered/train
/root/.keras/datasets/cats_and_dogs_filtered/train/dogs
/root/.keras/datasets/cats_and_dogs_filtered/train/cats
/root/.keras/datasets/cats_and_dogs_filtered/validation
/root/.keras/datasets/cats_and_dogs_filtered/validation/dogs
/root/.keras/datasets/cats_and_dogs_filtered/validation/cats

We'll now assign variables with the proper file path for the training and validation sets.

In [7]:
base_dir = os.path.join(os.path.dirname(zip_dir), 'cats_and_dogs_filtered')
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')

train_cats_dir = os.path.join(train_dir, 'cats')  # directory with our training cat pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')  # directory with our training dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')  # directory with our validation cat pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')  # directory with our validation dog pictures

Understanding our data

Let's look at how many cats and dogs images we have in our training and validation directory

In [8]:
num_cats_tr = len(os.listdir(train_cats_dir))
num_dogs_tr = len(os.listdir(train_dogs_dir))

num_cats_val = len(os.listdir(validation_cats_dir))
num_dogs_val = len(os.listdir(validation_dogs_dir))

total_train = num_cats_tr + num_dogs_tr
total_val = num_cats_val + num_dogs_val
In [9]:
print('total training cat images:', num_cats_tr)
print('total training dog images:', num_dogs_tr)

print('total validation cat images:', num_cats_val)
print('total validation dog images:', num_dogs_val)
print("--")
print("Total training images:", total_train)
print("Total validation images:", total_val)
total training cat images: 1000
total training dog images: 1000
total validation cat images: 500
total validation dog images: 500
--
Total training images: 2000
Total validation images: 1000

Setting Model Parameters

For convenience, we'll set up variables that will be used later while pre-processing our dataset and training our network.

In [10]:
BATCH_SIZE = 100  # Number of training examples to process before updating our models variables
IMG_SHAPE  = 150  # Our training data consists of images with width of 150 pixels and height of 150 pixels

Data Preparation

Images must be formatted into appropriately pre-processed floating point tensors before being fed into the network. The steps involved in preparing these images are:

  1. Read images from the disk
  2. Decode contents of these images and convert it into proper grid format as per their RGB content
  3. Convert them into floating point tensors
  4. Rescale the tensors from values between 0 and 255 to values between 0 and 1, as neural networks prefer to deal with small input values.

Fortunately, all these tasks can be done using the class tf.keras.preprocessing.image.ImageDataGenerator.

We can set this up in a couple of lines of code.

In [11]:
train_image_generator      = ImageDataGenerator(rescale=1./255)  # Generator for our training data
validation_image_generator = ImageDataGenerator(rescale=1./255)  # Generator for our validation data

After defining our generators for training and validation images, flow_from_directory method will load images from the disk, apply rescaling, and resize them using single line of code.

In [12]:
train_data_gen = train_image_generator.flow_from_directory(batch_size=BATCH_SIZE,
                                                           directory=train_dir,
                                                           shuffle=True,
                                                           target_size=(IMG_SHAPE,IMG_SHAPE), #(150,150)
                                                           class_mode='binary')
Found 2000 images belonging to 2 classes.
In [13]:
val_data_gen = validation_image_generator.flow_from_directory(batch_size=BATCH_SIZE,
                                                              directory=validation_dir,
                                                              shuffle=False,
                                                              target_size=(IMG_SHAPE,IMG_SHAPE), #(150,150)
                                                              class_mode='binary')
Found 1000 images belonging to 2 classes.

Visualizing Training images

We can visualize our training images by getting a batch of images from the training generator, and then plotting a few of them using matplotlib.

In [14]:
sample_training_images, _ = next(train_data_gen) 

The next function returns a batch from the dataset. One batch is a tuple of (many images, many labels). For right now, we're discarding the labels because we just want to look at the images.

In [15]:
# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip(images_arr, axes):
        ax.imshow(img)
    plt.tight_layout()
    plt.show()
In [16]:
plotImages(sample_training_images[:5])  # Plot images 0-4

Model Creation

Define the model

The model consists of four convolution blocks with a max pool layer in each of them. Then we have a fully connected layer with 512 units, with a relu activation function. The model will output class probabilities for two classes — dogs and cats — using softmax.

In [17]:
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),

    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(2)
])

Compile the model

As usual, we will use the adam optimizer. Since we output a softmax categorization, we'll use sparse_categorical_crossentropy as the loss function. We would also like to look at training and validation accuracy on each epoch as we train our network, so we are passing in the metrics argument.

In [18]:
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

Model Summary

Let's look at all the layers of our network using summary method.

In [19]:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 148, 148, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 74, 74, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 72, 72, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 36, 36, 64)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 34, 34, 128)       73856     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 17, 17, 128)       0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 15, 15, 128)       147584    
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 7, 7, 128)         0         
_________________________________________________________________
flatten (Flatten)            (None, 6272)              0         
_________________________________________________________________
dense (Dense)                (None, 512)               3211776   
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 1026      
=================================================================
Total params: 3,453,634
Trainable params: 3,453,634
Non-trainable params: 0
_________________________________________________________________

Train the model

It's time we train our network.

Since our batches are coming from a generator (ImageDataGenerator), we'll use fit_generator instead of fit.

In [20]:
EPOCHS = 100
history = model.fit_generator(
    train_data_gen,
    steps_per_epoch=int(np.ceil(total_train / float(BATCH_SIZE))),
    epochs=EPOCHS,
    validation_data=val_data_gen,
    validation_steps=int(np.ceil(total_val / float(BATCH_SIZE)))
)
Epoch 1/100
20/20 [==============================] - 8s 422ms/step - loss: 0.8096 - accuracy: 0.4960 - val_loss: 0.6930 - val_accuracy: 0.5000
Epoch 2/100
20/20 [==============================] - 9s 431ms/step - loss: 0.6888 - accuracy: 0.5515 - val_loss: 0.6700 - val_accuracy: 0.6000
Epoch 3/100
20/20 [==============================] - 9s 426ms/step - loss: 0.6680 - accuracy: 0.5885 - val_loss: 0.6601 - val_accuracy: 0.6030
Epoch 4/100
20/20 [==============================] - 9s 426ms/step - loss: 0.6483 - accuracy: 0.6215 - val_loss: 0.6438 - val_accuracy: 0.6220
Epoch 5/100
20/20 [==============================] - 9s 428ms/step - loss: 0.6319 - accuracy: 0.6320 - val_loss: 0.6078 - val_accuracy: 0.6720
Epoch 6/100
20/20 [==============================] - 9s 429ms/step - loss: 0.5789 - accuracy: 0.6905 - val_loss: 0.5962 - val_accuracy: 0.6950
Epoch 7/100
20/20 [==============================] - 9s 430ms/step - loss: 0.5385 - accuracy: 0.7370 - val_loss: 0.5956 - val_accuracy: 0.6970
Epoch 8/100
20/20 [==============================] - 9s 431ms/step - loss: 0.5308 - accuracy: 0.7190 - val_loss: 0.6142 - val_accuracy: 0.6770
Epoch 9/100
20/20 [==============================] - 9s 426ms/step - loss: 0.4811 - accuracy: 0.7710 - val_loss: 0.5897 - val_accuracy: 0.7130
Epoch 10/100
20/20 [==============================] - 8s 419ms/step - loss: 0.4379 - accuracy: 0.7955 - val_loss: 0.5813 - val_accuracy: 0.7090
Epoch 11/100
20/20 [==============================] - 8s 418ms/step - loss: 0.3938 - accuracy: 0.8215 - val_loss: 0.5651 - val_accuracy: 0.7270
Epoch 12/100
20/20 [==============================] - 8s 422ms/step - loss: 0.3221 - accuracy: 0.8575 - val_loss: 0.6258 - val_accuracy: 0.7340
Epoch 13/100
20/20 [==============================] - 8s 421ms/step - loss: 0.2717 - accuracy: 0.8900 - val_loss: 0.6174 - val_accuracy: 0.7320
Epoch 14/100
20/20 [==============================] - 8s 421ms/step - loss: 0.2247 - accuracy: 0.9110 - val_loss: 0.7124 - val_accuracy: 0.7370
Epoch 15/100
20/20 [==============================] - 9s 428ms/step - loss: 0.1852 - accuracy: 0.9255 - val_loss: 0.7461 - val_accuracy: 0.7350
Epoch 16/100
20/20 [==============================] - 9s 433ms/step - loss: 0.1394 - accuracy: 0.9450 - val_loss: 0.8017 - val_accuracy: 0.7460
Epoch 17/100
20/20 [==============================] - 9s 427ms/step - loss: 0.1167 - accuracy: 0.9570 - val_loss: 0.8403 - val_accuracy: 0.7360
Epoch 18/100
20/20 [==============================] - 9s 428ms/step - loss: 0.1117 - accuracy: 0.9585 - val_loss: 0.9705 - val_accuracy: 0.7120
Epoch 19/100
20/20 [==============================] - 8s 419ms/step - loss: 0.1269 - accuracy: 0.9515 - val_loss: 0.9362 - val_accuracy: 0.7280
Epoch 20/100
20/20 [==============================] - 8s 421ms/step - loss: 0.0552 - accuracy: 0.9840 - val_loss: 1.1240 - val_accuracy: 0.7490
Epoch 21/100
20/20 [==============================] - 8s 423ms/step - loss: 0.0362 - accuracy: 0.9890 - val_loss: 1.1232 - val_accuracy: 0.7420
Epoch 22/100
20/20 [==============================] - 8s 424ms/step - loss: 0.0172 - accuracy: 0.9975 - val_loss: 1.2110 - val_accuracy: 0.7430
Epoch 23/100
20/20 [==============================] - 9s 426ms/step - loss: 0.0071 - accuracy: 0.9995 - val_loss: 1.3715 - val_accuracy: 0.7560
Epoch 24/100
20/20 [==============================] - 9s 427ms/step - loss: 0.0036 - accuracy: 1.0000 - val_loss: 1.4229 - val_accuracy: 0.7530
Epoch 25/100
20/20 [==============================] - 9s 432ms/step - loss: 0.0018 - accuracy: 1.0000 - val_loss: 1.4783 - val_accuracy: 0.7530
Epoch 26/100
20/20 [==============================] - 9s 428ms/step - loss: 0.0011 - accuracy: 1.0000 - val_loss: 1.5167 - val_accuracy: 0.7510
Epoch 27/100
20/20 [==============================] - 9s 426ms/step - loss: 7.8736e-04 - accuracy: 1.0000 - val_loss: 1.5440 - val_accuracy: 0.7560
Epoch 28/100
20/20 [==============================] - 9s 433ms/step - loss: 6.2151e-04 - accuracy: 1.0000 - val_loss: 1.5699 - val_accuracy: 0.7570
Epoch 29/100
20/20 [==============================] - 9s 425ms/step - loss: 5.0948e-04 - accuracy: 1.0000 - val_loss: 1.5964 - val_accuracy: 0.7520
Epoch 30/100
20/20 [==============================] - 9s 426ms/step - loss: 4.4122e-04 - accuracy: 1.0000 - val_loss: 1.6202 - val_accuracy: 0.7510
Epoch 31/100
20/20 [==============================] - 9s 428ms/step - loss: 3.8227e-04 - accuracy: 1.0000 - val_loss: 1.6424 - val_accuracy: 0.7520
Epoch 32/100
20/20 [==============================] - 9s 429ms/step - loss: 3.3343e-04 - accuracy: 1.0000 - val_loss: 1.6637 - val_accuracy: 0.7510
Epoch 33/100
20/20 [==============================] - 9s 430ms/step - loss: 2.9624e-04 - accuracy: 1.0000 - val_loss: 1.6825 - val_accuracy: 0.7530
Epoch 34/100
20/20 [==============================] - 9s 432ms/step - loss: 2.5613e-04 - accuracy: 1.0000 - val_loss: 1.7063 - val_accuracy: 0.7530
Epoch 35/100
20/20 [==============================] - 9s 426ms/step - loss: 2.3177e-04 - accuracy: 1.0000 - val_loss: 1.7199 - val_accuracy: 0.7510
Epoch 36/100
20/20 [==============================] - 9s 426ms/step - loss: 1.9818e-04 - accuracy: 1.0000 - val_loss: 1.7465 - val_accuracy: 0.7610
Epoch 37/100
20/20 [==============================] - 9s 428ms/step - loss: 1.7650e-04 - accuracy: 1.0000 - val_loss: 1.7611 - val_accuracy: 0.7510
Epoch 38/100
20/20 [==============================] - 9s 429ms/step - loss: 1.5270e-04 - accuracy: 1.0000 - val_loss: 1.7829 - val_accuracy: 0.7540
Epoch 39/100
20/20 [==============================] - 9s 427ms/step - loss: 1.3736e-04 - accuracy: 1.0000 - val_loss: 1.7986 - val_accuracy: 0.7550
Epoch 40/100
20/20 [==============================] - 9s 427ms/step - loss: 1.2223e-04 - accuracy: 1.0000 - val_loss: 1.8213 - val_accuracy: 0.7520
Epoch 41/100
20/20 [==============================] - 9s 427ms/step - loss: 1.0740e-04 - accuracy: 1.0000 - val_loss: 1.8392 - val_accuracy: 0.7520
Epoch 42/100
20/20 [==============================] - 9s 427ms/step - loss: 9.7915e-05 - accuracy: 1.0000 - val_loss: 1.8563 - val_accuracy: 0.7560
Epoch 43/100
20/20 [==============================] - 9s 442ms/step - loss: 8.5205e-05 - accuracy: 1.0000 - val_loss: 1.8765 - val_accuracy: 0.7520
Epoch 44/100
20/20 [==============================] - 9s 438ms/step - loss: 7.6511e-05 - accuracy: 1.0000 - val_loss: 1.8898 - val_accuracy: 0.7510
Epoch 45/100
20/20 [==============================] - 9s 432ms/step - loss: 6.8942e-05 - accuracy: 1.0000 - val_loss: 1.9107 - val_accuracy: 0.7540
Epoch 46/100
20/20 [==============================] - 9s 429ms/step - loss: 6.3459e-05 - accuracy: 1.0000 - val_loss: 1.9249 - val_accuracy: 0.7540
Epoch 47/100
20/20 [==============================] - 9s 428ms/step - loss: 5.6821e-05 - accuracy: 1.0000 - val_loss: 1.9426 - val_accuracy: 0.7500
Epoch 48/100
20/20 [==============================] - 9s 431ms/step - loss: 5.1537e-05 - accuracy: 1.0000 - val_loss: 1.9598 - val_accuracy: 0.7520
Epoch 49/100
20/20 [==============================] - 9s 430ms/step - loss: 4.7722e-05 - accuracy: 1.0000 - val_loss: 1.9758 - val_accuracy: 0.7510
Epoch 50/100
20/20 [==============================] - 9s 430ms/step - loss: 4.3178e-05 - accuracy: 1.0000 - val_loss: 1.9867 - val_accuracy: 0.7510
Epoch 51/100
20/20 [==============================] - 9s 430ms/step - loss: 3.9436e-05 - accuracy: 1.0000 - val_loss: 2.0022 - val_accuracy: 0.7510
Epoch 52/100
20/20 [==============================] - 9s 436ms/step - loss: 3.6217e-05 - accuracy: 1.0000 - val_loss: 2.0172 - val_accuracy: 0.7510
Epoch 53/100
20/20 [==============================] - 9s 431ms/step - loss: 3.3731e-05 - accuracy: 1.0000 - val_loss: 2.0304 - val_accuracy: 0.7480
Epoch 54/100
20/20 [==============================] - 9s 428ms/step - loss: 3.1018e-05 - accuracy: 1.0000 - val_loss: 2.0450 - val_accuracy: 0.7470
Epoch 55/100
20/20 [==============================] - 9s 430ms/step - loss: 2.8913e-05 - accuracy: 1.0000 - val_loss: 2.0595 - val_accuracy: 0.7490
Epoch 56/100
20/20 [==============================] - 9s 429ms/step - loss: 2.7061e-05 - accuracy: 1.0000 - val_loss: 2.0693 - val_accuracy: 0.7490
Epoch 57/100
20/20 [==============================] - 9s 429ms/step - loss: 2.5142e-05 - accuracy: 1.0000 - val_loss: 2.0814 - val_accuracy: 0.7500
Epoch 58/100
20/20 [==============================] - 9s 429ms/step - loss: 2.3294e-05 - accuracy: 1.0000 - val_loss: 2.0937 - val_accuracy: 0.7490
Epoch 59/100
20/20 [==============================] - 9s 429ms/step - loss: 2.2061e-05 - accuracy: 1.0000 - val_loss: 2.1060 - val_accuracy: 0.7530
Epoch 60/100
20/20 [==============================] - 9s 427ms/step - loss: 2.0605e-05 - accuracy: 1.0000 - val_loss: 2.1175 - val_accuracy: 0.7490
Epoch 61/100
20/20 [==============================] - 9s 433ms/step - loss: 1.9316e-05 - accuracy: 1.0000 - val_loss: 2.1272 - val_accuracy: 0.7540
Epoch 62/100
20/20 [==============================] - 9s 431ms/step - loss: 1.8129e-05 - accuracy: 1.0000 - val_loss: 2.1364 - val_accuracy: 0.7490
Epoch 63/100
20/20 [==============================] - 9s 429ms/step - loss: 1.7281e-05 - accuracy: 1.0000 - val_loss: 2.1500 - val_accuracy: 0.7550
Epoch 64/100
20/20 [==============================] - 9s 428ms/step - loss: 1.6443e-05 - accuracy: 1.0000 - val_loss: 2.1608 - val_accuracy: 0.7530
Epoch 65/100
20/20 [==============================] - 9s 430ms/step - loss: 1.5526e-05 - accuracy: 1.0000 - val_loss: 2.1701 - val_accuracy: 0.7530
Epoch 66/100
20/20 [==============================] - 9s 431ms/step - loss: 1.4586e-05 - accuracy: 1.0000 - val_loss: 2.1755 - val_accuracy: 0.7510
Epoch 67/100
20/20 [==============================] - 9s 435ms/step - loss: 1.3712e-05 - accuracy: 1.0000 - val_loss: 2.1866 - val_accuracy: 0.7510
Epoch 68/100
20/20 [==============================] - 9s 433ms/step - loss: 1.3017e-05 - accuracy: 1.0000 - val_loss: 2.1954 - val_accuracy: 0.7500
Epoch 69/100
20/20 [==============================] - 9s 430ms/step - loss: 1.2422e-05 - accuracy: 1.0000 - val_loss: 2.2054 - val_accuracy: 0.7500
Epoch 70/100
20/20 [==============================] - 9s 434ms/step - loss: 1.1978e-05 - accuracy: 1.0000 - val_loss: 2.2155 - val_accuracy: 0.7500
Epoch 71/100
20/20 [==============================] - 9s 428ms/step - loss: 1.1242e-05 - accuracy: 1.0000 - val_loss: 2.2221 - val_accuracy: 0.7550
Epoch 72/100
20/20 [==============================] - 9s 429ms/step - loss: 1.0760e-05 - accuracy: 1.0000 - val_loss: 2.2297 - val_accuracy: 0.7540
Epoch 73/100
20/20 [==============================] - 9s 430ms/step - loss: 1.0299e-05 - accuracy: 1.0000 - val_loss: 2.2376 - val_accuracy: 0.7510
Epoch 74/100
20/20 [==============================] - 9s 431ms/step - loss: 9.7647e-06 - accuracy: 1.0000 - val_loss: 2.2459 - val_accuracy: 0.7510
Epoch 75/100
20/20 [==============================] - 9s 429ms/step - loss: 9.2981e-06 - accuracy: 1.0000 - val_loss: 2.2531 - val_accuracy: 0.7510
Epoch 76/100
20/20 [==============================] - 9s 431ms/step - loss: 8.9627e-06 - accuracy: 1.0000 - val_loss: 2.2613 - val_accuracy: 0.7510
Epoch 77/100
20/20 [==============================] - 9s 433ms/step - loss: 8.6171e-06 - accuracy: 1.0000 - val_loss: 2.2690 - val_accuracy: 0.7510
Epoch 78/100
20/20 [==============================] - 9s 438ms/step - loss: 8.1954e-06 - accuracy: 1.0000 - val_loss: 2.2762 - val_accuracy: 0.7520
Epoch 79/100
20/20 [==============================] - 9s 433ms/step - loss: 7.8847e-06 - accuracy: 1.0000 - val_loss: 2.2843 - val_accuracy: 0.7540
Epoch 80/100
20/20 [==============================] - 9s 428ms/step - loss: 7.5495e-06 - accuracy: 1.0000 - val_loss: 2.2924 - val_accuracy: 0.7520
Epoch 81/100
20/20 [==============================] - 9s 429ms/step - loss: 7.2676e-06 - accuracy: 1.0000 - val_loss: 2.2982 - val_accuracy: 0.7550
Epoch 82/100
20/20 [==============================] - 9s 427ms/step - loss: 6.9721e-06 - accuracy: 1.0000 - val_loss: 2.3052 - val_accuracy: 0.7550
Epoch 83/100
20/20 [==============================] - 9s 432ms/step - loss: 6.7207e-06 - accuracy: 1.0000 - val_loss: 2.3126 - val_accuracy: 0.7550
Epoch 84/100
20/20 [==============================] - 9s 430ms/step - loss: 6.4807e-06 - accuracy: 1.0000 - val_loss: 2.3189 - val_accuracy: 0.7540
Epoch 85/100
20/20 [==============================] - 9s 443ms/step - loss: 6.2277e-06 - accuracy: 1.0000 - val_loss: 2.3241 - val_accuracy: 0.7540
Epoch 86/100
20/20 [==============================] - 9s 435ms/step - loss: 5.9643e-06 - accuracy: 1.0000 - val_loss: 2.3315 - val_accuracy: 0.7540
Epoch 87/100
20/20 [==============================] - 9s 429ms/step - loss: 5.7918e-06 - accuracy: 1.0000 - val_loss: 2.3383 - val_accuracy: 0.7520
Epoch 88/100
20/20 [==============================] - 9s 434ms/step - loss: 5.6068e-06 - accuracy: 1.0000 - val_loss: 2.3456 - val_accuracy: 0.7540
Epoch 89/100
20/20 [==============================] - 9s 431ms/step - loss: 5.3537e-06 - accuracy: 1.0000 - val_loss: 2.3502 - val_accuracy: 0.7550
Epoch 90/100
20/20 [==============================] - 9s 429ms/step - loss: 5.2595e-06 - accuracy: 1.0000 - val_loss: 2.3567 - val_accuracy: 0.7550
Epoch 91/100
20/20 [==============================] - 9s 431ms/step - loss: 5.0659e-06 - accuracy: 1.0000 - val_loss: 2.3608 - val_accuracy: 0.7550
Epoch 92/100
20/20 [==============================] - 9s 431ms/step - loss: 4.8495e-06 - accuracy: 1.0000 - val_loss: 2.3696 - val_accuracy: 0.7530
Epoch 93/100
20/20 [==============================] - 9s 431ms/step - loss: 4.6728e-06 - accuracy: 1.0000 - val_loss: 2.3756 - val_accuracy: 0.7550
Epoch 94/100
20/20 [==============================] - 9s 430ms/step - loss: 4.5531e-06 - accuracy: 1.0000 - val_loss: 2.3802 - val_accuracy: 0.7530
Epoch 95/100
20/20 [==============================] - 9s 431ms/step - loss: 4.3859e-06 - accuracy: 1.0000 - val_loss: 2.3878 - val_accuracy: 0.7550
Epoch 96/100
20/20 [==============================] - 9s 432ms/step - loss: 4.2508e-06 - accuracy: 1.0000 - val_loss: 2.3940 - val_accuracy: 0.7520
Epoch 97/100
20/20 [==============================] - 9s 435ms/step - loss: 4.1416e-06 - accuracy: 1.0000 - val_loss: 2.3980 - val_accuracy: 0.7550
Epoch 98/100
20/20 [==============================] - 9s 432ms/step - loss: 3.9790e-06 - accuracy: 1.0000 - val_loss: 2.4042 - val_accuracy: 0.7550
Epoch 99/100
20/20 [==============================] - 9s 431ms/step - loss: 3.8611e-06 - accuracy: 1.0000 - val_loss: 2.4087 - val_accuracy: 0.7530
Epoch 100/100
20/20 [==============================] - 9s 435ms/step - loss: 3.7581e-06 - accuracy: 1.0000 - val_loss: 2.4158 - val_accuracy: 0.7540

Visualizing results of the training

We'll now visualize the results we get after training our network.

In [21]:
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.savefig('./foo.png')
plt.show()

As we can see from the plots, training accuracy and validation accuracy are off by large margin and our model has achieved only around 70% accuracy on the validation set (depending on the number of epochs you trained for).

This is a clear indication of overfitting. Once the training and validation curves start to diverge, our model has started to memorize the training data and is unable to perform well on the validation data.

In [21]: