Image classifier using CNN



Machine learning is currently one of the hottest topics in the world. Well, it could even be said to be the new electricity in the modern world. But to be precise, what is machine learning, then this is just one of the ways to teach a machine, providing a large amount of data. To learn more about machine learning and its algorithms, you can refer to some of the links provided in the reference sections of this article.

Today we will create a custom image classifier that can distinguish if a given snapshot is a dog or a cat or something else, depending on your data. To achieve our goal, we will use one of the well-known machine learning algorithms that is used to classify images, i.e. Convolutional Neural Network (or CNN).
So basically what is CNN — since we know its machine learning algorithm for machines to understand the features of the image with foresight and memorize functions to guess if the name of the new image is being fed to the machine. Since this is not an article explaining CNN, so I`ll add some links at the end if you guys are curious about how CNN works and behaves.

So, after going through all these links, let`s see how to create our own cat-vs-dog image classifier. For the dataset, we will use the kaggle cat-vs-dog dataset:

Now after getting the dataset , we need to preprocess the data a bit and provide labels to each image specified there while training the dataset. To do this, we can see that the name of each image of the training dataset starts with “cat” or “dog”, so we will use that to our advantage and then use one hot encoder for the machine to understand the labels (cat [1, 0 ] or dog [0, 1]).

 def label_img (img): word_label = img.split (`.`) [- 3] # DIY One hot encoder if word_label ==` cat`: return [1, 0] elif word_label == `dog`: return [0, 1] 

Required libraries:

  • TFLearn — a deep learning library with a high-level API for TensorFlow used to create our CNN layers
  • tqdm — Instantly have your loops display a smart progress bar, just for design convenience
  • numpy — handle image matrices
  • open-cv — for image processing such as converting them to grayscale, etc.
  • os — To access the file system to read the train image and check the catalog from our machines
  • random — shuffle the data to overcome the bias
  • matplotlib — to display the result of our predictive result.
  • flow tensor — just use a tensor board to compare the loss and Adam`s curve with our result data or the resulting log.

TRAIN_DIR and TEST_DIR should be set according to user experience and play with basic hyperparameters like epoch, learning speed, etc. to improve accuracy. I converted the image to grayscale so that we only have to deal with the 2nd matrix, otherwise the 3D matrix is ​​difficult to apply directly to CNN, especially not recommended for beginners. Below is the code which is carefully commented or you can find it here on my GitHub account at this link .

# Python program for creating
# Image classifier using CNN

 
# Import required libraries

import cv2

import os

import numpy as np

from random import shuffle

from tqdm import tqdm

 
& # 39; & # 39; & # 39; Setting env & # 39; & # 39; & # 39;

 

TRAIN_DIR = `E: / dataset / Cats_vs_Dogs / train`

TEST_DIR = ` E: / dataset / Cats_vs_Dogs / test1`

IMG_SIZE = 50

LR = 1e - 3

 

 
& # 39; & # 39; & # 39; Model setup to help with tensorflow models & # 39; & # 39; & # 39;

MODEL_NAME = ` dogsvscats - {} - {}. model` . format (LR, ` 6conv-basic` )

 
& # 39; & # 39; & # 39; Dataset Marking & # 39; & # 39; & # 39;

def label_img (img):

word_label = img.split ( ` .` ) [ - 3 ]

# DIY One hot encoder

if word_label = = `cat` : return [ 1 , 0 ]

elif word_label = = `dog` : return [ 0 , 1 ]

 
& # 39; & # 39; & # 39; Create training data & # 39; & # 39; & # 39;

def create_train_data ():

# Create an empty list where we should store training data

# after a little preprocessing of the data

training_data = []

 

# tqdm is used for interactive download only

# load training data

  for img in tqdm (os.listdir (TRAIN_DIR)):

 

# labeling images

  label = label_img (img)

 

path = os.path.join (TRAIN_DIR, img)

 

# load images from path and then convert them to

# grayscale for easier covnet search

img = cv2.imread (path, cv2.IMREAD_GRAYSCALE)

 

# resize the image for processing them in covnet

img = cv2.resize (img, (IMG_SIZE, IMG_SIZE))

 

# the last step is to generate a training data list with an array of images

training_data.append ([np.array (img), np.array (label)])

 

# shuffle the training data to keep our random state data

shuffle (training_data)

  

  # save our trained data for later use as needed

np.save ( `train_data.npy` , training_data)

return training_data

 
& # 39; & # 39; & # 39; Data processing of this test & # 39; & # 39; & # 39;
# Almost the same as processing training data, but
# we shouldn`t label this.

def process_test_data ():

testing_data = []

  for img in tqdm (os.listdir (TEST_DIR)):

path = os.path.join (TEST_DIR , img)

img_num = img.split ( `.` ) [ 0 ]

img = cv2.imread (path, cv2.IMREAD_GRAYSCALE)

img = cv2.resize (img, (IMG_SIZE, IMG_SIZE))

testing_data.append ([np.array (img), img_num])

 

shuffle ( testing_data)

np.save ( `test_data.npy` , testing_data)

  return testing_data

  
& # 39; & # 39; & # 39; Training and testing on the dataset for our model & # 39; & # 39; & # 39;

train_data = create_train_data ()

test_data = process_test_data ()

 
# train_data = np.load (& # 39 ; train_data.npy & # 39;)
# test_data = np.load (& # 39; test_data.npy & # 39;)
& # 39; & # 39; & # 39; Building a neural network using tensorflow & # 39; & # 39; & # 39;
# Import required libraries

import tflearn

from tflearn.layers.conv import conv_2d, max_pool_2d

from tflearn.layers.core import input_data, dropout, fully_connected

from tflearn.layers.estimator import regression

 

import tensorflow as tf

tf.reset_default_graph ()

convnet = input_data (shape = [ None , IMG_SIZE, IMG_SIZE , 1 ], name = `input` )

  

convnet = conv_2d (convnet, 32 , 5 , activation = `relu` )

convnet = max_pool_2d (convnet, 5 )

 

convnet = conv _2d (convnet, 64 , 5 , activation = `relu` )

convnet = max_pool_2d (convnet, 5 )

 

convnet = conv_2d (convnet, 128 , 5 , activation = `relu` )

convnet = max_pool_2d (convnet, 5 )

  

convnet = conv_2d (convnet, 64 , 5 , activation = `relu` )

convnet = max_pool_2d (convnet, 5 )

 

convnet = conv_2d (convnet, 32 , 5 , activation = `relu` )

convnet = max_pool_2d (convnet, 5 )

 

convnet = fully_connected (convnet, 1024 , activation = `relu` )

convnet = dropout (convnet, 0.8 )

 

convnet = fully_connected (convnet, 2 , activation = `softmax` )

convnet = regression (convnet, optimizer = ` adam` , learning_rate = LR,

loss = `categorical_crossentropy` , name = ` targets` )

 

model = tflearn .DNN (convnet, tensorboard_dir = `log` )

 
# Separate test and training data

train = train_data [: - 500 ]

test = train_data [ - 500 :]

 
& # 39; & # 39; & # 39; Customizing Features & Tags & # 39; & # 39; & # 39;
# X-Features & amp; Y -Labels

  

X = np.array ([i [ 0 ] for i in train]). reshape ( - 1 , IMG_SIZE, IMG_SIZE, 1 )

Y = [i [ 1 ] for i in train]

test_x = np.array ([i [ 0 ] for i in test]). reshape ( - 1 , IMG_SIZE, IMG_SIZE, 1 )

test_y = [ i [ 1 ] for i in test]

  
& # 39; & # 39; & # 39; Fitting data into our model & # 39; & # 39; & # 39;
# era = 5 accepted

model. fit ({ `input` : X}, { ` targets` : Y}, n_epoch = 5

validation_set = ({ `input` : test_x}, { `targets` : test_y}), 

  snapshot_step = 500 , show_metric = True , run_id = MODEL_NAME)

model.save (MODEL_NAME)

 
& # 39; & # 39; & # 39; Data Testing & # 39; & # 39; & # 39;

import matplotlib.pyplot as plt

# if you need to create data:
# test_data = process_test_data ()
# if you already have some saved:

test_data = np.load ( `test_data.npy` )

 

fig = plt.figure ()

 

for num, data in enumerate (test_data [: 20 ]):

  # cat: [1, 0]

  # dog: [0, 1]

 

img_num = data [ 1 ]

img_data = data [ 0 ]

 

y = fig.add_subplot ( 4 , 5 , num + 1 )

orig = img_data

  data = img_data.reshape (IMG_SIZE, IMG_SIZE, 1 )

 

# model_out = model.predict ([data]) [0]

model_out = model.predict ([data]) [ 0 ]

 

if np.argmax (model_out) = = 1 : str_label = `Dog`

else : str_label = `Cat`

  

y.imshow (orig, cmap = `gray` )

  plt.title (str_label)

y.axes.get_xaxis ( ) .set_visible ( False )

                                                                                                                                                                                                                                                                                                                                                                                                 y.axes.get_yaxis (). set_visible ( False )

plt.show ()

The output image will not be very sharp as the entire image is scaled down to 50X50 so that the machine can process quickly despite the tradeoff between speed and loss. 
And to access the tensorboard use the following command in your cmd (windows user)

 tensorboard --logdir = foo: C: UsersknapseckDesktopDevCov_Netlog 

Output:

Links Links for beginners to machine learning:

Links Links For CNN: