728x90
반응형

# 성능 평가

Metrics - IOU (Intersection over Union)

=> 모델이 예측한 결과와 실측(Gound Truth) box가 얼마나 정확하게 겹치는가를 나타내는 지표

 

생성된 전체 면적 중에 예측이 맞은 면적을 구하는 것이기 때문에 1에 가까울수록 좋다

pascal voc 0.5, ms coco competition 0.5 ~ 0.95

 

# 입력인자로 후보 박스와 실제 박스를 받아서 IOU를 계산하는 함수 생성

import numpy as np 

def compute_iou(cand_box, gt_box):
    # cand_box ss에서 추천한 box, gt_box 실제 box
    # Calculate intersection areas : 쉽게 구하는 방법
    x1 = np.maximum(cand_box[0], gt_box[0])
    y1 = np.maximum(cand_box[1], gt_box[1])
    x2 = np.minimum(cand_box[2], gt_box[2])
    y2 = np.minimum(cand_box[3], gt_box[3])
    
    intersection = np.maximum(x2 - x1, 0) * np.maximum(y2 - y1, 0)
    
    cand_box_area = (cand_box[2] - cand_box[0]) * (cand_box[3] - cand_box[1])
    gt_box_area = (gt_box[2] - gt_box[0]) * (gt_box[3] - gt_box[1])
    union = cand_box_area + gt_box_area - intersection
    
    iou = intersection / union
    return iou

# gt_box 생성

import cv2
import matplotlib.pyplot as plt
%matplotlib inline

# 실제 box(Ground Truth)의 좌표를 아래와 같다고 가정. 
gt_box = [60, 15, 320, 420]


img = cv2.imread('./data/audrey01.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

red = (255, 0 , 0)
img_rgb = cv2.rectangle(img_rgb, (gt_box[0], gt_box[1]), (gt_box[2], gt_box[3]), color=red, thickness=2)

plt.figure(figsize=(8, 8))
plt.imshow(img_rgb)
plt.show()

사람이 입력한 gt_box 정보

# 예측하기

import selectivesearch

#selectivesearch.selective_search()는 이미지의 Region Proposal정보를 반환 
img = cv2.imread('./data/audrey01.jpg')
img_rgb2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
_, regions = selectivesearch.selective_search(img_rgb2, scale=100, min_size=2000)

print(type(regions), len(regions))
#<class 'list'> 41

# 평가하기

cand_rects = [cand['rect'] for cand in regions]
for index, cand_box in enumerate(cand_rects):
    # 전처리
    cand_box = list(cand_box)
    cand_box[2] += cand_box[0]
    cand_box[3] += cand_box[1]
    
    # 평가하기
    iou = compute_iou(cand_box, gt_box)
    print('index:', index, "iou:", iou)
    
# index: 0 iou: 0.06157293686705451
# index: 1 iou: 0.07156308851224105
# index: 2 iou: 0.2033654637255666
# index: 3 iou: 0.04298195631528965
# index: 4 iou: 0.14541310541310543
# index: 5 iou: 0.10112060778727446
# index: 6 iou: 0.11806905615946989
# index: 7 iou: 0.1420163334272036
# index: 8 iou: 0.035204259342190375
# index: 9 iou: 0.004256894317971497
# index: 10 iou: 0.5184766640298338
# index: 11 iou: 0.04465579710144928
# index: 12 iou: 0.0853656220322887
# index: 13 iou: 0.015722240419259743
# index: 14 iou: 0.037833068643021
# index: 15 iou: 0.22523535071077264
# index: 16 iou: 0.0
# index: 17 iou: 0.053941120607787274
# index: 18 iou: 0.05154006626579948
# index: 19 iou: 0.05660327592118798
# index: 20 iou: 0.01165009904393209
# index: 21 iou: 0.18588082901554404
# index: 22 iou: 0.19555555555555557
# index: 23 iou: 0.5409250175192712
# index: 24 iou: 0.205679012345679
# index: 25 iou: 0.042245111210628454
# index: 26 iou: 0.34848824374009246
# index: 27 iou: 0.18588082901554404
# index: 28 iou: 0.10952135872362326
# index: 29 iou: 0.29560078245307364
# index: 30 iou: 0.045470015655843715
# index: 31 iou: 0.3126506582607083
# index: 32 iou: 0.4934902582553282
# index: 33 iou: 0.5490037131949166
# index: 34 iou: 0.1018867924528302
# index: 35 iou: 0.31513409961685823
# index: 36 iou: 0.3423913043478261
# index: 37 iou: 0.6341234282410753
# index: 38 iou: 0.6270619201314865
# index: 39 iou: 0.6270619201314865
# index: 40 iou: 0.6270619201314865

 

 

# 수정 반영하기

# cand['size'] > 5000인 box들만
cand_rects = [cand['rect'] for cand in regions if cand['size'] > 5000]
cand_rects.sort()

# 다시로드
img = cv2.imread('./data/audrey01.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print('img shape:', img.shape)

# 추천 box
green_rgb = (125, 255, 51)
cand_rects = [cand['rect'] for cand in regions if cand['size'] > 3000]
gt_box = [60, 15, 320, 420]
img_rgb = cv2.rectangle(img_rgb, (gt_box[0], gt_box[1]), (gt_box[2], gt_box[3]), color=red, thickness=2)

for index, cand_box in enumerate(cand_rects):
    # 전처리
    cand_box = list(cand_box)
    cand_box[2] += cand_box[0]
    cand_box[3] += cand_box[1]
    # 평가
    iou = compute_iou(cand_box, gt_box)
    
    # 성능이 좋은 box만
    if iou > 0.5:
        print('index:', index, "iou:", iou, 'rectangle:',(cand_box[0], cand_box[1], cand_box[2], cand_box[3]) )
        cv2.rectangle(img_rgb, (cand_box[0], cand_box[1]), (cand_box[2], cand_box[3]), color=green_rgb, thickness=1)
        text = "{}: {:.2f}".format(index, iou)
        cv2.putText(img_rgb, text, (cand_box[0]+ 100, cand_box[1]+10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, color=green_rgb, thickness=1)
    
plt.figure(figsize=(12, 12))
plt.imshow(img_rgb)
plt.show()

# img shape: (450, 375, 3)
# index: 8 iou: 0.5184766640298338 rectangle: (72, 171, 324, 393)
# index: 18 iou: 0.5409250175192712 rectangle: (72, 171, 326, 449)
# index: 28 iou: 0.5490037131949166 rectangle: (0, 97, 374, 449)
# index: 32 iou: 0.6341234282410753 rectangle: (0, 0, 374, 444)
# index: 33 iou: 0.6270619201314865 rectangle: (0, 0, 374, 449)
# index: 34 iou: 0.6270619201314865 rectangle: (0, 0, 374, 449)
# index: 35 iou: 0.6270619201314865 rectangle: (0, 0, 374, 449)

반응형

+ Recent posts