R-CNN을 활용한 객체 탐지

PyTorch로 배우는 이미지 딥러닝

Michal Oleszak

Machine Learning Engineer

영역 기반 CNN 계열: R-CNN

R-CNN 계열: R-CNN, Fast R-CNN, Faster R-CNN

R-CNN

  • 모듈 1: 영역 제안 생성
1 인용: Jason Brownlee. 2019. Deep Learning for Computer Vision.
PyTorch로 배우는 이미지 딥러닝

영역 기반 CNN 계열: R-CNN

R-CNN 계열: R-CNN, Fast R-CNN, Faster R-CNN

R-CNN

  • 모듈 1: 영역 제안 생성
  • 모듈 2: 특징 추출(합성곱 계층)
1 인용: Jason Brownlee. 2019. Deep Learning for Computer Vision.
PyTorch로 배우는 이미지 딥러닝

영역 기반 CNN 계열: R-CNN

R-CNN 계열: R-CNN, Fast R-CNN, Faster R-CNN

R-CNN

  • 모듈 1: 영역 제안 생성
  • 모듈 2: 특징 추출(합성곱 계층)
  • 모듈 3: 클래스 및 바운딩 박스 예측
1 인용: Jason Brownlee. 2019. Deep Learning for Computer Vision.
PyTorch로 배우는 이미지 딥러닝

R-CNN: 백본

  • 합성곱 계층: 사전 학습 모델
    • 백본: 특징 추출을 담당하는 핵심 CNN 구조

  백본

  • 합성곱 및 풀링 계층
  • 영역 제안과 객체 탐지를 위한 특징 추출
PyTorch로 배우는 이미지 딥러닝

R-CNN: PyTorch로 백본 구성

import torch.nn as nn
from torchvision.models import vgg16,
    VGG16_Weights


vgg = vgg16(weights=VGG16_Weights.DEFAULT)

vgg 모델

PyTorch로 배우는 이미지 딥러닝

R-CNN: PyTorch로 백본 구성

import torch.nn as nn
from torchvision.models import vgg16,
    VGG16_Weights


vgg = vgg16(weights=VGG16_Weights.DEFAULT)

vgg 모델 특징

  • .features: 합성곱 계층만 포함
PyTorch로 배우는 이미지 딥러닝

R-CNN: PyTorch로 백본 구성

import torch.nn as nn
from torchvision.models import vgg16,
    VGG16_Weights


vgg = vgg16(weights=VGG16_Weights.DEFAULT)

vgg 모델

  • .features: 합성곱 계층만 포함
  • .children(): 블록의 모든 계층
PyTorch로 배우는 이미지 딥러닝

R-CNN: PyTorch로 백본 구성

import torch.nn as nn
from torchvision.models import vgg16,
    VGG16_Weights


vgg = vgg16(weights=VGG16_Weights.DEFAULT)
backbone = nn.Sequential( *list(vgg.features.children()) )
  • nn.Sequential(*list()): 하위 계층을 리스트로 시퀀셜 블록에 배치
    • *: 리스트 요소 언패킹

vgg 모델

  • .features: 합성곱 계층만 포함
  • .children(): 블록의 모든 계층
PyTorch로 배우는 이미지 딥러닝

R-CNN: 분류기 계층

  • 백본 출력 차원 추출
input_dimension = nn.Sequential(*list(
    vgg_backbone.classifier.children())
)[0].in_features
  • 새 분류기 생성
classifier = nn.Sequential(
    nn.Linear(input_dimension, 512),
    nn.ReLU(),
    nn.Linear(512, num_classes),
)
PyTorch로 배우는 이미지 딥러닝

R-CNN: 박스 회귀 계층

  • 백본 위에 위치
  • 4개 출력: 박스 4개 좌표
box_regressor = nn.Sequential(
    nn.Linear(input_dimension, 32),
    nn.ReLU(),
    nn.Linear(32, 4),
)
PyTorch로 배우는 이미지 딥러닝

모두 합치기: 객체 탐지 모델

class ObjectDetectorCNN(nn.Module):
    def __init__(self):
        super(ObjectDetectorCNN, self).__init__()

vgg = vgg16(weights=VGG16_Weights.DEFAULT) self.backbone = nn.Sequential(*list(vgg.features.children()))
input_features = nn.Sequential(*list(vgg.classifier.children()))[0].in_features
self.classifier = nn.Sequential( nn.Linear(input_features, 512), nn.ReLU(), nn.Linear(512, 2), )
self.box_regressor = nn.Sequential( nn.Linear(input_features, 32), nn.ReLU(), nn.Linear(32, 4), )
PyTorch로 배우는 이미지 딥러닝

모두 합치기: 객체 탐지 모델

class ObjectDetector(nn.Module):
    (...)

    def forward(self, x):

features = self.backbone(x)
bboxes = self.regressor(features) classes = self.classifier(features) return bboxes, classes
PyTorch로 배우는 이미지 딥러닝

객체 인식 실행

  1. 이미지를 로드하고 변환
  2. 배치 차원 추가를 위해 unsqueeze() 호출
  3. 이미지 텐서를 모델에 전달
  4. 출력에 비최대 억제(nms()) 적용
  5. 이미지에 draw_bounding_boxes() 적용
PyTorch로 배우는 이미지 딥러닝

연습해 봅시다!

PyTorch로 배우는 이미지 딥러닝

Preparing Video For Download...