Stuck behind the paywall? Click here to read the full story with my friend link!

According to dogtime.com, there are 266 different breeds of dogs, and by alone thinking about this number, it frightens me to distinguish them. And most of the people, if they’re normal, just know about 5–10 breeds because you don’t see the chapter “266 Different Dog Breeds” in a Bachelor’s Curriculum.


Overview

The main aim of this project is to build an algorithm to classify the different Dog Breeds from the dataset.

This seems like a simple task but when we think of Machine Learning, then it is not! The Images are in random order, having dogs at random spaces in the images, the images are shot in different lightenings, there is no preprocessing done on the data, it’s just a dataset containing simple dogs pictures.

So, the first step is to give the dataset a look.

Environment and tools

Data

The Dataset used for this project is Stanford Dogs Dataset. The Dataset contains a total of 20,580 images of 120 different dog breeds.

The Stanford Dogs dataset contains images of 120 breeds of dogs from around the world. This dataset has been built using images and annotation from ImageNet for the task of fine-grained image categorization.

Importing Libraries

import os
import sys
import keras
import tarfile
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.engine.training import Model
from sklearn.preprocessing import LabelBinarizer
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Add, Dropout, Flatten, Dense, Activation

Data Preprocessing

I found 5 directories to be unusable and hence, didn’t used them. So, I imported a total of 115 Breeds.

import cv2

BASEPATH = './Images'
LABELS = set()
paths = []
for d in os.listdir(BASEPATH):
LABELS.add(d)
paths.append((BASEPATH + '/' + d, d))
## resizing and converting to RGB
def load_and_preprocess_image(path):
image = cv2.imread(path)
image = cv2.resize(image, (224, 224))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
X, y = [], []
i = 0
for path, label in paths:
i += 1
## Faulty Directories
if i == 18 or i == 23 or i == 41 or i == 49 or i == 90: continue 
if path == "./Images/.DS_Store": continue
for image_path in os.listdir(path):
image = load_and_preprocess_image(path + "/" + image_path)
X.append(image)
y.append(label)

Now, the names of the folder are in this pattern ‘n8725563753-Husky’, hence, we need to clean this up to be left with the _‘Husky’ _part of the name.

Y = []

## Cleaning the names of the directories/targets
for i in y:
Y.append(i.split('-')[1])

#transfer-learning #machine-learning #classification #deep-learning #convolutional-network

Deep Learning for Dog Breed Classification
6.15 GEEK