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