Difference between ReflectionPadding2d and padding_mode='reflect'

Hello, this is my first post here, so don’t be too harsh on me :smile:

I was just wondering if there is any difference between using nn.ReflectionPadding2d before Conv2d layer and just setting padding_mode of Conv2d to ‘reflect’?

It should yield the same results as seen here:

x = torch.randn(2, 3, 24, 24)
conv = nn.Conv2d(3, 6, 3)
pad = nn.ReflectionPad2d(1)

out = pad(x)
out = conv(out)

conv_pad = nn.Conv2d(3, 6, 3, 1, 1, padding_mode='reflect')

out_pad = conv_pad(x)

print((out - out_pad).abs().max())
> tensor(0., grad_fn=<MaxBackward1>)
1 Like

Hello, your posts are always so helpful! I’ve been using pytorch a lot over the past year and I find myself coming across your posts all the time. I totally want to thank you for all your helpful posts. Meta, give this man a raise! :grinning:

1 Like