简体   繁体   中英

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 .

However for an Unet architecture (which involves upsampling) I am going wrong. The formula I am using for a conv2d layer is - k*k*i*o + o, where:

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. What am I doing wrong?

The keras code is the following:

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.

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?

Because something is considering a kernel size of 3 (*9) and another thing is considering a kernel size of 2 (*4).

This is a code problem, not a parameter calculation problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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