Building a ResNet-34 Model with Keras

Building a ResNet-34 Model with Keras

Deep Learning has evolved tremendously over the years, and one significant advancement is the inception of Residual Networks (ResNet). We'll guide you through constructing a ResNet-34 model using Keras in TensorFlow.

What is ResNet?

Residual Networks, or ResNet, introduced 'skip connections' or 'shortcuts', allowing the gradient to bypass layers during backpropagation. This architectural improvement helps with the vanishing gradient problem, thus allowing the construction of much deeper networks.

Getting Started: ResNet-34

ResNet-34 is a version of ResNet with 34 layers, including convolutional and fully connected layers.

Let's dive into the code!

Prerequisites:

Ensure you have TensorFlow (v2+) installed:

pip install tensorflow

Step 1: Defining a Residual Block

ResNet is composed of several residual blocks. Let's start by defining one:

from tensorflow.keras.layers import Conv2D, BatchNormalization, ReLU, Add

def residual_block(x, filters, kernel_size=3, stride=1, conv_shortcut=True):
    if conv_shortcut:
        shortcut = Conv2D(filters, kernel_size=1, strides=stride, padding='same')(x)
        shortcut = BatchNormalization()(shortcut)
    else:
        shortcut = x

    x = Conv2D(filters, kernel_size=kernel_size, strides=stride, padding='same')(x)
    x = BatchNormalization()(x)
    x = ReLU()(x)

    x = Conv2D(filters, kernel_size=kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization()(x)

    x = Add()([shortcut, x])
    x = ReLU()(x)

    return x

The residual_block function creates two convolutional layers and an optional shortcut connection.

Step 2: Constructing the ResNet-34 Architecture

After defining the residual blocks, we can stack them to create the ResNet-34 architecture.

from tensorflow.keras.layers import Input, MaxPooling2D, GlobalAveragePooling2D, Dense

def resnet_34(input_shape=(224, 224, 3), num_classes=1000):
    inputs = Input(shape=input_shape)

    # Initial Conv
    x = Conv2D(64, kernel_size=7, strides=2, padding='same')(inputs)
    x = BatchNormalization()(x)
    x = ReLU()(x)
    x = MaxPooling2D(pool_size=3, strides=2, padding='same')(x)

    # Residual blocks
    for size in [64, 128, 256, 512]:
        strides = 1 if size == 64 else 2
        x = residual_block(x, size, stride=strides)
        for _ in range(1, (34-2)//6 if size == 64 else 2):
            x = residual_block(x, size, conv_shortcut=False)

    # Global Average Pooling and Fully Connected Layer
    x = GlobalAveragePooling2D()(x)
    outputs = Dense(num_classes, activation='softmax')(x)

    return tf.keras.models.Model(inputs=inputs, outputs=outputs)

Step 3: Viewing the Model

To see our ResNet-34 model in action:

model = resnet_34()
model.summary()

This will print a summary of the model's architecture, showing the stacked layers and their shapes.

Conclusion

With just a few lines of code, you've successfully constructed a ResNet-34 model using Keras! This powerful deep-learning model is versatile and ideal for tasks requiring depth.

Happy modelling!