Conv2DTranspose parameters are not clear to me

Viewed 50

I am learning CNN with an excellent teacher https://github.com/bnsreenu. Usually his explanations are very clear but I found difficulty with his explanation of UpSampling2D vs Conv2DTranspose. The more I went into Conv2DTranspose the more confused I became.

I found a nice explanation on https://towardsdatascience.com/types-of-convolutions-in-deep-learning-717013397f4d but it doesn’t seem to fit what I see. The code is

from numpy import asarray
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import UpSampling2D, Conv2DTranspose, Input

#x = asarray([[1,2,3],[4,5,6],[7,8,9]])
x = asarray([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
x = x.reshape(1, x.shape[0], x.shape[1],1)

model = Sequential()
model.add(Input(shape=(x.shape[1], x.shape[2], 1)))
#model.add(UpSampling2D(size=(2,2)))
model.add(Conv2DTranspose(1,(3*3), strides=(1,1), kernel_initializer='ones'))
model.summary()

x1 = model.predict(x)
x1 = x1.reshape(x1.shape[1], x1.shape[2])
print(x1)

I can make either a 3*3 array or a 4*4 array and get the same problems with the Conv2D filter. A 1*1 filter is the only one which makes sense. It reproduces the input array with 2 trainable parameters, i.e. y = Ax+B, the multiplier and bias. A 2*2 filter already doesn’t make sense

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_transpose_1 (Conv2DTr (None, 7, 7, 1)           17        
=================================================================
Total params: 17
Trainable params: 17
Non-trainable params: 0
_________________________________________________________________
[[  1.   3.   6.  10.   9.   7.   4.]
 [  6.  14.  24.  36.  30.  22.  12.]
 [ 15.  33.  54.  78.  63.  45.  24.]
 [ 28.  60.  96. 136. 108.  76.  40.]
 [ 27.  57.  90. 126.  99.  69.  36.]
 [ 22.  46.  72. 100.  78.  54.  28.]
 [ 13.  27.  42.  58.  45.  31.  16.]]

How did it reach the conclusion of 17 trainable parameters? 2*2 is an even number, so how does an odd number of 17 come in? One explanation is the multipliers are for each filter position and the bias is a single value for all. 2^4 + 1 = 17. Also how does the output matrix have 7*7 entries? The input filter and matrix are both even numbers. My guess would have been 6*6. (4*4 plus padding of 1 on each end.)

A 3*3 filter is equally confusing

Layer (type) Output Shape Param # 
================================================================= 
conv2d_transpose_2 (Conv2DTr (None, 12, 12, 1) 82
=================================================================
Total params: 82 Trainable params: 82 Non-trainable params: 0 
_________________________________________________________________ 
[[ 1. 3. 6. 10. 10. 10. 10. 10. 10. 9. 7. 4.]
 [ 6. 14. 24. 36. 36. 36. 36. 36. 36. 30. 22. 12.]
 [ 15. 33. 54. 78. 78. 78. 78. 78. 78. 63. 45. 24.]
 [ 28. 60. 96. 136. 136. 136. 136. 136. 136. 108. 76. 40.]
 [ 28. 60. 96. 136. 136. 136. 136. 136. 136. 108. 76. 40.]
 [ 28. 60. 96. 136. 136. 136. 136. 136. 136. 108. 76. 40.]
 [ 28. 60. 96. 136. 136. 136. 136. 136. 136. 108. 76. 40.]
 [ 28. 60. 96. 136. 136. 136. 136. 136. 136. 108. 76. 40.]
 [ 28. 60. 96. 136. 136. 136. 136. 136. 136. 108. 76. 40.]
 [ 27. 57. 90. 126. 126. 126. 126. 126. 126. 99. 69. 36.]
 [ 22. 46. 72. 100. 100. 100. 100. 100. 100. 78. 54. 28.]
 [ 13. 27. 42. 58. 58. 58. 58. 58. 58. 45. 31. 16.]]

3^4+1 = 82 trainable parameters. 12*12 output matrix looks again too big. The beginning and end of the first (and last) row makes sense, but there seem to be too many numbers in the middle.

Can anyone give me an explanation of what I am missing here? Thanks, Ilan

1 Answers
  • (3*3) kernel-size is equal to (9,9). It has 81 trainable parameters. 81 for kernel (9*9) and 1 bias.
  • (2*2) kernel-size is equal to (4,4). It has 17 trainable parameters. 16 for kernel (4*4) and 1 bias.

I think you have made a mistake. You have set kernel_size to (3*3) which gives output a kernel size (9,9). If you want to define kernel size for example 2 by 2, you should set it like (2,2). Then you will have 5 trainable parameters, which 4 of them are weights and 1 for bias.

In this example, you have set a conv layer in this way:

model.add(Conv2DTranspose(1,(2*2), strides=(1,1), kernel_initializer='ones'))

Which is equal to:

model.add(Conv2DTranspose(1,(4,4), strides=(1,1), kernel_initializer='ones'))

Then, 17 parameters make sense, since 16 of them (a kernel with (4,4) size, so 16 parameters) are weights and 1 of them is bias. If you set it to (3*3), then trainable parameters will be 82, which 81 of them are weights (a (9,9) kernel size) and one of them is bias.

You can check them like this:

print(model.layers[0].get_weights()[0].shape) #weights
print(model.layers[0].get_weights()[1].shape) #bias
Related