Python 运行时错误:输入类型(torch.cuda.FloatTensor)和权重类型(torch.cuda.HalfTensor)应该相同

Python 运行时错误:输入类型(torch.cuda.FloatTensor)和权重类型(torch.cuda.HalfTensor)应该相同,python,pytorch,yolo,yolov5,Python,Pytorch,Yolo,Yolov5,我刚刚开始学习YoloV5Pytorch版本,并且我能够构建一个模型,因此我尝试使用这个经过训练的模型实现一个用于实时预测的flask应用程序 负荷模型和预测类 class Model(object): def __init__(self, model): self.device = torch_utils.select_device() print(self.device) model = torch.load(model, map_

我刚刚开始学习YoloV5Pytorch版本,并且我能够构建一个模型,因此我尝试使用这个经过训练的模型实现一个用于实时预测的flask应用程序

负荷模型和预测类

class Model(object):

    def __init__(self, model):

        self.device = torch_utils.select_device()
        print(self.device)
        model = torch.load(model, map_location=self.device)['model']

        self.half = False and self.device.type != 'cpu'
        print('half = ' + str(self.half))

        if self.half:
            model.half()

        # model  = model.to(self.device).eval()
        model.cuda()

        self.loaded_model = model

    def predict(self, img):
        global session
        # img1 = torch.from_numpy(img).to(self.device)
        # img = img1.reshape(1, 3, 640, 640)
        img = img.half() if self.half else img.float()  # uint8 to fp16/32
        img /= 255.0  # 0 - 255 to 0.0 - 1.0
        print(img.ndimension())
        if img.ndimension() == 3:
            img = img.unsqueeze(0)
        print(self.loaded_model)
        img = img.to(self.device)
        # img = img.half()
        self.preds = self.loaded_model(img, augment=False)[0]
        print(self.predict())
        return self.preds
用于从相机或视频读取帧的相机类

model = Model("weights/best.pt")
class Camera(object):
    def __init__(self):
        # self.video = cv2.VideoCapture('facial_exp.mkv')
        self.video = cv2.VideoCapture(0)

    def __del__(self):
        self.video.release()

    def get_frame(self):
        _, fr = self.video.read()
        loader = transforms.Compose([transforms.ToTensor()])


        image = cv2.resize(fr, (640, 640), interpolation=cv2.INTER_AREA)
        input_im = image.reshape(1, 640, 640, 3)

        pil_im = Image.fromarray(fr)
        image = loader(pil_im).float()
        # image = Variable(image, requires_grad=True)
        image = image.unsqueeze(0)

        pred = model.predict(input_im)
        pred = model.predict(image)
        print(pred)

        _, jpeg = cv2.imencode('.jpg', fr)
        return jpeg.tobytes()
一些被评论的行是我尝试过的方式,但一直都是下面的行

self.preds=self.loaded\u模型(img,augment=False)[0]
抛出以下错误

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

任何解决此错误的想法或指导,谢谢。

请将标签垃圾邮件控制在最低限度。这显然不是一个CUDA编程问题,不应该被标记为一个问题。我已经删除了标签,希望它不会被重新添加