网上的解决方案意思是对的,但并没有给出相应的实际解决方法:
问题描述:
当使用ImageFolder方式构建数据集的时候:
train_data = torchvision.datasets.ImageFolder(train_path, transform=train_transform)
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=6)
pytorch会自己扫描train_path下的每一个文件夹(每类图片都位于其类别的文件夹下),并将每一个类映射成数值,比如有4类,类别标签就是[0,1,2,3]。
在进行二分类的时候的确是将标签映射成了[0,1],但是在进行4分类的时候,标签却映射成了[1,2,3,4],因此就会报错:
RuntimeError: CUDA error: device-side assert triggered
我们可以这样打印下相关的输出:
from torch.autograd import Variable
#load_fzdataset是自己定义的读取数据的函数,其返回的是DataLoader对象
train_data,test_data=load_fzdataset(8)
for epoch in range(2):
for i, data in enumerate(train_data):
# 将数据从 train_loader 中读出来,一次读取的样本数是8个
inputs, labels = data
# 将这些数据转换成Variable类型
inputs, labels = Variable(inputs), Variable(labels)
# 接下来就是跑模型的环节了,我们这里使用print来代替
print("epoch:", epoch, "的第" , i, "个inputs", inputs.data.size(), "labels", labels.data)
报错时的信息是:
epoch: 0 的第 0 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 2, 4, 4, 3, 4, 3, 1])
epoch: 0 的第 1 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 1, 1, 3, 4, 4, 4, 2])
epoch: 0 的第 2 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 2, 2, 4, 4, 4, 3, 3])
epoch: 0 的第 3 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 3, 4, 1, 2, 1, 2, 1])
epoch: 0 的第 4 个inputs torch.Size([8, 3, 224, 224]) labels tensor([1, 1, 1, 1, 4, 4, 3, 1])
epoch: 0 的第 5 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 3, 4, 4, 4, 4, 1, 4])
epoch: 0 的第 6 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 4, 1, 1, 4, 2, 4, 1])
epoch: 0 的第 7 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 4, 4, 3, 4, 3, 4, 4])
epoch: 0 的第 8 个inputs torch.Size([6, 3, 224, 224]) labels tensor([1, 4, 4, 1, 2, 1])
epoch: 1 的第 0 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 4, 3, 4, 4, 4, 4, 4])
epoch: 1 的第 1 个inputs torch.Size([8, 3, 224, 224]) labels tensor([2, 4, 1, 1, 4, 4, 2, 4])
epoch: 1 的第 2 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 4, 2, 1, 1, 4, 4, 3])
epoch: 1 的第 3 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 3, 1, 1, 1, 3, 4, 1])
epoch: 1 的第 4 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 4, 2, 4, 1, 1, 4, 1])
epoch: 1 的第 5 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 4, 1, 2, 4, 3, 4, 1])
epoch: 1 的第 6 个inputs torch.Size([8, 3, 224, 224]) labels tensor([4, 2, 4, 1, 3, 4, 4, 4])
epoch: 1 的第 7 个inputs torch.Size([8, 3, 224, 224]) labels tensor([1, 1, 2, 4, 1, 4, 4, 4])
epoch: 1 的第 8 个inputs torch.Size([6, 3, 224, 224]) labels tensor([2, 1, 3, 3, 4, 4])
我们只需要这么修改就行了:
from torch.autograd import Variable
#load_fzdataset是自己定义的读取数据的函数,其返回的是DataLoader对象
train_data,test_data=load_fzdataset(8)
for epoch in range(2):
for i, data in enumerate(train_data):
# 将数据从 train_loader 中读出来,一次读取的样本数是8个
inputs, labels = data
# 将这些数据转换成Variable类型
inputs, labels = Variable(inputs), Variable(labels)-1
# 接下来就是跑模型的环节了,我们这里使用print来代替
print("epoch:", epoch, "的第" , i, "个inputs", inputs.data.size(), "labels", labels.data)
输出:
epoch: 0 的第 0 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 1, 0, 3, 2, 1, 3, 2])
epoch: 0 的第 1 个inputs torch.Size([8, 3, 224, 224]) labels tensor([1, 3, 3, 3, 3, 3, 2, 2])
epoch: 0 的第 2 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 3, 0, 0, 3, 2, 1, 3])
epoch: 0 的第 3 个inputs torch.Size([8, 3, 224, 224]) labels tensor([0, 3, 3, 0, 0, 3, 2, 1])
epoch: 0 的第 4 个inputs torch.Size([8, 3, 224, 224]) labels tensor([2, 0, 1, 0, 3, 0, 0, 2])
epoch: 0 的第 5 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 3, 0, 0, 0, 3, 3, 3])
epoch: 0 的第 6 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 3, 0, 3, 3, 3, 0, 2])
epoch: 0 的第 7 个inputs torch.Size([8, 3, 224, 224]) labels tensor([0, 3, 3, 2, 3, 3, 0, 0])
epoch: 0 的第 8 个inputs torch.Size([6, 3, 224, 224]) labels tensor([3, 3, 3, 1, 2, 1])
epoch: 1 的第 0 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 1, 0, 3, 2, 1, 3, 3])
epoch: 1 的第 1 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 1, 2, 1, 0, 3, 1, 0])
epoch: 1 的第 2 个inputs torch.Size([8, 3, 224, 224]) labels tensor([0, 3, 3, 0, 0, 1, 2, 2])
epoch: 1 的第 3 个inputs torch.Size([8, 3, 224, 224]) labels tensor([0, 3, 3, 2, 3, 3, 0, 2])
epoch: 1 的第 4 个inputs torch.Size([8, 3, 224, 224]) labels tensor([1, 3, 2, 3, 2, 3, 3, 3])
epoch: 1 的第 5 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 0, 3, 3, 0, 3, 0, 3])
epoch: 1 的第 6 个inputs torch.Size([8, 3, 224, 224]) labels tensor([3, 0, 3, 0, 3, 2, 0, 3])
epoch: 1 的第 7 个inputs torch.Size([8, 3, 224, 224]) labels tensor([0, 3, 0, 3, 3, 3, 3, 3])
epoch: 1 的第 8 个inputs torch.Size([6, 3, 224, 224]) labels tensor([2, 1, 0, 3, 2, 0])