Custom convolutional kernel

Hi,

I’d like to set the shape of my convolution kernel to a circle instead of a NN square. My idea is to make the corners of NN kernel unchanged (to be zero). Do you guys know if there is a way to do that in PyTorch?


I’m not sure whether setting the ‘weight’ in ‘torch.functional.conv2d(input,weight,…)’ is a way to do that.

Thank you

Yes, this would be one way to go. You would need to set the unwanted weights to zero and the gradient as well if that’s your use case.
Here is a small code sample:

out_channels = 6
in_channels = 1
kh, kw = 3, 3
weight = torch.randn(out_channels, in_channels, kh, kw, requires_grad=True)
with torch.no_grad():
    weight[:, :, 0, 0] = 0.
    weight[:, :, -1, 0] = 0.
    weight[:, :, 0, -1] = 0.
    weight[:, :, -1, -1] = 0.

x = torch.randn(1, in_channels, 5, 5)
output = F.conv2d(x, weight)
output.mean().backward()
print(weight.grad)

Thank you very much for your reply. I also wonder if there is other way to do that?

I found another similar question: Custom convolution shape
and you said ‘Tensor Comprehensions’ is another way. Do you think if it’s easier to go with TC?

I’m not sure, as TC wasn’t updated in the last couple of months.
Apparently TVM provides a similar functionality and is under current development.

Got it. Thank you so much!