Last modified: Apr 12, 2025 By Alexander Williams
Python Image Classification Guide
Image classification is a key task in computer vision. It involves labeling images based on their content. Python makes it easy with libraries like TensorFlow and Keras.
Table Of Contents
What Is Image Classification?
Image classification assigns a label to an image. For example, it can identify if an image contains a cat or a dog. It's used in many fields like healthcare and security.
Deep learning models are often used for this task. They learn patterns from large datasets. This guide will show you how to build one.
Setting Up Your Environment
First, install the required libraries. You'll need TensorFlow and Keras. Use pip for installation.
# Install TensorFlow and Keras
pip install tensorflow keras
These libraries provide tools for building and training models. They also include pre-trained models for quick starts.
Loading and Preparing Data
You need a dataset for training. The CIFAR-10 dataset is a good choice. It has 60,000 images in 10 classes.
from tensorflow.keras.datasets import cifar10
# Load the dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Normalize pixel values
x_train = x_train / 255.0
x_test = x_test / 255.0
Normalizing pixel values helps the model train faster. Values are scaled between 0 and 1.
Building the Model
Next, create a convolutional neural network (CNN). CNNs are great for image tasks. They detect features like edges and textures.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Create the model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
The Sequential
model stacks layers. Conv2D
and MaxPooling2D
extract features. Dense
layers classify the image.
Training the Model
Now, train the model on the dataset. This process adjusts the model's weights to minimize errors.
# Train the model
history = model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
Epoch 1/10
1563/1563 [==============================] - 10s 6ms/step - loss: 1.5000 - accuracy: 0.4560 - val_loss: 1.2500 - val_accuracy: 0.5500
...
Epoch 10/10
1563/1563 [==============================] - 9s 6ms/step - loss: 0.7000 - accuracy: 0.7500 - val_loss: 0.9000 - val_accuracy: 0.7000
The output shows training progress. Accuracy improves over epochs. Validation accuracy checks performance on unseen data.
Evaluating the Model
After training, evaluate the model on the test set. This gives a final accuracy score.
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f'Test accuracy: {test_acc}')
Test accuracy: 0.7000
A 70% accuracy is decent for a simple model. For better results, try more complex architectures.
Making Predictions
Use the trained model to classify new images. Here's how to predict a single image's class.
import numpy as np
# Predict a single image
image = x_test[0]
prediction = model.predict(np.expand_dims(image, axis=0))
predicted_class = np.argmax(prediction)
print(f'Predicted class: {predicted_class}')
Predicted class: 3
The model outputs class probabilities. np.argmax
picks the most likely class.
Improving Your Model
To improve accuracy, try these tips. Use more layers or increase training data. Data augmentation also helps.
Check our Python Image Rescaling Guide for preprocessing tips. Also, see Python Image Segmentation Guide for advanced techniques.
Conclusion
Python makes image classification easy. With TensorFlow and Keras, you can build powerful models. Start simple and improve as you learn.
For more image tasks, explore our Python OCR guide. Happy coding!