Building a Simple Dog vs. Cat Classifier using Python and Scikit-Learn
Image classification is one of the most exciting applications of Artificial Intelligence. While deep learning models like Convolutional Neural Networks (CNNs) are the industry standard for computer vision, you can actually build a functional classifier using traditional Machine Learning libraries like Scikit-Learn. This is an excellent way for beginners to understand how image data is structured and processed.
In this tutorial, we will walk through the process of creating a simple Python script that distinguishes between images of dogs and cats.
Why Scikit-Learn for Image Classification?
Scikit-Learn is primarily designed for tabular data, but images are essentially just grids of numbers (pixels). By flattening these grids into a single row of data, we can apply classic algorithms like Support Vector Machines (SVM) or Logistic Regression. This approach helps you learn the fundamentals of data preprocessing before moving on to complex frameworks like TensorFlow or PyTorch.
Prerequisites
Before we begin, ensure you have Python installed on your system. You will also need to install the following libraries using pip:
pip install numpy scikit-learn matplotlib opencv-python
- NumPy: For numerical operations.
- Scikit-Learn: For the machine learning model and evaluation tools.
- Matplotlib: To visualize our results.
- OpenCV: To load and resize images.
Step 1: Preparing the Dataset
To train our model, we need a dataset. You can download the "Dogs vs. Cats" dataset from Kaggle. For this simple tutorial, create a folder structure like this:
- data/
- cats/ (Place 50-100 cat images here)
- dogs/ (Place 50-100 dog images here)
Step 2: Loading and Preprocessing Images
Machines cannot "see" images; they see arrays of pixel values. We need to convert our images into a format the model understands. We will resize every image to 64x64 pixels to ensure consistency and flatten them into a 1D array.
import os
import numpy as np
import cv2
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
# Define paths and categories
datadir = "data/"
categories = ['cats', 'dogs']
data = []
def create_data():
for category in categories:
path = os.path.join(datadir, category)
class_num = categories.index(category) # 0 for cat, 1 for dog
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
resized_array = cv2.resize(img_array, (64, 64))
data.append([resized_array.flatten(), class_num])
except Exception as e:
pass
create_data()
Step 3: Splitting the Data
We need to separate our features (the pixel data) from our labels (cat or dog). We then split the data into a training set and a testing set. The training set is used to teach the model, while the testing set evaluates how well it learned.
# Separate features and labels
features = []
labels = []
for feature, label in data:
features.append(feature)
labels.append(label)
# Convert to numpy arrays
features = np.array(features)
labels = np.array(labels)
# Split into 80% training and 20% testing
x_train, x_test, y_train, y_test = train_test_split(features, labels, test_size=0.20)
Step 4: Training the Model
For this task, we will use a Support Vector Machine (SVM). SVMs are powerful for binary classification tasks. We will initialize the model and fit it to our training data.
# Initialize and train the Support Vector Machine model = SVC(C=1, kernel='poly', gamma='auto') model.fit(x_train, y_train)
Step 5: Evaluating the Results
Now that the model is trained, let’s see how accurate it is. We will use the built-in score method to check the accuracy percentage on our test data.
accuracy = model.score(x_test, y_test)
print(f"Accuracy: {accuracy * 100}%")
Making a Prediction
You can now test the model on a brand new image to see if it correctly identifies the animal.
# Load a new image
test_img = cv2.imread('test_image.jpg', cv2.IMREAD_GRAYSCALE)
test_img = cv2.resize(test_img, (64, 64)).flatten().reshape(1, -1)
# Predict
prediction = model.predict(test_img)
print(f"Prediction: {categories[prediction[0]]}")
Conclusion
Congratulations! You have built a basic AI model that can distinguish between dogs and cats using Python and Scikit-Learn. While this model may not be as accurate as a deep learning model, it provides a solid foundation for understanding the machine learning pipeline: data collection, preprocessing, training, and evaluation.
Tips for Better Accuracy:
- Increase Dataset Size: The more images the model sees, the better it becomes.
- Normalize Data: Scale pixel values from 0-255 to 0-1.
- Feature Engineering: Instead of raw pixels, try using HOG (Histogram of Oriented Gradients) features.
- Hyperparameter Tuning: Experiment with different kernels in the SVC (like 'rbf' or 'linear').
Keep experimenting and happy coding!
Comments
Post a Comment