Cainvas
Tags: animals pups breed dogs

Similar Use Cases: Fish Breed Prediction App

Dog Breed Classification

Credit: AITS Cainvas Community

Photo by Emma Gilberg on Dribbble

In this notebook, we will classify the breed of a dog (among 5 selected breeds,) based on the image of a dog.

We will import all the required libraries

In [1]:
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import wget
import os

from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import classification_report, log_loss, accuracy_score
from sklearn.model_selection import train_test_split
In [2]:
!pip install wget
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: wget in ./.local/lib/python3.7/site-packages (3.2)
WARNING: You are using pip version 20.3.1; however, version 21.1.3 is available.
You should consider upgrading via the '/opt/tljh/user/bin/python -m pip install --upgrade pip' command.

Unzip the dataset so that we can use it in our notebook

In [3]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/dog_photos.zip"
!unzip -qo dog_photos.zip
--2021-07-21 04:42:04--  https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/dog_photos.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.64.120
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.64.120|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9256669 (8.8M) [application/x-zip-compressed]
Saving to: ‘dog_photos.zip’

dog_photos.zip      100%[===================>]   8.83M  --.-KB/s    in 0.07s   

2021-07-21 04:42:04 (127 MB/s) - ‘dog_photos.zip’ saved [9256669/9256669]

In [4]:
directory = "dog_photos/train/"

Our dataset consists of the given 5 different breeds. Each folder is dedicated to a single breed and contains upto 120 different images of that breed.

In [5]:
Name=[]
for file in os.listdir(directory):
    Name+=[file]
print(Name)
print(len(Name))
['bulldog', 'pug', 'rottweiler', 'german shepherds', 'labrador']
5

Map the classifications i.e. classes to an integer and display the list of all unique 5 breeds.

In [6]:
breed_map = dict(zip(Name, [t for t in range(len(Name))]))
print(breed_map)
r_breed_map=dict(zip([t for t in range(len(Name))],Name)) 
{'bulldog': 0, 'pug': 1, 'rottweiler': 2, 'german shepherds': 3, 'labrador': 4}

Displaying some images from our dataset.

In [7]:
Breed = 'dog_photos/train/pug'
import os 
sub_class = os.listdir(Breed)

fig = plt.figure(figsize=(10,5))
for e in range(len(sub_class[:10])):
    plt.subplot(2,5,e+1)
    img = plt.imread(os.path.join(Breed,sub_class[e]))
    plt.imshow(img, cmap=plt.get_cmap('gray'))
    plt.axis('off')
In [8]:
def mapper(value):
    return r_breed_map[value]

Perform data augmentation by using ImageDataGenerator so that we can acquire more relevant data from the existing images by making minor alterations to the dataset.

In [9]:
img_datagen = ImageDataGenerator(rescale=1./255,
                                vertical_flip=True,
                                horizontal_flip=True,
                                rotation_range=40,
                                width_shift_range=0.2,
                                height_shift_range=0.2,
                                zoom_range=0.1,
                                validation_split=0.2)
In [10]:
test_datagen = ImageDataGenerator(rescale=1./255)

Divide the training dataset into train set and validation set.

In [11]:
train_generator = img_datagen.flow_from_directory(directory,
                                                 shuffle=True,
                                                 batch_size=32,
                                                 subset='training',
                                                 target_size=(100, 100))
Found 459 images belonging to 5 classes.
In [12]:
valid_generator = img_datagen.flow_from_directory(directory,
                                                 shuffle=True,
                                                 batch_size=16,
                                                 subset='validation',
                                                 target_size=(100, 100))
Found 112 images belonging to 5 classes.
In [13]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Dropout,Flatten,Activation,BatchNormalization
from tensorflow.keras.models import model_from_json
from tensorflow.keras.models import load_model
from tensorflow.keras import regularizers

Train a sequential model.

In [14]:
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3),input_shape=(100,100,3), activation='relu', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))

model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(len(breed_map)))
model.add(Activation('softmax'))

model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 100, 100, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 50, 50, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 50, 50, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 25, 25, 64)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 25, 25, 64)        36928     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 12, 12, 64)        36928     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 6, 6, 64)          0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 6, 6, 64)          36928     
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 3, 3, 64)          0         
_________________________________________________________________
dropout (Dropout)            (None, 3, 3, 64)          0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 3, 3, 64)          36928     
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 1, 1, 64)          0         
_________________________________________________________________
flatten (Flatten)            (None, 64)                0         
_________________________________________________________________
dense (Dense)                (None, 256)               16640     
_________________________________________________________________
activation (Activation)      (None, 256)               0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 5)                 1285      
_________________________________________________________________
activation_1 (Activation)    (None, 5)                 0         
=================================================================
Total params: 185,029
Trainable params: 185,029
Non-trainable params: 0
_________________________________________________________________
In [15]:
model.compile(optimizer='adam',
             loss='categorical_crossentropy',
             metrics=['accuracy'])
In [16]:
history = model.fit(train_generator, validation_data=valid_generator,
                   steps_per_epoch=train_generator.n//train_generator.batch_size,
                   validation_steps=valid_generator.n//valid_generator.batch_size,
                   epochs=170)
Epoch 1/170
14/14 [==============================] - 2s 112ms/step - loss: 1.6117 - accuracy: 0.2014 - val_loss: 1.6012 - val_accuracy: 0.2411
Epoch 2/170
14/14 [==============================] - 1s 99ms/step - loss: 1.6029 - accuracy: 0.2248 - val_loss: 1.6008 - val_accuracy: 0.2411
Epoch 3/170
14/14 [==============================] - 1s 99ms/step - loss: 1.6061 - accuracy: 0.2201 - val_loss: 1.6019 - val_accuracy: 0.2411
Epoch 4/170
14/14 [==============================] - 1s 99ms/step - loss: 1.6024 - accuracy: 0.2529 - val_loss: 1.6009 - val_accuracy: 0.2411
Epoch 5/170
14/14 [==============================] - 1s 98ms/step - loss: 1.6006 - accuracy: 0.2459 - val_loss: 1.6026 - val_accuracy: 0.2411
Epoch 6/170
14/14 [==============================] - 1s 98ms/step - loss: 1.6028 - accuracy: 0.2459 - val_loss: 1.6001 - val_accuracy: 0.2411
Epoch 7/170
14/14 [==============================] - 1s 99ms/step - loss: 1.6055 - accuracy: 0.2248 - val_loss: 1.6009 - val_accuracy: 0.2411
Epoch 8/170
14/14 [==============================] - 1s 99ms/step - loss: 1.6011 - accuracy: 0.2436 - val_loss: 1.5989 - val_accuracy: 0.2411
Epoch 9/170
14/14 [==============================] - 1s 104ms/step - loss: 1.5957 - accuracy: 0.2455 - val_loss: 1.5968 - val_accuracy: 0.2411
Epoch 10/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5991 - accuracy: 0.2389 - val_loss: 1.5946 - val_accuracy: 0.2411
Epoch 11/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5967 - accuracy: 0.2389 - val_loss: 1.5970 - val_accuracy: 0.2411
Epoch 12/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5991 - accuracy: 0.2084 - val_loss: 1.6026 - val_accuracy: 0.2411
Epoch 13/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5994 - accuracy: 0.2272 - val_loss: 1.5999 - val_accuracy: 0.2411
Epoch 14/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5923 - accuracy: 0.2342 - val_loss: 1.5975 - val_accuracy: 0.2500
Epoch 15/170
14/14 [==============================] - 1s 103ms/step - loss: 1.5954 - accuracy: 0.2459 - val_loss: 1.5984 - val_accuracy: 0.2411
Epoch 16/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5906 - accuracy: 0.2389 - val_loss: 1.5916 - val_accuracy: 0.2411
Epoch 17/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5939 - accuracy: 0.2389 - val_loss: 1.5970 - val_accuracy: 0.2411
Epoch 18/170
14/14 [==============================] - 1s 98ms/step - loss: 1.5886 - accuracy: 0.2436 - val_loss: 1.5868 - val_accuracy: 0.2411
Epoch 19/170
14/14 [==============================] - 1s 100ms/step - loss: 1.5872 - accuracy: 0.2389 - val_loss: 1.5828 - val_accuracy: 0.2411
Epoch 20/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5813 - accuracy: 0.2389 - val_loss: 1.6025 - val_accuracy: 0.2411
Epoch 21/170
14/14 [==============================] - 1s 104ms/step - loss: 1.5963 - accuracy: 0.2482 - val_loss: 1.5970 - val_accuracy: 0.1964
Epoch 22/170
14/14 [==============================] - 1s 102ms/step - loss: 1.5788 - accuracy: 0.2522 - val_loss: 1.5766 - val_accuracy: 0.2589
Epoch 23/170
14/14 [==============================] - 1s 100ms/step - loss: 1.5766 - accuracy: 0.2740 - val_loss: 1.5698 - val_accuracy: 0.2768
Epoch 24/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5746 - accuracy: 0.2553 - val_loss: 1.5665 - val_accuracy: 0.2589
Epoch 25/170
14/14 [==============================] - 1s 100ms/step - loss: 1.5659 - accuracy: 0.2787 - val_loss: 1.5687 - val_accuracy: 0.2768
Epoch 26/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5753 - accuracy: 0.2974 - val_loss: 1.6017 - val_accuracy: 0.2500
Epoch 27/170
14/14 [==============================] - 1s 100ms/step - loss: 1.5709 - accuracy: 0.2763 - val_loss: 1.5632 - val_accuracy: 0.2589
Epoch 28/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5579 - accuracy: 0.2881 - val_loss: 1.5518 - val_accuracy: 0.2411
Epoch 29/170
14/14 [==============================] - 1s 100ms/step - loss: 1.5451 - accuracy: 0.2693 - val_loss: 1.5752 - val_accuracy: 0.2411
Epoch 30/170
14/14 [==============================] - 1s 99ms/step - loss: 1.5584 - accuracy: 0.2693 - val_loss: 1.5315 - val_accuracy: 0.2857
Epoch 31/170
14/14 [==============================] - 1s 104ms/step - loss: 1.4990 - accuracy: 0.2881 - val_loss: 1.4992 - val_accuracy: 0.2768
Epoch 32/170
14/14 [==============================] - 1s 99ms/step - loss: 1.4704 - accuracy: 0.3232 - val_loss: 1.4361 - val_accuracy: 0.3214
Epoch 33/170
14/14 [==============================] - 1s 99ms/step - loss: 1.4539 - accuracy: 0.3677 - val_loss: 1.4121 - val_accuracy: 0.3393
Epoch 34/170
14/14 [==============================] - 1s 99ms/step - loss: 1.4384 - accuracy: 0.3513 - val_loss: 1.3810 - val_accuracy: 0.3750
Epoch 35/170
14/14 [==============================] - 1s 100ms/step - loss: 1.4016 - accuracy: 0.3630 - val_loss: 1.4044 - val_accuracy: 0.4107
Epoch 36/170
14/14 [==============================] - 1s 104ms/step - loss: 1.4063 - accuracy: 0.3607 - val_loss: 1.3947 - val_accuracy: 0.4732
Epoch 37/170
14/14 [==============================] - 1s 99ms/step - loss: 1.3962 - accuracy: 0.4052 - val_loss: 1.3593 - val_accuracy: 0.3839
Epoch 38/170
14/14 [==============================] - 1s 103ms/step - loss: 1.3646 - accuracy: 0.4219 - val_loss: 1.3636 - val_accuracy: 0.3839
Epoch 39/170
14/14 [==============================] - 1s 99ms/step - loss: 1.3410 - accuracy: 0.4450 - val_loss: 1.3225 - val_accuracy: 0.4732
Epoch 40/170
14/14 [==============================] - 1s 99ms/step - loss: 1.3574 - accuracy: 0.4028 - val_loss: 1.3526 - val_accuracy: 0.4286
Epoch 41/170
14/14 [==============================] - 1s 100ms/step - loss: 1.3244 - accuracy: 0.4309 - val_loss: 1.3998 - val_accuracy: 0.4286
Epoch 42/170
14/14 [==============================] - 1s 103ms/step - loss: 1.2909 - accuracy: 0.4192 - val_loss: 1.3388 - val_accuracy: 0.3571
Epoch 43/170
14/14 [==============================] - 1s 103ms/step - loss: 1.2461 - accuracy: 0.4520 - val_loss: 1.2548 - val_accuracy: 0.4554
Epoch 44/170
14/14 [==============================] - 1s 100ms/step - loss: 1.2920 - accuracy: 0.4122 - val_loss: 1.2984 - val_accuracy: 0.4107
Epoch 45/170
14/14 [==============================] - 1s 99ms/step - loss: 1.2628 - accuracy: 0.4543 - val_loss: 1.2490 - val_accuracy: 0.4286
Epoch 46/170
14/14 [==============================] - 1s 103ms/step - loss: 1.2048 - accuracy: 0.4988 - val_loss: 1.1781 - val_accuracy: 0.4911
Epoch 47/170
14/14 [==============================] - 1s 103ms/step - loss: 1.2802 - accuracy: 0.4637 - val_loss: 1.2762 - val_accuracy: 0.4643
Epoch 48/170
14/14 [==============================] - 1s 100ms/step - loss: 1.2283 - accuracy: 0.4614 - val_loss: 1.2594 - val_accuracy: 0.4464
Epoch 49/170
14/14 [==============================] - 1s 100ms/step - loss: 1.2269 - accuracy: 0.4918 - val_loss: 1.2873 - val_accuracy: 0.4554
Epoch 50/170
14/14 [==============================] - 1s 100ms/step - loss: 1.1803 - accuracy: 0.4988 - val_loss: 1.1693 - val_accuracy: 0.5357
Epoch 51/170
14/14 [==============================] - 1s 99ms/step - loss: 1.1860 - accuracy: 0.4988 - val_loss: 1.2233 - val_accuracy: 0.4911
Epoch 52/170
14/14 [==============================] - 1s 100ms/step - loss: 1.1667 - accuracy: 0.5082 - val_loss: 1.1965 - val_accuracy: 0.4911
Epoch 53/170
14/14 [==============================] - 1s 100ms/step - loss: 1.2165 - accuracy: 0.4684 - val_loss: 1.3036 - val_accuracy: 0.4375
Epoch 54/170
14/14 [==============================] - 1s 99ms/step - loss: 1.2413 - accuracy: 0.4801 - val_loss: 1.2788 - val_accuracy: 0.4911
Epoch 55/170
14/14 [==============================] - 1s 100ms/step - loss: 1.1402 - accuracy: 0.5246 - val_loss: 1.2840 - val_accuracy: 0.4821
Epoch 56/170
14/14 [==============================] - 1s 101ms/step - loss: 1.1585 - accuracy: 0.5222 - val_loss: 1.2642 - val_accuracy: 0.4732
Epoch 57/170
14/14 [==============================] - 1s 100ms/step - loss: 1.1828 - accuracy: 0.4965 - val_loss: 1.1917 - val_accuracy: 0.5268
Epoch 58/170
14/14 [==============================] - 1s 100ms/step - loss: 1.0718 - accuracy: 0.5293 - val_loss: 1.1193 - val_accuracy: 0.5179
Epoch 59/170
14/14 [==============================] - 1s 99ms/step - loss: 1.1838 - accuracy: 0.4684 - val_loss: 1.1983 - val_accuracy: 0.4643
Epoch 60/170
14/14 [==============================] - 1s 100ms/step - loss: 1.0939 - accuracy: 0.5293 - val_loss: 1.2139 - val_accuracy: 0.4911
Epoch 61/170
14/14 [==============================] - 1s 101ms/step - loss: 1.0248 - accuracy: 0.5527 - val_loss: 1.1115 - val_accuracy: 0.5714
Epoch 62/170
14/14 [==============================] - 1s 100ms/step - loss: 1.0639 - accuracy: 0.5691 - val_loss: 1.1590 - val_accuracy: 0.5000
Epoch 63/170
14/14 [==============================] - 1s 100ms/step - loss: 1.1048 - accuracy: 0.5316 - val_loss: 1.1457 - val_accuracy: 0.5179
Epoch 64/170
14/14 [==============================] - 1s 99ms/step - loss: 1.0418 - accuracy: 0.5480 - val_loss: 1.0895 - val_accuracy: 0.5804
Epoch 65/170
14/14 [==============================] - 1s 100ms/step - loss: 1.0224 - accuracy: 0.5691 - val_loss: 1.0776 - val_accuracy: 0.5179
Epoch 66/170
14/14 [==============================] - 1s 100ms/step - loss: 0.9857 - accuracy: 0.5831 - val_loss: 0.9972 - val_accuracy: 0.5893
Epoch 67/170
14/14 [==============================] - 1s 100ms/step - loss: 0.9181 - accuracy: 0.6253 - val_loss: 1.1152 - val_accuracy: 0.5714
Epoch 68/170
14/14 [==============================] - 1s 101ms/step - loss: 1.0429 - accuracy: 0.5433 - val_loss: 1.1736 - val_accuracy: 0.5179
Epoch 69/170
14/14 [==============================] - 1s 100ms/step - loss: 1.0245 - accuracy: 0.5480 - val_loss: 1.0843 - val_accuracy: 0.5982
Epoch 70/170
14/14 [==============================] - 1s 99ms/step - loss: 0.9829 - accuracy: 0.5948 - val_loss: 1.0859 - val_accuracy: 0.5714
Epoch 71/170
14/14 [==============================] - 1s 100ms/step - loss: 0.9423 - accuracy: 0.5878 - val_loss: 1.1201 - val_accuracy: 0.5804
Epoch 72/170
14/14 [==============================] - 1s 100ms/step - loss: 0.9839 - accuracy: 0.5808 - val_loss: 1.0213 - val_accuracy: 0.5893
Epoch 73/170
14/14 [==============================] - 1s 99ms/step - loss: 0.9504 - accuracy: 0.6066 - val_loss: 0.9979 - val_accuracy: 0.6161
Epoch 74/170
14/14 [==============================] - 1s 100ms/step - loss: 0.9155 - accuracy: 0.6370 - val_loss: 1.1465 - val_accuracy: 0.5536
Epoch 75/170
14/14 [==============================] - 1s 100ms/step - loss: 1.0698 - accuracy: 0.5691 - val_loss: 1.0713 - val_accuracy: 0.5982
Epoch 76/170
14/14 [==============================] - 1s 99ms/step - loss: 1.0014 - accuracy: 0.5808 - val_loss: 1.0689 - val_accuracy: 0.5179
Epoch 77/170
14/14 [==============================] - 1s 104ms/step - loss: 0.9071 - accuracy: 0.6183 - val_loss: 0.9674 - val_accuracy: 0.6071
Epoch 78/170
14/14 [==============================] - 1s 100ms/step - loss: 0.8294 - accuracy: 0.6183 - val_loss: 1.1370 - val_accuracy: 0.5536
Epoch 79/170
14/14 [==============================] - 1s 100ms/step - loss: 0.9045 - accuracy: 0.6159 - val_loss: 1.1033 - val_accuracy: 0.5625
Epoch 80/170
14/14 [==============================] - 1s 100ms/step - loss: 0.8274 - accuracy: 0.6581 - val_loss: 0.9823 - val_accuracy: 0.6250
Epoch 81/170
14/14 [==============================] - 1s 104ms/step - loss: 0.8264 - accuracy: 0.6698 - val_loss: 1.2541 - val_accuracy: 0.5714
Epoch 82/170
14/14 [==============================] - 1s 100ms/step - loss: 0.9337 - accuracy: 0.6183 - val_loss: 0.9881 - val_accuracy: 0.5625
Epoch 83/170
14/14 [==============================] - 1s 105ms/step - loss: 0.8058 - accuracy: 0.6604 - val_loss: 0.9810 - val_accuracy: 0.6518
Epoch 84/170
14/14 [==============================] - 1s 106ms/step - loss: 0.8403 - accuracy: 0.6487 - val_loss: 1.0169 - val_accuracy: 0.5893
Epoch 85/170
14/14 [==============================] - 1s 100ms/step - loss: 0.8031 - accuracy: 0.6721 - val_loss: 0.9308 - val_accuracy: 0.6339
Epoch 86/170
14/14 [==============================] - 1s 101ms/step - loss: 0.7764 - accuracy: 0.6604 - val_loss: 1.1162 - val_accuracy: 0.5982
Epoch 87/170
14/14 [==============================] - 1s 100ms/step - loss: 0.7555 - accuracy: 0.6862 - val_loss: 1.1001 - val_accuracy: 0.5446
Epoch 88/170
14/14 [==============================] - 1s 100ms/step - loss: 0.8013 - accuracy: 0.6979 - val_loss: 0.9850 - val_accuracy: 0.6250
Epoch 89/170
14/14 [==============================] - 1s 99ms/step - loss: 0.7203 - accuracy: 0.7073 - val_loss: 0.9914 - val_accuracy: 0.6250
Epoch 90/170
14/14 [==============================] - 1s 100ms/step - loss: 0.7327 - accuracy: 0.7049 - val_loss: 1.0000 - val_accuracy: 0.5982
Epoch 91/170
14/14 [==============================] - 1s 104ms/step - loss: 0.8106 - accuracy: 0.6652 - val_loss: 1.1040 - val_accuracy: 0.6250
Epoch 92/170
14/14 [==============================] - 1s 100ms/step - loss: 0.7480 - accuracy: 0.6768 - val_loss: 1.1604 - val_accuracy: 0.6161
Epoch 93/170
14/14 [==============================] - 1s 99ms/step - loss: 0.7526 - accuracy: 0.6909 - val_loss: 1.0337 - val_accuracy: 0.5982
Epoch 94/170
14/14 [==============================] - 1s 100ms/step - loss: 0.8298 - accuracy: 0.6651 - val_loss: 0.9828 - val_accuracy: 0.6607
Epoch 95/170
14/14 [==============================] - 1s 101ms/step - loss: 0.7020 - accuracy: 0.7002 - val_loss: 1.1924 - val_accuracy: 0.6429
Epoch 96/170
14/14 [==============================] - 1s 99ms/step - loss: 0.7356 - accuracy: 0.6932 - val_loss: 0.9578 - val_accuracy: 0.6429
Epoch 97/170
14/14 [==============================] - 1s 99ms/step - loss: 0.7357 - accuracy: 0.7049 - val_loss: 0.9474 - val_accuracy: 0.6518
Epoch 98/170
14/14 [==============================] - 1s 99ms/step - loss: 0.6577 - accuracy: 0.7330 - val_loss: 0.9873 - val_accuracy: 0.6696
Epoch 99/170
14/14 [==============================] - 1s 100ms/step - loss: 0.6829 - accuracy: 0.7283 - val_loss: 0.9481 - val_accuracy: 0.7321
Epoch 100/170
14/14 [==============================] - 1s 99ms/step - loss: 0.6678 - accuracy: 0.7283 - val_loss: 1.0019 - val_accuracy: 0.6429
Epoch 101/170
14/14 [==============================] - 1s 100ms/step - loss: 0.6843 - accuracy: 0.7307 - val_loss: 0.9844 - val_accuracy: 0.7143
Epoch 102/170
14/14 [==============================] - 1s 99ms/step - loss: 0.7696 - accuracy: 0.7096 - val_loss: 0.9292 - val_accuracy: 0.6875
Epoch 103/170
14/14 [==============================] - 1s 100ms/step - loss: 0.6584 - accuracy: 0.7494 - val_loss: 0.9640 - val_accuracy: 0.6696
Epoch 104/170
14/14 [==============================] - 1s 104ms/step - loss: 0.6743 - accuracy: 0.7377 - val_loss: 0.9758 - val_accuracy: 0.6607
Epoch 105/170
14/14 [==============================] - 1s 100ms/step - loss: 0.6255 - accuracy: 0.7541 - val_loss: 0.9916 - val_accuracy: 0.7321
Epoch 106/170
14/14 [==============================] - 1s 101ms/step - loss: 0.6574 - accuracy: 0.7471 - val_loss: 0.9247 - val_accuracy: 0.7054
Epoch 107/170
14/14 [==============================] - 1s 104ms/step - loss: 0.6509 - accuracy: 0.7283 - val_loss: 0.9398 - val_accuracy: 0.7321
Epoch 108/170
14/14 [==============================] - 1s 100ms/step - loss: 0.6331 - accuracy: 0.7447 - val_loss: 1.0055 - val_accuracy: 0.6071
Epoch 109/170
14/14 [==============================] - 1s 100ms/step - loss: 0.5903 - accuracy: 0.7705 - val_loss: 1.2134 - val_accuracy: 0.6786
Epoch 110/170
14/14 [==============================] - 1s 99ms/step - loss: 0.8423 - accuracy: 0.6862 - val_loss: 1.1995 - val_accuracy: 0.5893
Epoch 111/170
14/14 [==============================] - 1s 99ms/step - loss: 0.8344 - accuracy: 0.6862 - val_loss: 1.2192 - val_accuracy: 0.5625
Epoch 112/170
14/14 [==============================] - 1s 105ms/step - loss: 0.7543 - accuracy: 0.7330 - val_loss: 1.0993 - val_accuracy: 0.6161
Epoch 113/170
14/14 [==============================] - 1s 100ms/step - loss: 0.8214 - accuracy: 0.6885 - val_loss: 0.8132 - val_accuracy: 0.7054
Epoch 114/170
14/14 [==============================] - 1s 100ms/step - loss: 0.6374 - accuracy: 0.7447 - val_loss: 0.8186 - val_accuracy: 0.6875
Epoch 115/170
14/14 [==============================] - 1s 101ms/step - loss: 0.6216 - accuracy: 0.7471 - val_loss: 1.0306 - val_accuracy: 0.6875
Epoch 116/170
14/14 [==============================] - 1s 99ms/step - loss: 0.5478 - accuracy: 0.7845 - val_loss: 0.8625 - val_accuracy: 0.7054
Epoch 117/170
14/14 [==============================] - 1s 101ms/step - loss: 0.5598 - accuracy: 0.7799 - val_loss: 0.8657 - val_accuracy: 0.7232
Epoch 118/170
14/14 [==============================] - 1s 100ms/step - loss: 0.5168 - accuracy: 0.7939 - val_loss: 0.9600 - val_accuracy: 0.6875
Epoch 119/170
14/14 [==============================] - 1s 99ms/step - loss: 0.5673 - accuracy: 0.7588 - val_loss: 1.1419 - val_accuracy: 0.6607
Epoch 120/170
14/14 [==============================] - 1s 100ms/step - loss: 0.7136 - accuracy: 0.7471 - val_loss: 1.0537 - val_accuracy: 0.6696
Epoch 121/170
14/14 [==============================] - 1s 100ms/step - loss: 0.5726 - accuracy: 0.7822 - val_loss: 0.8318 - val_accuracy: 0.7232
Epoch 122/170
14/14 [==============================] - 1s 105ms/step - loss: 0.5599 - accuracy: 0.8056 - val_loss: 1.0877 - val_accuracy: 0.6429
Epoch 123/170
14/14 [==============================] - 1s 101ms/step - loss: 0.5169 - accuracy: 0.7963 - val_loss: 0.8707 - val_accuracy: 0.7321
Epoch 124/170
14/14 [==============================] - 1s 100ms/step - loss: 0.5094 - accuracy: 0.7916 - val_loss: 1.0653 - val_accuracy: 0.6607
Epoch 125/170
14/14 [==============================] - 1s 100ms/step - loss: 0.5266 - accuracy: 0.8173 - val_loss: 0.9710 - val_accuracy: 0.6964
Epoch 126/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4988 - accuracy: 0.8009 - val_loss: 0.9626 - val_accuracy: 0.7232
Epoch 127/170
14/14 [==============================] - 1s 104ms/step - loss: 0.4983 - accuracy: 0.7946 - val_loss: 0.9777 - val_accuracy: 0.6875
Epoch 128/170
14/14 [==============================] - 1s 100ms/step - loss: 0.4235 - accuracy: 0.8173 - val_loss: 1.0410 - val_accuracy: 0.7054
Epoch 129/170
14/14 [==============================] - 1s 100ms/step - loss: 0.5119 - accuracy: 0.8009 - val_loss: 0.7701 - val_accuracy: 0.7768
Epoch 130/170
14/14 [==============================] - 1s 101ms/step - loss: 0.5059 - accuracy: 0.8126 - val_loss: 0.8662 - val_accuracy: 0.7143
Epoch 131/170
14/14 [==============================] - 1s 101ms/step - loss: 0.5121 - accuracy: 0.8126 - val_loss: 0.9001 - val_accuracy: 0.6786
Epoch 132/170
14/14 [==============================] - 1s 101ms/step - loss: 0.5085 - accuracy: 0.8056 - val_loss: 0.9805 - val_accuracy: 0.6518
Epoch 133/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4568 - accuracy: 0.8197 - val_loss: 0.8003 - val_accuracy: 0.7679
Epoch 134/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4059 - accuracy: 0.8407 - val_loss: 0.8546 - val_accuracy: 0.7321
Epoch 135/170
14/14 [==============================] - 1s 105ms/step - loss: 0.3803 - accuracy: 0.8595 - val_loss: 0.9593 - val_accuracy: 0.7054
Epoch 136/170
14/14 [==============================] - 1s 100ms/step - loss: 0.4311 - accuracy: 0.8267 - val_loss: 0.8905 - val_accuracy: 0.7768
Epoch 137/170
14/14 [==============================] - 1s 105ms/step - loss: 0.4334 - accuracy: 0.8147 - val_loss: 1.1366 - val_accuracy: 0.6518
Epoch 138/170
14/14 [==============================] - 1s 101ms/step - loss: 0.5346 - accuracy: 0.7892 - val_loss: 0.9859 - val_accuracy: 0.6518
Epoch 139/170
14/14 [==============================] - 1s 101ms/step - loss: 0.5416 - accuracy: 0.7799 - val_loss: 0.9841 - val_accuracy: 0.6429
Epoch 140/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4333 - accuracy: 0.8384 - val_loss: 1.0966 - val_accuracy: 0.7143
Epoch 141/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4238 - accuracy: 0.8197 - val_loss: 1.0585 - val_accuracy: 0.7232
Epoch 142/170
14/14 [==============================] - 1s 105ms/step - loss: 0.4651 - accuracy: 0.8173 - val_loss: 1.1820 - val_accuracy: 0.5625
Epoch 143/170
14/14 [==============================] - 1s 102ms/step - loss: 0.5246 - accuracy: 0.7986 - val_loss: 1.2815 - val_accuracy: 0.6518
Epoch 144/170
14/14 [==============================] - 1s 104ms/step - loss: 0.4874 - accuracy: 0.8103 - val_loss: 1.0808 - val_accuracy: 0.6875
Epoch 145/170
14/14 [==============================] - 1s 101ms/step - loss: 0.3979 - accuracy: 0.8618 - val_loss: 0.9828 - val_accuracy: 0.7321
Epoch 146/170
14/14 [==============================] - 1s 101ms/step - loss: 0.3740 - accuracy: 0.8571 - val_loss: 1.0767 - val_accuracy: 0.6607
Epoch 147/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4167 - accuracy: 0.8571 - val_loss: 1.0912 - val_accuracy: 0.7232
Epoch 148/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4474 - accuracy: 0.8290 - val_loss: 1.1903 - val_accuracy: 0.6786
Epoch 149/170
14/14 [==============================] - 1s 105ms/step - loss: 0.4219 - accuracy: 0.8214 - val_loss: 1.1897 - val_accuracy: 0.6518
Epoch 150/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4001 - accuracy: 0.8595 - val_loss: 1.0485 - val_accuracy: 0.6875
Epoch 151/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4631 - accuracy: 0.8126 - val_loss: 1.0839 - val_accuracy: 0.7054
Epoch 152/170
14/14 [==============================] - 1s 100ms/step - loss: 0.3413 - accuracy: 0.8689 - val_loss: 1.2102 - val_accuracy: 0.6607
Epoch 153/170
14/14 [==============================] - 1s 100ms/step - loss: 0.4864 - accuracy: 0.8126 - val_loss: 1.1980 - val_accuracy: 0.6875
Epoch 154/170
14/14 [==============================] - 1s 105ms/step - loss: 0.4387 - accuracy: 0.8431 - val_loss: 1.0346 - val_accuracy: 0.7500
Epoch 155/170
14/14 [==============================] - 1s 105ms/step - loss: 0.4845 - accuracy: 0.8407 - val_loss: 1.1417 - val_accuracy: 0.6607
Epoch 156/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4328 - accuracy: 0.8501 - val_loss: 0.9922 - val_accuracy: 0.7143
Epoch 157/170
14/14 [==============================] - 1s 101ms/step - loss: 0.4548 - accuracy: 0.8314 - val_loss: 1.0846 - val_accuracy: 0.7232
Epoch 158/170
14/14 [==============================] - 1s 100ms/step - loss: 0.4269 - accuracy: 0.8478 - val_loss: 1.1466 - val_accuracy: 0.6607
Epoch 159/170
14/14 [==============================] - 1s 101ms/step - loss: 0.3734 - accuracy: 0.8642 - val_loss: 1.1919 - val_accuracy: 0.6875
Epoch 160/170
14/14 [==============================] - 1s 105ms/step - loss: 0.3387 - accuracy: 0.8806 - val_loss: 1.2281 - val_accuracy: 0.7054
Epoch 161/170
14/14 [==============================] - 1s 100ms/step - loss: 0.3478 - accuracy: 0.8571 - val_loss: 0.9892 - val_accuracy: 0.7054
Epoch 162/170
14/14 [==============================] - 1s 101ms/step - loss: 0.3453 - accuracy: 0.8665 - val_loss: 0.9406 - val_accuracy: 0.7321
Epoch 163/170
14/14 [==============================] - 1s 105ms/step - loss: 0.3653 - accuracy: 0.8712 - val_loss: 1.1604 - val_accuracy: 0.6607
Epoch 164/170
14/14 [==============================] - 1s 101ms/step - loss: 0.3385 - accuracy: 0.8735 - val_loss: 1.1445 - val_accuracy: 0.6964
Epoch 165/170
14/14 [==============================] - 1s 102ms/step - loss: 0.3401 - accuracy: 0.8689 - val_loss: 1.0978 - val_accuracy: 0.6875
Epoch 166/170
14/14 [==============================] - 1s 101ms/step - loss: 0.3308 - accuracy: 0.8923 - val_loss: 1.1052 - val_accuracy: 0.7054
Epoch 167/170
14/14 [==============================] - 1s 104ms/step - loss: 0.3685 - accuracy: 0.8665 - val_loss: 1.5219 - val_accuracy: 0.6875
Epoch 168/170
14/14 [==============================] - 1s 101ms/step - loss: 0.3192 - accuracy: 0.8782 - val_loss: 1.2869 - val_accuracy: 0.7143
Epoch 169/170
14/14 [==============================] - 1s 100ms/step - loss: 0.2738 - accuracy: 0.9040 - val_loss: 1.2734 - val_accuracy: 0.7054
Epoch 170/170
14/14 [==============================] - 1s 100ms/step - loss: 0.3143 - accuracy: 0.8899 - val_loss: 1.4702 - val_accuracy: 0.7143

Plot accuracy curves.

In [17]:
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title('Training and validation accuracy')
plt.show()

Making Predictions

In [18]:
load_img("dog_photos/test/image4.jpg",target_size=(180,180))
Out[18]:

Randomly select an image from the test set and feed it to our model to make predictions.

In [19]:
image=load_img("dog_photos/test/image4.jpg",target_size=(100,100))

image=img_to_array(image) 
image=image/255.0
prediction_image=np.array(image)
prediction_image= np.expand_dims(image, axis=0)
In [20]:
prediction=model.predict(prediction_image)
value=np.argmax(prediction)
move_name=mapper(value)
print("Prediction is {}.".format(move_name))
Prediction is bulldog.
In [21]:
model.save('saved_models/DogBreed.tf')
WARNING:tensorflow:From /opt/tljh/user/lib/python3.7/site-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
WARNING:tensorflow:From /opt/tljh/user/lib/python3.7/site-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
INFO:tensorflow:Assets written to: saved_models/DogBreed.tf/assets

deepCC

In [24]:
!deepCC 'saved_models/DogBreed.tf'
[INFO]
Reading [tensorflow model] 'saved_models/DogBreed.tf'
[SUCCESS]
Saved 'DogBreed_deepC/DogBreed.tf.onnx'
[INFO]
Reading [onnx model] 'DogBreed_deepC/DogBreed.tf.onnx'
[INFO]
Model info:
  ir_vesion : 4
  doc       : 
[WARNING]
[ONNX]: terminal (input/output) conv2d_input_0's shape is less than 1. Changing it to 1.
[WARNING]
[ONNX]: terminal (input/output) Identity_0's shape is less than 1. Changing it to 1.
[INFO]
Running DNNC graph sanity check ...
[SUCCESS]
Passed sanity check.
[INFO]
Writing C++ file 'DogBreed_deepC/DogBreed.cpp'
[INFO]
deepSea model files are ready in 'DogBreed_deepC/' 
[RUNNING COMMAND]
g++ -std=c++11 -O3 -fno-rtti -fno-exceptions -I. -I/opt/tljh/user/lib/python3.7/site-packages/deepC-0.13-py3.7-linux-x86_64.egg/deepC/include -isystem /opt/tljh/user/lib/python3.7/site-packages/deepC-0.13-py3.7-linux-x86_64.egg/deepC/packages/eigen-eigen-323c052e1731 "DogBreed_deepC/DogBreed.cpp" -D_AITS_MAIN -o "DogBreed_deepC/DogBreed.exe"
[RUNNING COMMAND]
size "DogBreed_deepC/DogBreed.exe"
   text	   data	    bss	    dec	    hex	filename
 939263	   3968	    760	 943991	  e6777	DogBreed_deepC/DogBreed.exe
[SUCCESS]
Saved model as executable "DogBreed_deepC/DogBreed.exe"