Dog vs Cat image classifier using TensorFlow | Python
In this tutorial, you will learn about how to create a CNN which can predict whether a given image is a cat or dog. This is done using TensorFlow and Keras, which are the most renowned libraries for the purpose of data science in Python. This post will also teach you about the basics of convolutional neural networks.
I know that you are here to learn about the basics of convolutional neural networks in a real application scenario. Don’t worry, This tutorial will guide you through the basics as well as provide you with the code, which will be helpful for your reference.
In case you do not know how to load a dataset, split it into training and testing, this tutorial will also help you with that. These elements are very crucial for any data science project.
Python program to Classify dogs and cats using TensorFlow
Before starting the implementation, make sure you have these dependencies installed in your system:
In case you are not having any dependency, the usual format is pip install <library>. Once you have installed the dependencies on your system, you can start by downloading the dataset.
The dataset has to be downloaded from here. After you download the dataset, locate it in the working directory. Now the first step is to import the dependencies.
import numpy as np import pandas as pd import tensorflow from tensorflow import keras import os import PIL import matplotlib.pyplot as plt from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Flatten
from tensorflow.keras import Activation from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Conv2D from tensorflow.keras.layers import MaxPooling2D from tensorflow.keras.callbacks import TensorBoard from tensorflow.keras.preprocessing.image import load_img from tensorflow.keras.preprocessing.image import img_to_array from PIL import Image
After importing the required dependencies, the next task is loading the dataset into to train the model.
dpath="D:\DATASET\PetImages" #path to the main directory of the dataset. cate=["Dog","Cat"] for category in cate: path=os.path.join(dpath,category) for img in os.listdir(path): img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
new_arr=cv2.resize(img_array,(img_size,img_size)) plt.imshow(img_array,cmap="gray") plt.show() break
The above section of code basically takes the images that are present in the dataset, reads it using the cv2 library, converting the image to grayscale simultaneously. After this, the image is displayed using Matplotlib. This basically gives us the idea of how the images look now. The next component in the arrangement of the dataset is converting the images to an array, which would thus help in pushing the data into the neural network architecture which you are going to learn in the latter part of this tutorial.
training_data= def trainingd(): for category in cate: path=os.path.join(dpath,category) class_num=cate.index(category) for img in os.listdir(path): try: img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE) new_arr=cv2.resize(img_array,(img_size,img_size)) training_data.append([new_arr,class_num]) except Exception as e: pass trainingd()
In the above section of code, the images and their respective tables are added to the training list. Use “random.shuffle” to shuffle the training data. This helps the model in better learning about the data. This further helps in the betterment of its efficiency. The next part of the tutorial is the most important part of the tutorial.
for feature,label in training_data: X.append(feature) y.append(label) X=np.array(X).reshape(-1,img_size,img_size,1) X=X/255.0 model=Sequential() model.add(Conv2D(64,(3,3),input_shape=X.shape[1:],activation="relu")) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Conv2D(64,(3,3),activation="relu")) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation("relu")) model.add(Dense(1)) model.add(Activation("sigmoid")) model.compile(loss="binary_crossentropy",optimizer="adam",metrics=["accuracy"]) model.fit(X,y,batch_size=32,epochs=10,validation_split=0.1)
Creating CNN Architecture:
The above snippet consists of the preparation of data and development of the neural network architecture. The architecture consists of a convolution layer followed by a max-pooling layer. Repeat the same pattern again.
The convolution layer understands the patterns in the data. The max-pooling layer has the power to reduce the size of the image, retaining its content. The Flatten command creates a chain of neurons from the 2D structure.
The “Dense” creates a hidden layer of neurons. Use the Relu activation for the hidden layers. Depending on the output label, use sigmoid or softmax functions. The understanding of this usage is beyond the scope of this tutorial. Use “binary_crossentropy” as the output categories are binary(i.e whether the image is a cat or dog). The “batch_size” tells the system how many training samples to send to send together during an epoch.