0
0
Computer Visionml~5 mins

Training an image classifier in Computer Vision

Choose your learning style9 modes available
Introduction

Training an image classifier helps a computer learn to recognize and sort pictures into groups. This makes it easier to find or understand images automatically.

You want your phone to recognize faces in photos.
You need a system to sort pictures of animals into categories like cats or dogs.
You want to detect objects like cars or traffic signs in street images.
You are building a tool to help doctors identify diseases from medical images.
You want to organize a large photo collection by content automatically.
Syntax
Computer Vision
model = Sequential([
    Conv2D(filters, kernel_size, activation='relu', input_shape=input_shape),
    MaxPooling2D(pool_size=pool_size),
    Flatten(),
    Dense(units, activation='relu'),
    Dense(num_classes, activation='softmax')
])

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

model.fit(train_images, train_labels, epochs=number_of_epochs, validation_data=(val_images, val_labels))

This example uses a simple neural network with convolution layers for images.

Activation 'relu' helps the model learn complex patterns, 'softmax' is for classifying into categories.

Examples
A simple model for grayscale 28x28 images with 10 classes.
Computer Vision
model = Sequential([
    Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
    MaxPooling2D((2,2)),
    Flatten(),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])
Compile the model using Adam optimizer and sparse categorical loss for integer labels.
Computer Vision
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Train the model for 5 rounds using 20% of data for validation.
Computer Vision
model.fit(train_images, train_labels, epochs=5, validation_split=0.2)
Sample Model

This program trains a simple image classifier on handwritten digits. It shows training progress and prints test accuracy.

Computer Vision
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Load example dataset (MNIST handwritten digits)
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

# Normalize images to 0-1 range and add channel dimension
train_images = train_images / 255.0
test_images = test_images / 255.0
train_images = train_images[..., tf.newaxis]
test_images = test_images[..., tf.newaxis]

# Build a simple CNN model
model = Sequential([
    Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
    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'])

# Train the model
history = model.fit(train_images, train_labels, epochs=3, validation_split=0.1, verbose=2)

# Evaluate on test data
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=0)

print(f"Test accuracy: {test_acc:.4f}")
OutputSuccess
Important Notes

Always normalize image pixel values to help the model learn better.

Use validation data to check if the model is learning well and not just memorizing.

More epochs usually improve accuracy but can cause overfitting if too many.

Summary

Training an image classifier means teaching a model to recognize picture categories.

Use convolutional layers to help the model understand image features.

Check accuracy on new images to see how well the model learned.