공돌하우스

[딥러닝]SSD모델 default anchor box 그려보기 본문

AI

[딥러닝]SSD모델 default anchor box 그려보기

전자둥이 2021. 9. 3. 14:27

안녕하세요

 

SSD모델에대해서 study하는 중에 default anchor box가 실제 feature map에서 어떻게 그려지는지 시각적으로 보고 싶어서 코드를 간단하게 작성 한 후 그려보았습니다.

 

해당 그림은 10x10 feature map 에서 각 grid마다 6개의 서로 다른 aspect ration를 갖고있는 박스를 그려봤습니다. 

 

10x10 feature map기준

작성한 코드 공유합니다.

import numpy as np
import math
if __name__ == "__main__":
    #color setting
    blue_color = (255,0,0)
    red_color = (0,0,255)

    #decide featuremap size
    feature_map_size = 10
    length_of_one_side = feature_map_size*50

    img = np.zeros((200 + length_of_one_side,200 + length_of_one_side,3), np.uint8)
    img = cv2.rectangle(img,(100 , 100),(100+length_of_one_side,100 + length_of_one_side),blue_color,3)

    for i in range(100,100+length_of_one_side,50):
        img = cv2.line(img,(i,100), (i,100+length_of_one_side),blue_color,2)
    for i in range(100,100+length_of_one_side,50):
        img = cv2.line(img,(100,i), (100 +length_of_one_side,i),blue_color,2)

    s_min = 0.1
    s_max = 0.9
    num_pyramid = 6
    ratio = [0.5,"a",1,2,1/3,3]
    for r in ratio:
        #default box의 한변의 길이 비율 i값 조절로 정하기
        i = 1
        s = s_min + (s_max - s_min) * i / (num_pyramid-1)
        s_ = s_min + (s_max - s_min) * (i+1) / (num_pyramid-1)
     
        if r == "a":
            w = h = math.sqrt(s * s_) * length_of_one_side
            break
        else:
            w = s * math.sqrt(r) * length_of_one_side
            h = s * math.sqrt(1./r) * length_of_one_side
        for a in range(feature_map_size):
            for b in range(feature_map_size):
                center_x = a*50 + 25
                center_y = b*50 + 25
                img = cv2.rectangle(img,(100 + center_x - (int)(w/2),100 + center_y - (int)(h/2)),(100 + center_x + (int)(w/2),100 + center_y + (int)(h/2)),red_color,1)

    cv2.imshow('rectangle',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

feature_map_size와 i값을 조절하며 각 feature_map 에서의 default_box가 어떻게 그려지는지 확인 할 수 있습니다.

 

감사합니다~

'AI' 카테고리의 다른 글

Distance-IOU Loss  (0) 2021.07.25
자연어 처리 중 wordnet  (0) 2021.06.20
[딥러닝] Activation Function 소개  (0) 2021.06.04
ResNet skip-connection  (0) 2021.05.30