Last modified: Jan 07, 2026 By Alexander Williams

Build Image Classification Models with TensorFlow

Image classification is a core task in computer vision. It involves teaching a model to recognize and categorize images. TensorFlow makes this process accessible.

This guide walks you through building a model from scratch. We will use the Keras API within TensorFlow. It is perfect for beginners starting their deep learning journey.

Understanding the Basics

Image classification models are a type of neural network. They learn patterns from pixel data. Convolutional Neural Networks (CNNs) are the standard architecture for this.

CNNs use layers called convolutions. These layers scan images for features like edges and textures. This hierarchical learning is very effective for visual data.

For a broader foundation, consider reading our Intro to Deep Learning with TensorFlow Keras. It covers essential neural network concepts.

Setting Up Your Environment

First, ensure you have TensorFlow installed. Use pip, the Python package manager. The command is simple and quick.


# Install TensorFlow
pip install tensorflow
    

You will also need NumPy and Matplotlib. These are for data handling and visualization. Install them if you haven't already.


pip install numpy matplotlib
    

Preparing Your Dataset

A good dataset is crucial for model success. We will use the CIFAR-10 dataset. It contains 60,000 small color images in 10 classes.

TensorFlow provides easy access to common datasets. We load it using the keras.datasets module. Then, we preprocess the data.


import tensorflow as tf
from tensorflow import keras

# Load CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()

# Normalize pixel values to be between 0 and 1
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# Convert class vectors to binary class matrices (one-hot encoding)
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

print(f"Training data shape: {x_train.shape}")
print(f"Test data shape: {x_test.shape}")
    

Training data shape: (50000, 32, 32, 3)
Test data shape: (10000, 32, 32, 3)
    

Building the CNN Model

Now, we define our Convolutional Neural Network. We will use the Keras Sequential API. It allows us to stack layers linearly.

The model starts with convolutional layers. Then, we add pooling layers to reduce spatial size. Finally, dense layers perform the classification.


model = keras.Sequential([
    # First Convolutional Block
    keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(32, 32, 3)),
    keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same'),
    keras.layers.MaxPooling2D(pool_size=(2, 2)),
    keras.layers.Dropout(0.25),

    # Second Convolutional Block
    keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
    keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
    keras.layers.MaxPooling2D(pool_size=(2, 2)),
    keras.layers.Dropout(0.25),

    # Flatten and Classify
    keras.layers.Flatten(),
    keras.layers.Dense(512, activation='relu'),
    keras.layers.Dropout(0.5),
    keras.layers.Dense(10, activation='softmax') # 10 output classes
])

# Print a summary of the model architecture
model.summary()
    

The model.summary() function shows the layer structure. It also displays the number of trainable parameters. This helps you understand model complexity.

Compiling the Model

Before training, we must compile the model. This step configures the learning process. We define the optimizer, loss function, and metrics.

For classification, categorical crossentropy is a common loss. The Adam optimizer is a good default choice. Accuracy is a clear metric to track.


model.compile(
    optimizer='adam',
    loss='categorical_crossentropy',
    metrics=['accuracy']
)
    

Training the Model

Training is done with the fit method. We pass the training data and labels. We also specify batch size and number of epochs.

Using a validation split is important. It checks model performance on unseen data during training. This helps prevent overfitting.


history = model.fit(
    x_train, y_train,
    batch_size=64,
    epochs=20,
    validation_split=0.2, # Use 20% of training data for validation
    verbose=1
)
    

The fit method returns a history object. It contains logs of loss and accuracy per epoch. You can plot these to visualize learning.

Evaluating Model Performance

After training, evaluate the model on the test set. This gives the final, unbiased performance metric. Use the evaluate method.


test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Loss: {test_loss:.4f}")
print(f"Test Accuracy: {test_accuracy:.4f}")
    

Test Loss: 0.6754
Test Accuracy: 0.7821
    

An accuracy around 78% is decent for CIFAR-10 with a simple model. More complex architectures can achieve over 90%. Practice is key to improvement.

Making Predictions

You can use the trained model to classify new images. The predict method returns probability scores for each class. Then, you find the class with the highest score.


# Get predictions for the first 5 test images
predictions = model.predict(x_test[:5])

# Get the class index with the highest probability
predicted_classes = predictions.argmax(axis=-1)

# CIFAR-10 class names
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']

print("Predictions for first 5 test images:")
for i in range(5):
    print(f"Image {i+1}: Predicted = {class_names[predicted_classes[i]]}")
    

Improving Your Model

This is a basic model. You can improve it in many ways. Use data augmentation to create more training examples.

Try deeper architectures like ResNet or VGG. Adjust hyperparameters like learning rate. Tuning these elements is a core part of machine learning.

For more advanced Python techniques that can aid in building robust ML pipelines, such as managing function state, explore our guide on Understanding Python Closures.

Conclusion

You have built an image classification model with TensorFlow. The process involves data preparation, model building, training, and evaluation.

Start with simple models and datasets. Gradually increase complexity as you learn. The field of deep learning offers endless possibilities.

Remember, mastering the fundamentals in our Deep Learning with Python Guide is the best first step. Keep experimenting and building.