Hi,
I want to convolve some images with some known kernels. I thought I could use torch.nn.functional.conv2d for this. So suppose I have some images with shape [2048, 1, 12, 12] (that is, a batch of 2048 images with 1 channel of size 12x12 pixels) and a kernel like kernel = torch.tensor([[-1, 1],[-1, 1]]), i.e. a 2x2 kernel. How can I convolve every image in my batch with this kernel?
I thought this could be done with torch.nn.functional.conv2d(images, kernel.expand(2048, 1, 2, 2)) but this results a final matrix of size [2048, 2048, 11, 11]. If I read the docs correctly, this is due to having the number of groups the same as the amount of out-channels, so every input is convolved with every filter.
This is not what I want. What I want is that every image is convolved with the same kernel. Also, I want the output size to match the input size, as if I am calling scipy.signal.convolve2d(image, kernel, mode='same', boundary='symm') for each image in the batch (this is what I am doing now, but this is slow because it involves many copies from GPU to CPU).
Can I do this operation with pytorch’s functional.conv2d as to avoid copying data from the GPU to CPU? If so what kind of parameters do I need to pass to match my expected result?
Thanks in advance.