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還有待探索)
補(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))