要渲染形状为27x35的图像,请使用:
random_image = []
for x in range(1 , 946):
random_image.append(random.randint(0 , 255))
random_image_arr = np.array(random_image)
matplotlib.pyplot.imshow(random_image_arr.reshape(27 , 35))
这将产生:
然后,我尝试使用来对图像应用卷积torch.nn.Conv2d:
conv2 = torch.nn.Conv2d(3, 18, kernel_size=3, stride=1, padding=1)
image_d = np.asarray(random_image_arr.reshape(27 , 35))
conv2(torch.from_numpy(image_d))
但这显示错误:
~/.local/lib/python3.6/site-packages/torch/nn/modules/conv.py in forward(self, input)
299 def forward(self, input):
300 return F.conv2d(input, self.weight, self.bias, self.stride,
--> 301 self.padding, self.dilation, self.groups)
302
303
RuntimeError: input has less dimensions than expected
输入的形状image_d是(27, 35)
Conv2d为了将卷积应用于图像,是否应该更改的参数?
更新。来自@McLawrence的答案是:
random_image = []
for x in range(1 , 946):
random_image.append(random.randint(0 , 255))
random_image_arr = np.array(random_image)
matplotlib.pyplot.imshow(random_image_arr.reshape(27 , 35))
这将渲染图像:
应用卷积运算:
conv2 = torch.nn.Conv2d(1, 18, kernel_size=3, stride=1, padding=1)
image_d = torch.FloatTensor(np.asarray(random_image_arr.reshape(1, 1, 27 , 35))).numpy()
fc = conv2(torch.from_numpy(image_d))
matplotlib.pyplot.imshow(fc [0] [0] .data.numpy())
渲染图像:
森栏
相关分类