0
0
Computer Visionml~5 mins

ResNet and skip connections in Computer Vision

Choose your learning style9 modes available
Introduction

ResNet helps deep learning models learn better by letting information skip some layers. This avoids problems when models get too deep.

When building very deep neural networks for image recognition.
When training models that start to perform worse as they get deeper.
When you want to improve accuracy without adding too much training time.
When you want to avoid the problem of vanishing gradients in deep networks.
Syntax
Computer Vision
def residual_block(x, filters):
    shortcut = x
    x = Conv2D(filters, (3,3), padding='same', activation='relu')(x)
    x = Conv2D(filters, (3,3), padding='same')(x)
    x = Add()([x, shortcut])
    x = Activation('relu')(x)
    return x

The shortcut is the input that skips the convolution layers.

The Add() layer combines the shortcut and the processed input.

Examples
A basic residual block with two convolution layers and a skip connection.
Computer Vision
def simple_residual_block(x):
    shortcut = x
    x = Conv2D(64, (3,3), padding='same', activation='relu')(x)
    x = Conv2D(64, (3,3), padding='same')(x)
    x = Add()([x, shortcut])
    x = Activation('relu')(x)
    return x
Use a 1x1 convolution on the shortcut to match dimensions when filters change.
Computer Vision
def residual_block_with_projection(x, filters):
    shortcut = Conv2D(filters, (1,1), padding='same')(x)
    x = Conv2D(filters, (3,3), padding='same', activation='relu')(x)
    x = Conv2D(filters, (3,3), padding='same')(x)
    x = Add()([x, shortcut])
    x = Activation('relu')(x)
    return x
Sample Model

This code builds a small ResNet-like model using residual blocks with skip connections. It trains on MNIST digits for one epoch and prints test loss and accuracy.

Computer Vision
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, Add, Activation, Flatten, Dense
from tensorflow.keras.models import Model

# Define a simple residual block

def residual_block(x, filters):
    shortcut = x
    x = Conv2D(filters, (3,3), padding='same', activation='relu')(x)
    x = Conv2D(filters, (3,3), padding='same')(x)
    x = Add()([x, shortcut])
    x = Activation('relu')(x)
    return x

# Build a small ResNet-like model
inputs = Input(shape=(28,28,1))
x = Conv2D(32, (3,3), padding='same', activation='relu')(inputs)
x = residual_block(x, 32)
x = residual_block(x, 32)
x = Flatten()(x)
outputs = Dense(10, activation='softmax')(x)

model = Model(inputs, outputs)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Load MNIST data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train[..., None] / 255.0
x_test = x_test[..., None] / 255.0

# Train for 1 epoch for demo
history = model.fit(x_train, y_train, epochs=1, batch_size=64, validation_split=0.1)

# Evaluate on test data
loss, accuracy = model.evaluate(x_test, y_test)

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

Skip connections help the model learn identity functions easily, so deeper layers don't harm performance.

If input and output shapes differ, use a 1x1 convolution on the shortcut to match them.

ResNet models won many image recognition challenges because of this simple idea.

Summary

ResNet uses skip connections to let information flow directly across layers.

This helps train very deep networks without losing accuracy.

Skip connections add the input to the output of some layers, making learning easier.