【发布时间】:2019-05-20 00:16:58
【问题描述】:
我制作了一个卷积神经网络,我希望它获取输入图片和输出图片,但是当我将图片转换为张量时,它们的维度错误:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [20, 3, 5, 5], but got 3-dimensional input of size [900, 1440, 3] instead
如何更改图片的尺寸?为什么需要改变? 以及如何使输出成为图片? 我尝试使用
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
对 img 进行规范化,但它没有改变尺寸。 这是我的神经网络
def __init__(self):
super(Net, self).__init__()
torch.nn.Module.dump_patches = True
self.conv1 = nn.Conv2d(3, 20, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(20, 16, 5)
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 16*5*5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 )
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
我在这里获取图像并将其放入列表中:
for i in range(4):
l.append(ImageGrab.grab())
这是将 img 转换为张量的代码
k=torch.from_numpy(np.asarray(l[1],dtype="int32" ))
【问题讨论】:
-
torch.nn仅支持小批量。输入的格式应为(batch_size、channels、height、width)。您似乎缺少批处理维度。添加.unsqueeze(0)以添加假批量维度。 -
我改变了它,现在我得到了错误
RuntimeError: Given groups=1, weight of size [20, 3, 5, 5], expected input[1, 900, 1440, 3] to have 3 channels, but got 900 channels instead -
我在解压之前通过转换来修复它,但现在我得到了错误 ` x = x.view(-1, 16 * 5 *5) RuntimeError: shape '[-1, 400]' is invalid对于大小为 1268064` 的输入,我猜这是 convelution 中的一个错误,所以谢谢你帮助我
标签: python-3.x multidimensional-array python-imaging-library conv-neural-network pytorch