I'm trying to port some pytorch code to tensorflow 2.0 and am having difficulty figuring out how to translate the convolution functions between the two. The way both libraries deal with padding is the sticking point. Basically, I'd like to understand how I can manually produce the padding that pytorch does under the hood so that I can translate that to tensorflow.
The code below works if I don't do any padding, but I can't figure out how to make the two implementations match once any padding is added.
output_padding = SOME NUMBER
padding = SOME OTHER NUMBER
strides = 128
tensor = np.random.rand(2, 258, 249)
filters = np.random.rand(258, 1, 256)
out_torch = F.conv_transpose1d(
torch.from_numpy(tensor).float(),
torch.from_numpy(filters).float(),
stride=strides,
padding=padding,
output_padding=output_padding)
def pytorch_transpose_conv1d(inputs, filters, strides, padding, output_padding):
N, L_in = inputs.shape[0], inputs.shape[2]
out_channels, kernel_size = filters.shape[1], filters.shape[2]
time_out = (L_in - 1) * strides - 2 * padding + (kernel_size - 1) + output_padding + 1
padW = (kernel_size - 1) - padding
# HOW DO I PAD HERE TO GET THE SAME OUTPUT AS IN PYTORCH
inputs = tf.pad(inputs, [(?, ?), (?, ?), (?, ?)])
return tf.nn.conv1d_transpose(
inputs,
tf.transpose(filters, perm=(2, 1, 0)),
output_shape=(N, out_channels, time_out),
strides=strides,
padding="VALID",
data_format="NCW")
out_tf = pytorch_transpose_conv1d(tensor, filters, strides, padding, output_padding)
assert np.allclose(out_tf.numpy(), out_torch.numpy())
from How to manually implement padding for pytorch convolutions
No comments:
Post a Comment