解決ToPILImage時出現(xiàn)維度報錯問題pic should be 2/3 dimensional. Got 4 dimensions.
ToPILImage時出現(xiàn)維度報錯pic should be 2/3 dimensional. Got 4 dimensions.
主要原因是在數(shù)據(jù)集加載過程中加入了batch_size,將tensor變?yōu)榱怂木S。
print(img.shape) img1 = img[0] print(img1.shape)
直接將其轉(zhuǎn)為三維即可
PyTorch中常見報錯
本部分介紹一些PyTorch中常見的報錯信息及其解決方法
報錯1:
ValueError: num_samples should be a positive integer value, but got num_samples=0
可能的原因:傳入的Dataset中的len(self.data_info)==0,即傳入該dataloader的dataset里沒有數(shù)據(jù)
解決方法:
- 檢查dataset中的路徑,路徑不對,讀取不到數(shù)據(jù)。
- 檢查Dataset的__len__()函數(shù)為何輸出為零
報錯2:
TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>
可能的原因:當前操作需要PIL Image或ndarray數(shù)據(jù)類型,但傳入了Tensor
解決
- 檢查transform中是否存在兩次ToTensor()方法
- 檢查transform中每一個操作的數(shù)據(jù)類型變化
報錯3:
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 93 and 89 in dimension 1 at /Users/soumith/code/builder/wheel/pytorch-src/aten/src/TH/generic/THTensorMath.cpp:3616
可能的原因:dataloader的__getitem__函數(shù)中,返回的圖片形狀不一致,導致無法stack
解決方法:檢查__getitem__函數(shù)中的操作
報錯4:
# 通道數(shù)不匹配
conv: RuntimeError: Given groups=1, weight of size 6 1 5 5, expected input[16, 3, 32, 32] to have 1 channels, but got 3 channels instead
# 維度不匹配
linear: RuntimeError: size mismatch, m1: [16 x 576], m2: [400 x 120] at ../aten/src/TH/generic/THTensorMath.cpp:752
可能的原因:網(wǎng)絡層輸入數(shù)據(jù)與網(wǎng)絡的參數(shù)不匹配
解決方法:
- 檢查對應網(wǎng)絡層前后定義是否有誤
- 檢查輸入數(shù)據(jù)shape
報錯5:
AttributeError: 'DataParallel' object has no attribute 'linear'
可能的原因:并行運算時,模型被dataparallel包裝,所有module都增加一個屬性 module.
因此需要通過 net.module.linear調(diào)用
解決方法:
網(wǎng)絡層前加入module.
報錯6:
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
可能的原因:gpu訓練的模型保存后,在無gpu設備上無法直接加載
解決方法:
需要設置map_location=“cpu”
報錯7:
AttributeError: Can't get attribute 'FooNet2' on <module '__main__' from '
可能的原因:保存的網(wǎng)絡模型在當前python腳本中沒有定義
解決方法:
提前定義該類
報錯8:
這個錯誤經(jīng)常在交叉熵損失函數(shù)中碰到
RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at ../aten/src/THNN/generic/ClassNLLCriterion.c:94
可能的原因:
標簽數(shù)大于等于類別數(shù)量,即不滿足 cur_target < n_classes,通常是因為標簽從1開始而不是從0開始
解決方法:
修改label,從0開始,例如:10分類的標簽取值應該是0-9
報錯9:
RuntimeError: expected device cuda:0 and dtype Long but got device cpu and dtype Long
可能的原因:需計算的兩個數(shù)據(jù)不在同一個設備上
解決方法:
采用to
函數(shù)將數(shù)據(jù)遷移到同一個設備上
報錯10:
Expected object of backend CPU but got backend CUDA for argument #2 'weight'
可能的原因:張量的to
函數(shù)非原地操作,轉(zhuǎn)換數(shù)據(jù)設備未賦值,即inputs.to("CUDA")
,此時,數(shù)據(jù)仍然在CPU上,未轉(zhuǎn)換成功
解決辦法:
inputs = inputs.to(device)
報錯11:
RuntimeError: DataLoader worker (pid 27) is killed by signal: Killed. Details are lost
due to multiprocessing. Rerunning with num_workers=0 may give better error trace.
可能原因:內(nèi)存不夠(不是gpu顯存,是內(nèi)存)
解決方法:申請更大內(nèi)存
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Flask框架運用Ajax實現(xiàn)輪詢動態(tài)繪圖
Ajax是異步JavaScript和XML可用于前后端交互,本文將通過Ajax輪詢獲取后端的數(shù)據(jù),前臺使用echart繪圖庫進行圖形的生成與展示,最后實現(xiàn)動態(tài)監(jiān)控內(nèi)存利用率的這個功能,需要的可以參考一下2022-11-11python pycurl驗證basic和digest認證的方法
這篇文章主要介紹了python pycurl驗證basic和digest認證的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05numpy庫ndarray多維數(shù)組的維度變換方法(reshape、resize、swapaxes、flatten)
這篇文章主要介紹了numpy庫ndarray多維數(shù)組的維度變換方法(reshape、resize、swapaxes、flatten),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04Python confluent kafka客戶端配置kerberos認證流程詳解
這篇文章主要介紹了Python confluent kafka客戶端配置kerberos認證流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10