pytorch图像和张量的相互转换_如何加载图像并将其转换为PyTorch的适当张量?
我正在嘗試自定義加載一些帶有標簽的圖像文件(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
相反。
總結
以上是生活随笔為你收集整理的pytorch图像和张量的相互转换_如何加载图像并将其转换为PyTorch的适当张量?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python卸载模块的方法汇总_Pyth
- 下一篇: git钩子放服务器_linux服务器布置