tf.image.non_max_suppression(
    boxes,
    scores,
    max_output_size,
    iou_threshold=0.5,
    score_threshold=float('-inf'),
    name=None
)

NMS라고 불리우는 함수이며, 모델이 예측한 bbox중 겹치는 부분이 많은 것들을 제거한다.

다음과 같은 과정을 거친다

1. 가장 높은 Score를 가진 순대로 내림차순한다.

2. 선택된 bbox와 나머지 bbox의 IOU를 계산한다. 이때, IOU threshold(보통 0.5)이상이면 제거해준다.

3. 객체당 하나의 bbox가 남거나 더 이상 계산할 bbox가 없을 때까지 위의 과정을 반복하게 된다.

아래가 non_max_suppression의 구현이다.

import numpy as np

def non_max_suppression(boxes, overlapThresh):
    if(len(boxes) == 0):
        raise ValueError('nonono')
        
    picked_class = []
    
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]
    
    area = (x2 - x1 + 1) * (y2 - y1 + 1)
    idxs = np.argsort(y2)
    print(idxs)
    
    pick = []
    
    while len(idxs) > 0:
        # grab the last index in the indexes list and add the
        # index value to the list of picked indexes
        last = len(idxs) - 1
        i = idxs[last]
        print(last, i)
        pick.append(i)

        # find the largest (x, y) coordinates for the start of
        # the bounding box and the smallest (x, y) coordinates
        # for the end of the bounding box
        xx1 = np.maximum(x1[i], x1[idxs[:last]])
        yy1 = np.maximum(y1[i], y1[idxs[:last]])
        xx2 = np.minimum(x2[i], x2[idxs[:last]])
        yy2 = np.minimum(y2[i], y2[idxs[:last]])

        # compute the width and height of the bounding box
        w = np.maximum(0, xx2 - xx1 + 1)
        h = np.maximum(0, yy2 - yy1 + 1)

        # compute the ratio of overlap
        overlap = (w * h) / area[idxs[:last]]
        print('overlap : ',overlap)

        # delete all indexes from the index list that have
        idxs = np.delete(idxs, np.concatenate(([last],
            np.where(overlap > overlapThresh)[0])))
        
        print('idx : ', idxs)

        # return only the bounding boxes that were picked using the
        # integer data type
    return boxes[pick].astype("int")

출처 : https://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python/

 

(Faster) Non-Maximum Suppression in Python - PyImageSearch

Non-maximum suppression is crucial for HOG + Linear SVM object detection systems. Learn how to obtain a 100x speedup when applying non-maximum suppression.

www.pyimagesearch.com