Hi, I wonder if there is a way to apply conv2d to input batch with multiple kernels.
I want to apply conv2d operation for each input of the minibatch with its corresponding kernel where there are B input feature maps and B kernels within each minibatch.
This is similar to torch.bmm operation where each matrix in a batch is multiplied by a different weight matrix. I found this backend function ‘THCudaTensor_conv2Dmm’. Is this function related to what I want? If so, how can I access it in my python code?
You can use the groups parameter in nn.Conv2d to achieve this. Try to set groups=in_channels and each input channel will have its own set of filters.
Note that both in_channels and out_channels have to be divisible by groups.
Can you please explain how I can group such that the sample code works?
A = torch.randn(5, 512, 7, 7) # minibatch, in_channels, iH, iW
B = torch.randn(5, 512, 1, 1) # minibatch, filter_channels, h, w
# Aim is to run conv2d at batch level
out = F.conv2d(A, B)
# However, It applies all the filters to all the batch samples of A
# Desired output shape = [5, 1, 7, 7]
# Obtained output shape = [5, 5, 7, 7]
Using a groups = 512 or 5 raises RuntimeError due to shape mismatch.