我正在尝试自定义加载一些带有标签的图像文件(JPG文件),并按照示例datasets.html" rel="nofollow noreferrer">here将它们输入PyTorch中的卷积神经网络(CNN)。然而,似乎仍然有no decent end-to-end tutorials。我看到的问题如下。RuntimeError: thnn_conv2d_forward is not implemented for type
torch.ByteTensor
我的Dataset如下所示。class ImageData(Dataset):
def __init__(self, width=256, height=256, transform=None):
self.width = width
self.height = height
self.transform = transform
y, x = get_images() #y is a list of labels, x is a list of file paths
self.y = y
self.x = x
def __getitem__(self, index):
img = Image.open(self.x[index]) # use pillow to open a file
img = img.resize((self.width, self.height)) # resize the file to 256x256
img = img.convert('RGB') #convert image to RGB channel
if self.transform is not None:
img = self.transform(img)
img = np.asarray(img).transpose(-1, 0, 1) # we have to change the dimensions from width x height x channel (WHC) to channel x width x height (CWH)
img = torch.from_numpy(np.asarray(img)) # create the image tensor
label = torch.from_numpy(np.asarray(self.y[index]).reshape([1, 1])) # create the label tensor
return img, label
def __len__(self):
return len(self.x)
CNN取自here,并被修改为处理NCWH(批处理x通道x宽度x高度),如下所示。class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 256, 256)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
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 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
学习循环也取自same tutorial,如下所示。for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(dataloader, 0):
# get the inputs
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
但是,上面提到的RuntimeError被抛出。关于我做错了什么有什么想法吗?
另外,我知道在不转置图像数据的情况下,它的形状是WHC,但NN模型要求它是CWH。问题是,如果我们从WHC改为CWH,那么如果我们在DataLoader上迭代,就不能再简单地绘制图像了。data = ImageData()
dataloader = DataLoader(data, batch_size=10, shuffle=True, num_workers=1)
imgs, labels = next(iter(dataloader))
plt.imshow(imgs.numpy()[0,:,:,:])
plt.show()
尝试执行此操作将引发以下错误。TypeError: Invalid dimensions for image data
对我来说,那个枕头给了你WHC,你可以用它来策划,但是PyTorch CNN想让CWH来处理,这是个麻烦。你知道如何一致或容易地不做这么多的转换,但能够绘图和输入数据到CNN吗?或是WHC与CWH的不匹配只是我们必须面对的问题?
在不转置图像的情况下,当将图像传送到CNN时,会抛出以下错误。RuntimeError: Given groups=1, weight[256, 3, 256, 256], so expected
input[10, 256, 256, 3] to have 3 channels, but got 256 channels
相反。