পাঠ ১৭ · ৩৫-এর মধ্যে · মডিউল ৩
Home / AI Courses / Computer Vision / Detection intro

Object detection কী

What is object detection — locate + classify
৬ মিনিট পড়া মাঝারি · Intermediate PyTorch + COCO

এই পাঠে যা শিখবেন

  • Detection vs classification — fundamental difference
  • Bounding box, IoU, mAP
  • One-stage vs two-stage detectors
  • Application landscape

১ · Detection task definition

Input: একটি ছবি। Output: list of detection — প্রতিটি = (class, bounding box, confidence)।

Example: একটি traffic photo-তে — তিনটি car, দু'টি pedestrian, একটি motorcycle। প্রতিটির ছবিতে কোথায় (rectangle) এবং কী (class)।

কেন্দ্রীয় ধারণা

Classification: "image-এ কী আছে?" (single answer)। Detection: "প্রতিটি object কোথায় ও কী?" (multiple answers)। Output structure থেকে training pipeline ভিন্ন।

২ · Bounding box

একটি rectangle যা object cover করে। Format-এর variation:

  • Pascal VOC: $(x_1, y_1, x_2, y_2)$ — top-left ও bottom-right।
  • COCO: $(x, y, w, h)$ — top-left + width/height।
  • YOLO: $(x_c, y_c, w, h)$ normalized [0,1]।

৩ · IoU (Intersection over Union)

দু'টি bbox-এর match measure:

$$\text{IoU} = \frac{|A \cap B|}{|A \cup B|}$$

  • 1.0 = perfect overlap।
  • 0 = no overlap।
  • 0.5 — typical correctness threshold।

৪ · mAP (mean Average Precision)

Detection-এর primary metric। Per-class AP-এর mean।

  • Per-class: recall-precision curve-এর area।
  • mAP@0.5: IoU 0.5 threshold-এ AP।
  • mAP@0.5:0.95: COCO standard — IoU 0.5, 0.55, ..., 0.95-এর average।
  • mAP-small/medium/large: object size-ভিত্তিক।

৫ · Classification থেকে detection-এর challenge

  • Variable output count: এক image-এ ০ থেকে শতাধিক object।
  • Localization accuracy: tight bounding box।
  • Multi-scale: ছোট ও বড় বস্তু একসাথে।
  • Class imbalance: background pixel object-এর চেয়ে অনেক বেশি।
  • Compute: per-image বহু operation।

৬ · Two-stage detectors

  • Stage 1: Region Proposal — "এই region-এ কিছু আছে?" (objectness, no class)।
  • Stage 2: Classify each proposal + refine box।
  • Examples: R-CNN, Fast R-CNN, Faster R-CNN, Mask R-CNN।
  • Pro: high accuracy।
  • Con: slower (multi-stage compute)।

৭ · One-stage detectors

  • Single shot: grid-এর প্রতি cell-এ direct (class + box) predict।
  • No proposals: end-to-end one pass।
  • Examples: YOLO (v1-v8), SSD, RetinaNet, EfficientDet।
  • Pro: realtime — 30-100+ FPS।
  • Con: traditionally less accurate (gap shrunk)।

৮ · Modern transformer-based

  • DETR (২০২০): end-to-end transformer detector। NMS-free।
  • Deformable DETR, DINO-DETR: faster convergence।
  • RT-DETR: realtime DETR — YOLO competitor।
Classification = "এই ছবি কী?"। Detection = "এই ছবিতে কতগুলো objects, কোথায়, কী?"। Difference — single answer vs structured list of answers। Pipeline complexity-এর difference।
Computer Vision tasks — output difference 📷 Classification 🐱 cat: 0.95 single label 🎯 Detection cat 0.92 dog 0.88 class + box list 🎨 Segmentation pixel-level mask single class label {cat: 0.95} list of (class, box, conf) [(cat, [..]), (dog, [..])] per-pixel class H×W class map complexity ↑ → accuracy bar ↑
CV-র মূল task hierarchy — classification → detection → segmentation। Output structure ভিন্ন।

৯ · Detection pipeline-এর elements

  • Backbone: ResNet/EfficientNet feature extractor।
  • Neck: FPN (Feature Pyramid Network) — multi-scale feature।
  • Head: classification + bbox regression।
  • NMS: non-maximum suppression — duplicate detection remove।

১০ · COCO dataset

  • ৩৩০K image, ৮০ class।
  • Annotation: bbox + segmentation mask + keypoints।
  • Modern detection-এর benchmark।
  • আজকের SOTA: mAP 65-70%।

১১ · Application landscape

  • Autonomous driving: car, pedestrian, traffic sign।
  • Surveillance: intrusion, weapon detection।
  • Retail: product detection — Amazon Go-style।
  • Medical: tumor, lesion detection।
  • Agriculture: crop disease, pest counting।
  • Sports: player tracking।
  • Industrial: defect detection।
Detection — CV-র সবচেয়ে practical task। Classification থেকে complexity 10x, কিন্তু real-world value-ও অনেক বেশি। Bangladesh-এর traffic, agriculture, medical — সবকিছুর core CV need।

ভাবনার প্রশ্ন

প্র ০১ Detection-এর output variable length — ০ থেকে ১০০ object। Neural network-এর fixed output size constraint কীভাবে handle?

এটি detection architecture design-এর central challenge। Multiple solutions evolved।

Approach 1: Dense grid prediction (YOLO):

  • Image-কে $S \times S$ grid-এ ভাগ।
  • Each cell predict $B$ box (typical 3-5)।
  • Total $S \times S \times B$ candidate output।
  • Confidence threshold + NMS — final detection।
  • Fixed output: $S \times S \times B \times (5 + C)$।

Approach 2: Region proposal (Faster R-CNN):

  • RPN proposes ~2000 candidate region।
  • Each candidate independent classify + refine।
  • Per-image variable count detection।
  • Fixed at proposal stage, variable at output।

Approach 3: Set prediction (DETR):

  • Fixed $N$ "object queries" (e.g., 100)।
  • Each query — one slot in output (or "no object")।
  • Hungarian matching loss।
  • Truly fixed output size।

Approach 4: Anchor-free (FCOS, CenterNet):

  • Each pixel predict — center? size?
  • Heatmap-style output।
  • Local maxima → detection।

NMS — common deduplication:

  • Multiple cell same object detect।
  • NMS — highest confidence keep, IoU > 0.5 with high — discard।
  • Variable output count emerge।

Trade-offs:

  • Dense grid: dense candidate, NMS-dependent।
  • Two-stage: accurate, slow।
  • DETR: elegant set, slow convergence।
  • Anchor-free: simpler, gaining popularity।

Loss function complexity:

  • Each ground-truth box → assign to one prediction (anchor matching)।
  • Unassigned predictions → "background" class।
  • Loss combination: classification + localization + objectness।

মূল উপলব্ধি: Variable output — neural network fundamental limitation। Detection-এর architectural creativity এই challenge-কে different ways-এ address। Modern trend — set prediction (DETR) elegant।

প্র ০২ mAP@0.5 ও mAP@0.5:0.95 — পার্থক্য কেন matter? কোনটা production-এ ভালো metric?

এটি detection community-র ongoing debate। Different metric different reality capture।

mAP@0.5 (Pascal VOC standard):

  • Single IoU threshold।
  • "Box approximately right" — counted।
  • Lenient localization criterion।
  • Pre-2017 standard।

mAP@0.5:0.95 (COCO):

  • 10 IoU threshold (0.5, 0.55, ..., 0.95) average।
  • Tight localization rewarded।
  • Higher bar — harder to score high।
  • Modern standard।

Numerical difference:

  • Faster R-CNN: mAP@0.5 = 70%, mAP@0.5:0.95 = 35-40%।
  • YOLOv8: mAP@0.5 = 65%, mAP@0.5:0.95 = 47%।
  • "Better localization" matters।

Production metric choice:

  • Self-driving: mAP@0.7 or higher — safety, tight box matters।
  • Surveillance: mAP@0.5 sufficient — "presence detect" goal।
  • Medical: mAP@0.7-0.9 — precise lesion border।
  • Counting: mAP@0.5 — count more important than tightness।
  • Robot grasping: mAP@0.9 — tight box critical।

Other relevant metrics:

  • AR (Average Recall): max detection / proposal recall।
  • AP-small/medium/large: size-stratified।
  • F1 score at threshold: single confidence level।
  • FPS / latency: realtime constraint।

Beyond traditional mAP:

  • Open-vocabulary detection: CLIP-based — zero-shot class।
  • Probability calibration: confidence reliability।
  • Robustness: COCO-Stuff, OOD eval।

Caveats:

  • mAP — single number, hides per-class variation।
  • Class imbalance — rare class poorly score।
  • "Hard negative" — confused background as object।
  • Always look at confusion matrix, qualitative samples।

Bangladesh use case:

  • Traffic detection — mAP@0.5 + size-specific।
  • Crop disease — mAP@0.5 + class-balanced।
  • Document text — IoU > 0.7 (tight letter box)।

মূল উপলব্ধি: Single metric oversimplify। Production-এ multiple metric track + qualitative review। Numbers tell story, but visual inspection ground truth।

প্র ০৩ One-stage (YOLO) vs two-stage (Faster R-CNN) — কখন কোনটা? Modern era-এ এই distinction এখনো relevant?

২০১৬-এর strict dichotomy ক্রমশ blur হচ্ছে। কিন্তু core trade-off relevant।

Historical context:

  • Two-stage era (2014-2017): R-CNN → Fast R-CNN → Faster R-CNN। Accuracy lead।
  • One-stage emergence (2016+): YOLO, SSD, RetinaNet — speed-focused।
  • Modern: YOLOv8, RT-DETR — accuracy gap closed।

Two-stage strengths:

  • Localization accuracy: RPN refine + RoI align — tight boxes।
  • Small object: proposal at multiple scales।
  • Custom objective: stage modification flexible।
  • Used in: Mask R-CNN, Cascade R-CNN।

One-stage strengths:

  • Speed: 30-150+ FPS।
  • Mobile-friendly: lightweight backbones, quantizable।
  • Simpler training: single loss।
  • Used in: realtime application।

Modern landscape:

  • YOLOv8 (2023): mAP 53.9% on COCO, 280 FPS।
  • Faster R-CNN ResNet-50: mAP 41.0%, 17 FPS।
  • RT-DETR: 53% mAP, 100 FPS।
  • DINO-DETR: 63% mAP, slow।

Choice criteria:

  • Realtime (>30 FPS): YOLOv8, RT-DETR।
  • Maximum accuracy: DINO-DETR, Co-DETR।
  • Mobile/edge: YOLOv8-nano, MobileDet।
  • Instance segmentation: Mask2Former (DETR-style)।
  • Custom annotation type: two-stage flexibility।

DETR family — third paradigm:

  • Set prediction, no anchor, no NMS।
  • Traditionally slower convergence।
  • DINO, Co-DETR — accuracy SOTA।
  • RT-DETR — realtime competitive।

Bangladesh deployment:

  • Mobile traffic app: YOLOv8-nano।
  • Server CCTV: YOLOv8-medium।
  • Medical research: Faster R-CNN + RX, accuracy priority।
  • Edge IoT: YOLOv8-tiny, MobileDet।

Future trend:

  • Distinction blurring।
  • "Anchor-free + NMS-free" — DETR direction।
  • Foundation model detection (Grounding DINO) — text prompt!

মূল উপলব্ধি: Old binary (one vs two stage) misleading। Modern question — speed vs accuracy + which architecture family + foundation model leverage।

প্র ০৪ NMS (Non-Maximum Suppression) — almost সব detector-এর post-processing। কী algorithm, কেন এটি একটি limitation?

NMS — detection-এর "necessary evil"। Heuristic, deterministic, কিন্তু modern criticism।

NMS algorithm:

  1. Confidence-এ sort detections (descending)।
  2. Highest confidence keep।
  3. IoU > threshold-এ overlap-এর সব discard।
  4. Repeat next highest confidence।
  5. Continue till empty।
def nms(boxes, scores, iou_thresh=0.5):
    keep = []
    sorted_idx = scores.argsort(descending=True)
    while len(sorted_idx) > 0:
        idx = sorted_idx[0]
        keep.append(idx)
        ious = compute_iou(boxes[idx], boxes[sorted_idx[1:]])
        sorted_idx = sorted_idx[1:][ious <= iou_thresh]
    return keep

Why needed:

  • Detector dense prediction — same object multiple time।
  • Without NMS — output cluttered।
  • Visualization inspires confusion।

NMS-এর problems:

  • Hyperparameter: IoU threshold task-dependent।
  • Crowded scene: two cars touching → one suppress।
  • Inference cost: $O(N^2)$ box pairs।
  • Non-differentiable: end-to-end training-এ obstacle।

Variants:

  • Soft-NMS: linear/Gaussian decay instead of hard suppress।
  • Weighted NMS: bbox merge instead of suppress।
  • Adaptive NMS: threshold per-instance।
  • Matrix NMS: parallelizable।

NMS-free detectors:

  • DETR: set prediction — Hungarian matching ensure unique।
  • Sparse R-CNN: learnable proposals।
  • FCOS-style centerness: implicit duplicate suppress।

Modern hybrid:

  • Most production system এখনো NMS use।
  • YOLOv8: NMS।
  • RT-DETR: NMS-free!
  • Co-DETR: NMS-free with high accuracy।

Realtime impact:

  • YOLOv8 GPU: NMS 1-2 ms।
  • Mobile: NMS often biggest bottleneck post-conv।
  • NMS-free — mobile-attractive।

Crowded scene — case study:

  • Pedestrian dense crowd — overlap inevitable।
  • Standard NMS → many missed।
  • Soft-NMS → some recover।
  • DETR-style → handles natively।

Bangladesh context:

  • Dhaka traffic — extremely crowded।
  • Standard YOLO + NMS — overlap rickshaw + CNG miss।
  • RT-DETR or specialized crowd detector preferable।

মূল উপলব্ধি: NMS — historical artifact। Modern direction NMS-free। কিন্তু practical deployment-এ এখনো ubiquitous। Tool change হলেও limitation conceptual।

অনুশীলন

  1. IoU calc: Box A = (10, 10, 50, 50), B = (30, 30, 60, 60) — IoU?

    Intersection: $(30-50) \times (30-50) = 20 \times 20 = 400$. A area = $40 \times 40 = 1600$, B area = $30 \times 30 = 900$. Union = $1600 + 900 - 400 = 2100$. IoU = $400/2100 ≈ 0.19$।

  2. Pretrained detector: torchvision Faster R-CNN load করে inference।
    from torchvision.models.detection import fasterrcnn_resnet50_fpn
    m = fasterrcnn_resnet50_fpn(weights='DEFAULT').eval()
    img = torch.rand(3, 800, 800)
    with torch.no_grad():
        out = m([img])
    print(out[0].keys())  # 'boxes', 'labels', 'scores'
  3. ভাবুন: ১টি 1024×768 ছবিতে YOLOv8 inference নিতে কত compute? 3 anchor, 80 class।

    Roughly: backbone ~10 GFLOPs, head per-pixel grid (1024/32 × 768/32 × 3 anchors) ~6300 candidate। Total ~15-20 GFLOPs। RTX 3060 — 30+ FPS।

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

কোড রানার কাজ না করলে? ব্রাউজারে কাজ না করলে Google Colab ব্যবহার করুন — Google-এর ফ্রি অনলাইন Python পরিবেশ, শুধু Gmail অ্যাকাউন্ট লাগে।
পূর্ববর্তী পাঠ
পাঠ ১৬ · Transfer learning