Hi all.
Currently, I am working on copying a trained pytorch model to tensorflow 2.3 platform.
For the Conv2d layers, the feature map output of pytorch and tensoflows are the same. Thus, the conversion for conv2d layers from pytorch to tf2 are all fine now.
However, when I add a Depth_to_space layer to the model, the output contains lots of mosaic, whereas the output of Pixelshuffle does not have this problem.
I am curious that is the mechanism of Depth_to_space are different to Pixelshuffle?
Or is my code has some problem?
Here is part of my code.
Pytorch model
some linear mapping layer with nn.Conv2d
self.pslayers = [nn.Conv2d(d, 412,3 ,1, 3//2)]
self.pslayers.extend([nn.PixelShuffle(2)])
self.pslayers.extend([nn.Conv2d(12, 4num_channels,3 ,1, 3//2)])
self.pslayers.extend([nn.PixelShuffle(2)])
self.pslayers = nn.Sequential(*self.pslayers)
The TF2 model:
some linear mapping layer with Conv2d
x = Conv2D(4*12, 3, padding=“same”)(x)
x = tf.nn.depth_to_space(x, 2, data_format=‘NHWC’)
x = Conv2D(4, 3, padding=“same”)(x)
out = tf.nn.depth_to_space(x, 2, data_format=‘NHWC’)
The conversion for Conv2d layer from torch weights to tf2:
onnx_1_w_num = onnx_l.weight.data.numpy().transpose(2, 3, 1, 0)
onnx_1_b_num = onnx_l.bias.data.numpy()
tf_l.set_weights([onnx_1_w_num,onnx_1_b_num])
I am struggling with this problem for a long while.
Very appreciate for those helping me!