【模型推理时遇半精度问题】

问题描述

yolo模型推理时遇到half问题,报错如下

RuntimeError: "slow_conv2d_cpu" not implemented for 'Half'

追溯一下,问题在

    def forward_fuse(self, x):
        return self.act(self.conv(x))	# 这里
# 往前找
        for m in self.model:
            if m.f != -1:  # if not from previous layer
                x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f]  # from earlier layers
            if profile:
                self._profile_one_layer(m, x, dt)
            x = m(x)  # run	# 这里

#再往前

    def forward(self, x, augment=False, profile=False, visualize=False):
        if augment:
            return self._forward_augment(x)  # augmented inference, None
        return self._forward_once(x, profile, visualize)  # single-scale inference, train	#这里

#还不是,再找

        if self.pt:  # PyTorch
            y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im) 	# 这里就是答案       

这里其他人的解决方式是这样

im = im.half() if half else im.float()

因为数据的类型设置为half(半精度浮点数,能加快计算速度),但是half只有GPU支持。
遇到新错误

RuntimeError: Input type (float) and bias type (struct c10::Half) should be the same

这里基本可以确定不仅是数据精度问题。
最后可以发现是模型加载时,比如.pt格式的模型,会默认为.half(),这里改下就好了

        if pt:  # PyTorch
            model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
            stride = max(int(model.stride.max()), 32)  # model stride
            names = model.module.names if hasattr(model, 'module') else model.names  # get class names
            fp16 = device.type != 'cpu'  # half precision only supported on CUDA	# 新增这句判断
            model.half() if fp16 else model.float()
            self.model = model  # explicitly assign for to(), cpu(), cuda(), half()
        

这里仅判断模型加载是不是使用cpu,如果是gpu则不起作用,成功解决问题。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值