无法让火炬模型在 GPU 上运行,即使它是可识别的设备

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

我正在使用 resnet18 进行接地图像分割,但是当我将输入传递给模型时,我收到此火炬错误:

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor

我从这里得到了权重:

'https://download.pytorch.org/models/resnet18-5c106cde.pth'

我假设权重是在 nvidia 硬件上训练的,因为为什么不这样,这样就忽略了我的输入张量在我的 CPU (r7 1600x) 上处理,但这很奇怪,因为我想确保所有东西都在运行我的 nvidia GPU (rtx 3060) 已被识别 (

torch.cuda.is_available() == True
)。

这是我认为相关的代码


class Masking:
    def __init__(
            self, 
            device_handle:Provider='cuda:0',  #<--------------------------
            classifier:Module=None,
            face_parser:Module=None,
            num_classes=19
    ) -> None:
        self.device = device(device_handle)
        self.classifier = classifier
        self.face_parser = face_parser
        self.num_classes = num_classes
        self.provided_image = None 

        print("is cuda available:  ", torch.cuda.is_available())  # True

    def startup_model(self):
        self.classifier.to(self.device)  #<-----------------------------
        self.classifier.load_state_dict(self.face_parser)
        self.classifier.eval()

    def preprocess_image(self, image_path):
        to_tensor = transforms.Compose([ 
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
        ])

        self.provided_image = Image.open(image_path)
        w, h = self.provided_image.size
        interpolated_image = self.provided_image.resize((w, h), Image.BILINEAR)
        composed_image = to_tensor(interpolated_image)
        expanded_tensor = unsqueeze(composed_image, 0)
        expanded_tensor.to(self.device) #<--------------------------------
        #expanded_tensor.to("cuda:0") # doesn't work either
        #expanded_tensor.cuda() # no dice
        out = self.classifier(expanded_tensor)[0]  # <<<<< ERROR

        return out


    masker = Masking(
        'cuda:0',
        BiSeNet(n_classes=19),
        load('path/to/my/models/79999_iter.pth', device('cuda:0')) #<---------------------
    )

....然后我运行上面定义的方法等

python pytorch image-segmentation resnet
1个回答
0
投票

nn.Module.to
不同,
torch.Tensor.to
无法到位。
因此您必须将结果分配回变量:

expanded_tensor = expanded_tensor.to(self.device)
© www.soinside.com 2019 - 2024. All rights reserved.