SoFunction
Updated on 2024-11-15

DenseNet121 model for a 26-letter alphabet recognition task

i. overview of the mandate

26 Alphabet Recognition is a computer vision based image classification task designed to train a deep learning model from a dataset containing images of 26 different alphabets in order to make accurate classification predictions of the input alphabet images. In this paper, we will implement this task using the DenseNet121 model.

II. Introduction to DenseNet

DenseNet is a deep learning architecture for image classification, the core idea of which is to enhance the information flow by connecting all the feature maps of the previous layer to the current layer, thus making the network deeper and more accurate. Compared to traditional convolutional neural network architectures (e.g., AlexNet and VGG), DenseNet has fewer parameters, better model generalization, and higher efficiency.

The network structure of DenseNet is similar to ResNet and consists of multiple Dense Blocks (Dense Blocks), where each Dense Block is composed of multiple convolutional layers and batch normalization layers. Unlike ResNet, the input of each layer in DenseNet contains the outputs of all the previous layers, and this dense connection avoids the problem of information bottleneck and gradient vanishing, which promotes the transfer and utilization of information. At the same time, DenseNet also introduces a Transition Layer to adjust the size of the feature map, reducing the amount of computation and memory consumption.DenseNet finally generates the prediction results through the global average pooling layer and the softmax output layer.

III. Introduction to the data set

In this task, we train and test the model using 26 uppercase letter images from EMNIST dataset, which are composed of handwritten character images of 28x28 pixel size. The dataset contains 340,000 images of which 240,000 are used for training, 60,000 for validation and 40,000 for testing.

IV. Model realization

Here we will use the Keras library from the TensorFlow 2.0 framework to implement the model. First you need to import the required libraries and modules.

import numpy as np
from  import Sequential, Model
from  import Dense, Dropout, Flatten
from  import Conv2D, MaxPooling2D, BatchNormalization
from  import Input, concatenate
from  import Adam
from  import l2
from  import EarlyStopping
from sklearn.model_selection import train_test_split
from PIL import Image

Next, define some hyperparameters, such as batch_size, num_classes, epochs, and so on.

batch_size = 128 # Batch size
num_classes = 26 # of classifications
epochs = 50 # of training rounds

Next, load the EMNIST dataset. Here we need to extract the dataset file to the specified path and read all the images and labels.

# Load data sets
def load_dataset(path):
    with (path) as data:
        X_train = data['X_train']
        y_train = data['y_train']
        X_test = data['X_test']
        y_test = data['y_test']
    return (X_train, y_train), (X_test, y_test)
# Load and normalize the dataset
def preprocess_data(X_train, y_train, X_test, y_test):
    # Normalize the image matrix to between 0 and 1
    X_train = X_train.astype('float32') / 255.
    X_test = X_test.astype('float32') / 255.
    # Convert the label matrix to one-hot coding
    y_train = .to_categorical(y_train, num_classes)
    y_test = .to_categorical(y_test, num_classes)
    return X_train, y_train, X_test, y_test
# Load training and test data
(X_train_val, y_train_val), (X_test, y_test) = load_dataset('/data/emnist/')
# Delineate training and validation sets
X_train, X_val, y_train, y_val = train_test_split(X_train_val, y_train_val,
                                                  test_size=0.2, random_state=42)
# Normalization of data
X_train, y_train, X_val, y_val = preprocess_data(X_train, y_train, X_val, y_val)
X_test, y_test = preprocess_data(X_test, y_test, [], [])

After data preprocessing, we need to define the DenseNet121 model.

# Define the dense_block function
def dense_block(x, blocks, growth_rate):
    for i in range(blocks):
        x1 = BatchNormalization()(x)
        x1 = Conv2D(growth_rate * 4, (1, 1), padding='same', activation='relu',
                    kernel_initializer='he_normal')(x1)
        x1 = BatchNormalization()(x1)
        x1 = Conv2D(growth_rate, (3, 3), padding='same', activation='relu',
                    kernel_initializer='he_normal')(x1)
        x = concatenate([x, x1])
    return x
# Define the transition_layer function
def transition_layer(x, reduction):
    x = BatchNormalization()(x)
    x = Conv2D(int(.as_list()[-1] * reduction), (1, 1), activation='relu',
                kernel_initializer='he_normal')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)
    return x
# DenseNet network constructed
def DenseNet(input_shape, num_classes, dense_blocks=3, dense_layers=-1,
             growth_rate=12, reduction=0.5, dropout_rate=0.0, weight_decay=1e-4):
    # Specify the initial number of channels and blocks
    depth = dense_blocks * dense_layers + 2
    in_channels = 2 * growth_rate
    inputs = Input(shape=input_shape)
    # The first layer of convolution
    x = Conv2D(in_channels, (3, 3), padding='same', use_bias=False,
               kernel_initializer='he_normal')(inputs)
    # Stacking dense blocks and transition layers
    for i in range(dense_blocks):
        x = dense_block(x, dense_layers, growth_rate)
        in_channels += growth_rate * dense_layers
        if i != dense_blocks - 1:
            x = transition_layer(x, reduction)
    # Global average pooling
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D()(x)
    # Output layer
    outputs = Dense(num_classes, activation='softmax',
                     kernel_initializer='he_normal')(x)
    # Define the model
    model = Model(inputs=inputs, outputs=outputs, name='DenseNet')
    return model
# DenseNet 121 network constructed
model = DenseNet(input_shape=(28, 28, 1), num_classes=num_classes, dense_blocks=3,
           dense_layers=4, growth_rate=12, reduction=0.5, dropout_rate=0.0,
           weight_decay=1e-4)
# Specify optimizer, loss function and evaluation metrics
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
# Output model profiles
()

After the model is defined, we can start training the model, using the EarlyStopping strategy for early stopping and keeping the best model.

# Define an early stop strategy
earlystop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5,
                          verbose=1, mode='auto', restore_best_weights=True)
# Training models
history = (X_train, y_train, batch_size=batch_size, epochs=epochs,
          verbose=1, validation_data=(X_val, y_val), callbacks=[earlystop])

Finally, we can test the model and calculate metrics such as accuracy.

# Evaluation of the model
score = (X_test, y_test, verbose=0)
# Calculate the indicators
test_accuracy = score[1]
print('Test accuracy:', test_accuracy)
# Save the model
('densenet121.h5')

V. Experimental results and analysis

Using the above code, the DenseNet121 model is trained on the EMNIST dataset with 28x28 pixel letter images as input and 26 letter categories as output, and the final performance is evaluated on the test set. The results show that the model achieves more than 96% classification accuracy on the test set, proving its better generalization ability and robustness.

VI. Summary

This paper introduces a method based on DenseNet121 model to realize the task of recognizing 26 English alphabets, which mainly involves the steps of data preprocessing, model definition and training, and evaluation, etc. DenseNet has the advantages of strong interpretability and low computational complexity, which can effectively improve the model accuracy and speed. It is worth noting that aspects such as adjusting the model hyperparameters, optimizing the dataset and model structure are also needed in practical applications to further improve the model performance and generalizability.

Above is the details of DenseNet121 model to realize 26 English letters recognition task, more information about DenseNet121 to realize 26 English letters recognition task please pay attention to my other related articles!