[TIP / Pytorch] calculate convolution output shae (conv2d , pooling) (Conv 아웃풋 값

2020. 10. 31. 14:11분석 Python/Pytorch

기존 계산은 다음과 같음.

 

 

하지만 pytorch에서 convolution layer output shape를 계산식은 다음과 같음

 

 

https://pytorch.org/docs/master/generated/torch.nn.Conv2d.html#torch.nn.Conv2d

 

함수 

def cal_conv_output_shape_torch(h_w, layer):
    from math import floor
    dilation = layer.dilation
    kernel_size = layer.kernel_size 
    stride = layer.stride
    pad = layer.padding
    if type(dilation) is not tuple:
        pass
    else :
        dilation = dilation[0]
        stride = stride[0]
        pad = pad[0]
    if type(kernel_size) is not tuple:
        kernel_size = (kernel_size, kernel_size)
    h_out = floor( ((h_w[0] + (2 * pad) - ( dilation * (kernel_size[0] - 1) ) - 1 )/ stride) + 1)
    w_out = floor( ((h_w[1] + (2 * pad) - ( dilation * (kernel_size[1] - 1) ) - 1 )/ stride) + 1)
    if layer.__class__.__name__  == "MaxPool2d" :
        return h_out, w_out
    elif layer.__class__.__name__  == "Conv2d" :
        c_out = layer.out_channels 
        return c_out , h_out, w_out

예시

(channel_in , h_in , w_in) : (1,32,128)

 

 

 

conv2 d 및 pooling 적용하기

layer1 = nn.Conv2d(in_channels=1, out_channels=5, kernel_size=5, stride=2,padding=0)
max_pool1 = nn.MaxPool2d(kernel_size=2)

실제 계산을 하면 다음과 같이 계산된다고 함.

c_out, h_out , w_out = cal_conv_output_shape_torch([32,128],layer1)
print(c_out, h_out , w_out)
# 5 14 62
h_out , w_out = cal_conv_output_shape_torch([h_out,w_out],max_pool1)
print(h_out , w_out)
# 7 31

실제 처리값

 

 

 

 

convolution 계산하는 것이 먼가 항상 외우기 어려우니 함수를 많이 하는 것 같다.

conv2d의 모든 것을 커버하는지는 잘 모르겠다.

 

 

 

discuss.pytorch.org/t/utility-function-for-calculating-the-shape-of-a-conv-output/11173/3

Utility function for calculating the shape of a conv output

I imagine if you wanted a function you could generate a random input, pass it to conv2d, and check the size of the output (this is definitely not efficient but works as a sanity check)

discuss.pytorch.org

 

728x90