[英]How to actually apply a Conv2d filter in Pytorch
I'm new to Python and trying to do some manipulations with filters in PyTorch.我是 Python 的新手,并尝试对 PyTorch 中的过滤器进行一些操作。
I'm struggling re how to apply a Conv2d.我正在努力重新如何应用 Conv2d。 I've got the following code which creates a 3x3 moving average filter:我有以下代码创建一个 3x3 移动平均过滤器:
resized_image4D = np.reshape(image_noisy, (1, 1, image_noisy.shape[0], image_noisy.shape[1]))
t = torch.from_numpy(resized_image4D)
conv = torch.nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3, padding=1, bias=False)
conv.weight = torch.nn.Parameter(torch.ones((1,1,3, 3))/9.0)
Normally in NumPy I'd just call filtered_image = convolve2d(image, kernel)
, but I've not been able to figure out what the PyTorch equivalent is after days of searching.通常在 NumPy 中,我只是调用 filters_image filtered_image = convolve2d(image, kernel)
,但经过几天的搜索,我无法弄清楚 PyTorch 等效项是什么。
I think you are looking for torch.nn.functional.conv2d
.我认为您正在寻找torch.nn.functional.conv2d
。
Hence, your snippets becomes:因此,您的片段变为:
resized_image4D = np.reshape(image_noisy, (1, 1, image_noisy.shape[0], image_noisy.shape[1]))
t = torch.from_numpy(resized_image4D)
conv = torch.nn.functional.conv2d(in_channels=1, out_channels=1, kernel_size=3, padding=1, bias=False)
conv.weight = torch.nn.Parameter(torch.ones((1,1,3, 3))/9.0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.