During my childhood, I used to think that lions belong to Canidae and tigers belong to Felidae. I also recall instances when people decorated their dogs to look like cats, which can be misleading at first glance. Accurately classifying pictures of different species can be challenging, even for those with prior experience. While people may rely on their past experiences to distinguish between different species, the accuracy of such methods is often overestimated.
To begin, we can simplify our broad questions by focusing on distinguishing between pictures of cats and dogs. One way to approach this is to generate models and visualize the training accuracy. If our model performs well, we can progress to more complex tasks, we can even attempt to distinguish between closely related species such as Canidae (e.g. wolves) and Felidae (e.g. lions). The goal of this project is to first develop a model to accurately identify pictures of cats and dogs. Once we have validated our model, we can then use it to differentiate between species within the Canidae and Felidae families. By doing so, we can determine the extent of the differences between these families.
At present, our image classifiers are designed to differentiate between cats and dogs, as well as other closely related species within the feline and canid families. If the initial model proves to be highly effective, we can expand its capabilities to identify other species that are not currently recognized. This could offer valuable insights into the characteristics of unknown species and their similarities to known species. Additionally, this model can aid in the identification of specific individuals in densely populated environments, resulting in improved speed and accuracy of identification.
We will use a Dogs vs. Cats [1] from Kaggle to train and test the model. The test and train data comprise solely of pictures of cats and dogs.
# import necessary libraraies
import os
import random
from IPython.display import display, Image
# Set the train directory path
train_dir = 'dogs-vs-cats/train'
# Get a list of all the image files in the directory
img_files = [f for f in os.listdir(train_dir)]
# Randomly select five images from the list
random_imgs = random.sample(img_files, 5)
# Display each image
for img in random_imgs:
img_path = os.path.join(train_dir, img)
display(Image(filename=img_path))
# create a dictionary to store the number of images of each category
counts = {'cat': 0, 'dog': 0}
# Count the number of images of each category
for filename in os.listdir(train_dir):
if filename.startswith('cat'):
counts['cat'] += 1
elif filename.startswith('dog'):
counts['dog'] += 1
# Print the number of images of each category
print(counts)
{'cat': 12500, 'dog': 12500}
# choose random images from a directory
num_images = 10
# Get all filenames in the directory
filenames = os.listdir(train_dir)
# Shuffle the filenames
random.shuffle(filenames)
# Select the first num_images filenames
selected_filenames = filenames[:num_images]
# Print the selected filenames
for filename in selected_filenames:
print(filename)
dog.6729.jpg cat.4032.jpg dog.6838.jpg dog.2110.jpg dog.4941.jpg dog.174.jpg cat.2887.jpg dog.1619.jpg dog.8112.jpg dog.11156.jpg
In our "train" folder, all the images are systematically named in the format cat.xxx.jpg
. The folder contains 25,000 images, with an equal distribution of cats and dogs. As shown in the accompanying image, half of the images feature dogs and the other half feature cats.
In the "test" folder, the images have the same name format - a number followed by the ".jpg" extension. Each image has a different number assigned to it, which serves as its unique identifier. The large number of images in the dataset makes it less likely for the test results to be affected by chance or randomness.
In some instances, there may be pictures that include humans. For example,
In addition, some of the images for our dataset had facial features that were partially or completely obscured. However, humans are still able to recognize them by taking into account subtle differences such as the shapes of ears of cats and dogs. Therefore, we included these seemingly blurry images in our dataset to improve the accuracy of our computer vision model. For example,
By exposing the model to a wider range of image variations, we can help it learn to better identify and distinguish features beyond facial ones, even in challenging, low-visibility conditions.
Currently, we are only expecting our model to classify cats and dogs, which means that even a random guess would have a 50% chance of being correct.
If the model is ideal, we hope to extend the model's ability to classify animals within the Canidae and Felidae families. In order to achieve this, we will need to obtain additional pictures beyond just cats and dogs.
Also, it is worth noting that the distinction between the Canidae and Felidae families may not be solely based on physical appearance. According to Wikipedia, Canidae[2] is a biological family of dog-like carnivores, while Felidae[3] is the family of mammals in the order Carnivora colloquially referred to as cats. Therefore, the difference between these two families may go beyond just their physical appearance. As a result, it is possible that the model's predictive performance may be lower when classifying images of animals in these families, compared to images of cats and dogs.
To classify images of cats and dogs, we will utilize CNN as follows: