Module 16 of 21 · Computer Vision · Intermediate

Project: Implementing Semantic Segmentation

Duration: 10 min

This module delves into the practical implementation of semantic segmentation, a crucial technique in computer vision that allows for pixel-wise classification of images. Understanding and implementing semantic segmentation is vital for applications such as autonomous driving, medical imaging, and augmented reality.

Understanding Semantic Segmentation

Semantic segmentation involves assigning a class label to each pixel in an image. This differs from object detection, where the goal is to locate objects within an image. Techniques like U-Net and Mask R-CNN are commonly used for semantic segmentation due to their effectiveness in capturing fine-grained details.

import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate

# Define a simple U-Net model
def unet():
    inputs = Input((256, 256, 3))
    conv1 = Conv2D(64, 3, activation='relu', padding='same')(inputs)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    up1 = UpSampling2D(size=(2, 2))(pool1)
    conv2 = Conv2D(64, 3, activation='relu', padding='same')(up1)
    model = Model(inputs=inputs, outputs=conv2)
    return model

# Create and summarize the model
model = unet()
model.summary()

Try it in Google Colab: Open in Colab

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 256, 256, 3)]     0         
_________________________________________________________________
conv2d (Conv2D)              (None, 256, 256, 64)      1792      
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 128, 128, 64)      0         
_________________________________________________________________
up_sampling2d (UpSampling2D) (None, 256, 256, 64)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 256, 256, 64)      36928     
=================================================================
Total params: 38,720
Trainable params: 38,720
Non-trainable params: 0
_________________________________________________________________

Implementing Semantic Segmentation with U-Net

The U-Net architecture is particularly well-suited for semantic segmentation tasks due to its symmetric expanding and contracting paths, which allow it to capture context and localization information effectively. The contracting path captures context, while the expansive path enables precise localization.

import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate

# Define a U-Net model
def unet():
    inputs = Input((256, 256, 3))
    conv1 = Conv2D(64, 3, activation='relu', padding='same')(inputs)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    conv2 = Conv2D(128, 3, activation='relu', padding='same')(pool1)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    up1 = UpSampling2D(size=(2, 2))(pool2)
    concat1 = concatenate([up1, conv2], axis=3)
    conv3 = Conv2D(64, 3, activation='relu', padding='same')(concat1)
    up2 = UpSampling2D(size=(2, 2))(conv3)
    concat2 = concatenate([up2, conv1], axis=3)
    conv4 = Conv2D(64, 3, activation='relu', padding='same')(concat2)
    outputs = Conv2D(1, 1, activation='sigmoid')(conv4)
    model = Model(inputs=inputs, outputs=outputs)
    return model

# Create and summarize the model
model = unet()
model.summary()

💡 Tip: When implementing semantic segmentation, ensure that your dataset is properly preprocessed and augmented to avoid overfitting. Additionally, fine-tuning hyperparameters such as learning rate and batch size can significantly impact the model's performance.

❓ What is the primary purpose of the contracting path in a U-Net architecture?

❓ What is the role of the expansive path in a U-Net architecture?

← Previous Continue interactively → Next →

Related Courses