컴퓨터비전(Computer Vision)

객체탐지 - IoU /python 코드

zzoming 2023. 10. 15. 20:20

 

🤔 Detection에서 FP , TP 을 어떻게 판단할 수 있을까?

IOU (Intersection Over Union) 

: 예측된 바운더리 박스와 사용자가 설정한 바운더리 박스 간 중첩되는 부분의 면적을 측정해서 중첩된 면적을 합집합의 면적으로 나눠준다 

 

IOU =  중첩영역 / 결합영역  

중첩영역 계산은 박스 A, 박스 B의 왼쪽 상단, 오른쪽 하단 좌표 값을 이용하여 중첩 부분의 왼쪽 상단, 오른쪽 하단의 값을 구하여 계산한다.

Python 코드 

여기서 epsilon은 0으로 나눠지는 것을 방지하는 값 

def get_iou(a,b, epsilon = 1e-5) :
    """ Given two boxes `a` and `b` defined as a list of four numbers:
            [x1,y1,x2,y2]
        where:
            x1,y1 represent the upper left corner
            x2,y2 represent the lower right corner
        It returns the Intersect of Union score for these two boxes.

    Args:
        a:          (list of 4 numbers) [x1,y1,x2,y2]
        b:          (list of 4 numbers) [x1,y1,x2,y2]
        epsilon:    (float) Small value to prevent division by zero

    Returns:
        (float) The Intersect of Union score.
    """
    
	#COORDINATES OF RHE INTERSECTION 
    x1 = max(a[0] , b[0]) 
    y1 = max(a[1] , b[1])
    x2 = min(a[2] , b[2]) 
    y2 = min(a[3] , b[3]) 
    
    
    #AREA OF OVERLAP - Area where the boxes intersect
    width = (x2 -x1) 
    height = (y2-y1)
    
    #hadel case where there is no overlap 
    if (width <0 ) or (height <0) :
    	return 0.0 
    
    
    area_overlap = width*height 
    
    #COMBINED AREA 
    area_a = (a[2]-a[0]) * (a[3] - a[1])
    area_b = (b[2]-b[0]) * (b[3] - b[1])
    area_combined = area_a + area_b - area_overlap 
    
    #IOU
    iou = area_overlap / (area_combined + epsilon)
    return iou

 
참고) 
mAP50 : IOU가 0.5 이상인 경우 True로 판단했을 때 mAP 값 
mAP60 : IOU가 0.6 이상인 경우 True로 판단했을 때 mAP 값 
mAP70 : IOU가 0.7 이상인 경우 True로 판단했을 때 mAP 값


속도 

 FPS(Frames Per Second) 

: 초 당 처리하는 frame의 수. object detection에서 fps란 초당 detection 하는 비율. 만일 초당 20개의 frame에 대해서 detection 을 수행하게 된다면 20fps이다. 하나의 frame을 detection 하는데 걸리는 시간 inference time 이라고 한다. 
우리가 Real time이라고 느끼는 fps는 30fps으로 즉,초당 연속적인 frame을 30개 이상 처리할 수 있으면 끊기지 않은 자연스러운 영상을 인식하게 된다 

 
FPS = Frame per Second = Frame / Second = 1/ 0.1 = 10fps
 

때문에 object detection 모델의 성능을 평가할 때 inference time도 중요한 지표로 사용된다. (만일 자율주행자동차에 inference time 시간이 오래 걸리는 object detecion 모델을 쓰게 되면 갑자기 뛰어드는 물체에 대해서 dectection 하지 못할 확률이 높을 것이다..) 
 

 

FLOPs (Floating point Operations)

  • Model이 얼마나 빠르게 동작하는지 측정하는 metric 
  • 연산량 횟수 (곱하기, 더하기, 빼기 등) 

MUL (3X2, 2X3) = 3X3 
각 셀 당 연산 :  
곱셈 
한 셀 당 2번 3X3X2 = 18번 
덧셈
한 셀 당 1번 3X3X1 = 9번 
Flops : 18+9 = 27번 
 
만일 모델 A의 FLOPs 보다 모델 B의 FLOPs 가 크다면, 모델 B가 비교적 많은 연산량을 요구하는 모델이다 


Object Detection Domain 특성 

  • 통합된 library의 부재 
  • 엔지니어링 적인 측면이 강함 
    • 어떤 모델이 풀고자 하는 문제에 적합한지 실험적인 증명이 필요 
    • Custom, tuning 하는 과정에서 개발 수준의 활용 필요 
  • 복잡한 파이프 라인
  • 높은 성능을 내기 위해서 무거운 모델 활용 
  • Resolution( 이미지의 해상도 및 사이즈)성능에 영향을 많이 끼쳐 사진의 크기가 큼 

Detection 모델의 대표적인 Library 

  • MMDetection  : pytorch 기반인 objection detection 

https://github.com/open-mmlab/mmdetection

 

GitHub - open-mmlab/mmdetection: OpenMMLab Detection Toolbox and Benchmark

OpenMMLab Detection Toolbox and Benchmark. Contribute to open-mmlab/mmdetection development by creating an account on GitHub.

github.com

  • Detectron2 : 페이스북 AI 리서치의 라이브러리로 pytorch 기반의 Object detection과 segmentation 알고리즘 제공 

https://github.com/facebookresearch/detectron2

 

GitHub - facebookresearch/detectron2: Detectron2 is a platform for object detection, segmentation and other visual recognition t

Detectron2 is a platform for object detection, segmentation and other visual recognition tasks. - GitHub - facebookresearch/detectron2: Detectron2 is a platform for object detection, segmentation a...

github.com

  • YOLOv5 : coco 데이터셋으로 사전 학습된 모델로 Object Detection 모델 
  • EfficientDet :Google Research & Brain에서 연구한 모델로 EfficientNet을 응용해서 만든 Object Detection 모델 

 

'컴퓨터비전(Computer Vision)' 카테고리의 다른 글

컴퓨터 비전의 시작  (1) 2023.10.18
Object Detecion [3] - FAST R-CNN  (0) 2023.10.17
Object Detecion [2] - SPPNET  (0) 2023.10.16
Object Detecion [1] - R-CNN  (0) 2023.10.16
부스트캠프 자가진단 STUDY  (2) 2023.09.18