我是 pytorch 的新手,在 AlexNet 中遇到了频道问题。我将它用于“gta san andreas 自驾车”项目,我从具有一个通道的黑白图像中收集数据集并尝试使用脚本训练 AlexNet:
from AlexNetPytorch import*
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
import torch.utils.data
import numpy as np
import torch
from IPython.core.debugger import set_trace
AlexNet = AlexNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(AlexNet.parameters(), lr=0.001, momentum=0.9)
all_data = np.load('training_data.npy')
inputs= all_data[:,0]
labels= all_data[:,1]
inputs_tensors = torch.stack([torch.Tensor(i) for i in inputs])
labels_tensors = torch.stack([torch.Tensor(i) for i in labels])
data_set = torch.utils.data.TensorDataset(inputs_tensors,labels_tensors)
data_loader = torch.utils.data.DataLoader(data_set, batch_size=3,shuffle=True, num_workers=2)
if __name__ == '__main__':
for epoch in range(8):
runing_loss = 0.0
for i,data in enumerate(data_loader , 0):
inputs= data[0]
inputs = torch.FloatTensor(inputs)
labels= data[1]
labels = torch.FloatTensor(labels)
optimizer.zero_grad()
# set_trace()
inputs = torch.unsqueeze(inputs, 1)
outputs = AlexNet(inputs)
loss = criterion(outputs , labels)
loss.backward()
optimizer.step()
runing_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')
我正在使用来自链接的 AlexNet: https ://github.com/pytorch/vision/blob/master/torchvision/models/alexnet.py
但是将第 18 行从:
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2)
至 :
nn.Conv2d(1, 64, kernel_size=11, stride=4, padding=2)
因为我在训练图像中只使用一个通道,但是我收到了这个错误:
File "training_script.py", line 44, in <module>
outputs = AlexNet(inputs)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\Mukhtar\Documents\AI_projects\gta\AlexNetPytorch.py", line 34, in forward
x = self.features(x)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torch\nn\modules\container.py", line 91, in forward
input = module(input)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torch\nn\modules\pooling.py", line 142, in forward
self.return_indices)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torch\nn\functional.py", line 396, in max_pool2d
ret = torch._C._nn.max_pool2d_with_indices(input, kernel_size, stride, padding, dilation, ceil_mode)
RuntimeError: Given input size: (256x1x1). Calculated output size: (256x0x0). Output size is too small at c:\programdata\miniconda3\conda-bld\pytorch-cpu_1532499824793\work\aten\src\thnn\generic/SpatialDilatedMaxPooling.c:67
我不知道出了什么问题,像这样改变通道大小是不是错了,如果错了,请引导我使用一个通道的神经网络,正如我所说我是 pytorch 的新手和我不想自己写nn。