Image classification is a fundamental computer vision task that allows AI to identify and categorize objects in images. With TensorFlow and Keras, we can easily build a powerful deep learning model to classify images.
What is Image Classification? πΌοΈπ
Image classification is the process of assigning a label (class) to an image based on its contents. AI models learn to recognize patterns and distinguish between different categories, such as:
- β Cats vs. Dogs π±πΆ
- β Healthy vs. Diseased Plants π±π¨
- β Vehicles: Cars, Bikes, Trucks πποΈπ
π Real-World Applications:
- π Self-Driving Cars β Detecting pedestrians and road signs.
- π₯ Medical Diagnosis β Identifying diseases from X-rays.
- πΉ Security Surveillance β Recognizing suspicious activity.
Setting Up the Development Environment π οΈ
πΉ Install TensorFlow & Required Libraries
pip install tensorflow numpy matplotlib opencv-python
Import Necessary Libraries π
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
import matplotlib.pyplot as plt
Load and Preprocess the Dataset π
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
class_names = ["airplane", "automobile", "bird", "cat", "deer",
"dog", "frog", "horse", "ship", "truck"]
πΉ Visualize Sample Images
plt.figure(figsize=(10, 5))
for i in range(10):
plt.subplot(2, 5, i+1)
plt.imshow(x_train[i])
plt.title(class_names[y_train[i][0]])
plt.axis("off")
plt.show()
Build the Convolutional Neural Network (CNN) π§
model = keras.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.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
πΉ Compile and Train the Model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
πΉ Visualize Training Progress
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
Evaluate and Test the Model π
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f"Test Accuracy: {test_acc * 100:.2f}%")
πΉ Make Predictions on New Images
import random
index = random.randint(0, len(x_test) - 1)
image = x_test[index]
true_label = class_names[y_test[index][0]]
predictions = model.predict(np.expand_dims(image, axis=0))
predicted_label = class_names[np.argmax(predictions)]
plt.imshow(image)
plt.title(f"True: {true_label} | Predicted: {predicted_label}")
plt.axis("off")
plt.show()
Save and Load the Model for Future Use πΎ
model.save("image_classifier.h5")
loaded_model = keras.models.load_model("image_classifier.h5")
Improving the Image Classifier π₯
- β Data Augmentation β Apply transformations to improve training.
- β Using a More Powerful CNN β Add extra layers.
- β Transfer Learning β Use a pre-trained model like MobileNetV2.
base_model = keras.applications.MobileNetV2(weights='imagenet', include_top=False, input_shape=(32, 32, 3))
Real-World Applications of AI-Powered Image Classification π
- π· Face Recognition β AI detects faces for security.
- π₯ Medical Imaging β AI classifies X-rays and MRI scans.
- ποΈ E-Commerce β AI recommends products based on images.
- π Autonomous Vehicles β AI classifies traffic signs.
Conclusion π
In this tutorial, we built an AI-powered image classifier using TensorFlow and CNNs. We covered:
- β Data loading & preprocessing
- β Building a CNN model
- β Training & evaluating performance
- β Making predictions
π Ready to take it further? Try training the model on your own custom dataset!