简体   繁体   English

conv2d 层有多少个参数

[英]how many parameters for a conv2d layer

I am trying to understand how the number of parameters are arrived at for a CNN application, as per the answers given in https://ai.stackexchange.com/questions/5769 .根据https://ai.stackexchange.com/questions/5769 中给出的答案,我试图了解 CNN 应用程序的参数数量是如何得出的。

However for an Unet architecture (which involves upsampling) I am going wrong.但是,对于 Unet 架构(涉及上采样),我出错了。 The formula I am using for a conv2d layer is - k*k*i*o + o, where:我用于 conv2d 层的公式是 - k*k*i*o + o,其中:

k = kernel size, eg 3
i = input_depth
o = output_depth

Only for the conv2d layers immediately following the upsampling, my calculation does not match what is printed by keras.仅对于紧接上采样后的 conv2d 层,我的计算与 keras 打印的不匹配。 What am I doing wrong?我究竟做错了什么?

The keras code is the following: keras 代码如下:

inputs = Input(256,256,1)
conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same')(inputs)
conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same')(pool1)
conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same')(pool2)
conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same')(pool3)
conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same')(conv4)
drop4 = Dropout(0.5)(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same')(pool4)
conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same')(conv5)
drop5 = Dropout(0.5)(conv5)
up6 = Conv2D(512, 2, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(drop5))
merge6 = concatenate([drop4,up6], axis = 3)
conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same')(merge6)
conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same')(conv6)
up7 = Conv2D(256, 2, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv6))
merge7 = concatenate([conv3,up7], axis = 3)
conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same')(merge7)
conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same')(conv7)
up8 = Conv2D(128, 2, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv7))
merge8 = concatenate([conv2,up8], axis = 3)
conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same')(merge8)
conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same')(conv8)
up9 = Conv2D(64, 2, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv8))
merge9 = concatenate([conv1,up9], axis = 3)
conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same')(merge9)
conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same')(conv9)
conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same')(conv9)
conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)
model = Model(input = inputs, output = conv10)

And my calculations are the following (as per the above formula):我的计算如下(根据上述公式):

name,     depth,  kernel, out-size, params,     should be
input,     1,     ,       256
conv2d_1,  64,    3,      256,      640
conv2d_2,  64,    3,      256,      36928
pool2d_1,  64,    ,       128,      0,
conv2d_3,  128,   3,      128,      73856,
conv2d_4,  128,   3,      128,      147584,
pool2d_2,  128,   ,       64,       0,
conv2d_5,  256,   3,      64,       295168,
conv2d_6,  256,   3,      64,       590080,
pool2d_3,  256,   ,       32,       0,
conv2d_7,  512,   3,      32,       1180160,
conv2d_8,  512,   3,      32,       2359808,
dropout_1, 512,   ,       32,       0,
pool2d_4,  512,   ,       16,       0,
conv2d_9,  1024,  3,      16,       4719616,
conv2d_10, 1024,  3,      16,       9438208,
dropout_2, 1024,  ,       16,       0
upsample_1,1024,  ,       32,       0
conv2d_11, 512,   3,      32,       4719104,    2097664
concat_1,  1024,  ,       32,       0
conv2d_12, 512,   3,      32,       4719104
conv2d_13, 512,   3,      32,       2359808
upsample_2,512,   ,       64,       0
conv2d_14, 256,   3,      64,       1179904,    524544
concat_2,  512,   ,       64,       0
conv2d_15, 256,   3,      64,       1179904
conv2d_16, 256,   3,      64,       590080
upsample_3,256,   ,       128,      0
conv2d_17, 128,   3,      128,      295040,     131200
concat_3,  256,   ,       128,      0
conv2d_18, 128,   3,      128,      295040
conv2d_19, 128,   3,      128,      147584
upsample_4,128,   ,       256,      0
conv2d_20, 64,    3,      256,      73792,      32832
concat_4,  128,   ,       256,      0
conv2d_21, 64,    3,      256,      73792
conv2d_22, 64,    3,      256,      36928
conv2d_23, 2,     3,      256,      1154
conv2d_24, 1,     1,      256,      3

Taking one specific example, for conv2d_11: as per my calculation it should be 9*1024*512+512 = 4719104. However, keras prints the number as 2097664.举一个具体的例子,对于 conv2d_11:根据我的计算,它应该是 9*1024*512+512 = 4719104。但是,keras 将数字打印为 2097664。

Back-calculating, it looks like I would get the keras result if I had used 4 instead of 9. But the kernel size for conv2d_11 is 3x3, so why should I use 4 only for this layer?反向计算,如果我使用 4 而不是 9,我似乎会得到 keras 结果。但是 conv2d_11 的内核大小是 3x3,那么为什么我应该只对这一层使用 4?

Because something is considering a kernel size of 3 (*9) and another thing is considering a kernel size of 2 (*4).因为有些事情正在考虑 3 (*9) 的内核大小,而另一件事正在考虑 2 (*4) 的内核大小。

This is a code problem, not a parameter calculation problem.这是代码问题,不是参数计算问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM