RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same error when inception_v3 model

问题描述 投票:0回答:0

我是 ComputerVision 的新手。我正在尝试使用 inception_v3 对二进制图像数据集进行图像分类。我看到以下错误

Epoch: 1 
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-110-822a36ffbce7> in <module>
     12         total+=labels.size(0)
     13         images=images.cuda()
---> 14         output,aux1,aux2=model(images)
     15         aux1_loss=criterion(aux1, labels)
     16         aux2_loss=criterion(aux2, labels)

7 frames
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    457                             weight, bias, self.stride,
    458                             _pair(0), self.dilation, self.groups)
--> 459         return F.conv2d(input, weight, bias, self.stride,
    460                         self.padding, self.dilation, self.groups)
    461 

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

我试过将图像更新为image.cuda(),但到目前为止还没有成功

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
for epoch in range(n_epochs):
    print("Epoch: %d "%(epoch+1))
    model.train()
    running_loss = 0.0
    running_correct = 0.0
    total=0
    for i, data in enumerate(train_loader):
        images,labels=data
        images=images.to(device)
        labels=labels.to(device)
        total+=labels.size(0)
        output,aux1,aux2=model(images)
        aux1_loss=criterion(aux1, labels)
        aux2_loss=criterion(aux2, labels)
        output_loss = criterion(output, labels)
        loss = output_loss + 0.3*(aux1_loss + aux2_loss)
        running_loss += loss.item() * output.size(0)
        pred = output.max(1, keepdim=True)[1]
        running_correct+=pred.eq(labels.view_as(pred)).sum().item()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        traning_accuracy = 100. * running_correct / len(train_loader.dataset)`
python-3.x pytorch computer-vision conv-neural-network
© www.soinside.com 2019 - 2024. All rights reserved.