So I am trying to do filtering via Conv2d
, I define my kernel and then change the weights of Conv2d and thought that should be it, but the results does not match
For a toy example, I define a 3x3
kernel : [[0, 0, 0],[0, 1, 0],[0, 0, 0]]
, the output results of using this kernel should basically give me identical results as an input. but I am not sure why it is not happening like that:
Here is the whole toy code:
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch
Input = torch.rand(1,2,6,6)
Guass = nn.Conv2d(Input.size(1), Input.size(1), 3, padding = 1)
KernelGauss = torch.ones(3,3)*0.
KernelGauss[1,1] = 1.
KernelGaussExpand = KernelGauss.expand(Guass.weight.size())
Guass.weight = torch.nn.Parameter(KernelGaussExpand, requires_grad=False)
OutPut = Guass(Input)
print('KernelGauss: {}' .format(KernelGauss))
print('Input: {}' .format(Input))
print('OutPut: {}' .format(OutPut))
KernelGauss: tensor([[0., 0., 0.],
[0., 1., 0.],
[0., 0., 0.]])
Input: tensor([[[[0.1694, 0.0436, 0.7638, 0.4422, 0.6436, 0.1260],
[0.2136, 0.2536, 0.1094, 0.6026, 0.2102, 0.8117],
[0.6218, 0.5365, 0.9977, 0.4297, 0.4261, 0.7260],
[0.8062, 0.3274, 0.4722, 0.2773, 0.4304, 0.7448],
[0.6605, 0.8976, 0.7353, 0.7233, 0.9757, 0.2564],
[0.6651, 0.2333, 0.2524, 0.8768, 0.0754, 0.4961]],
[[0.8020, 0.6601, 0.4271, 0.3141, 0.6387, 0.8583],
[0.3664, 0.9589, 0.7841, 0.6330, 0.0851, 0.4114],
[0.4414, 0.9218, 0.1864, 0.5323, 0.4601, 0.4648],
[0.0841, 0.8478, 0.8975, 0.1383, 0.0706, 0.0810],
[0.8904, 0.7208, 0.2150, 0.0034, 0.4385, 0.7455],
[0.8900, 0.6735, 0.5207, 0.4046, 0.6463, 0.9222]]]])
OutPut: tensor([[[[1.1774, 0.9097, 1.3969, 0.9623, 1.4883, 1.1903],
[0.7860, 1.4185, 1.0995, 1.4416, 0.5013, 1.4290],
[1.2692, 1.6643, 1.3901, 1.1680, 1.0922, 1.3967],
[1.0962, 1.3812, 1.5756, 0.6216, 0.7070, 1.0318],
[1.7568, 1.8244, 1.1563, 0.9327, 1.6201, 1.2078],
[1.7611, 1.1128, 0.9791, 1.4874, 0.9277, 1.6242]],
[[1.0561, 0.7884, 1.2756, 0.8410, 1.3670, 1.0690],
[0.6647, 1.2972, 0.9782, 1.3203, 0.3800, 1.3077],
[1.1479, 1.5430, 1.2688, 1.0467, 0.9709, 1.2754],
[0.9749, 1.2599, 1.4543, 0.5003, 0.5857, 0.9105],
[1.6355, 1.7031, 1.0349, 0.8114, 1.4988, 1.0865],
[1.6398, 0.9915, 0.8578, 1.3661, 0.8064, 1.5029]]]],
grad_fn=<ThnnConv2DBackward>)