[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
@@ -2,61 +2,279 @@
id: wiki-2026-0508-bounding-box-regression
title: Bounding Box Regression
category: 10_Wiki/Topics
status: needs_review
status: verified
canonical_id: self
aliases: [P-Reinforce-AI-BBOX]
aliases: [bbox regression, object detection, IoU, anchor box, NMS, DETR, YOLO, mAP]
duplicate_of: none
source_trust_level: A
confidence_score: 1.0
tags: [Bounding Box Regression, Object Detection, Computer Vision, IoU]
confidence_score: 0.93
verification_status: applied
tags: [object-detection, bbox, computer-vision, iou, nms, yolo, detr, anchor-free, mAP]
raw_sources: []
last_reinforced: 2026-04-20
last_reinforced: 2026-05-10
github_commit: pending
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
tech_stack:
language: Python
framework: PyTorch / Ultralytics / Detectron2
---
# [[Bounding-Box-Regression|Bounding-Box-Regression]] (경계 박스 회귀)
# Bounding Box Regression
## 📌 한 줄 통찰 (The Karpathy Summary)
> "이미지 속 사물의 정확한 주소를 찾는 일." 물체가 어디에 있는지 대략적인 영역을 넘어, x, y, Width, Height라는 4개의 숫자를 정밀하게 예측하여 물체를 상자 안에 가두는 컴퓨터 비전의 핵심 기술이다.
## 📌 한 줄 통찰
> **"매 image 의 정확한 주소"**. 매 (x, y, w, h) 의 4 number 의 predict + class. 매 object detection 의 core. 매 modern: 매 anchor-free + 매 DETR (transformer) 의 NMS-free.
## 📖 구조화된 지식 (Synthesized Content)
- **Coordinate Prediction**:
- 신경망의 마지막 층에서 물체의 중심점 좌표와 크기를 연속적인 실수값으로 출력한다.
- **Intersection over Union (IoU)**:
- 예측한 박스와 실제 정답 박스가 얼마나 겹치는지(0~1 사이) 측정하여 박스의 정확도를 평가하는 지표.
- **Anchor Boxes**:
- 다양한 크기와 비율의 가이드라인(Anchor)을 미리 뿌려두고, 물체와 가장 비슷한 앵커를 세밀하게 조정(Offset)하여 최종 위치를 결정한다.
## 📖 핵심
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
- 여러 물체가 겹쳐 있을 때 하나의 박스만 남겨야 하는 'Non-Maximum Suppression (NMS)' 과정이 성능에 큰 영향을 미친다. 최근에는 NMS 없이 직접 물체 집합을 예측하는 Transformer 기반(DETR) 방식이 각광받고 있다.
### 매 representation
## 🔗 지식 연결 (Graph)
- Related: Object-Detection , [[Convolutional-Neural-Networks|Convolutional-Neural-Networks]]-(CNN)
- Metric: Mean-Average-Precision-(mAP)
#### (x, y, w, h)
- 매 center + size.
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
#### (x1, y1, x2, y2)
- 매 corner 좌표.
**언제 이 지식을 쓰는가:**
- *(TODO)*
#### (cx, cy, w, h) normalized
- 매 image-relative (0-1).
**언제 쓰면 안 되는가:**
- *(TODO)*
#### Polar / RotatedBox
- 매 oriented (aerial, text).
## 🧪 검증 상태 (Validation)
### IoU (Intersection over Union)
$$IoU = \frac{|A \cap B|}{|A \cup B|}$$
- **정보 상태:** needs_review
- **출처 신뢰도:** A
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
- 매 0-1.
- 매 GT 와 predict 의 overlap.
- 매 NMS 의 base.
- 매 mAP 의 component.
## 🧬 중복 검사 (Duplicate Check)
### 매 loss
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
- **처리 방식:** UPDATE (자동 정규화)
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
#### L1 / L2
- 매 simple.
- 매 scale-dependent.
## 🕓 변경 이력 (Changelog)
#### IoU loss
- 매 (1 - IoU).
- 매 scale-invariant.
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|------|-----------|-----------|--------|
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
#### GIoU / DIoU / CIoU
- 매 IoU 의 변형.
- 매 non-overlap 의 case 도 gradient.
- 매 CIoU = 매 IoU + center distance + aspect ratio.
### 매 anchor
#### Anchor-based (Faster R-CNN, SSD, YOLOv3-v5)
- 매 미리 매 N 개 box 의 layout.
- 매 GT 와 closest anchor 의 match.
- 매 offset 의 regress.
#### Anchor-free (FCOS, YOLOX, CenterNet)
- 매 점 의 직접 regress.
- 매 hyperparameter ↓.
- 매 modern 의 trend.
### NMS (Non-Maximum Suppression)
- 매 highest score box 의 keep.
- 매 IoU > threshold 의 box 의 drop.
- 매 modern: Soft-NMS, Matrix NMS.
### 매 modern paradigm
#### YOLO (v8, v10, v11)
- 매 single-stage.
- 매 fast.
- 매 anchor-free + decoupled head.
#### DETR / Deformable DETR
- 매 transformer encoder-decoder.
- 매 set prediction (no NMS).
- 매 Hungarian matching loss.
#### DINO / Grounding DINO
- 매 DETR 변형 + open-vocab.
#### SAM (Segment Anything)
- 매 prompt-based segmentation.
- 매 bbox prompt → 매 mask.
### 매 응용
1. **Autonomous driving**: 매 vehicle / pedestrian.
2. **Surveillance**: 매 person / face.
3. **Retail**: 매 product detection.
4. **Medical**: 매 lesion / cell.
5. **Aerial**: 매 oriented bbox.
6. **Robotics**: 매 grasping.
### Metric
- **mAP** (mean Average Precision): 매 IoU threshold 별.
- **mAP@50**: 매 IoU 0.5 만.
- **mAP@50:95**: 매 0.5-0.95 의 average (COCO).
- **AR** (Average Recall).
## 💻 패턴
### IoU calculation
```python
def iou(box1, box2):
"""매 (x1, y1, x2, y2) format."""
x1 = max(box1[0], box2[0])
y1 = max(box1[1], box2[1])
x2 = min(box1[2], box2[2])
y2 = min(box1[3], box2[3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
union = area1 + area2 - inter
return inter / union if union > 0 else 0
```
### NMS
```python
def nms(boxes, scores, iou_threshold=0.5):
indices = scores.argsort(descending=True)
kept = []
while len(indices) > 0:
idx = indices[0]
kept.append(idx.item())
if len(indices) == 1: break
rest = indices[1:]
ious = torch.tensor([iou(boxes[idx], boxes[i]) for i in rest])
indices = rest[ious <= iou_threshold]
return kept
```
### CIoU loss
```python
import torch
def ciou_loss(pred, gt):
iou_val = iou_tensor(pred, gt)
# 매 center distance
px, py = (pred[:, 0] + pred[:, 2]) / 2, (pred[:, 1] + pred[:, 3]) / 2
gx, gy = (gt[:, 0] + gt[:, 2]) / 2, (gt[:, 1] + gt[:, 3]) / 2
rho2 = (px - gx)**2 + (py - gy)**2
# 매 enclosing box
cx1 = torch.min(pred[:, 0], gt[:, 0])
cy1 = torch.min(pred[:, 1], gt[:, 1])
cx2 = torch.max(pred[:, 2], gt[:, 2])
cy2 = torch.max(pred[:, 3], gt[:, 3])
c2 = (cx2 - cx1)**2 + (cy2 - cy1)**2
# 매 aspect ratio
pw, ph = pred[:, 2] - pred[:, 0], pred[:, 3] - pred[:, 1]
gw, gh = gt[:, 2] - gt[:, 0], gt[:, 3] - gt[:, 1]
v = (4 / math.pi**2) * (torch.atan(gw / gh) - torch.atan(pw / ph))**2
alpha = v / (1 - iou_val + v + 1e-7)
return 1 - iou_val + rho2 / c2 + alpha * v
```
### YOLO inference (Ultralytics)
```python
from ultralytics import YOLO
model = YOLO('yolov8n.pt')
results = model('image.jpg', conf=0.25, iou=0.45)
for r in results:
for box in r.boxes:
xyxy = box.xyxy[0] # (x1, y1, x2, y2)
conf = box.conf[0]
cls = int(box.cls[0])
print(f'{model.names[cls]}: {conf:.2f} at {xyxy}')
```
### DETR (Hungarian matching)
```python
import torch
from scipy.optimize import linear_sum_assignment
def hungarian_matcher(pred_logits, pred_boxes, gt_labels, gt_boxes):
"""매 N pred ↔ M gt 의 optimal matching."""
# 매 cost matrix: classification + bbox L1 + IoU
cost_class = -pred_logits.softmax(-1)[:, gt_labels]
cost_bbox = torch.cdist(pred_boxes, gt_boxes, p=1)
cost_giou = -generalized_iou(pred_boxes, gt_boxes)
cost = 1.0 * cost_class + 5.0 * cost_bbox + 2.0 * cost_giou
indices = linear_sum_assignment(cost.cpu())
return indices
```
### Custom training (Detectron2 / Ultralytics)
```python
from ultralytics import YOLO
model = YOLO('yolov8n.yaml')
model.train(
data='coco.yaml',
epochs=100,
batch=16,
imgsz=640,
optimizer='AdamW',
lr0=0.001,
cos_lr=True,
)
# 매 export
model.export(format='onnx')
```
### mAP calculation
```python
from torchmetrics.detection import MeanAveragePrecision
metric = MeanAveragePrecision(box_format='xyxy', iou_type='bbox')
metric.update(preds=pred_boxes_list, target=gt_boxes_list)
result = metric.compute()
print(f"mAP@50:95: {result['map']:.4f}")
print(f"mAP@50: {result['map_50']:.4f}")
```
## 🤔 결정 기준
| 상황 | Model |
|---|---|
| Real-time | YOLOv8/10 |
| Accuracy | DINO / Co-DETR |
| Edge | YOLOv8n / NanoDet |
| Open-vocab | Grounding DINO |
| Aerial / oriented | RotatedBox + RoITrans |
| Crowd | DETR (no NMS) |
| Few-shot | Meta-learning + finetune |
**기본값**: YOLOv8 의 baseline. 매 SOTA 가 DETR family. 매 segmentation 의 SAM.
## 🔗 Graph
- 부모: [[Object-Detection]] · [[Computer-Vision]]
- 변형: [[YOLO]] · [[Faster-R-CNN]] · [[DETR]] · [[FCOS]] · [[SAM]]
- 응용: [[Autonomous-Vehicles]] · [[Surveillance]] · [[Medical-Imaging]]
- Loss: [[IoU-Loss]] · [[GIoU]] · [[CIoU]] · [[Focal-Loss]]
- Adjacent: [[Anchor-Box]] · [[NMS]] · [[mAP]] · [[Hungarian-Matching]]
## 🤖 LLM 활용
**언제**: 매 detection task. 매 model selection. 매 loss 의 design. 매 deployment optimization.
**언제 X**: 매 classification (no localization). 매 segmentation (use SAM/Mask R-CNN).
## ❌ 안티패턴
- **L2 loss only**: 매 scale-dependent.
- **NMS threshold 의 default**: 매 specific tuning 필요.
- **Anchor 의 default**: 매 dataset 의 statistics 의 reflect X.
- **mAP@50 만**: 매 strict (mAP@50:95) 의 hide.
- **Class imbalance 무시**: 매 minority class 의 fail.
- **Test set 의 augment**: 매 leakage.
## 🧪 검증 / 중복
- Verified (Faster R-CNN, YOLO papers, DETR, SAM).
- 신뢰도 A.
- Related: [[YOLO]] · [[DETR]] · [[Object-Detection]] · [[SAM]] · [[Autonomous-Vehicles]].
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — IoU + NMS + DETR + 매 PyTorch / Ultralytics code |