0

I have the following PyTorch layer definition:

self.e_conv_1 = nn.Sequential(
    nn.ZeroPad2d((1, 2, 1, 2)),
    nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(5, 5), stride=(2, 2)),
    nn.LeakyReLU(),
)

I want to have the same exact layer declaration in Tensorflow. How can I get to that?

self.e_conv_1 = tf.keras.Sequential([
  layers.Conv2D(64, kernel_size=(5, 5), activation=partial(tf.nn.leaky_relu, alpha=0.01), padding='same', strides=(1, 2))
])

Should it be something like this code above? I think that at least strides and padding isn't the same.

Thanks in advance to anyone who helps.

mavaras
  • 53
  • 5

1 Answers1

1

I think you can use layers in this way according to tenssorflow documentation:

tf.keras.Sequential([
    layers.ZeroPadding2D(padding=((1,2), (1,2)))
    layers.Conv2D(64, kernel_size=(5, 5), activation=partial(tf.nn.leaky_relu, 
    alpha=0.01), padding='valid', strides=(2, 2))
])

the main difference is between torch zero padding and tensroflow zero padding arguments. in torch padding arguments are:

m = nn.ZeroPad2d((left, right, top, bottom))

in tensorflow:

tf.keras.layers.ZeroPadding2D(padding=((top,bottom),(left,right)))
Milad Yousefi
  • 191
  • 2
  • 9
  • Thank you so much. The thing that was confusing me is that zeroPad2d. Thanks for your knowledge!! – mavaras Feb 20 '21 at 18:44
  • Another question. Do you think that I need padding='same' in this concrete pytorch-translation problem? – mavaras Feb 20 '21 at 18:57
  • you're welcome. the default attribute in torch conv2d for padding is zero. It seems it's equal tensorflow 'valid' padding not 'same'. I think if you want to get same result, you should change padding to 'valid'. thank you.I'll edit my answer. – Milad Yousefi Feb 20 '21 at 19:47
  • Thanks again. About that ZeroPadding2D, I'm reading the documentation but I can't completely understand how it works or exactly what it does. Can you briefly tell me about that? – mavaras Feb 20 '21 at 20:46
  • convolution layer, thorouh it's concept, make image smaller. for deep neural networks(much convolution layer), without padding we may lose some main feature of image. so the simplest way is add padding to image. padding add 0 pixels around the main image. for example if main image size is 64*64, padding2d with argument 1, add a row of zero pixel to top and bottom and a clumn od zero pixel to left and right the image. so output size is 66*66. for illustrated example you can look at https://deeplizard.com/images/zero%20padding%20example%202.jpg – Milad Yousefi Feb 21 '21 at 08:59
  • Thanks again, you're so friendly. I understood it well with your explanation. I asked that because of the problem I'm having with my architecture. I've posted a new comment in this post describing that. Sorry for my "noobieness" with this topic :) – mavaras Feb 21 '21 at 16:02