MakeAI Logo

An Introduction to CNNs with the Cat vs Dog Classifier

Blog / By MakeAI / September 12, 2023

Blog cover

For those unfamiliar with the world of coding, words like deep learning, artificial intelligence, and Convolutional Neural Network might sound like abstract concepts. Today, we're breaking down our new cat vs. dog classifier demo and explaining the major implementations of our code.

Our code is designed to discern if a provided image depicts a cat or a dog. This is achieved through the use of Convolutional Neural Networks (CNN). A CNN is a deep learning architecture especially effective for image recognition. They work by taking an image and breaking it down into smaller sections or 'features'. These features move through multiple layers in the network. Each layer inspects the image in more detail, focusing on specific patterns and structures.

CNN Scheme

After processing the image through these layers, the CNN produces an output for the final layer with 2 nodes, outputting a number between 0 and 1 for both cat and dog. A value close to 0 suggests the image is unlikely to be a cat/dog, whereas a value of 1 indicates that the image is likely to be a cat/dog.

Consequently, for the prediction, we take the larger value of the two and output 0 as cat and 1 as dog. To ensure its predictions are accurate, the model is trained using a large set of cat and dog images. During this training, the model fine-tunes its internal parameters (weights & biases) and utilizes hyperparameters (epochs[times of iteration through training data], nodes [neurons/circles from the layers], activation functions [ReLU, Sigmoid, tanh, etc.], learning rate) to recognize patterns specific to cats and dogs. If the model makes a wrong prediction, it adjusts itself using a process called backpropagation, aided by a mathematical tool called gradient descent. This helps the CNN minimize its errors over time and get better at distinguishing between cats and dogs.

Cat vs Dog CNN

Helpful Functions and Definitions:

FastAPI: This is the framework we use to build and operate our web application. It helps us handle the user requests.

Torch & torchvision: Libraries that help us build, train, and evaluate our deep learning model.

PIL (Python Imaging Library): Used to open, manipulate, and save images.

Cat vs Dog

Steps for CNN implementation:

1. Loading Model
load_model(): This function sets up the model and loads its previous training knowledge.

2. Preprocessing the Data
preprocess_image(img: Image.Image): The training data images will be put through transformations such as resizing, compressing, and normalizing to facilitate the criteria for training the model.

3. Classifying Images
classify_image(): Given an image, this function lets our model use its learned or saved knowledge to make a prediction. The model examines the image and decides if it's a cat or a dog respectively between a 0 and a 1.

If you'd like to learn more about than just the function's pseudo code, the actual code is on GitHub.

Cat and Dog

This trained model has around a ~95% accuracy rate when it comes to telling apart cats and dogs.

When you interact with our Cat vs Dog Classifier, here's what goes on behind the scenes:

  • You upload or use a randomized image from our library.
  • Our program processes the image to make it suitable for our model.
  • The pre-trained model examines the image and predicts whether it's a cat or a dog.
  • You see the result displayed on your screen!

While the inner workings of AI and deep learning might seem complex, the main idea is straightforward: teaching machines to learn from data. Our Cat vs Dog Classifier Demo is just one fun and practical application of AI.

We hope you enjoy using it as much as we enjoyed building it! Feel free to comment or ask questions on our subreddit, r/MakeAI.