Yes, you can apply a cross-correlation with a normalized filter returning a peak signal (if normalized it would be 1.
) in the output image. Since nn.Conv2d
layers are actually using a cross-correlation approach you could initialize the weight with the same shape as your target:
image = torch.zeros(1, 1, 12, 12)
image[:, :, 3:8, 3] = 1.
image[:, :, 3, 3:8] = 1.
print(image)
# tensor([[[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 1., 1., 1., 1., 1., 0., 0., 0., 0.],
# [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]]])
plt.imshow(image[0, 0])
conv = nn.Conv2d(1, 1, 5, padding=2, bias=False)
with torch.no_grad():
weight = torch.zeros(1, 1, 5, 5)
weight[:, :, 3:8, 3] = 1.
weight[:, :, 3, 3:8] = 1.
weight = weight / weight.sum()
conv.weight.copy_(weight)
out = conv(image)
print(out)
tensor([[[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.3333, 0.3333, 0.3333, 0.3333, 0.3333, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.3333, 1.0000, 0.6667, 0.6667, 0.6667, 0.3333, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.3333, 0.6667, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.3333, 0.6667, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.3333, 0.6667, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.3333, 0.3333, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000]]]], grad_fn=<ConvolutionBackward0>)
plt.imshow(out[0, 0].detach().numpy())