Create an Image Classifier with TensorFlow πŸ“ΈπŸ€–

Create an Image Classifier with TensorFlow πŸ“ΈπŸ€–

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!