๐ก ํ ์ค ์์ฝ#
๊ฐ์ฒด ํ์ง๋ฅผ ์ต์ปค(Anchor)๋ NMS(Non-Maximum Suppression) ๊ฐ์ ์์์ ํ์ฒ๋ฆฌ ์ฐ์ฐ ์์ด ์ด๋ถ ๋งค์นญ(Bipartite Matching) ์์ค๊ณผ Transformer ์ธ์ฝ๋-๋์ฝ๋ ์ฟผ๋ฆฌ(Object Query) ์์คํ ์ ํตํด ์ง์ ์งํฉ ์์ธก(Direct Set Prediction) ๋ฌธ์ ๋ก ์์ ํ ์ฌ์ ์ํ๋ค.
๐ ๊ฐ์ ๋ฐ ํต์ฌ ๊ธฐ์ฌ (Key Contributions)#
- ์ ์: Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko (Facebook AI Research)
- ๋ฐํ๋ ๋: 2020 (arXiv:2005.12872, ECCV 2020)
- ์ฃผ์ ๊ธฐ์ฌ์ :
- NMS ๋ฐ Anchor ์์์ ์ค๊ณ ์์ ์ ๊ฑฐ: ํ๊ฐ๋ฆฌ์ ์๊ณ ๋ฆฌ์ฆ(Hungarian Algorithm) ๊ธฐ๋ฐ 1:1 ๋งค์นญ ์์ค๋ก ์ค๋ณต ์์ธก์ ์์ฒ ๋ฐฉ์ง.
- End-to-End Transformer ๊ธฐ๋ฐ ํ์ง ํ์ดํ๋ผ์ธ: CNN ๋ฐฑ๋ณธ + Transformer ์ธ์ฝ๋-๋์ฝ๋ + $N$๊ฐ์ ํ์ต ๊ฐ๋ฅํ Object Query๋ก ๊ตฌ์ฑ๋ ์ง๊ด์ ๊ตฌ์กฐ.
- GIoU ๋ฐ L1 ํ๊ท ์์ค ๊ฒฐํฉ: ๋ฐ์ค ํฌ๊ธฐ์ ๋ถ๋ณํ GIoU(Generalized IoU)์ L1 ์์ค์ ๊ฒฐํฉํ์ฌ ๋ํ ๊ฐ์ฒด ํ์ง ์ฑ๋ฅ ๋ํญ ํฅ์(Faster R-CNN ๋๋น AP$_L$ +7.8 AP).
๐ฏ ๊ด๋ จ ์ฐ๊ตฌ ํ๋ฆ ๋ฐ ๊ธฐ์กด ํ๊ณ (Related Work & Motivation)#
๊ด๋ จ ์ฐ๊ตฌ ํ๋ฆ#
- Anchor/Proposal ๊ธฐ๋ฐ ๊ธฐ์กด ํ์ง๊ธฐ (Faster R-CNN, YOLO): ์์ฒ ๊ฐ์ Candidate Bounding Box ์ต์ปค๋ฅผ ์ฌ์ ์ ์ํ๊ณ NMS ์ต์ ์ฐ์ฐ์ผ๋ก ์ค๋ณต ๋ฐ์ค๋ฅผ ์ ๊ฑฐํ๋ ์์์ ์๋ฌ๊ฒ์ดํธ(Surrogate) ๋ฐฉ์.
- DETR: ์งํฉ ๊ฐ ๋งค์นญ์ ์ํ์ ์ผ๋ก ์ ๋ฆฝํ์ฌ ์ด๋ฏธ์ง ์ ๋ ฅ์์ ์ ๋ต ๋ฐ์ด๋ฉ ๋ฐ์ค ์งํฉ์ ์ง์ ์ถ๋ ฅํ๋ ์ต์ด์ End-to-End ์ฟผ๋ฆฌ ์ํคํ ์ฒ.
๐ ๋ชฉ์ฐจ#
- Chapter 1: ํ๊ฐ๋ฆฌ์ ์๊ณ ๋ฆฌ์ฆ ๊ธฐ๋ฐ ์ด๋ถ ๋งค์นญ (Bipartite Matching) ์์
- Chapter 2: Hungarian Multi-Task ์์ค ํจ์ (๋ถ๋ฅ + ๋ฐ์ค)
- Chapter 3: L1 ๋ฐ GIoU (Generalized IoU) ๋ฐ์ค ์์ค ์์
- Chapter 4: Object Query & Transformer Decoder ๊ตฌ์กฐ
- Chapter 5: ์ฃผ์ ์คํ ๋ฐ ๊ฒฐ๊ณผ
- Chapter 6: ๊ฒฐ๋ก ๋ฐ ์์ฌ์
๐ ๏ธ Chapter 1: ํ๊ฐ๋ฆฌ์ ์๊ณ ๋ฆฌ์ฆ ๊ธฐ๋ฐ ์ด๋ถ ๋งค์นญ ์์#
1. ์์ฝ#
$N$๊ฐ์ ์์ธก ๋ฐ์ค์ ์ ๋ต(GT) ๋ฐ์ค ์งํฉ ์ฌ์ด์ ๋งค์นญ ๋น์ฉ $\mathcal{L}_{\text{match}}$๋ฅผ ์ต์ํํ๋ ์ต์ ์ 1:1 ๋จ์ฌ ํจ์ ์์ด $\hat{\sigma}$๋ฅผ ํ๊ฐ๋ฆฌ์ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ์ฐ์ถํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\hat{\sigma} = \arg\min_{\sigma \in \mathfrak{S}_N} \sum_{i=1}^N \mathcal{L}_{\text{match}}(y_i, \hat{y}_{\sigma(i)})$$$$\mathcal{L}_{\text{match}}(y_i, \hat{y}_{\sigma(i)}) = -\mathbb{1}_{\{c_i \neq \varnothing\}} \hat{p}_{\sigma(i)}(c_i) + \mathbb{1}_{\{c_i \neq \varnothing\}} \mathcal{L}_{\text{box}}(b_i, \hat{b}_{\sigma(i)})$$import torch
from scipy.optimize import linear_sum_assignment
def hungarian_bipartite_matching(
pred_logits: torch.Tensor, # (N_queries, Num_classes)
pred_boxes: torch.Tensor, # (N_queries, 4) -> (x, y, w, h)
gt_classes: torch.Tensor, # (N_gt,)
gt_boxes: torch.Tensor # (N_gt, 4)
) -> tuple:
"""
DETR: Scipy linear_sum_assignment๋ฅผ ํ์ฉํ Bipartite Matching
"""
N_q = pred_logits.shape[0]
N_gt = gt_classes.shape[0]
# 1. ๋ถ๋ฅ ๋น์ฉ: -p(c_i)
prob = pred_logits.softmax(-1)
cost_class = -prob[:, gt_classes] # (N_q, N_gt)
# 2. ๋ฐ์ค L1 ๋น์ฉ
cost_bbox = torch.cdist(pred_boxes, gt_boxes, p=1) # (N_q, N_gt)
# 3. ์ด ๋งค์นญ ๋น์ฉ ํ๋ ฌ (Cost Matrix)
cost_matrix = 1.0 * cost_class + 5.0 * cost_bbox
cost_matrix_np = cost_matrix.detach().cpu().numpy()
# 4. ํ๊ฐ๋ฆฌ์ ์๊ณ ๋ฆฌ์ฆ ์ํ
pred_indices, gt_indices = linear_sum_assignment(cost_matrix_np)
return torch.as_tensor(pred_indices, dtype=torch.long), torch.as_tensor(gt_indices, dtype=torch.long)
# --- ์ฌ์ฉ ์์ ---
p_logits = torch.randn(100, 91) # 100 queries, 91 classes
p_boxes = torch.rand(100, 4)
g_cls = torch.tensor([1, 5, 12]) # 3๊ฐ์ GT ๊ฐ์ฒด
g_bx = torch.rand(3, 4)
p_idx, g_idx = hungarian_bipartite_matching(p_logits, p_boxes, g_cls, g_bx)
print("๋งค์นญ๋ Query ์ธ๋ฑ์ค:", p_idx, "GT ์ธ๋ฑ์ค:", g_idx)๐ ๏ธ Chapter 2: Hungarian Multi-Task ์์ค ํจ์ (๋ถ๋ฅ + ๋ฐ์ค)#
1. ์์ฝ#
ํ๊ฐ๋ฆฌ์ ๋งค์นญ ์ธ๋ฑ์ค $\hat{\sigma}$์ ๋ฐ๋ผ ๋งค์นญ๋ Query-GT ์์ ๋ํด์๋ Cross Entropy ๋ถ๋ฅ ๋ฐ ๋ฐ์ค ํ๊ท ์์ค์, ๋งค์นญ๋์ง ์์ N-N_gt ๊ฐ Query์ ๋ํด์๋ ๋ฐฐ๊ฒฝ(No Object, $\varnothing$) ๋ถ๋ฅ ์์ค๋ง ๋ถ์ฌํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\mathcal{L}_{\text{Hungarian}}(y, \hat{y}) = \sum_{i=1}^N \left[ -\log \hat{p}_{\hat{\sigma}(i)}(c_i) + \mathbb{1}_{\{c_i \neq \varnothing\}} \mathcal{L}_{\text{box}}(b_i, \hat{b}_{\hat{\sigma}(i)}) \right]$$import torch
import torch.nn.functional as F
def compute_detr_hungarian_loss(
pred_logits: torch.Tensor, # (N_q, Num_classes)
pred_boxes: torch.Tensor, # (N_q, 4)
gt_classes: torch.Tensor, # (N_gt,)
gt_boxes: torch.Tensor, # (N_gt, 4)
match_pred_idx: torch.Tensor,
match_gt_idx: torch.Tensor,
bg_class_id: int = 90
) -> torch.Tensor:
"""
DETR Hungarian Loss (Cross-Entropy + Box Loss)
"""
N_q = pred_logits.shape[0]
# Target Class ๋ผ๋ฒจ ์ด๊ธฐํ (๊ธฐ๋ณธ: ๋ฐฐ๊ฒฝ class)
target_classes = torch.full((N_q,), bg_class_id, dtype=torch.long, device=pred_logits.device)
target_classes[match_pred_idx] = gt_classes[match_gt_idx]
# 1. ๋ถ๋ฅ ์์ค (๋
ผ๋ฌธ๊ณผ ๋์ผํ๊ฒ ๋ฐฐ๊ฒฝ ํด๋์ค ๊ฐ์ค์น๋ฅผ 0.1๋ก ๋ฎ์ถฐ,
# ์ฟผ๋ฆฌ ๋๋ถ๋ถ์ด ๋ฐฐ๊ฒฝ์ธ ํด๋์ค ๋ถ๊ท ํ์ด ์์ค์ ์ง๋ฐฐํ์ง ์๋๋ก ํจ)
num_classes = pred_logits.shape[-1]
class_weight = torch.ones(num_classes, device=pred_logits.device)
class_weight[bg_class_id] = 0.1
loss_ce = F.cross_entropy(pred_logits, target_classes, weight=class_weight)
# 2. ๋ฐ์ค ํ๊ท ์์ค (๋งค์นญ๋ ์ค์ ๊ฐ์ฒด์ ๋ํด์๋ง)
loss_box = F.l1_loss(pred_boxes[match_pred_idx], gt_boxes[match_gt_idx], reduction='sum') / max(1, len(match_gt_idx))
return loss_ce + 5.0 * loss_box
# --- ์ฌ์ฉ ์์ ---
p_logits = torch.randn(100, 91)
p_boxes = torch.rand(100, 4)
g_cls = torch.tensor([1, 5, 12])
g_bx = torch.rand(3, 4)
p_i = torch.tensor([10, 45, 80])
g_i = torch.tensor([0, 1, 2])
print("DETR ์ด ํ๊ฐ๋ฆฌ์ ์์ค:", compute_detr_hungarian_loss(p_logits, p_boxes, g_cls, g_bx, p_i, g_i).item())๐ ๏ธ Chapter 3: L1 ๋ฐ GIoU (Generalized IoU) ๋ฐ์ค ์์ค ์์#
1. ์์ฝ#
L1 ์์ค $\| b_i - \hat{b} \|_1$๊ณผ ๋ฐ์ค ํฌ๊ธฐ์ ์ ๊ทํ๋๋ ์ค์ผ์ผ ๋ถ๋ณ GIoU ์์ค $\mathcal{L}_{\text{giou}}$๋ฅผ ์ ํ ๊ฒฐํฉํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\mathcal{L}_{\text{box}}(b_i, \hat{b}) = \lambda_{\text{iou}} \mathcal{L}_{\text{giou}}(b_i, \hat{b}) + \lambda_{\text{L1}} \| b_i - \hat{b} \|_1$$$$\mathcal{L}_{\text{giou}}(A, B) = 1 - \left( \frac{|A \cap B|}{|A \cup B|} - \frac{|C \setminus (A \cup B)|}{|C|} \right)$$import torch
def compute_giou_loss(pred_boxes: torch.Tensor, gt_boxes: torch.Tensor) -> torch.Tensor:
"""
Generalized IoU (GIoU) Loss ๊ณ์ฐ (center_x, center_y, w, h -> x1, y1, x2, y2 ๋ณํ ์ ์ฉ)
"""
# center_x, center_y, w, h -> x1, y1, x2, y2
b1_x1, b1_x2 = pred_boxes[:, 0] - pred_boxes[:, 2]/2, pred_boxes[:, 0] + pred_boxes[:, 2]/2
b1_y1, b1_y2 = pred_boxes[:, 1] - pred_boxes[:, 3]/2, pred_boxes[:, 1] + pred_boxes[:, 3]/2
b2_x1, b2_x2 = gt_boxes[:, 0] - gt_boxes[:, 2]/2, gt_boxes[:, 0] + gt_boxes[:, 2]/2
b2_y1, b2_y2 = gt_boxes[:, 1] - gt_boxes[:, 3]/2, gt_boxes[:, 1] + gt_boxes[:, 3]/2
# ๊ต์งํฉ(Intersection)
inter_x1 = torch.max(b1_x1, b2_x1)
inter_y1 = torch.max(b1_y1, b2_y1)
inter_x2 = torch.min(b1_x2, b2_x2)
inter_y2 = torch.min(b1_y2, b2_y2)
inter_area = (inter_x2 - inter_x1).clamp(min=0) * (inter_y2 - inter_y1).clamp(min=0)
# ํฉ์งํฉ(Union)
area1 = (b1_x2 - b1_x1) * (b1_y2 - b1_y1)
area2 = (b2_x2 - b2_x1) * (b2_y2 - b2_y1)
union_area = area1 + area2 - inter_area
iou = inter_area / (union_area + 1e-7)
# ์ต์ ๋ณผ๋ก ์ธ๊ฒฝ ์์(Conclosing Enclosing Box C)
enc_x1 = torch.min(b1_x1, b2_x1)
enc_y1 = torch.min(b1_y1, b2_y1)
enc_x2 = torch.max(b1_x2, b2_x2)
enc_y2 = torch.max(b1_y2, b2_y2)
enc_area = (enc_x2 - enc_x1).clamp(min=0) * (enc_y2 - enc_y1).clamp(min=0)
giou = iou - (enc_area - union_area) / (enc_area + 1e-7)
return (1.0 - giou).mean()
# --- ์ฌ์ฉ ์์ ---
p_box = torch.tensor([[0.5, 0.5, 0.2, 0.2]])
g_box = torch.tensor([[0.55, 0.55, 0.2, 0.2]])
print("GIoU Loss:", compute_giou_loss(p_box, g_box).item())๐ ๏ธ Chapter 4: Object Query & Transformer Decoder ๊ตฌ์กฐ#
1. ์์ฝ#
$N$๊ฐ์ ํ์ต ๊ฐ๋ฅํ ์์น ๋ฒกํฐ์ธ Object Query๊ฐ ๋์ฝ๋ Cross-Attention์ ํตํด ์ธ์ฝ๋ ๋น์ ํผ์ฒ์ ์ํธ์์ฉํ๊ณ , 3์ธต MLP ๋ฐ์ค ํค๋๋ก ์์ธก๊ฐ์ ์ฐ์ถํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\text{Decoder}(Q_{\text{query}}, \mathcal{F}_{\text{enc}}) \to \mathbf{H}_{\text{out}} \in \mathbb{R}^{N \times d_{\text{model}}}$$$$\hat{b}_i = \text{MLP}_{\text{box}}(\mathbf{h}_i), \quad \hat{c}_i = \text{Linear}_{\text{class}}(\mathbf{h}_i)$$import torch
import torch.nn as nn
class DETRDecoderHead(nn.Module):
"""
DETR Object Query Decoder ๋ฐ Prediction Heads
"""
def __init__(self, num_queries: int = 100, d_model: int = 256, num_classes: int = 91):
super().__init__()
self.object_queries = nn.Parameter(torch.randn(num_queries, d_model))
self.decoder_layer = nn.TransformerDecoderLayer(d_model=d_model, nhead=8)
self.decoder = nn.TransformerDecoder(self.decoder_layer, num_layers=6)
self.class_head = nn.Linear(d_model, num_classes)
self.bbox_head = nn.Sequential(
nn.Linear(d_model, d_model),
nn.ReLU(),
nn.Linear(d_model, d_model),
nn.ReLU(),
nn.Linear(d_model, 4),
nn.Sigmoid() # 0 ~ 1 ๋ฒ์ ์ ๊ทํ ์ขํ
)
def forward(self, enc_features: torch.Tensor) -> tuple:
"""
enc_features: (HW, B, d_model)
"""
B = enc_features.shape[1]
queries = self.object_queries.unsqueeze(1).repeat(1, B, 1) # (N_q, B, d_model)
hs = self.decoder(queries, enc_features) # (N_q, B, d_model)
hs = hs.permute(1, 0, 2) # (B, N_q, d_model)
pred_logits = self.class_head(hs)
pred_boxes = self.bbox_head(hs)
return pred_logits, pred_boxes
# --- ์ฌ์ฉ ์์ ---
detr_head = DETRDecoderHead()
f_enc = torch.randn(400, 2, 256) # 20x20 ํผ์ฒ ๋งต
logits, boxes = detr_head(f_enc)
print("DETR Class Logits Shape:", logits.shape, "Box Predictions Shape:", boxes.shape)๐ ์ฃผ์ ์คํ ๋ฐ ๊ฒฐ๊ณผ (Experiments & Results)#
1. COCO 2017 ๊ฐ์ฒด ํ์ง ๋ฆฌ๋๋ณด๋ ๋น๊ต#
| ์๊ณ ๋ฆฌ์ฆ (Method) | ๋ฐฑ๋ณธ (Backbone) | ๋ํ ๊ฐ์ฒด AP$_L$ โ | ์ ์ฒด mAP โ | ์ถ๋ก ์๋ (FPS) โ |
|---|---|---|---|---|
| Faster R-CNN + FPN | ResNet-50 | 53.4 | 42.0 | 26 FPS |
| DETR (Ours) | ResNet-50 | 61.1 (+7.7%) | 42.0 | 28 FPS (End-to-End) |
| DETR-DC5 (Ours) | ResNet-50 | 61.1 | 43.3 (+1.3%) | 12 FPS |
- ๊ฒฐ๊ณผ: NMS ์์ด ์์ End-to-End๋ก ๋์ํ๋ฉด์ ์ ์ญ Self-Attention ๋๋ถ์ ๋ํ ๊ฐ์ฒด ํ์ง ์ฑ๋ฅ(AP$_L$)์์ ๊ธฐ์กด SOTA๋ฅผ +7.7%p ๊ฒฉ์ฐจ๋ก ์ํ.
๐ก ๊ฒฐ๋ก ๋ฐ ์์ฌ์ (Conclusion & Insights)#
1. ๊ฒฐ๋ก #
DETR์ ๊ฐ์ฒด ํ์ง๋ฅผ ์ง์ ์งํฉ ์์ธก(Direct Set Prediction)์ผ๋ก ์ฌ์ ์ํ๊ณ Bipartite Matching ์์ค์ ๋์ ํ์ฌ, ํ์ MapTR, DETR3D ๋ฑ ๋ชจ๋ ์์จ์ฃผํ ์ฟผ๋ฆฌ ๊ธฐ๋ฐ ํ์ง ๋ชจ๋ธ์ ์๋ฅ๋ฅผ ์ ๋ฆฝํ ์๋๋ฅผ ์ฐ ๊ฑฐ๋์ ๋๋ค.
2. ํ๊ณ์ ๋ฐ ์์ฌ์ด ์ #
- ์ํ ๊ฐ์ฒด ์ฑ๋ฅ์ด Faster R-CNN ๋๋น ์ด์ธ โ Deformable DETR(2021)์ด deformable attention์ผ๋ก ํด๊ฒฐ.
- ๋งค์ฐ ๊ธด ํ์ต ์ค์ผ์ค ํ์ (500 epoch) โ Conditional DETR, DAB-DETR ๋ฑ์ผ๋ก ์๋ ด ์๋ ๊ฐ์ .
- ์ธ์ฝ๋์ ๋์ ๊ณ์ฐ ๋น์ฉ (HW ํฌ๊ธฐ self-attention) โ Deformable DETR, Sparse DETR๋ก ํจ์จํ.
- ํ๊ฐ๋ฆฌ์ ๋งค์นญ์ด ํ์ต ์ด๋ฐ ๋ถ์์ ํ ์ ์๋ค๋ ์ ์ ๋ ผ๋ฌธ์์ ์ถฉ๋ถํ ๋ค๋ฃจ์ง ์์ ์์ฌ์ด ๋ถ๋ถ.
์ฐธ๊ณ ์๋ฃ#
๊ด๋ จ ๋ ผ๋ฌธ: Attention Is All You Need, ResNet, DETR3D, VectorMapNet, MapTR