Deep Learning (CNN) — Discover the Breed of a Dog in an Image
Last Updated on July 19, 2023 by Editorial Team
Author(s): Saniya Parveez
Originally published on Towards AI.
Deep Learning
Introduction
Convolutional Neural Networks (CNNs) are deep learning networks, which are excellent in object recognition of images. Its design is inspired by a part of the brain called the visual cortex. The role of the visual cortex is to process visual data. It has created many wonderful impacts on advanced artificial intelligence especially achieved great success in image classification problems.
It is very important to understand the basic concepts in image recognition before the study of CNN:
- Feature Detection
- Convolution
Feature Detection
Images are composed of a grid composed of many small squares. This is called a pixel. Each pixel is associated with a value in the range of [0–225], where 0 is black and 255 is white. Finally, a color image is represented by a group of three metrics and each corresponds to one color channel (red, green, blue). This representation is shown below:
So, it creates a matrix called kernel filter or feature detector.
Convolution
In Mathematics especially in functional analysis, convolution is a mathematical operation on two functions that produces a third function and tells the shape of one in case of medication by others. So, the kernel filter moves along the input matrix and performs a scalar product between the kernel values and those of the matrix portion to which it applied. This result is a new matrix called the Convolution matrix.
CNN Architecture
Let’s take a black and white image (5*5 matrix) and the input matrix will be 5*5. So, here CNN consists of an input layer consisting of 25 neurons (5*5 = 25). Its main aim is to acquire the input value corresponding to each pixel and transfer it to the next hidden layer.
In CNN, each neuron is connected to a certain region of the input area called the receptive field. To effectively recognize an image, we need different kernel filters applied to the same receptive field, because each filter should recognize a different feature’s image. The set of neurons that identifies the same feature define a single feature map.
This picture represents the architecture of CNN. Here the input size of the image is 28*28 and will be analyzed by a convolutional layer composed of 32 feature maps with a size of 28*28.
This architecture also shows a receptive field and the kernel filter of a 3*3 size.
The CNN can consist of several convolutional layers connected in cascade. The output of each convolution layer is a set of feature maps and all these matrices define a new input layer that will be used by the next layer.
In CNN, each neuron produces an output, after an activation threshold. The CNNs also use pooling layers positioned immediately after the convolutional layers.
The last hidden layer of a convolutional network is generally a fully-connected network with a softmax activation function for the output layer.
Dog Breed Identification by using CNN
CNN can easily identify the type of good pup, breed, etc.
Building Dataset
- test:- images of dogs
- train:- images of different dogs
- label.csv:- different types of breeds
import os
import seaborn as sns
import matplotlib
from shutil import copyfile
import matplotlib.pyplot as plt
Read label data for breeds:
labels = pd.read_csv('labels.csv')labels.head()
Convert breeds into different classes:
labels_dict = {i:j for i,j in zip(labels['id'],labels['breed'])}classes = set(labels_dict.values())classes
Get all training images of dogs:
images = [f for f in os.listdir('train/')]
Split training images into training and validation images:
split = int(len(images) * 0.85)training_images = images[:split]validation_images = images[split:]
Keep training_images and validation_images into two separate directories:
if not os.path.exists('training_images'):
os.makedirs('training_images')if not os.path.exists('validation_images'):
os.makedirs('validation_images')
Make subdirectory of classes for the training_images:
for curClass in classes:
if not os.path.exists(os.path.join('training_images', curClass)):
os.makedirs(os.path.join('training_images', curClass))
Make subdirectory of classes for the validation_images:
for curClass in classes:
if not os.path.exists(os.path.join('validation_images', curClass)):
os.makedirs(os.path.join('validation_images', curClass))
Keep divided images into two different directories:
count = 0destination_directory = 'training_images/'for item in images:
if count >7999:
destination_directory = 'validation_images/'
filekey = os.path.splitext(item)[0]
des = destination_directory + labels_dict[filekey]+'/'+item
print(des)
if not os.path.exists(des):
src = '/kaggle/input/dog-breed-identification/train/' + item
copyfile(src, des)
print(labels_dict[filekey])
count +=1
Preprocessing
Import the preprocessing library of Keras:
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_imgdatagen = ImageDataGenerator(
rotation_range=50,
width_shift_range=0.3,
height_shift_range=0.2,
shear_range=0.3,
zoom_range=0.3,
horizontal_flip=True,
fill_mode='nearest')
Convert to the array:
x = img_to_array(img)x = x.reshape((1,) + x.shape)
Import more libraries:
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Densefrom keras.layers import Conv2D,Dropoutfrom keras.preprocessing.image import ImageDataGeneratortrain_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)test_datagen = ImageDataGenerator(rescale=1./255)training_set = train_datagen.flow_from_directory(
'training_images',
target_size=(128, 128),
batch_size=20,
class_mode='categorical')test_datagen = ImageDataGenerator(rescale=1./255)training_set = train_datagen.flow_from_directory(
'training_images',
target_size=(128, 128),
batch_size=20,
class_mode='categorical')test_set = test_datagen.flow_from_directory(
'validation_images',
target_size=(128, 128),
batch_size=20,
class_mode='categorical')
Build Layer
from keras.layers import Dropout
clf = Sequential()
#Convolution
#32 is number of kernals of 3x3, we can use 64 128 256 etc in next layers
#input shape can be 128, 256 later
clf.add(Conv2D(32,(3,3),input_shape=(128,128,3),activation='relu'))
#Max Pooling size reduces divided by 2
clf.add(MaxPooling2D(pool_size=(2,2)))#clf.add(Dropout(0.5))clf.add(Conv2D(32,(3,3), activation='relu'))
clf.add(MaxPooling2D(pool_size=(2,2)))
#clf.add(Dropout(0.25))clf.add(Conv2D(64, (3, 3), activation='relu'))
clf.add(MaxPooling2D(pool_size=(2, 2)))
#clf.add(Dropout(0.10))
#Flattening
clf.add(Flatten())
#Adding An ANN
#lets take 128 hidden nodes in hidden layer
#clf.add(Dense(units=128,activation='relu'))
clf.add(Dense(units=64, activation='relu'))
clf.add(Dropout(0.5))
clf.add(Dense(units=120,activation='softmax'))
#stochastic gradient descent -Adam -optimizerclf.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
Add early stopping:
from keras.callbacks import EarlyStoppingearly_stopping_monitor=EarlyStopping(patience=6)
Fit generator:
hist=clf.fit_generator(
training_set,
steps_per_epoch=400,
epochs=50,
validation_data=test_set,
validation_steps=2222,
callbacks=[early_stopping_monitor])
Predict
Import test data to predict.
import cv2test_set = []test_set_ids = []for curImage in os.listdir('/kaggle/input/dog-breed-identification/test'):
test_set_ids.append(os.path.splitext(curImage)[0])
curImage = cv2.imread('/kaggle/input/dog-breed-identification/test/'+curImage)
test_set.append(cv2.resize(curImage,(128, 128)))test_set = np.array(test_set, np.float32)/255.0
Predict:
predictions= clf.predict(test_set)
Map the prediction data with test data:
predictions_df = pd.DataFrame(predictions)predictions_df.columns = column_namespredictions_df.insert(0,'id', test_set_ids)predictions_df
Plot
Validation loss vs epochs:
plt.plot(hist.history['val_loss'])plt.xlabel('epochs')plt.ylabel('validation loss')plt.show()
Training loss vs Validation loss:
plt.plot(hist.history['loss'],label="traing loss")plt.plot(hist.history['val_loss'], label="Validation loss")plt.legend()plt.xlabel('epochs')plt.show()
Conclusion
The design of convolutional neural networks takes inspiration from the visual cortex — the area of the brain that processes visual input. It’s safe to say that CNN is underpinning many of the most impactful current advances in artificial intelligence and machine learning. Variants of CNN are applied to some of the most sophisticated visual, linguistic, and problem-solving applications in existence.
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.
Published via Towards AI