TypeError: conv2d() received an invalid combination of arguments
生活随笔
收集整理的這篇文章主要介紹了
TypeError: conv2d() received an invalid combination of arguments
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
完整報錯:
TypeError: conv2d() received an invalid combination of arguments - got (str, Parameter, NoneType, tuple, tuple, tuple, int), but expected one of:* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)didn't match because some of the arguments have invalid types: (str, Parameter, NoneType, tuple, tuple, tuple, int)* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)didn't match because some of the arguments have invalid types: (str, Parameter, NoneType, tuple, tuple, tuple, int)場景:在模型訓練完用模型做預測的時候報錯,預測代碼:
def predict_process(root_path, transform):imgs = os.listdir(root_path)res=[]label_arry=['ant','bee']for img in imgs:img_path = root_path+'/'+imgprint(img_path)img_item = Image.open(img_path)img_item = transform(img_item)# 將輸入圖片的shape修改成符合輸入條件的shapeimg_item = torch.reshape(img_item, (1,3, 224, 224))img_item=img_item.to(device)print(type(img_item))mymodel.eval()with torch.no_grad():output = mymodel(img)ret,index=torch.max(output,1)index=index.datares.append(label_arry[index])return res原因分析:在卷積的時候接收到了無效的參數,所以要么就是輸入的參數有問題,要么就是卷積層有問題,但是這里的mymodel我用的是pytorch中自帶的resnet50所以卷積層肯定是沒有問題的,所以問題就定位在我輸入圖片的格式,因為我在訓練的時候是沒有報錯的,所以我只需要保證我在做預測的時候輸入圖片的格式和我訓練的時候輸入的圖片格式是一樣的就可以了,然后我把做預測的時候的圖片img_item的shape和自身都輸出了一遍,print(img_item.shape),print(img_item)去和訓練的時候的圖片做對比,發現是一模一樣的,于是我開始瘋狂改預測時輸入的img_item的格式,希望能試出看看什么格式是ok的,就這樣搞了一天還是不行,后來發現我搞了半天的img_item,最后輸入模型做預測的時候用的是img,output = mymodel(img)img是一個str類型的變量"xxx.png",所以我浪費了一天的生命,但如果有類似的問題以上的分析步驟是沒問的,問題一般就出在輸入圖片和卷積層上。
總結
以上是生活随笔為你收集整理的TypeError: conv2d() received an invalid combination of arguments的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英语语法——句子成分总结
- 下一篇: 使用Scratch制作打弹球游戏(一)弹