def get_pad_size(input_shape, filter_size, strides): #{
w_input = input_shape[0] # width of input
h_input = input_shape[1] # height of input
w_stride = strides[0] # stride of width
h_stride = strides[1] # stride of height
w_filter = filter_size[0] # width of filter
h_filter = filter_size[1] # width of filter
output_w = np.ceil(w_input/w_stride)
output_h = np.ceil(h_input/h_stride)
if (w_input % w_stride) == 0: #{
w_pad = max(w_filter - w_stride, 0)
#}
else: #{
w_pad = max(w_filter - (w_input % w_stride), 0)
#}
if (h_input % h_stride) == 0: #{
h_pad = max(h_filter - h_stride, 0)
#}
else: #{
h_pad = max(h_filter - (h_input % h_stride), 0)
#}
pad_top = int(np.floor(h_pad/2))
pad_bottom = int(h_pad - pad_top)
pad_left = int(np.floor(w_pad/2))
pad_right = int(w_pad - pad_left)
return (pad_top, pad_right, pad_bottom, pad_left)
#}
간단하게 만들어보았다. tf/keras에서 convolution, pooling layer에서 그냥 padding='same'하면 알아서 padding size 계산해서 패딩을 해주긴 하지만, 가끔 직접 패딩사이즈가 어떻게 되는지 필요할 때도 있어서 padding size를 리턴해주는 함수를 만들어보았다.
인풋으로는 input_shape, filter_size, strides 가 들어온다. 모두 (width, height)을 가진 리스트이다.
이때 padding = 'same'일 경우 padding size를 계산해서, 리턴해준다.
'AI' 카테고리의 다른 글
| [SNU AI] EHR Data in DeepLearning 발표 유튜브 업로드! (1) | 2021.02.19 |
|---|---|
| Image Classification 연구의 동향(교양과목 보고서) (0) | 2020.10.27 |
| Convolution 후의 shape을 구하는 공식 (0) | 2020.08.05 |