অধ্যায়Phase 5 · ডিপ লার্নিং ফর ভিশন
5.6 15 মিনিট পড়া

Segmentation Basics

Semantic ও instance segmentation।

🎬 গল্প দিয়ে শুরু
Detection bounding box দেয়, segmentation প্রতিটি pixel-এর class দেয়। Medical tumor, lane detection, background replacement — এসব detection-এ অসম্ভব, segmentation-এই সম্ভব।

দুটি প্রধান ধরন

  • Semantic — একই class-এর সব pixel একসাথে (সব মানুষ → "person")।
  • Instance — প্রতিটি object আলাদা (person-1, person-2)।
  • Panoptic — দুটোর combination।

YOLOv8 segmentation

python
from ultralytics import YOLO

model = YOLO("yolov8n-seg.pt")
res = model("street.jpg")[0]
res.save("seg.jpg")

import numpy as np
for mask, cls in zip(res.masks.data, res.boxes.cls):
    m = mask.cpu().numpy().astype(np.uint8)   # binary mask
    print(model.names[int(cls)], m.shape, m.sum(), "pixels")

Mask2Former / SAM — modern

Meta SAM (Segment Anything) — prompt দিয়ে যেকোনো object segment। হাজার class train করার দরকার নেই।

python
# pip install segment-anything-2
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor

sam = build_sam2("sam2_hiera_small.yaml", "sam2_hiera_small.pt")
predictor = SAM2ImagePredictor(sam)
predictor.set_image(img_rgb)
masks, _, _ = predictor.predict(point_coords=[[450, 320]],
                                point_labels=[1])

Classic U-Net (custom training)

python
# pip install segmentation-models-pytorch
import segmentation_models_pytorch as smp
import torch

model = smp.Unet(
    encoder_name="resnet34",
    encoder_weights="imagenet",
    in_channels=3, classes=1,
)
loss = smp.losses.DiceLoss(mode="binary")
opt  = torch.optim.AdamW(model.parameters(), lr=1e-3)
# usual train loop — model(x) → mask logits
Mask metrics
IoU (Jaccard)Dice — pixel-level overlap measure।

Real applications

  • Background remove (Zoom virtual bg, photo editor)।
  • Medical — tumor/organ segmentation।
  • Lane/road segmentation — self-driving।
  • Garments — defect area % calculation।
  • Satellite — building footprint, crop area।

Background removal mini-demo

python
from ultralytics import YOLO
import cv2, numpy as np

m = YOLO("yolov8n-seg.pt")
img = cv2.imread("portrait.jpg")
res = m(img)[0]

mask = np.zeros(img.shape[:2], np.uint8)
for seg, cls in zip(res.masks.data, res.boxes.cls):
    if m.names[int(cls)] == "person":
        s = (seg.cpu().numpy() * 255).astype(np.uint8)
        s = cv2.resize(s, (img.shape[1], img.shape[0]))
        mask = cv2.bitwise_or(mask, s)

bg = np.full_like(img, (200, 200, 200))           # plain grey
out = np.where(mask[..., None] > 0, img, bg)
cv2.imwrite("nobg.jpg", out)
প্র্যাকটিস টাস্ক
  1. YOLOv8-seg দিয়ে নিজের একটি photo-তে person segment করুন।
  2. Background grey-এর বদলে blur version দিয়ে portrait mode বানান।
  3. SAM দিয়ে click দিয়ে একটি বস্তু segment করার ছোট demo লিখুন।

সারসংক্ষেপ

  • Segmentation = pixel-level class assignment।
  • Semantic vs Instance vs Panoptic — কাজ অনুযায়ী choose।
  • YOLOv8-seg fast, SAM prompt-based, U-Net custom।
  • IoU/Dice ভালো metric; pixel accuracy misleading।