【问题标题】:How to calculate output sizes after a convolution layer in a configuration file?如何计算配置文件中卷积层后的输出大小?
【发布时间】:2019-06-04 20:33:12
【问题描述】:

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

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

# (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)

【问题讨论】:

    标签: pytorch object-detection


    【解决方案1】:

    总之,输出dims计算有一个通用公式:

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

    另外,我想推荐一篇精彩的文章A guide to convolution arithmetic for deep learning

    还有这个带有卷积动画的 repo conv_arithmetic

    【讨论】:

      【解决方案2】:

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

      我找到的最可靠的方法:

      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)))
      

      它只是简单地通过层运行一次输入,然后打印输出的大小。所以这有点浪费,但即使在 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),
          ],
      ))
      

      【讨论】:

        【解决方案3】:

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

        Pytorch Conv2D Formulas to compute High and Width

        【讨论】:

          猜你喜欢
          • 2019-05-03
          • 1970-01-01
          • 2017-03-13
          • 2017-10-26
          • 1970-01-01
          • 2016-05-17
          • 1970-01-01
          • 1970-01-01
          • 2018-04-26
          相关资源
          最近更新 更多