praca_magisterska/pytania/odpowiedzi/40-detekcja-obiektow.md

364 lines
21 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Pytanie 40: Detekcja obiektów w obrazach
## Pytanie
**"Opisać problem detekcji obiektów w obrazach. Przedstawić podstawowe strategie i algorytmy detekcji przy użyciu metod klasycznych oraz sieci neuronowych. Jak skonstruować detektor obiektów dysponując istniejącym klasyfikatorem tych obiektów?"**
Przedmiot: TWM (Techniki Wizji Maszynowej)
---
## 📚 Odpowiedź główna
### 1. Definicja problemu detekcji
```
┌─────────────────────────────────────────────────────────────────┐
│ DETEKCJA OBIEKTÓW │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Obraz wejściowy: Wynik detekcji: │
│ ┌────────────────┐ ┌────────────────┐ │
│ │ 🚗 🚶 │ │ ┌──┐ ┌──┐ │ │
│ │ 🚗 │ → │ │🚗│ │🚶│ │ │
│ │ 🚶 │ │ └──┘ └──┘ ┌──┐ │ │
│ │ │ │ ┌──┐│🚗│ │ │
│ └────────────────┘ │ ┌──┤🚶│└──┘ │ │
│ │ └──┴──┘ │ │
│ └────────────────┘ │
│ │
│ Wynik: lista (class, bounding_box, confidence) │
│ [(car, [x1,y1,x2,y2], 0.95), (person, [...], 0.87)] │
│ │
│ Różnica od klasyfikacji: │
│ • Klasyfikacja: Co jest na obrazie? │
│ • Detekcja: Co i GDZIE jest na obrazie? │
└─────────────────────────────────────────────────────────────────┘
```
---
### 2. Metody klasyczne
#### 2.1 Sliding Window + HOG/SIFT
```
┌─────────────────────────────────────────────────────────────────┐
│ SLIDING WINDOW │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. Przesuwaj okno po obrazie (różne skale, pozycje) │
│ │
│ ┌──┐ │
│ │░░│→→→→→→→→ │
│ └──┘ │
│ ↓ ↓ ↓ ↓ ↓ ↓ ↓ │
│ │
│ 2. Dla każdego okna: wyekstrahuj features (HOG, SIFT) │
│ 3. Klasyfikuj (SVM, Random Forest) │
│ 4. Non-Maximum Suppression (NMS) │
│ │
│ Problem: O(scales × positions × classifier) → WOLNE! │
└─────────────────────────────────────────────────────────────────┘
```
#### 2.2 HOG (Histogram of Oriented Gradients)
```
HOG descriptor:
1. Oblicz gradienty (Gx, Gy)
2. Magnitude: √(Gx² + Gy²)
3. Orientacja: arctan(Gy/Gx)
4. Podziel na cells (8×8 pikseli)
5. Histogram orientacji (9 bins)
6. Normalizacja w blokach
┌───┬───┬───┬───┐
│ │ │ │ │
├───┼───┼───┼───┤
│ │▓▓▓│▓▓▓│ │ ← histogram dla każdej cell
├───┼───┼───┼───┤
│ │▓▓▓│▓▓▓│ │
└───┴───┴───┴───┘
Użycie: HOG + linear SVM → pedestrian detection (Dalal & Triggs)
```
#### 2.3 Viola-Jones (Haar Cascades)
```
┌─────────────────────────────────────────────────────────────────┐
│ VIOLA-JONES (2001) - real-time face detection │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Haar-like features: │
│ ┌───┬───┐ ┌───────┐ ┌─┬─┐ │
│ │░░░│███│ │░░░░░░░│ │░│█│ │
│ │░░░│███│ │███████│ │█│░│ │
│ └───┴───┘ └───────┘ └─┴─┘ │
│ │
│ Feature = Σ(white) - Σ(black) │
│ Integral image: O(1) dla każdej feature! │
│ │
│ AdaBoost cascade: │
│ Stage1 → Stage2 → Stage3 → ... → Face! │
│ ↓ ↓ ↓ │
│ Reject Reject Reject (szybko odrzuca nie-twarze) │
│ │
│ Bardzo szybkie! (30fps w 2001) │
└─────────────────────────────────────────────────────────────────┘
```
---
### 3. Metody Deep Learning
#### 3.1 Two-Stage Detectors (R-CNN family)
```
┌─────────────────────────────────────────────────────────────────┐
│ R-CNN (2014) │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. Selective Search → ~2000 region proposals │
│ 2. Dla każdego regionu: │
│ - Resize do 224×224 │
│ - CNN (AlexNet) → features │
│ - SVM classifier │
│ 3. Bounding box regression │
│ 4. NMS │
│ │
│ Problem: 2000 × CNN forward pass → ~50 sec/image! │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ Fast R-CNN (2015) │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Image → CNN → Feature map │
│ ↓ │
│ ROI Pooling (region proposals) │
│ ↓ │
│ FC layers │
│ ↓ ↓ │
│ class bbox │
│ │
│ CNN tylko raz! ROI pooling wycina regiony z feature map │
│ ~2 sec/image (25× szybciej) │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ Faster R-CNN (2016) │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Image → CNN → Feature map → RPN (Region Proposal Network) │
│ │ ↓ │
│ └──────→ ROI Pooling │
│ ↓ │
│ Detection head │
│ ↓ ↓ │
│ class bbox │
│ │
│ RPN: mała sieć generująca proposals (zamiast Selective Search) │
│ End-to-end training! │
│ ~0.2 sec/image (~5 fps) │
└─────────────────────────────────────────────────────────────────┘
```
#### 3.2 One-Stage Detectors (YOLO, SSD)
```
┌─────────────────────────────────────────────────────────────────┐
│ YOLO (You Only Look Once) - 2016 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Image → CNN → S×S×(B×5 + C) tensor │
│ │
│ Dzieli obraz na S×S grid: │
│ ┌───┬───┬───┬───┬───┬───┬───┐ │
│ │ │ │ │ │ │ │ │ │
│ ├───┼───┼───┼───┼───┼───┼───┤ │
│ │ │ │ ● │ │ │ │ │ ← cell odpowiada za obiekt │
│ ├───┼───┼───┼───┼───┼───┼───┤ którego centrum tu jest │
│ │ │ │ │ │ │ │ │ │
│ └───┴───┴───┴───┴───┴───┴───┘ │
│ │
│ Każda cell predykuje: │
│ • B bounding boxes: (x, y, w, h, confidence) │
│ • C class probabilities │
│ │
│ Zaleta: Bardzo szybki! 45-155 fps │
│ Wada: Gorszy dla małych obiektów, obiektów blisko siebie │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ SSD (Single Shot Detector) - 2016 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Multi-scale feature maps: │
│ │
│ ┌────────────┐ │
│ │ 38×38×512 │ ← detect small objects │
│ └─────┬──────┘ │
│ ↓ │
│ ┌──────┐ │
│ │19×19 │ ← detect medium objects │
│ └──┬───┘ │
│ ↓ │
│ ┌────┐ │
│ │10×10│ ← detect large objects │
│ └────┘ │
│ │
│ Default boxes (anchors) o różnych aspect ratios │
│ Łączy zalety YOLO (szybkość) i Faster R-CNN (multi-scale) │
└─────────────────────────────────────────────────────────────────┘
```
#### 3.3 Nowoczesne architektury
```
YOLOv8 (2023):
• Anchor-free detection
• Decoupled head (cls/box separate)
• C2f module (CSP + bottleneck)
• ~80 mAP na COCO, real-time
DETR (2020):
• Transformer-based
• Bipartite matching loss (Hungarian)
• No anchors, no NMS
• End-to-end set prediction
RT-DETR (2023):
• Real-time DETR
• Efficient hybrid encoder
• IoU-aware query selection
```
---
### 4. Konstrukcja detektora z klasyfikatora
```
┌─────────────────────────────────────────────────────────────────┐
│ JAK ZROBIĆ DETEKTOR MAJĄC KLASYFIKATOR? │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Metoda 1: SLIDING WINDOW │
│ ───────────────────────── │
│ for scale in scales: │
│ for (x, y) in positions: │
│ crop = image[y:y+h, x:x+w] │
│ if classifier(crop) > threshold: │
│ detections.append((x, y, w, h, class)) │
│ return NMS(detections) │
│ │
│ Problem: Bardzo wolne (wiele przesunięć × skale) │
│ │
│ │
│ Metoda 2: REGION PROPOSALS + CLASSIFIER │
│ ───────────────────────────────────────── │
│ 1. Selective Search / EdgeBoxes → candidate regions │
│ 2. Dla każdego regionu: │
│ resize + classifier → class, confidence │
│ 3. NMS │
│ │
│ Szybsze niż sliding window (mniej regionów) │
│ │
│ │
│ Metoda 3: FINE-TUNE na DETECTION │
│ ───────────────────────────────── │
│ 1. Użyj pretrained classifier jako backbone │
│ 2. Dodaj detection head (bbox regression + cls) │
│ 3. Fine-tune na detection dataset │
│ │
│ Najlepsza jakość! │
└─────────────────────────────────────────────────────────────────┘
```
---
### 5. Non-Maximum Suppression (NMS)
```
Problem: Wiele overlapping detections
┌─────────┐
│ ┌──────┼──┐
│ │ 🚗 │ │ ← 3 nakładające się bbox
│ │ │ │
└──┼──────┘ │
└─────────┘
NMS Algorithm:
1. Sortuj detekcje wg confidence
2. Weź najlepszą, dodaj do wyników
3. Usuń wszystkie z IoU > threshold
4. Powtarzaj dla pozostałych
Soft-NMS: Nie usuwa, tylko obniża confidence
score = score × f(IoU) gdzie f maleje z IoU
```
---
### 6. Metryki
| Metryka | Opis |
|---------|------|
| **IoU** | Intersection over Union bbox |
| **Precision** | TP / (TP + FP) |
| **Recall** | TP / (TP + FN) |
| **AP** | Area under Precision-Recall curve |
| **mAP** | Mean AP across classes |
| **mAP@0.5** | mAP przy IoU threshold = 0.5 |
| **mAP@[.5:.95]** | mAP average across IoU thresholds |
---
## 🧠 Mnemoniki
### "YOLO = You Only Look Once":
Jednokrotne przejście przez sieć
### "R-CNN → Fast → Faster":
Ewolucja: 50s → 2s → 0.2s
### "Two-stage = proposals + classify":
Faster R-CNN: RPN + detection head
### "One-stage = direct":
YOLO, SSD: bezpośrednia predykcja
---
## ❓ Pytania dodatkowe
### Q1: "YOLO vs Faster R-CNN?"
**Odpowiedź:** YOLO: szybszy (real-time), gorszy dla małych obiektów. Faster R-CNN: dokładniejszy, wolniejszy, two-stage (RPN + detection). Trade-off speed vs accuracy.
### Q2: "Co to są anchor boxes?"
**Odpowiedź:** Predefiniowane boxy o różnych rozmiarach i aspect ratios. Sieć predykuje offset od anchora, nie absolutne koordynaty. Ułatwia uczenie (mniejsza przestrzeń wyjściowa).
### Q3: "Jak działają anchor-free detektory?"
**Odpowiedź:** Predykują bezpośrednio: punkt centralny + rozmiar (FCOS, CenterNet) lub keypoints (CornerNet). Prostsze, mniej hiperparametrów, konkurencyjne wyniki.
---
## 🎯 Kluczowe punkty
1. **Klasyczne:** HOG+SVM, Viola-Jones (Haar cascades)
2. **Two-stage:** R-CNN → Fast → Faster (RPN)
3. **One-stage:** YOLO, SSD (szybsze, real-time)
4. **NMS:** Eliminacja overlapping detekcji
5. **Metryka:** mAP@[.5:.95]
---
## 📖 Źródła
1. Girshick et al. - "R-CNN" (2014), "Fast R-CNN" (2015)
2. Ren et al. - "Faster R-CNN" (2016)
3. Redmon et al. - "YOLO" series (2016-2018)
4. Liu et al. - "SSD" (2016)
5. Carion et al. - "DETR" (2020)