如何计算配置文件中卷积层后的输出大小?

How to calculate output sizes after a convolution layer in a configuration file?

我是卷积神经网络的新手,想知道如何在给定 pytorch 配置文件的情况下计算或计算模型层之间的输出大小,类似于 this link 中的说明。

我已经看过的大部分内容都不是很清楚和简洁。我应该如何计算每一层的尺寸? 下面是将被解析的配置文件的片段。

# (3, 640, 640)
[convolutional]
batch_normalize=1
filters=16
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

# (16, 320, 320)

简而言之,输出dims的计算有一个通用的公式:

您可以在 A guide to receptive field arithmetic for Convolutional Neural Networks 中找到解释。

此外,我想推荐一篇很棒的文章A guide to convolution arithmetic for deep learning

并且这个 repo conv_arithmetic 带有卷积动画。

直接的方法是查看 pytorch 文档,您可以在其中找到应用于图层的公式。

Pytorch Conv2D Formulas to compute High and Width

手工计算很容易出错(至少对我自己而言)

我找到的最可靠的方法:

import torch
from torch import nn

import functools
import operator

def shape_of_output(shape_of_input, list_of_layers):
    sequential = nn.Sequential(*list_of_layers)
    return tuple(sequential(torch.rand(1, *shape_of_input)).shape)

def size_of_output(shape_of_input, list_of_layers):
    return functools.reduce(operator.mul, list(shape_of_output(shape_of_input, list_of_layers)))

它只是将输入层运行一次,然后打印输出的大小。所以它有点浪费,但基本上保证是正确的,即使 new features/options 被添加到 pytorch.

示例(如果复制+粘贴则运行)

# 
# example setup
# 
import random
out_channel_of_first = random.randint(1,16)
kernel_size_of_first = random.choice([3,5,7,11])
grayscale_image_shape = (1, 48, 48)
color_image_shape     = (3, 48, 48) # alternative example

# 
# example usage
# 
print('the output shape will be', shape_of_output(
    shape_of_input=grayscale_image_shape,
    list_of_layers=[         
        nn.Conv2d(
            in_channels=grayscale_image_shape[0],
            out_channels=out_channel_of_first,
            kernel_size=kernel_size_of_first,
        ),
        nn.ReLU(),
        nn.MaxPool2d(2,2),
        
        # next major layer
        nn.Conv2d(
            in_channels=out_channel_of_first,
            out_channels=5,
            kernel_size=3
        ),
        nn.ReLU(),
        nn.MaxPool2d(2,2),
    ],
))