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:

  1. TensorFlow
  2. NumPy
  3. Pandas
  4. Pillow
  5. Keras
  6. Matplotlib
  7. OpenCV

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
import cv2

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.
for category in cate:
    for img in os.listdir(path):
new_arr=cv2.resize(img_array,(img_size,img_size)) plt.imshow(img_array,cmap="gray") 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.

def trainingd():
    for category in cate:
        for img in os.listdir(path):
            except Exception as e:

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:

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.


Leave a Reply

Your email address will not be published. Required fields are marked *