要渲染形状为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())渲染图像:
1 回答

森栏
TA贡献1810条经验 获得超5个赞
您的代码有两个问题:
首先,二维卷积中pytorch
被定义仅适用于四维张量。这在神经网络中使用很方便。第一维是批处理大小,而第二维是通道(例如RGB图像具有三个通道)。所以你必须像重塑你的张量
image_d = torch.FloatTensor(np.asarray(random_image_arr.reshape(1, 1, 27 , 35)))
FloatTensor
此处的值很重要,因为未在上定义卷积LongTensor
,如果您的numpy
数组仅包含int
s ,则将自动创建卷积。
其次,您创建了具有三个输入通道的卷积,而图像只有一个通道(灰度)。因此,您必须将卷积调整为:
conv2 = torch.nn.Conv2d(1, 18, kernel_size=3, stride=1, padding=1)
添加回答
举报
0/150
提交
取消