NOTE: This Use Case is not purposed for resource constrained devices.
In [1]:
# Import all the necessary libraries
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten, BatchNormalization
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import Sequential
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.models import load_model
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import os
import cv2
import pandas as pd
import random
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix
from sklearn.metrics import plot_confusion_matrix
Unzip the Database¶
In [2]:
!wget 'https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/grape.zip'
!unzip -qo grape.zip
!rm grape.zip
Training Path¶
In [3]:
train_path = os.path.join(os.getcwd(), 'data', 'train')
validation_path = os.path.join(os.getcwd(), 'data', 'validation')
os.listdir(train_path)
Out[3]:
Plotting Function to Visualize the Images¶
In [4]:
# Get image as numpy array
def load_image(name, path):
img_path = os.path.join(path, name)
img = cv2.imread(img_path)
# img = load_img(img_path, target_size = (256, 256))
img = img[:,:, ::-1]
return img
# Plot numpy array
def plot_image(img, name, title):
#image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.suptitle(title)
#plt.title(name)
# Plot a grid of examples
def plot_grid(img_names, img_root, title ,rows=2, cols=3):
fig = plt.figure(figsize=(8,8))
for i,name in enumerate(img_names):
#print(os.path.join(img_root, name))
fig.add_subplot(rows,cols,i+1)
img = load_image(name, img_root)
plt.axis("off")
plot_image(img, name, title)
plt.show()
In [5]:
red_path = os.path.join(train_path, "red")
red_images = os.listdir(red_path)
plot_grid(red_images[-6:], red_path, title = "Red Images")
In [6]:
yellow_path = os.path.join(train_path, "yellow")
yellow_images = os.listdir(yellow_path)
plot_grid(yellow_images[-6:], yellow_path, title = "Yellow Images")
Defining the training and validation image data generator to expand our dataset of training and validation images. This is done by artificially generating new images by processing the exsisting images. By random flipping, rotating, scaling, and perfroming similar operations, we can create new images.
In [7]:
train_image_generator = ImageDataGenerator(
rescale = 1.0/255.0,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True
)
valid_image_generator = ImageDataGenerator(
rescale = 1.0/255.0)
Loading the Training and Validation Images From Their Respective Directories¶
In [8]:
training_images = train_image_generator.flow_from_directory(train_path,
target_size = (256,256),
class_mode = 'categorical',
batch_size = 20,
classes = ['red', 'yellow']
#color_mode="grayscale"
)
validation_images = valid_image_generator.flow_from_directory(validation_path,
target_size = (256,256),
class_mode = 'categorical',
batch_size = 20,
classes = ['red', 'yellow']
#color_mode="grayscale"
)
Initializing the Model¶
In [9]:
# Defining the architecture for our neural network model
model=Sequential()
model.add(Conv2D(128,(3,3), activation='relu', input_shape=(256,256,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64,(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32,(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(16,(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(16,(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(150,activation='relu'))
model.add(Dense(100,activation='relu'))
model.add(Dense(2, activation='sigmoid'))
model.summary()
In [10]:
model.compile(loss="categorical_crossentropy",
optimizer=Adam(learning_rate = 10 ** -5),
metrics=['accuracy'])
In [11]:
# Defiining Early Stopping function to monitor Validation Loss
es = EarlyStopping(monitor='val_loss', patience=5)
Model Training¶
In [12]:
# Training the model
history = model.fit_generator(training_images,
steps_per_epoch = 15,
epochs = 50,
validation_data= validation_images,
validation_steps = 1,
callbacks = [es]
)
In [13]:
model.save('best_model.h5')
In [14]:
# Function to plot "accuracy vs epoch" graphs and "loss vs epoch" graphs for training and validation data
def plot_metrics(model_name, metric = 'accuracy'):
if metric == 'loss':
plt.title("Loss Values")
plt.plot(model_name.history['loss'], label = 'train')
plt.plot(model_name.history['val_loss'], label = 'test')
plt.legend()
plt.show()
else:
plt.title("Accuracy Values")
plt.plot(model_name.history['accuracy'], label='train')
plt.plot(model_name.history['val_accuracy'], label='test')
plt.legend()
plt.show()
In [15]:
plot_metrics(history, 'accuracy')
plot_metrics(history, 'loss')
Predicting using our Model¶
In [16]:
prediction_loss, prediction_accuracy = model.evaluate(validation_images)
print("Prediction Accuracy: ", prediction_accuracy)
In [17]:
model = load_model('best_model.h5')
Randomly Selecting a Red and Yellow Image from the Dataset to Predict the Colour¶
In [18]:
def predict(img_path, model):
image = load_img(img_path)
image = image.resize((256,256))
image = img_to_array(image)
image = np.expand_dims(image, axis = 0)
pred = np.argmax(model.predict(image))
if pred == 0:
return 'Red'
return 'Yellow'
In [19]:
img_path = os.path.join (red_path, random.choice(os.listdir(red_path)))
prediction = predict(img_path, model)
pred = 'Prediction is ' + prediction
img = cv2.imread(img_path)
img = img[:,:, ::-1]
plt.imshow(img)
plt.grid(False)
plt.axis("off")
plt.title(pred)
plt.suptitle("Actual Colour : Red")
Out[19]:
In [20]:
img_path = os.path.join (yellow_path, random.choice(os.listdir(yellow_path)))
prediction = predict(img_path, model)
pred = 'Prediction is ' + prediction
img = cv2.imread(img_path)
img = img[:,:, ::-1]
plt.imshow(img)
plt.grid(False)
plt.axis("off")
plt.title(pred)
plt.suptitle("Actual Colour : Yellow")
Out[20]: