1. 程式人生 > 實用技巧 >完美解決-RuntimeError: CUDA error: device-side assert triggered

完美解決-RuntimeError: CUDA error: device-side assert triggered

網上的解決方案意思是對的,但並沒有給出相應的實際解決方法:

問題描述:

當使用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 中讀出來,一次讀取的樣本數是32個 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 中讀出來,一次讀取的樣本數是32個
        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])