Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!


Deep Learning (CNN) — Discover the Breed of a Dog in an Image
Latest   Machine Learning

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


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.


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.

CNN Architecture

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'):
if not os.path.exists('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
if not os.path.exists(des):
src = '/kaggle/input/dog-breed-identification/train/' + item
copyfile(src, des)
count +=1


Import the preprocessing library of Keras:

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_imgdatagen = ImageDataGenerator(

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 Dense
from keras.layers import Conv2D,Dropoutfrom keras.preprocessing.image import ImageDataGeneratortrain_datagen = ImageDataGenerator(
test_datagen = ImageDataGenerator(rescale=1./255)training_set = train_datagen.flow_from_directory(
target_size=(128, 128),
test_datagen = ImageDataGenerator(rescale=1./255)training_set = train_datagen.flow_from_directory(
target_size=(128, 128),
test_set = test_datagen.flow_from_directory(
target_size=(128, 128),

Build Layer

from keras.layers import Dropout
clf = Sequential()
#32 is number of kernals of 3x3, we can use 64 128 256 etc in next layers
#input shape can be 128, 256 later
#Max Pooling size reduces divided by 2
#clf.add(Dropout(0.5))clf.add(Conv2D(32,(3,3), activation='relu'))
clf.add(Conv2D(64, (3, 3), activation='relu'))
clf.add(MaxPooling2D(pool_size=(2, 2)))

#Adding An ANN
#lets take 128 hidden nodes in hidden layer
clf.add(Dense(units=64, activation='relu'))
#stochastic gradient descent -Adam -optimizer

Add early stopping:

from keras.callbacks import EarlyStoppingearly_stopping_monitor=EarlyStopping(patience=6)

Fit generator:



Import test data to predict.

import cv2test_set = []test_set_ids = []for curImage in os.listdir('/kaggle/input/dog-breed-identification/test'):
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


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


Validation loss vs epochs:

plt.plot(hist.history['val_loss'])plt.xlabel('epochs')plt.ylabel('validation loss')

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')


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

Feedback ↓