如何计算卷积神经网络的参数个数?

How to calculate the number of parameters of convolutional neural networks?

我无法给出 AlexNet or VGG Net 的正确数量的参数。

比如计算VGG Net的conv3-256层的参数个数,答案为0.59M = (3*3)*(256*256),即(kernel size) *(联合层中两个通道数的乘积),但是那样的话,我无法获得 138M 参数。

那么你能告诉我我的计算哪里有问题,或者告诉我正确的计算过程吗?

如果您指的是 16 层的 VGG 网络(table 1,D 列),那么 138M 指的是 参数总数 这个网络,即包括所有卷积层,但也包括完全连接的层。

查看由 3 x conv3-256 层组成的第 3 个卷积阶段:

  • 第一个有N=128个输入平面和F=256个输出平面,
  • 另外两个有 N=256 个输入平面和 F=256 个输出平面。

每一层的卷积核都是 3x3。就参数而言,这给出了:

  • 128x3x3x256(权重)+ 256(偏差)= 第一个参数的 295,168 个参数,
  • 256x3x3x256(权重)+ 256(偏差)= 590,080 个其他两个参数。

如上所述,您必须对所有层以及全连接层执行此操作,并对这些值求和以获得最终的 138M 数字。

-

更新:各层之间的细分给出:

conv3-64  x 2       : 38,720
conv3-128 x 2       : 221,440
conv3-256 x 3       : 1,475,328
conv3-512 x 3       : 5,899,776
conv3-512 x 3       : 7,079,424
fc1                 : 102,764,544
fc2                 : 16,781,312
fc3                 : 4,097,000
TOTAL               : 138,357,544

特别是对于全连接层 (fc):

 fc1 (x): (512x7x7)x4,096 (weights) + 4,096 (biases)
 fc2    : 4,096x4,096     (weights) + 4,096 (biases)
 fc3    : 4,096x1,000     (weights) + 1,000 (biases)

(x) 见文章3.2节:先将全连接层转为卷积层(第一个FC层转为7×7的conv.层,最后两个FC层到 1 × 1 转换层)。

详情fc1

如上文所述,在输入全连接层之前的空间分辨率为 7x7 像素。这是因为此 VGG 网络在卷积之前使用 空间填充 ,如论文第 2.1 节中所述:

[...] conv 的空间填充。层输入使得空间分辨率在卷积后得以保留,即对于 3×3 卷积,填充为 1 个像素。图层.

使用这样的填充,并使用 224x224 像素的输入图像,分辨率随着层的变化而降低:112x112、56x56、28x28、14x14 和 7x7 在具有 512 个特征的最后一个 convolution/pooling 阶段之后地图。

这给出了传递给 fc1 的特征向量,维度为:512x7x7。

CS231n 讲义中还提供了 VGG-16 网络计算的重要细分。

INPUT:     [224x224x3]    memory:  224*224*3=150K   weights: 0
CONV3-64:  [224x224x64]   memory:  224*224*64=3.2M  weights: (3*3*3)*64 = 1,728
CONV3-64:  [224x224x64]   memory:  224*224*64=3.2M  weights: (3*3*64)*64 = 36,864
POOL2:     [112x112x64]   memory:  112*112*64=800K  weights: 0
CONV3-128: [112x112x128]  memory:  112*112*128=1.6M weights: (3*3*64)*128 = 73,728
CONV3-128: [112x112x128]  memory:  112*112*128=1.6M weights: (3*3*128)*128 = 147,456
POOL2:     [56x56x128]    memory:  56*56*128=400K   weights: 0
CONV3-256: [56x56x256]    memory:  56*56*256=800K   weights: (3*3*128)*256 = 294,912
CONV3-256: [56x56x256]    memory:  56*56*256=800K   weights: (3*3*256)*256 = 589,824
CONV3-256: [56x56x256]    memory:  56*56*256=800K   weights: (3*3*256)*256 = 589,824
POOL2:     [28x28x256]    memory:  28*28*256=200K   weights: 0
CONV3-512: [28x28x512]    memory:  28*28*512=400K   weights: (3*3*256)*512 = 1,179,648
CONV3-512: [28x28x512]    memory:  28*28*512=400K   weights: (3*3*512)*512 = 2,359,296
CONV3-512: [28x28x512]    memory:  28*28*512=400K   weights: (3*3*512)*512 = 2,359,296
POOL2:     [14x14x512]    memory:  14*14*512=100K   weights: 0
CONV3-512: [14x14x512]    memory:  14*14*512=100K   weights: (3*3*512)*512 = 2,359,296
CONV3-512: [14x14x512]    memory:  14*14*512=100K   weights: (3*3*512)*512 = 2,359,296
CONV3-512: [14x14x512]    memory:  14*14*512=100K   weights: (3*3*512)*512 = 2,359,296
POOL2:     [7x7x512]      memory:  7*7*512=25K      weights: 0
FC:        [1x1x4096]     memory:  4096             weights: 7*7*512*4096 = 102,760,448
FC:        [1x1x4096]     memory:  4096             weights: 4096*4096 = 16,777,216
FC:        [1x1x1000]     memory:  1000             weights: 4096*1000 = 4,096,000

TOTAL memory: 24M * 4 bytes ~= 93MB / image (only forward! ~*2 for bwd)
TOTAL params: 138M parameters

我知道这是旧的 post 不过,我认为 @deltheil 接受的答案包含错误。如果没有,我很乐意得到纠正。卷积层不应该有偏差。 IE。 128x3x3x256(权重)+ 256(偏差)= 295,168 应该 128x3x3x256(权重)= 294,9112

谢谢

以下是计算每个 cnn 层中参数数量的方法:
一些定义
n--过滤器的宽度
m--过滤器高度
k--输入特征图的数量
L--输出特征图的数量
那么参数的数量#= (n*m *k+1)*L 其中第一个贡献来自 权重,第二个来自偏差。

下面的 VGG-16 架构在 original paper as highlighted by @deltheil in (table 1, column D) 中,我从那里引用

2.1 ARCHITECTURE

During training, the input to our ConvNets is a fixed-size 224 × 224 RGB images. The only preprocessing we do is subtracting the mean RGB value, computed on the training set, from each pixel.

The image is passed through a stack of convolutional (conv.) layers, where we use filters with a very small receptive field: 3 × 3 (which is the smallest size to capture the notion of left/right, up/down, center). The convolution stride is fixed to 1 pixel; the spatial padding of conv. layer input is such that the spatial resolution is preserved after convolution, i.e. the padding is 1 pixel for 3 × 3 conv. layers. Spatial pooling is carried out by five max-pooling layers, which follow some of the conv. layers (not all the conv. layers are followed by max-pooling). Max-pooling is performed over a 2 × 2 pixel window, with stride 2.

A stack of convolutional layers (which has a different depth in different architectures) is followed by three Fully-Connected (FC) layers: the first two have 4096 channels each, the third performs 1000-way ILSVRC classification and thus contains 1000 channels (one for each class).

The final layer is the soft-max layer.

使用上面的,

  • 一个计算层激活形状的公式!

  • 每层对应权重的计算公式:

注:

  • 你可以简单地乘以各自的激活形状列来得到激活大小

  • CONV3:表示一个3*3的滤波器将在输入上进行卷积!

  • MAXPOOL3-2:意味着,第 3 个池化层,带 2*2 个过滤器,stride=2,padding=0(池化层中的相当标准)

  • Stage-3:表示它有多个CONV层堆叠!具有相同的 padding=1, , stride=1, and filter 3*3

  • Cin : 表示来自输入层的深度a.k.a通道!

  • Cout:表示深度a.k.a通道输出(你配置不同-学习更复杂的特征!),

Cin 和 Cout 是您堆叠在一起以学习不同尺度的多个特征的过滤器数量,例如在第一层中您可能想要学习垂直边缘、水平边缘和 45 度角的边缘,等等! , 64 种可能的不同过滤器,每种都有不同类型的边缘!!

  • n:没有深度的输入维度,例如在输入图像的情况下 n=224!

  • p: 每一层的padding

  • s:每层使用的步长

  • f:过滤器大小,即 CONV 层为 3*3,MAXPOOL 层为 2*2!

  • 在MAXPOOL5-2之后,你只需将体积扁平化并与第一个FC层接口即可。!

我们得到 table:

最后,如果将最后一列中计算的所有权重相加,您最终会得到 138,357,544(1.38 亿)个参数来训练 VGG-15!