๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #192

Semantic Segmentation

Semantic segmentation labels every pixel with a class โ€” "road," "sky," "car," "person" โ€” but makes no distinction between separate objects of the same class; two different cars are both simply labeled "car," with no way to tell them apart from the output alone.

Numerical Example

A tiny 4ร—4 image containing two cars and sky, semantically segmented (using class indices: 0=sky, 1=car):

\[ \begin{bmatrix}0&0&0&0\\0&1&1&0\\1&1&0&0\\0&0&0&0\end{bmatrix} \]

Both cars share the exact same class label (1) โ€” the output has no notion of "this is car A" versus "this is car B," even though a human looking at the original image could clearly see two separate vehicles.

Where Semantic Segmentation Is Sufficient

Tasks where counting or distinguishing individual objects doesn't matter โ€” e.g. self-driving car "drivable surface" detection (all road pixels matter equally, regardless of how many separate road segments are visible), or medical image segmentation of a single organ type (see U-Net's common application) where instance separation typically isn't needed.

Code

import torch
import torchvision.models.segmentation as seg_models

model = seg_models.fcn_resnet50(weights='DEFAULT')
model.eval()

x = torch.randn(1, 3, 224, 224)
output = model(x)['out']
print(output.shape)   # torch.Size([1, 21, 224, 224]) -- 21 classes, one prediction per pixel

predicted_classes = output.argmax(dim=1)
print(predicted_classes.shape)   # torch.Size([1, 224, 224]) -- final per-pixel class map

Common Mistakes

  • Using semantic segmentation when a task actually requires counting or distinguishing individual object instances โ€” this calls for instance segmentation (next note) instead.

Interview Relevance

Q: "If an image has three overlapping people, what would a semantic segmentation model's output look like?" Every pixel belonging to any person would be labeled with the same "person" class โ€” there would be no way to distinguish which pixels belong to which specific individual from the segmentation output alone; that distinction requires instance segmentation.

Practice Question

Why might semantic segmentation alone be sufficient for a "is this pixel drivable road or not" self-driving task, but insufficient for "how many pedestrians are in this scene"?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Semantic Segmentation โ€“ FAQs

Quick answers about learning Semantic Segmentation in Deep Learning.

This free note from CodingNow 2.0 explains Semantic Segmentation in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Semantic Segmentation, is 100% free with no signup required.
With focused practice, most students grasp Semantic Segmentation in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now