Image classification using Keras

In this Answer, we delve into the process of image classification using KerasA high-level, deep learning API developed by Google for implementing neural networks. The unique aspect of this guide is the interactive element that facilitates the creation of a custom dataset. This hands-on approach not only enhances understanding but also allows for a unique customization of the classification model.

The process can be illustrated in the following flowchart:

Flowchart of the image classification process
Flowchart of the image classification process

Creating a custom dataset

The first step in this process involves creating a custom dataset. A drawing widget has been developed for this purpose, enabling the drawing of two different shapes. These shapes will form the two classes for the image classification model. The flexibility of this approach allows the creation of a classifier between any two shapes of choice.

Note: Guidelines for using the sketchpad:

  1. Enter the names of the two classes (shapes) to be drawn in the “Class A Name” and “Class B Name” fields.

  2. Draw the shapes in the canvas area, ensuring that the shapes are drawn within the boundaries of the canvas.

  3. After drawing a shape, click on the “Save Image” button to save the image. The image will be saved under the class that is currently selected.

  4. Switch between classes by selecting the radio button of the class to be drawn next.

  5. To clear the canvas, click on the “Clear” button.

  6. Once an equal number of shapes (ideally >10 each) for both classes have been drawn, click on the “Done” button to stop the server and proceed with the next steps.

import React from 'react';
require('./style.css');

import ReactDOM from 'react-dom';
import App from './app.js';

ReactDOM.render(
  <App />, 
  document.getElementById('root')
);
Custom shape sketchpad for image classification dataset creation

Model building and training

The model.py script serves as a crucial element in this image classification guide because it plays a fundamental role in creating and training the classification model using Keras. It leverages the Keras framework to construct a Convolutional Neural Network (CNN) model.

  • The create_model function defines the model architecture.
  • To train the model, the data is prepared using the prepare_data function from the data_preparation.py script. This function loads the images, resizes them to a target size of 100x100 pixels, normalizes the pixel values, and splits the data into training and validation sets.
  • The architecture includes two Conv2D layers with MaxPooling2D layers to extract and downsample features.
  • The relu activation function introduces nonlinearity.
  • A Flatten layer converts 2D feature maps to a 1D vector, followed by a Dense layer with 128 units and relu activation to learn high-level representations.
  • The output layer with softmax activation produces class probabilities.

After defining the model, it’s compiled with the Adam optimizer and sparse categorical cross-entropy lossA loss function commonly used for multi-class classification tasks where each input sample belongs to only one class.. The fit method trains the model using training images and labels, specifying the number of epochs and optional validation data. We can customize the model by adjusting layer architecture, units, and activation functions. The trained model can be saved for future use.

Note: To commence the model training, please ensure that you’ve added the class labels in the model.py script. Subsequently, click the “Run” button. Lastly, in the terminal, input python3 /usercode/model.py and press enter to initiate the training process.

import os
import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array

def load_data(path, classes):
    images = []
    labels = []

    for label, class_name in enumerate(classes):
        class_path = os.path.join(path, class_name)
        for image_path in os.listdir(class_path):
            image = load_img(os.path.join(class_path, image_path), target_size=(100, 100))
            image_array = img_to_array(image) / 255.0
            images.append(image_array)
            labels.append(label)

    images = np.array(images)
    labels = np.array(labels)

    return images, labels

def prepare_data(dataset_path, classes):
    images, labels = load_data(dataset_path, classes)

    indices = np.arange(len(images))
    np.random.shuffle(indices)

    train_size = int(0.8 * len(images))

    train_indices = indices[:train_size]
    validation_indices = indices[train_size:]

    train_images = images[train_indices]
    train_labels = labels[train_indices]
    validation_images = images[validation_indices]
    validation_labels = labels[validation_indices]

    return (train_images, train_labels), (validation_images, validation_labels)
Training and saving the model using Keras

Evaluation

This section is added to assess the performance of the trained image classification model by submitting custom test images, drawn using the provided sketchpad, and observing the model's classification results.

Note: Guidelines for submitting test image:

  • Run the code by clicking the “Run” button.

  • Enter the command python3 /submit_test.py to execute the script for capturing the test image.

  • Open the provided app link to access the custom sketchpad.

  • Draw the test image within the canvas, ensuring it belongs to one of the two classes previously created.

  • After completing the drawing, click the “Submit” button to save the test image as test_img.png.

import React from 'react';
require('./style.css');

import ReactDOM from 'react-dom';
import App from './app.js';

ReactDOM.render(
  <App />, 
  document.getElementById('root')
);
Drawing the test image

The provided test code utilizes Keras to evaluate the trained image classification model and predict the class label for a custom test image.

  • First, the saved model classification_model.h5 is loaded using load_model from tensorflow.keras.models.

  • The class names, representing the possible labels, are defined.

  • Then, the test image located at /test_img.png is loaded and preprocessed.

  • The image is resized to match the input size expected by the model, normalized, and expanded to include a batch dimension.

  • The model is then used to predict the class of the test image using model.predict.

  • The predictions are obtained as probability scores for each class.

  • The np.argmax function identifies the class with the highest probability, and the corresponding class name is retrieved from the classes list.

  • Finally, the predicted class label is printed.

Note: Prior to running the code, verify and match the order of class labels with the training labels in the highlighted line. After clicking the “Run” button, use the command python3 /usercode/eval.py in the terminal to predict the label of the test image.

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf
import logging
tf.get_logger().setLevel(logging.ERROR)

from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np

# Loading the saved model
model = load_model('classification_model.h5')

# Defining the class labels
classes = ['square', 'circle']

# Loading and preprocessing the test image
image_path = '/test_img.png'
img = image.load_img(image_path, target_size=(100, 100))
img_array = image.img_to_array(img)
img_array /= 255.0
img_array = np.expand_dims(img_array, axis=0)

# Using the model to predict the class of the test image
predictions = model.predict(img_array)
predicted_class = np.argmax(predictions)
class_name = classes[predicted_class]
print('Predicted class:', class_name)
Predicting the class of the test image

In summary, this comprehensive Answer redefines image classification with its innovative approach, by allowing us to seamlessly generate custom datasets and build personalized classification models.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved