import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
import os
import PIL
import pathlib
import random
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from keras.preprocessing.image import ImageDataGenerator
#pip install keras
image_dir = '/home/ubuntu/Data/catsdogs/all'
image_set = pathlib.Path(image_dir)
Number of images
no_image = len(list(image_set.glob('*/*.jpg')))
print(no_image)
all_image = list(image_set.glob('*/*.jpg'))
plt.figure(figsize=(12,10))
for i in range(9):
img_no = random.randrange(0,len(all_image))
plt.subplot(3,3,i+1)
plt.title("Img " +str(img_no))
plt.imshow(PIL.Image.open(str(all_image[img_no])))
plt.axis('off')
plt.show()
cats= list(image_set.glob('cats/*'))
plt.figure(figsize=(12,10))
for i in range(9):
cat_no = random.randrange(0,len(cats))
plt.subplot(3,3,i+1)
plt.title("Cat "+str(cat_no))
plt.imshow(PIL.Image.open(str(cats[cat_no])))
plt.axis('off')
plt.show()
dogs= list(image_set.glob('dogs/*'))
plt.figure(figsize=(12,10))
for i in range(9):
dog_no = random.randrange(0,len(dogs))
plt.subplot(3,3,i+1)
plt.title("Dog "+str(cat_no))
plt.imshow(PIL.Image.open(str(dogs[dog_no])))
plt.axis("off")
plt.show()
batch_size = 32
img_height = 200
img_width = 200
# training dataset
train_img= tf.keras.preprocessing.image_dataset_from_directory(image_set,validation_split=0.2,
subset='training',seed=100,image_size = (img_height,img_width),
batch_size=batch_size)
# validating dataset
valid_img= tf.keras.preprocessing.image_dataset_from_directory(image_set,validation_split=0.2,
subset='validation',seed=100,image_size = (img_height,img_width),
batch_size=batch_size)
#check the number of models
class_names = train_img.class_names
print(class_names)
# image shape and batch size
for image_batch, labels_batch in train_img:
print(image_batch.shape)
print(labels_batch.shape)
break
num_classes = 2
model = Sequential([
layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height,img_width, 3)),
layers.Conv2D(16,3, padding = 'same', activation = 'relu'),
layers.MaxPooling2D(),
layers.Conv2D(32,3, padding = 'same', activation = 'relu'),
layers.MaxPooling2D(),
layers.Conv2D(64,3, padding = 'same', activation = 'relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(128, activation = 'relu'),
layers.Dense(num_classes)
])
model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
model.summary()
epochs= 10
history = model.fit(train_img, validation_data=valid_img,epochs=epochs)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
#epochs_range = range(1, len(acc) + 1)
epochs_range = range(epochs)
plt.figure(figsize=(10,8))
plt.plot(epochs_range,acc,'b',label = 'Training Accuracy')
plt.plot(epochs_range, val_acc,'r',label='Validation Accuracy')
plt.title('Training and Validation accuracy')
plt.legend()
plt.figure(figsize=(10,8))
plt.plot(epochs_range,loss,'b',label='Training loss')
plt.plot(epochs_range,val_loss,label='Validation loss')
plt.title('Training and Validation loss')
plt.legend()
plt.show()
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=(14, 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='lower left')
plt.title('Training and Validation Loss')
plt.show()
test_datagen = ImageDataGenerator(rescale=1./255)
In above graph, large diffence in accuracy between training and validation is sign of overfitting
data_augmentation = keras.Sequential(
[
layers.experimental.preprocessing.RandomFlip("horizontal",
input_shape=(img_height,
img_width,
3)),
layers.experimental.preprocessing.RandomRotation(0.1),
layers.experimental.preprocessing.RandomZoom(0.1),
]
)
plt.figure(figsize=(10, 10))
for images, _ in train_img.take(1):
for i in range(9):
augmented_images = data_augmentation(images)
ax = plt.subplot(3, 3, i + 1)
plt.imshow(augmented_images[0].numpy().astype("uint8"))
plt.axis("off")
model = Sequential([
data_augmentation,
layers.experimental.preprocessing.Rescaling(1./255),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
epochs = 15
history = model.fit(train_img, validation_data=valid_img, epochs=epochs)
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=(14, 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='lower left')
plt.title('Training and Validation Loss')
plt.show()