Working on a project where I needed to classify thousands of product images automatically. The challenge? Building a reliable and accurate image classification model using Python and Keras.
I’ve been using Keras for over four years now, and it remains one of my favorite deep learning frameworks. It’s simple, powerful, and integrates beautifully with TensorFlow.
In this tutorial, I’ll walk you through how to build a Convolutional Neural Network (CNN) for image classification in Python using Keras. I’ll also share a few tips I’ve learned from real-world projects to help you avoid common mistakes.
What is Image Classification?
Image classification is the process of teaching a computer to recognize and categorize images based on their content. For example, a model can learn to identify whether an image contains a cat, dog, or car.
In Python, this is commonly done using Convolutional Neural Networks (CNNs), a type of deep learning model designed to process image data efficiently. CNNs automatically extract features like edges, shapes, and textures, which makes them incredibly effective for visual tasks.
Use Keras for Image Classification
Keras provides a high-level API for building and training deep learning models.
It simplifies complex neural network operations and allows you to focus on model design rather than boilerplate code.
Some of the reasons I prefer using Keras for image classification in Python include:
- Easy-to-understand syntax
- Tight integration with TensorFlow
- Built-in layers for CNNs
- Pre-trained models for transfer learning
Dataset Used
For this tutorial, I’ll use the CIFAR-10 dataset, which is available directly in Keras.
It contains 60,000 color images in 10 categories such as airplanes, cars, birds, cats, and more.
This dataset is perfect for beginners who want to practice image classification using Python.
Method 1 – Build a CNN Model from Scratch
In this first method, we’ll create a CNN model from scratch using Keras. This approach helps you understand the building blocks of a CNN and how each layer contributes to learning.
Step 1: Import Required Libraries
We’ll start by importing all the necessary Python libraries.
# Importing essential Python libraries
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as npI always prefer importing libraries at the top of my Python file to keep things organized. Matplotlib will help us visualize images and model performance later.
Step 2: Load and Prepare the Dataset
Let’s load the CIFAR-10 dataset and preprocess it for training.
# Load CIFAR-10 dataset
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0
# Class names for reference
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']Normalizing the images ensures that all pixel values are scaled consistently, which speeds up training. This is a standard preprocessing step in Python deep learning workflows.
Step 3: Visualize the Dataset
It’s always a good idea to visualize your data before training the model.
# Display sample images
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.imshow(train_images[i])
plt.xlabel(class_names[int(train_labels[i])])
plt.show()This visualization helps confirm that the dataset is loaded correctly and gives you an idea of the image diversity. It’s also a sanity check before moving to model training.
Step 4: Build the CNN Model
Now, let’s define our Convolutional Neural Network using Keras.
# Building the CNN model
model = models.Sequential([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3,3), activation='relu')
])
# Add Dense layers for classification
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))Each convolutional layer helps the model learn different features of the image, such as edges or textures. The dense layers at the end perform the actual classification.
Step 5: Compile and Train the Model
Next, we’ll compile and train the CNN model using Keras.
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Train the model
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))I often use the Adam optimizer because it adapts learning rates automatically during training. The training process may take a few minutes, depending on your system.
Step 6: Evaluate the Model
After training, we can evaluate how well the model performs on unseen test data.
# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f"Test accuracy: {test_acc:.2f}")A good CNN model on CIFAR-10 should achieve around 70–80% accuracy after a few epochs.
You can improve this further by tuning hyperparameters or adding dropout layers.
Step 7: Visualize Training Results
Let’s plot the accuracy and loss curves to understand how the model learned over time.
# Plot accuracy and loss
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()I executed the above example code and added the screenshot below.


This simple visualization helps you spot overfitting or underfitting trends. It’s a great way to fine-tune your CNN model in Python.
Method 2 – Image Classification Using Pre-Trained CNN (Transfer Learning)
If you want faster results or have limited data, transfer learning is the way to go. It allows you to use pre-trained models like VGG16 or ResNet50 that have already learned useful features.
Step 1: Import Pre-Trained Model
Here’s how you can use a pre-trained CNN model in Keras.
from tensorflow.keras.applications import VGG16
from tensorflow.keras import layers, models
# Load pre-trained VGG16 model
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3))
# Freeze base model layers
base_model.trainable = False
# Add custom classification layers
model = models.Sequential([
base_model,
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])By freezing the base model layers, we retain the pre-learned features. The custom dense layers adapt the model to our specific dataset.
Step 2: Compile and Train
Now, we compile and train the model as before.
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5,
validation_data=(test_images, test_labels))I executed the above example code and added the screenshot below.

Transfer learning usually trains much faster and achieves higher accuracy. It’s one of the best techniques I use for real-world Python projects.
Tips for Better Image Classification in Python
- Use Data Augmentation: Helps prevent overfitting by generating new variations of images.
- Add Dropout Layers: Reduces overfitting by randomly turning off neurons during training.
- Experiment with Learning Rates: Try different optimizers or learning rate schedules.
- Use GPU Acceleration: Training CNNs on CPU is slow; use TensorFlow with GPU support.
So that’s how I build an image classification model using CNN in Python with Keras.
We explored both a from-scratch approach and a transfer learning method.
Both techniques are powerful, but I personally prefer starting with transfer learning for real-world projects, as it saves time and often delivers better accuracy.
If you’re new to Keras, start small, experiment with layers, and gradually move to complex architectures.
You may also like to read:
- Import Keras from TensorFlow in Python
- Save a Keras Model with a Custom Layer in Python
- How to Load a Keras Model in Python
- How to Import TensorFlow Keras in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.