Set weights for custom filters/kernels

undefined: <p>How do I know which weights to choose for filters when I want to use custom filters? I haven’t found anything on the internet.<br>
I’m using 9x9 grayscale images, with pixel values between 0 and 1. Let’s say for example the image looks somehow like this:</p>
<pre><code class="lang-auto">[1, 1, 1, 0, ...]
[1, 0, 0, 0, ...]
[1, 0, 0, 0, ...]
[0, 0, 0, 0, ...]
.
.
.
</code></pre>
<p>When I want to detect this kind of corner, would I use a filter like this?</p>
<pre><code class="lang-auto">[[1., 1., 1.],
[1., 0., 0.],
[1., 0., 0.]]
</code></pre>

ptrblck: <p>Yes, you can apply a cross-correlation with a normalized filter returning a peak signal (if normalized it would be <code>1.</code>) in the output image. Since <code>nn.Conv2d</code> layers are actually using a cross-correlation approach you could initialize the weight with the same shape as your target:</p>
<pre data-code-wrap="python"><code class="lang-python">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=&lt;ConvolutionBackward0&gt;)

plt.imshow(out[0, 0].detach().numpy())
</code></pre>