问题描述
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则不起作用,成功解决问题。