Hello, I’m new to Pytorch. I’m tring to convert a code that use functions from scipy and numpy library in Pytorch in order to build a NN and execute it on the GPU.
I have some convolution layers that perform the convolution between a gaussian filter and an image. Exploiting the separability of the gaussian filters I perform the convolution along the x-axis and then on the y-axis.
The code without PyTorch is:
from scipy.ndimage.filters import convolve

img_convolved = convolve(convolve(img, gy[:, None]), gx[None, :])
# default padding mode is 'reflect'

where gx and gy are the 1D gaussian filters.
Mine “translation” to PyTorch is:
import torch
import torch.nn.functional as F
import numpy as np

img = np.random.rand(512, 512) # random image with shape
pad_w, pad_h = int(np.ceil(img.shape[0] / gx.size()[0])), int(np.ceil(img.shape[1] / gx.size()[0]))

img = torch.from_numpy(img).repeat(1, 1, 1, 1)
gx, gy = torch.flip(gx, (0,)).type(torch.float32), \
         torch.flip(gy, (0,)).type(torch.float32)
gx, gy = gx.repeat(1, 1, 1, 1), gy.repeat(1, 1, 1).unsqueeze(3)
img = F.pad(img, [0, 0, pad_w, pad_h], 'reflect')

convY = F.conv2d(img.float(), gy, stride=1)
convX = F.conv2d(convY, gx, stride=1)

To notice that the dimension of the gaussian filters change based on the value of the sigma chosen (this will be an hyperparameter to optimize later). Some examples are 47,5,4,…
My problems here are:


The final output of the convolution (convX) has the shape that depends on the filters shape. What I want is an image with the same shape of the original image (512,512). This is the output of the scipy convolve method, but not the one of the PyTorch convolution.


Will I be able to run this on the GPU building a Convolutional layer?