Pytorch采坑記~~持續(xù)更新中......

1.nn.Conv2D()輸入?yún)?shù)數(shù)據(jù)格式不對

報錯:TypeError: new() received an invalid combination of arguments - got (float, int, int, int), but expected one of:
完整報錯

  File "G:/python/project/model/A2net.py", line 36, in <module>
    model = A2Block(64)
  File "G:/python/project/model/A2net.py", line 15, in __init__
    self.dimension_reduction = nn.Conv2d(in_channels=inplanes, out_channels=inplanes/2, kernel_size=1, stride=1)
  File "C:\Users\MSY\Anaconda3\lib\site-packages\torch\nn\modules\conv.py", line 297, in __init__
    False, _pair(0), groups, bias)
  File "C:\Users\MSY\Anaconda3\lib\site-packages\torch\nn\modules\conv.py", line 33, in __init__
    out_channels, in_channels // groups, *kernel_size))
TypeError: new() received an invalid combination of arguments - got (float, int, int, int), but expected one of:
 * (torch.device device)
 * (torch.Storage storage)
 * (Tensor other)
 * (tuple of ints size, torch.device device)
 * (object data, torch.device device)

問題定位:定位到報錯行為:

self.dimension_reduction = nn.Conv2d(in_channels=inplanes, out_channels=inplanes/2, kernel_size=1, stride=1)

問題分析: 根據(jù)報錯信息丸卷,是說本行代碼包含有float的數(shù)據(jù)類型向抢,通過分析可以看到糊余,只有inplanes/2可能是float類型典徘,由此想到在python3中n/2是帶有小數(shù)點(diǎn)的润樱,應(yīng)該為n//2為整數(shù)贵扰。(由于一個粗心瞭恰,報錯一個如此尷尬的bug)
問題解決:將輸出通道數(shù)inplanes/2改為inplanes//2完美解決腮郊。


2.make.sh 編譯NMS遇到問題

報錯:OSError: The CUDA lib64 path could not be located in /usr/lib64
完整報錯

Traceback (most recent call last):
  File "build.py", line 59, in <module>
    CUDA = locate_cuda()
  File "build.py", line 54, in locate_cuda
    raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
OSError: The CUDA lib64 path could not be located in /usr/lib64

問題定位:打開build.py(某些項(xiàng)目為setup.py)找到

cudaconfig = {'home': home, 'nvcc': nvcc,
                  'include': pjoin(home, 'include'),
                  'lib64': pjoin(home, 'lib64')}

問題分析:lib引用的問題
問題解決:將home, 'lib64'中的lib64改為lib完美解決


3.one of the variables needed for gradient computation has been modified by an inplace operation

報錯:one of the variables needed for gradient computation has been modified by an inplace operation
完整報錯

Traceback (most recent call last):
  File "train_test.py", line 454, in <module>
    train()
  File "train_test.py", line 327, in train
    loss.backward()
  File "/home/miao/anaconda3/lib/python3.6/site-packages/torch/tensor.py", line 93, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/home/miao/anaconda3/lib/python3.6/site-packages/torch/autograd/__init__.py", line 90, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

問題定位:這個bug報錯并沒有報明顯的錯誤位置是最難過的,wwwwww~~~~~
問題分析:此問題是在測試運(yùn)行網(wǎng)上關(guān)于一版Pytorch版本的SSD代碼時渺鹦,出現(xiàn)的扰法,根據(jù)網(wǎng)上的錯誤解釋,應(yīng)該時Pytorch0.4版本和0.3版本的某些不一致造成的毅厚。該問題的常用解決方法時:
1:如果使用的是pytorch0.4.0版本塞颁,回退到pytorch0.3.0版本
2:如果有inreplace參數(shù),設(shè)為False
3:報錯原因是pytorch0.4.0版本后tensor不支持inplace操作了,所以把所有inplace錯作去掉祠锣。
后在博客modified by an inplace operation中似乎找到了合適的答案.簡單來說:x += 1 這種改成 x = x+1 原因:x+=1的值會直接在原值上面做更新酷窥,是inplace=True的情況,而后一種是先讓x+1然后賦值給x,屬于inplace=False
但是由于自己的代碼較多伴网,開始很難具體定位到哪個錯誤的位置蓬推,后來使用Beyond Compare(一款很棒的軟件,強(qiáng)推~~~)與網(wǎng)上一版正確的代碼比較澡腾,發(fā)現(xiàn)了錯誤沸伏。

x /= norm  #(原本的錯誤代碼)

In-place的具體解釋可以參考。pytorch 學(xué)習(xí)筆記(二十二):關(guān)于 inplace operation
問題解決: 將x /= norm #改為x = x / norm
后記:后來偶然發(fā)現(xiàn)动分,Pycharm原來有全局搜索的功能毅糟,上述也說大致的問題由于 /= 操作符產(chǎn)生,但是代碼過多刺啦,無法有效的找到 /=代碼所在emmmmm留特,可以使用全局搜索Pycharm中按快捷鍵Ctrl + Shift + F或從從菜單Edit-》Find-》Find in Path進(jìn)入全局查找界面,輸入 /= 即可找到大致所在玛瘸,VS code也可蜕青,自行查找即可。(白白浪費(fèi)了那么多自己查找的時間糊渊,哇的一聲~~~右核,對Pycharm還有待探索)

Pycharm全局搜索

補(bǔ)充:后來運(yùn)行另外一個代碼的時候,發(fā)現(xiàn)報了相同的錯誤渺绒,后來找到的錯誤為:

x.unsqueeze_(1) 改為:x = x.unsqueeze(1) 

4.Fan in and fan out can not be computed for tensor with less than 2 dimensions

報錯: Fan in and fan out can not be computed for tensor with less than 2 dimensions
完整報錯

 File "train_test_RFB.py", line 143, in <module>
    net.extras.apply(weights_init)
  File "/home/miao/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 240, in apply
    module.apply(fn)
  File "/home/miao/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 240, in apply
    module.apply(fn)
  File "/home/miao/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 241, in apply
    fn(self)
  File "train_test_RFB.py", line 134, in weights_init
    init.kaiming_normal_(m.state_dict()[key], mode='fan_out')
  File "/home/miao/anaconda3/lib/python3.6/site-packages/torch/nn/init.py", line 323, in kaiming_normal_
    fan = _calculate_correct_fan(tensor, mode)
  File "/home/miao/anaconda3/lib/python3.6/site-packages/torch/nn/init.py", line 257, in _calculate_correct_fan
    fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
  File "/home/miao/anaconda3/lib/python3.6/site-packages/torch/nn/init.py", line 181, in _calculate_fan_in_and_fan_out
 raise ValueError("Fan in and fan out can not be computed for tensor with less than 2 dimensions")
ValueError: Fan in and fan out can not be computed for tensor with less than 2 dimensions

問題定位:

init.kaiming_normal_(m.state_dict()[key], mode='fan_out')

問題分析:根據(jù)報錯信息贺喝,可以知道,再使用init.kaiming_normal_()進(jìn)行初始化的時候宗兼,只能初始化不小于2的維度的tensor躏鱼,經(jīng)過分析得到,在常見的使用

 if 'conv' in key:
                    init.kaiming_normal_(m.state_dict()[key], mode='fan_out')

進(jìn)行初始化的過程中殷绍,問題出在定義的conv層染苛,回想自己的網(wǎng)絡(luò),在conv中的定義

self.conv = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
                                   nn.BatchNorm2d(inter_channels),
                                   nn.ReLU())

其中包含了BatchNorm層主到,而在 BatchNorm layer維度1 , 小于2茶行。'Fan in and fan out can not be computed for tensor with less than 2 dimensions'
問題解決: 將復(fù)合的conv拆開寫,或者改寫初始化


5.libpng error: Read Error

報錯:opencv2 報錯 libpng error: Read Error
完整報錯

libpng error: Read Error
Traceback (most recent call last):
  File "main.py", line 100, in <module>
    main(config)
  File "main.py", line 43, in main
    train.train()
  File "/home/msy/project/PoolNet-master/solver.py", line 84, in train
    for i, data_batch in enumerate(self.train_loader):
  File "/home/msy/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 637, in __next__
    return self._process_next_batch(batch)
  File "/home/msy/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 658, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
AttributeError: Traceback (most recent call last):
  File "/home/msy/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/msy/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
 File "/home/msy/project/PoolNet-master/dataset/dataset.py", line 27, in __getitem__
    sal_image = load_image(os.path.join(self.sal_root, im_name))
  File "/home/msy/project/PoolNet-master/dataset/dataset.py", line 77, in load_image
    if len(im.shape) != 3 or im.shape[2] != 3:
AttributeError: 'NoneType' object has no attribute 'shape'

問題定位

im = cv2.imread(name)
len(im.shape) != 3 or im.shape[2] != 3:

問題分析:圖片格式的問題登钥,有些圖片比如說原本是jpg的格式畔师,你后綴寫成了.png或者其他類似的操作,就可能會出現(xiàn)這個問題(還是不完全理解牧牢,歡迎補(bǔ)充解答)看锉。
問題解決

import cv2
import numpy as np
from PIL import Image
from PIL import ImageFile
import imghdr

ImageFile.LOAD_TRUNCATED_IMAGES = True
if imghdr.what(name) == "png":
    Image.open(name).convert("RGB").save(name)
img = np.array(Image.open(name))

參考
https://blog.csdn.net/andylei777/article/details/78095411
http://www.itdaan.com/blog/2016/11/22/d480f443ca62e56ddc47a7bed7cc85fd.html


6.TypeError: cannot assign 'torch.cuda.FloatTensor' as parameter 'edges' (torch.nn.Parameter or None expected)

報錯

TypeError: cannot assign 'torch.cuda.FloatTensor' as parameter 'edges' (torch.nn.Parameter or None expected)

問題定位

class Conv(nn.Module):
    def __init__(self, in_features, out_features, bias=False):
        super(Conv, self).__init__()  
  
        self.edges = Parameter(torch.Tensor(20, 20))
        self.sigmoid = nn.Sigmoid()
      
    def forward(self, input, adj):
        self.edges= self.sigmoid((self.edges))

報錯分析:根據(jù)報錯的原因是不能將torch.cuda.FloatTensor作為torch.nn.Parameter的結(jié)果類型姿锭,所以只能將torch.cuda.FloatTensor的結(jié)果賦值給torch.nn.Parameter變量的data屬性。
問題解決

self.edges.data= self.sigmoid((self.edges))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末伯铣,一起剝皮案震驚了整個濱河市艾凯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌懂傀,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蜡感,死亡現(xiàn)場離奇詭異蹬蚁,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)郑兴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門犀斋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人情连,你說我怎么就攤上這事叽粹。” “怎么了却舀?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵虫几,是天一觀的道長。 經(jīng)常有香客問我挽拔,道長辆脸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任螃诅,我火速辦了婚禮啡氢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘术裸。我一直安慰自己倘是,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布袭艺。 她就那樣靜靜地躺著搀崭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪匹表。 梳的紋絲不亂的頭發(fā)上门坷,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機(jī)與錄音袍镀,去河邊找鬼默蚌。 笑死,一個胖子當(dāng)著我的面吹牛苇羡,可吹牛的內(nèi)容都是我干的绸吸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼锦茁!你這毒婦竟也來了攘轩?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤码俩,失蹤者是張志新(化名)和其女友劉穎度帮,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體稿存,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡笨篷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了瓣履。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片率翅。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖袖迎,靈堂內(nèi)的尸體忽然破棺而出冕臭,到底是詐尸還是另有隱情,我是刑警寧澤燕锥,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布辜贵,位于F島的核電站,受9級特大地震影響脯宿,放射性物質(zhì)發(fā)生泄漏念颈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一连霉、第九天 我趴在偏房一處隱蔽的房頂上張望榴芳。 院中可真熱鬧,春花似錦跺撼、人聲如沸窟感。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽柿祈。三九已至,卻和暖如春哩至,著一層夾襖步出監(jiān)牢的瞬間躏嚎,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工菩貌, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留卢佣,地道東北人。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓箭阶,卻偏偏與公主長得像虚茶,于是被迫代替她去往敵國和親戈鲁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容