사전 정의된 앵커 박스(Anchor Box) 상세 설명

핵심 요약:
앵커 박스는 미리 정의된 바운딩 박스로, 각 위치(feature map의 그리드 셀)에서 다양한 크기(scale)와 종횡비(aspect ratio)를 가진 기준 박스를 생성한다. 모델은 이 앵커 박스를 바탕으로 객체의 존재 여부와 위치 오프셋(offset)을 예측하며, 복잡한 앵커 설계·매칭 과정을 통해 다양한 크기·비율의 객체를 효과적으로 검출한다.

1. 앵커 박스란?

앵커 박스는 이미지(feature map)의 각 위치마다 “이런 형태의 객체가 있을 수 있다”는 기준 경계 상자(prior box)를 미리 배치한 것이다.
모델은 각 앵커 박스에 대해
1) 객체 클래스 존재 여부 (objectness score 또는 class confidence)
2) 실제 객체와의 위치 차이 (center, width, height 오프셋)
를 예측한다. 이러한 예측을 적용해 최종 바운딩 박스를 얻는다[1].

2. 앵커 박스 생성 원리

2.1 그리드와 스트라이드

  • 입력 이미지가 백본 네트워크를 통과해 생성된 feature map을 M×N 그리드로 본다.
  • 각 그리드 셀의 중심이 앵커 박스의 기준점 기준(centroid)이 되며, 보통 stride=feature map 단위×(이미지 크기 ÷ feature map 크기)만큼 떨어진다[2].

2.2 스케일(scale)과 종횡비(aspect ratio)

  • 스케일(scale): 객체 크기를 반영하는 기준 박스의 면적 또는 너비·높이. 예: Faster R-CNN에서 [$$128^2$$, $$256^2$$, $$512^2$$] 픽셀 면적 사용[3][2].
  • 종횡비(aspect ratio): 너비(width)÷높이(height) 비율. 예: $$[0.5,1.0,2.0]$$[3][2].
  • 각 그리드 위치마다 $$\text{scales}\times\text{aspect ratios}$$ 조합만큼의 앵커 박스가 생성된다.

앵커 좌표 계산

중심 좌표 $$(c_x,c_y)$$, 너비 $$w$$, 높이 $$h$$인 앵커를 생성하려면
$$
c_x = \bigl(i + 0.5\bigr)\times\mathrm{stride},\quad
c_y = \bigl(j + 0.5\bigr)\times\mathrm{stride},
$$
$$
w = \frac{\sqrt{\text{area}\times\text{ratio}}}{\text{image_width}},\quad
h = \frac{\sqrt{\text{area}\,/\,\text{ratio}}}{\text{image_height}}
$$
로 정규화된 좌표를 구해 $$[c_x,c_y,w,h]$$ 형태로 저장한다[3].

3. 앵커 매칭 및 학습 과정

  1. IoU 기반 매칭: 학습 시 각 앵커와 GT 바운딩 박스 사이의 IoU를 계산해, 임계값(예: ≥0.7 양성, ≤0.3 음성)에 따라 positive/negative 샘플로 레이블링한다[1].
  2. 위치 보정 회귀: positive 앵커에 대해 바운딩 박스 회귀(offset) 파라미터 $$(\Delta x, \Delta y, \Delta w, \Delta h)$$를 학습한다.
  3. 손실 함수:
  • 클래스 예측에 Cross-Entropy Loss
  • 박스 회귀에 Smooth L1 Loss 또는 GIoU Loss
    를 사용해 멀티태스크 손실을 최적화한다[1].

4. 주요 하이퍼파라미터 예시

모델스케일(scale)종횡비(ratios)셀당 앵커 수스트라이드(stride)
Faster R-CNN[128², 256², 512²][0.5, 1, 2]9[2]feature map stride≈16[2]
RetinaNetwith 3 scales each[0.5, 1, 2]9 per FPN 레벨[4]FPN 레벨별 8,16,32,64,128
YOLOv3COCO 기준 k-means 클러스터링으로 결정된 9개클러스터 비율 기반3 per scale[3]grid sizes 13,26,52

5. 장단점 비교

장점

  • 다양한 크기·비율의 객체를 한 번에 검출할 수 있어 정밀도 향상
  • End-to-end 학습으로 RoI 추출 과정을 네트워크 내부에 통합

단점

  • 스케일·비율·개수 등 하이퍼파라미터 튜닝 필요[1]
  • 각 위치마다 수백~수만 개 앵커 처리로 메모리·연산 오버헤드
  • 복잡한 매칭·후처리(NMS) 로직 필요

결론

앵커 박스는 객체 검출 모델 성능을 크게 좌우하는 핵심 요소이다. 적절한 스케일·종횡비·스트라이드 설정과 클러스터링 기반 최적화 기법을 통해 하이퍼파라미터 부담을 줄이고, 더욱 효율적인 앵커 설계가 가능하다. 최근 DETR, anchor-free 방법이 등장했으나, anchor-based 기법은 여전히 높은 검출 성능을 제공하는 주류 접근법으로 활용된다.

출처
[1] [객체 검출] 앵커 박스(Anchor Box)란 무엇인가? – CV DOODLE https://mvje.tistory.com/246
[2] Faster R-CNN 모델 – 약초의 숲으로 놀러오세요 https://herbwood.tistory.com/10
[3] [Object Detection] Anchor Box 설명과 pytorch 구현 – 아무블로그 https://csm-kr.tistory.com/33
[4] torchvision.models.detection.retinanet – PyTorch documentation https://docs.pytorch.org/vision/0.9/modules/torchvision/models/detection/retinanet.html [5] how to determine anchor sizes for RetinaNet model? – Stack Overflow https://stackoverflow.com/questions/63949407/how-to-determine-anchor-sizes-for-retinanet-model [6] Functions http://docs.ros.org/en/indigo/api/rail_object_detector/html/namespacerpn_1_1rpn.html [7] Another Anchor Box Question · Issue #439 · fizyr/keras-retinanet https://github.com/fizyr/keras-retinanet/issues/439 [8] Anchored Rescaling Methods https://sawtoothsoftware.com/help/maxdiff-analyzer/manual/anchored-rescaling-methods.html [9] anchor_generator.py – GitHub https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/modeling/rpn/anchor_generator.py [10] Keras documentation: RetinaNetObjectDetector model https://keras.io/keras_hub/api/models/retinanet/retinanet_object_detector/ [11] C_3.01 Anchor Boxes – 위키독스 https://wikidocs.net/173914 [12] Yolov3 targets build, Do all 3 scales need to have an anchor box designed for each object in the image or just the scale with better IoU? https://stats.stackexchange.com/questions/581367/yolov3-targets-build-do-all-3-scales-need-to-have-an-anchor-box-designed-for-ea [13] CSD/DSD https://www.strongtie.com/mechanicalanchors_mechanicalanchoringproducts/sd_anchor/p/csd.dsd [14] keras-retinanet/keras_retinanet/utils/anchors.py at main – GitHub https://github.com/fizyr/keras-retinanet/blob/main/keras_retinanet/utils/anchors.py [15] Nums of AnchorGenerator output anchors https://discuss.pytorch.org/t/nums-of-anchorgenerator-output-anchors/71121 [16] [Object Detection] Faster R-CNN – Computistics – 티스토리 https://computistics.tistory.com/16 [17] No More M.2 Screw Rage? Asus Creates Latched Mounting System for M.2 SSDs https://www.tomshardware.com/news/screw-free-m2-latch-asus [18] [Pytorch] Yolo v2 논문 리뷰 및 모델 구현 https://velog.io/@krec7748/Pytorch-Yolo-v2-%EA%B5%AC%ED%98%84 [19] [Object Detection] Faster R-CNN – 시나브로개발자 성장기 – 티스토리 https://developer-lionhong.tistory.com/70
[20] What do strides and ratios mean in SSD anchor generator? https://stackoverflow.com/questions/75851553/what-do-strides-and-ratios-mean-in-ssd-anchor-generator

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다