I just want to perform a simple convolution with a 3x3 kernel in a 3x3 image with padding=1. Both kernel and image have a single channel only.
This is the image and the kernel (weights)
# Initialize image and filter
my_image = torch.Tensor([[0, 1, 0], [1, 1, 1], [0, 1, 0]]).unsqueeze(0).unsqueeze(0)
weights = torch.Tensor([[0, 1, 0], [0, 0, 0], [0, 0, 0]]).unsqueeze(0).unsqueeze(0)
Using torch.nn.Conv2d
to apply my convolution I have:
# First scenario
my_conv = torch.nn.Conv2d(1, 1, kernel_size=(3, 3), bias=False, padding=1)
my_conv.weight = torch.nn.Parameter(weights)
res1 = my_conv(my_image)
If I use the torch nn.functional.conv2d, I have:
# Second scenario
res2 = F.conv2d(weights, my_image, bias=None, padding=1)
The results I obtain are different:
res1 will be
tensor([[[[0., 0., 0.],
[0., 1., 0.],
[1., 1., 1.]]]], grad_fn=)
and res2:
tensor([[[[1., 1., 1.],
[0., 1., 0.],
[0., 0., 0.]]]])
What am I missing here? I appreciate all kinds of help.