Thanks for the code snippet.
Your approach looks valid assuming you are using different images in f_img.
However, based on your previous description:



 pierremrg:

But it’s common that I need to perform a cross-correlation on an unique image with many different kernels.
All kernels have the same shape, so a simple solution is to batch them, and duplicate the image along the batch dimension.


it seems that f_img would contain only a single unique image and you are repeating it in the batch dimension to use the view operation with the grouped conv approach.
Your grouped conv would create 3 groups (basically splitting the channels into the “images”) and would then apply each corresponding conv kernel to the input.
If so, you should be able to use a plain convolution to get the same result, if I’m not mistaken.
This workflow would use a single image and each filter would still be applied to it.
To do so, I’ve changed your code a bit to really repeat the input image in the batch dimension and initialized the filter kernels randomly.
Could you check, if I understand your use case correctly?
batch = 3
channels = 2

#define a sample image (the **same** image is batched 3 times)
f_img = torch.randn((1, channels, 5, 10)).repeat(batch, 1, 1, 1)

pattern = 1.
# Define 3 **different** kernel
f_krn = torch.randn((batch, channels, 3, 3))
f_krn[0, :, :, :] = pattern
f_krn[1, :, :, :] = pattern * 2
f_krn[2, :, :, :] = pattern * 3

print(f_img.shape, f_krn.shape)
# Image shape: [3, 2, 5, 10] / Kernel shape: [3, 2, 3, 3]

# Perform cross-correlation
f_img_ = f_img.view(1, batch * channels, f_img.shape[2], f_img.shape[3])
f_krn = f_krn.view(batch, channels, 3, 3)

result = F.conv2d(f_img_, f_krn, groups=batch)

# single image
out = F.conv2d(f_img[0:1], f_krn)
print((out == result).all())
> tensor(True)