Deep learning Cat and Dog Classification Using TensorFlow

kamal_DS
6 min readJan 29, 2023

--

TensorFlow is an open-source software library for deep learning and machine learning developed by Google Brain Team. It is used for a wide range of applications, including image and speech recognition, natural language processing, and computer vision. It is also used to create and train neural networks, which are the backbone of deep learning.

TensorFlow’s popularity can be attributed to its ease of use, flexibility, and scalability. It allows developers to build and train models on a wide range of hardware, including CPUs, GPUs, and TPUs. This makes it an ideal choice for both research and production environments. TensorFlow also provides a wide range of pre-built models and libraries, making it easier for developers to get started with deep learning.

In Computer Vision, TensorFlow has several pre-trained models for object detection, image classification, semantic segmentation, and more. Additionally, TensorFlow provides a convenient way to build custom models using its high-level APIs like Kera's and Estimators. With TensorFlow, it is easy to train and deploy models on a large scale, making it a popular choice for computer vision applications in the industry.

TensorFlow’s ability to run on multiple platforms and devices, its ease of use, and its flexibility to build custom models make it a powerful tool for deep learning and computer vision. It has a large and active community that provides support, tutorials, and resources to help developers get started with TensorFlow.

Deep learning is a powerful tool for image classification, and it can be used to train a model to distinguish between different types of animals, such as cats and dogs. In this blog post, we will explore how to build a deep learning model for cat and dog classification using TensorFlow with code implementation.

The first step in building a deep learning model is to gather and preprocess the data. In this case, we will need a dataset of images of cats and dogs. There are several public datasets available for this purpose, such as the Kaggle Cats and Dogs dataset. After downloading the dataset, we will need to preprocess the images by resizing and normalizing them.

# Importing required libraries
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Defining image size and batch size
img_size = (150, 150)
batch_size = 32

# Creating ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

# Creating training and testing sets
training_set = train_datagen.flow_from_directory('dataset/training_set',
target_size = img_size,
batch_size = batch_size,
class_mode = 'binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
target_size = img_size,
batch_size = batch_size,
class_mode = 'binary')

Next, we will define the architecture of our deep learning model. In this case, we will use a convolutional neural network (CNN) with multiple convolutional and pooling layers, followed by fully connected layers. TensorFlow provides several high-level APIs, such as Kera's, which make it easy to define the architecture of a deep learning model.

# Defining the model architecture
model = keras.Sequential([
keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(64, (3, 3), activation='relu'),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(128, (3, 3), activation='relu'),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(512, activation='relu'),
keras.layers.Dense(1, activation='sigmoid')
])

# Compiling the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(training_set,epochs=50,validation_data=0.1)

Once the model is defined, we can start training it on our dataset. The training process involves feeding.

To test the cat and dog classification model that we trained above, we can use the prediction method provided by TensorFlow Kera's library. The prediction method takes an image as input and returns the predicted class (cat or dog) and the associated probability.

Here’s an example of how you can test an image using the trained model:

import tensorflow as tf
import cv2
import numpy as np

# Load the trained model
model = tf.keras.models.load_model("cat_dog_model.h5")

# Read the image to be tested
image = cv2.imread("test_image.jpg")

# Resize the image to the input shape of the model
image = cv2.resize(image, (150, 150))

# Convert the image to a numpy array and add an additional dimension
image = np.expand_dims(image, axis=0)

# Normalize the image
image = image / 255.0

# Make a prediction
predictions = model.predict(image)

# Get the class with the highest probability
class_index = np.argmax(predictions[0])

# Define the classes
classes = ["cat", "dog"]

# Print the predicted class
print("The model predicts that the image is a:", classes[class_index])

In this code, we first import the necessary libraries, TensorFlow, OpenCV, and NumPy. We then load the trained model using the load_model function from the tf.keras.models module. The model is stored in an h5 format.

Next, we read the image to be tested using the cv2.imread function from the OpenCV library. The function reads the image from the specified file path and returns an image object. We then resize the image to the input shape of the model using the cv2.resize function.

We then convert the image to a numpy array and add an additional dimension using the np.expand_dims function. This is done because the model expects the input to be in the form of a batch of images, where each image is represented by a 4-D tensor.

We then normalize the image by dividing each pixel value by 255.0. This is a common preprocessing step to ensure that the pixel values are between 0 and 1.

Finally, we make a prediction using the predict method of the model, passing in the image as an argument. The predict method returns an array of predictions, where each element of the array corresponds to the probability of the image belonging to each class.

We then use the np.argmax function to get the index of the class with the highest probability. We define the classes as a list of strings, in this case ["cat", "dog"] and then use this index to get the predicted class. We then print the predicted class.

It is worth noting that this is a simplified example and in practice, the accuracy of the model can be improved by using a larger dataset, fine-tuning the model hyper-parameters, or using more advanced architectures. Moreover, this model can be used as a base model and can be fine-tuned on other similar classification tasks.

In conclusion, we have seen how to build a deep learning model for cat and dog classification using TensorFlow. The model, a convolutional neural network (CNN), was trained on a dataset of cat and dog images and was able to achieve an accuracy of around 97% on the Training set. We also saw how to test the model on a new image, by loading the image, converting it to a NumPy array, adding an additional dimension, normalizing the image, and making a prediction using the predict method.

Overall, this project demonstrates the power of deep learning in image classification tasks and how easy it is to build such models using TensorFlow. Additionally, it also showcases how to use the cv2 library to perform preprocessing on images and how to use the trained model to perform predictions on new images.

For the Entire Project code, You can get it from my GitHub =

https://github.com/saikamal3344/Cat-and-Dog-Classification/blob/main/Cat_and_Dog_Classiffication.ipynb

for more information about images and OpenCV follow my previous blogs

--

--

kamal_DS

Interested to work in the field of Artificial Intelligence, Machine Learning, Deep Learning, NLP and Computer Vision.