微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TypeError:conv2d:参数'input'位置1必须为Tensor,而不是int

如何解决TypeError:conv2d:参数'input'位置1必须为Tensor,而不是int

我正在用Pytorch制作CNN,但遇到此错误 这是我使用的模型,它是一个非常简单的CNN。我还尝试使用预先训练的模型,并且也抛出了相同的错误

class resnet18(nn.Module):
    def __init__(self):
        super(resnet18,self).__init__()
        self.network = nn.Sequential(
            nn.Conv2d(3,16,kernel_size=(3,3),stride=1),nn.MaxPool2d(kernel_size=(2,2)),nn.Batchnorm2d(16),nn.ReLU(),nn.Conv2d(16,32,nn.Batchnorm2d(32),nn.Flatten(),nn.Linear(16*16*32,64),nn.Linear(64,32),nn.Linear(32,7),)
        
    def forward(self,xb):
        return self.network(xb)

这是我的数据集类:

class CurrencyDataset(Dataset):
    def __init__(self,data_dir,transforms=None):
        self.data_dir = data_dir
        self.transforms = transforms
        
    def __len__(self):
        return len(self.data_dir)
        
    def __getitem__(self,idx):
        img,label = ImageFolder(self.data_dir,transform=self.transforms)[idx]
        return img,label

和训练循环:

def train(model,optimizer,DataLoader,EPOCHS):
    
    model.train()
    final_loss = 0
    for _ in range(EPOCHS):
        for img,label in tqdm(enumerate(DataLoader)):
            optimizer.zero_grad()
            out = model(img)
            loss = F.cross_entropy(out,label)
            
            loss.backward()
            optimizer.step()
            final_loss+=loss.item()
    return final_loss/len(DataLoader)

这是一个完整的错误,我也使用了ToTensor并调整了转换大小,但没有帮助:

0it [00:00,?it/s]
Traceback (most recent call last):
  File "c:/Users/LENOVO/Desktop/Dev/Currency-Classification/Using Pytorch/engine.py",line 52,in <module>
    train(model,train_dl,20)
  File "c:/Users/LENOVO/Desktop/Dev/Currency-Classification/Using Pytorch/engine.py",line 44,in train
    out = model(img)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\python36\lib\site-packages\torch\nn\modules\module.py",line 550,in __call__
    result = self.forward(*input,**kwargs)
  File "c:\Users\LENOVO\Desktop\Dev\Currency-Classification\Using Pytorch\model.py",line 29,in forward
    return self.network(xb)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\python36\lib\site-packages\torch\nn\modules\module.py",**kwargs)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\python36\lib\site-packages\torch\nn\modules\container.py",line 100,in forward
    input = module(input)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\python36\lib\site-packages\torch\nn\modules\module.py",**kwargs)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\python36\lib\site-packages\torch\nn\modules\conv.py",line 349,in forward
    return self._conv_forward(input,self.weight)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\python36\lib\site-packages\torch\nn\modules\conv.py",line 346,in _conv_forward
    self.padding,self.dilation,self.groups)
TypeError: conv2d(): argument 'input' (position 1) must be Tensor,not int

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。