RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same
I just start to learn about the YOLO v5 PyTorch version and I was able to build a model, so then I tried to implement a flask application for real-time prediction using this trained model.
class for load model and predict
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

Camera class for reading frames from camera or video
model = Model(&quot;weights/best.pt&quot;)
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()

Some of the commented lines are the ways which I tried but in all times bellow line
self.preds = self.loaded_model(img, augment=False)[0]  throws below error
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same

any idea or guidance for solving this error thank you.

this error means: the input type is float32, the weight type(of your model) is float16.
for exsample, this code below runned:
model.half()  # so the weight type is float16
but this code below not runned:
img = img.half()  # so the input type is float32
please check your code.
for more information about 'half', you can refer to torch.Tensor.to() and torch.nn.Module.to()
