Keras: What does Conv2DTranspose exactly do?
14:54 27 Feb 2023

I'm a beginner to Keras/Tensorflow. I think I have a basic understanding what Conv2DTranspose generally does. In detail I struggle.

Therefore I wrote an extremly simple example. This example shall show, how the values in a source-matrix, the kernel and a target-matrix are exactly connected:

  • 5x5 source-matrix has one 1 - rest is 0s
  • 3x3 kernel is enumerated by 1-9
  • stride is 2,2
import numpy as np
import keras as keras 

X = np.asarray([[0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0],
                [0, 0, 1, 0, 0],
                [0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0]])
X = X.reshape((5, 5, 1))
model = keras.models.Sequential()
layer = keras.layers.Conv2DTranspose(1, (3,3), strides=(2,2), padding='same', input_shape=(5, 5, 1))
model.add(layer)
weights = [np.array([ [[[1]],[[2]],[[3]]],
                      [[[4]],[[5]],[[6]]],
                      [[[7]],[[8]],[[9]]]
                    ], dtype="float32"), 
           np.array([0.], dtype="float32")]
layer.set_weights(weights)
y = model.predict(X).reshape((10, 10))
print(y)

The result is:

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 2.]
 [4. 5. 7. 8. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
  • Very surprising: the 9 in the lower-right position of the kernel does not even appear in the result
  • Also surprising: Positions of 1-9 in relation to the 1 in the source

Can anybody explain?

tensorflow keras