본문 바로가기
딥러닝관련/Detection

Tutorial to Object Detection

by 머리올리자 2022. 7. 10.

Detection에 대해서 정리하다가 아래 링크에 좋은 내용들이 많아 공부할 겸 정리해보고자 한다.

 

아래 링크의 내용은 single shot detector(SSD) based로 정리하였다.

 

https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection

 

GitHub - sgrvinod/a-PyTorch-Tutorial-to-Object-Detection: SSD: Single Shot MultiBox Detector | a PyTorch Tutorial to Object Dete

SSD: Single Shot MultiBox Detector | a PyTorch Tutorial to Object Detection - GitHub - sgrvinod/a-PyTorch-Tutorial-to-Object-Detection: SSD: Single Shot MultiBox Detector | a PyTorch Tutorial to Ob...

github.com

 

Concepts

 · Singe-shot detection. Object detection을 위한 이전의 architectures는 두 개의 별개의 stages로 구성된다. (1. object localization을 위한 region proposal network / 2. proposed region에 대해 types를 구분하는 classifier). Computationally, 이 방법은 매우 cost expensive하기 때문에 real-world, 실시간 application에 적합하지 않다. Single-shot model은 localization 및 detection task를 network의 single forward sweep으로 encapsulate하여 더 가벼운 hardware에 배포하면서 더 빠른 탐지가 가능하다.

 

 · Multiscale Feature Maps Image classification task에서, 원본 이미지의 가장 smallest하지만, deepest한 representation인 최종 convolutional feature map을 사용한다. Object Detection에서 intermediate convolutional layers의 feature maps는 원본 이미지에 대하여 다른 여러 scales로 나타내기 때문에 유용할 수 있음. 따라서 dfferenct feature maps에서 동작하는 fixed-size filter는 다양한 size의 objects를 detect할 수 있다.

 

 · Priors 특정 aspect rations and scales를 사용하여 특정 feature maps의 특정 positions에 정의된 pre-computed box. Prior box들은 datasets에서의 bounding box(ground truth)의 특성과 일치하도록 선택된다.

 

 · Multibox object의 bounding box를 predicting하는 것을 regression problem으로 formulate하는 것. 여기서 detected object's coordinates를 ground truth's coordinates로 regression된다. 추가적으로, 각 predicted box에 대해, 다양한 object types에 대해 scores가 생성된다.  Priors는 ground truth를 기반으로 하기에 prediction을 위한 feasible starting points 역할을 한다. 따라서 priors에 있는 만큼 predicted boxes가 많으며, 대부분은 object를 포함하고 있지 않다.

 

 · Hard Negative Mining model에서 predicted한 가장 심각한 false positives를 explicitly 선택하고, 이 선택한 sample들에 대해서 강제로 학습하도록 하는 것을 의미. 다른 말로, model이 올바르게 식별하기 가장 어렵다고 발견한 negatives만 mining한다. Predicted boxes의 대다수가 object를 포함하지 않는 object detection 영역에서, 이것은 negative-positive imbalance를 줄이는 역할을 한다.

 

 · Non-maximum Suppression. 특정 위치에서, 여러 prior boxes들이 겹칠 수 있다. 때문에 이런 priors에서 발생하는 predictions는 실제로 동일한 대상의 중복일 수 있음. Non-Maximium Suppression(NMS)는 maximum scores를 가진 것을 제외한 모든 predictions를 suppressing하여 redundant predictions를 제거하는 수단이다.

 

 

Some definitions

 · Boundary coordinates

    Box를 나타내는 가장 확실한 방법은 경계를 구성하는 x 및 y lines의 pixel coordinates를 사용하는 것

 

출처 : https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection/raw/master/img/bc1.PNG

box의 boundary coordinates는 (x_min, y_min, x_max, y_max).

하지만 image의 actual dimension을 모르는 경우 pixel values는 거의 쓸모가 없음.

더 나은 방법은, 모든 coordinates를 fractional form(0~1 normalize를 의미하는 것 같다) 으로 나타내는 것임.

 

. Center-Size coordinates

    더 명확한 방법은 아래와 같이 사용하는 것

그림 출처 : https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection/raw/master/img/cs.PNG

box의 boundary coordinates는 (c_x, c_y, w, h).

 

코드에서 task에 대한 suitablilty에 따라 두 좌표계를 일상적으로 사용하고 항상 fractional form을 사용한다.

 

Jaccard Index

Jaccard Index 또는 Jaccard Overlap 또는 Intersection-over-Union(IoU)은 두 상자가 겹치는 정도 또는 범위를 측정.

IoU 1 : 동일한 상자

IoU 0 : 상호 배타적인 공간(mutually exclusive spaces).

Multibox

Multibox : detecting objects를 위한 기술(predictions가 두 가지 components로 구성)

  1) Coordinates : objects를 포함하거나 포함하지 않을 수 있는 box의 좌표. →  regression task

  2) Scores : box에 object가 없음을 암시하는 background class를 포함하여, 이 box의 다양한 object type에 대한 점수

      →  classification task

 

Single Shot Detector (SSD)

  SSD는 세 부분으로 구성할 수 있는 purely convolutional neural network.

   ·  Base convolution:  기존 image classification archictecture에서 파생 →  lower-level feature map

   ·  Auxiliary convolution: Base network 위에 추가  →  higher-level feature map

   ·  Auxiliary convolution: 위 feature maps에서 object를 찾고 식별

 

SSD에서는 SSD300과 SSD512가 있으며 이는 입력 이미지 사이즈에 대한 차이를 나타냄.

두 네트워크는 구성 방식이 약간 다르지만 원칙적으로는 동일

SSD512가 단지 조금 더 큰 네트워크이며 결과적으로 조금 더 나은 성능을 제공함.

 

Base Convolutions – part 1

왜 기존의 네트워크들에서는 convolution을 사용하는가?

 

1) Image classification에서 잘 기능을 수행하는 것으로 입증된 model들은 이미 이미지의 basic essence를 잘 capturing하기 때문. 동일한 convolution features가 object detection에 유용하지만, 더 지역적인 의미에서는 전체 이미지보다 object가 존재하는 특정 영역에 더 관심이 있다.

(검증된 classification model들은 image의 feature를 잘 capturing한다고 볼 수 있음)

 

2) 신뢰할 수 있는 classification dataset에서 pre-trained layer를 사용할 수 있다는 이점. Transfer learning으로 object detection task에 사용되기 때문에, 다르지만 밀접하게 관련된 task에서 knowledge를 차용함으로써 작업에 진전을 이룸.

(ex, imagenet pre-trained classification model - vgg-16, resnet101 ...)

 

저자는 VGG-16 architecture를 사용

 

그림 출처 : https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection/raw/master/img/vgg16.PNG

ImageNet ILSVRC(Large Scale Visual Recognition Competition) classification에서 pre-trained된 모델을 사용.

다른 더 큰 모델들을 사용할 수 있으나 computational cost를 함께 고려해야 함.

 

- 위 보이는 것과 같이 dimensions를 반으로 만드는 3rd pooling layer의 경우 output size를 결정할 때 기본함수 floor function 대신 matematical ceil function을 사용한다. 이는 input feature map 사이즈가 홀수일 때가 중요하다.

- 이로 인하여 input 부터 feature map의 사이즈가 300 x 150 → 150 x 150 → 75 x 75 → 38 x 38 (not 37 x 37)로 흘러간다는 것을 알 수 있다.

 

- 또한, 5번 째 max-pooling layer에 대하여 2 x 2, stride 2에서 → 3 x 3, stride 1으로 바꿔 feature map 사이즈를 그대로 유지한다.

 

- 마지막으로 fc layer의 경우 필요하지 않기 때문에, 구조를 다시 설계한다. fc8의 경우 필요없기 때문에 완전히 삭제한 후 fc6, fc7을 conv6, conv7로 재작업한다.

 

FC → Convolutional Layer

Fully connected layer를 convolutional layer로 reparametrize 하는 방법

 

일반적인 image classification task에서 fully convolutional layer는 feature map을 1D 구조로 아래와 같이 flatten 해야함.

그림 출처 : https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection/raw/master/img/fcconv1.jpg

 

위 그림처럼 2 x 2 x 3의 feature map에 대해서, 크기 12인 1D vector로 flatten 되었다고 가정.

Size가 2인 output에 대해 FCL은 동일한 vector 두 개를 사용하여 이 flatten vector의 dot-product 연산 수행

회색 표시의 vectors는 fully connected layers의 parameters.

 

이제 convolutional layer를 사용하여 2개의 output을 출력하는 방법을 생각해봄

 

출처 : https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection/raw/master/img/fcconv2.jpg

 

이 경우 feature map은 flatten할 필요가 없이 두 개의 kernel을 이용하여 output을 출력할 수 있다.

회색은 convolutional layer의 parameters.

 

여기서 핵심 부분은 두 시나리오에서 output Y_0과 Y_1은 동일하다는 것.

 

 

두 시나리오는 동일하다.

 

I input channels를 가진, H, W 크기의 feature map에서 output size N의 fully connected layer는

kernel size가 feature map size h, w 및 n의 output channel과 동일한 convolutional layer와 동일하다

(위 시나리오 대로하면 FCL과 convolution layer의 기능이 동일한 output을 출력한다는 것)

 

이미지 출처 : https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection/raw/master/img/fcconv4.jpg

  • 위에 근거하여 fully connected layer는 parameters를 reshaping하여 동일한 convolutional layer로 변환할 수 있다.

Base Convolutions – part 2

  • fc6 → conv6, fc7 → conv7

크기가 224 x 224 x 3인 input에서 conv5_3의 출력은 7 x 7 x 512

(fc6 → conv6)

(fc6) Flatten input size가 7 * 7 * 512이고, output size가 4096인 fc6은 4096, 7 * 7 * 512의 parameters를 가진다.

(conv6) 7 x 7 kernel size와 4096 output channels이 있으며, (4096, 7, 7, 512)의 parameters를 가진다.

 

(fc7 → conv7)

(fc7) Input size가 4096이고, output size가 4096인 fc6은 dimension이 4096, 4096의 parameters를 가진다.

(conv7) 1 x 1 kernel size와 4096 output channels이 있으며, (4096, 1, 1, 4096)의 parameters를 가진다.

 

--

conv6에는 차원이 (7, 7, 512)인 4096개의 filter가 있고, conv7에는 (1, 1, 4096)인 4096개의 filter가 있다.