Python - Image Classification using keras

Image classification is a fundamental computer vision task that categorizes images into predefined classes. Keras provides powerful tools to build convolutional neural networks (CNNs) for this purpose. There are two main approaches ?

  • Training a small network from scratch
  • Fine tuning the top layers of the model using VGG16

Setting Up the Environment

First, we import the necessary libraries and define basic parameters ?

# Importing all necessary libraries
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K

# Define image dimensions and data directories
img_width, img_height = 224, 224
train_data_dir = 'v_data/train'
validation_data_dir = 'v_data/test'
nb_train_samples = 400
nb_validation_samples = 100
epochs = 10
batch_size = 16

Building the CNN Model

We create a Sequential model with multiple convolutional and pooling layers ?

# Check image data format
if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

# Build the model
model = Sequential()

# First convolutional block
model.add(Conv2D(32, (2, 2), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# Second convolutional block
model.add(Conv2D(32, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# Third convolutional block
model.add(Conv2D(64, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# Fully connected layers
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

Model Compilation

Configure the model with loss function, optimizer, and metrics ?

# Compile the model
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop', 
              metrics=['accuracy'])

Data Preparation and Augmentation

Use ImageDataGenerator to preprocess and augment training data ?

# Data augmentation for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# Only rescaling for validation data
test_datagen = ImageDataGenerator(rescale=1. / 255)

# Create data generators
train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

Training the Model

Train the model using the prepared data generators ?

# Train the model
model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

# Save the trained model
model.save_weights('ImgmodelKeras_saved.h5')

Key Components Explained

  • Conv2D: Convolutional layer that extracts features from images
  • MaxPooling2D: Reduces spatial dimensions while retaining important features
  • Dropout: Prevents overfitting by randomly setting neurons to zero
  • ImageDataGenerator: Applies data augmentation to increase dataset variety

Conclusion

This CNN architecture effectively classifies images using multiple convolutional layers for feature extraction and dense layers for classification. Data augmentation helps improve model generalization and prevents overfitting on limited training data.

Updated on: 2026-03-25T09:15:46+05:30

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements