要在形状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的回答中我得到了:
随机图像=[]
对于范围(1946)内的x:
random_image.append(random.randint(0,255))
随机图像arr=np.数组(随机图像)
matplotlib.pyplot.imshow(随机图像重组(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())
渲染图像: