ACmix icon indicating copy to clipboard operation
ACmix copied to clipboard

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

Open Zzzz1zzz opened this issue 1 year ago • 2 comments

使用YOLOv7结合ACmix,出现如下报错:

`Traceback (most recent call last):
  File "/home/liu/桌面/zwx/YOLOv7-main/train.py", line 613, in <module>
    train(hyp, opt, device, tb_writer)
  File "/home/liu/桌面/zwx/YOLOv7-main/train.py", line 415, in train
    results, maps, times = test.test(data_dict,
  File "/home/liu/桌面/zwx/YOLOv7-main/test.py", line 110, in test
    out, train_out = model(img, augment=augment)  # inference and training outputs
  File "/home/liu/anaconda3/envs/yolo-torch2/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/liu/桌面/zwx/YOLOv7-main/models/yolo.py", line 320, in forward
    return self.forward_once(x, profile)  # single-scale inference, train
  File "/home/liu/桌面/zwx/YOLOv7-main/models/yolo.py", line 346, in forward_once
    x = m(x)  # run
  File "/home/liu/anaconda3/envs/yolo-torch2/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/liu/桌面/zwx/YOLOv7-main/models/common.py", line 530, in forward
    pe = self.conv_p(position(h, w, x.is_cuda))
  File "/home/liu/anaconda3/envs/yolo-torch2/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/liu/anaconda3/envs/yolo-torch2/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 463, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "/home/liu/anaconda3/envs/yolo-torch2/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 459, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Input type (float) and bias type (c10::Half) should be the `same`

Zzzz1zzz avatar Apr 24 '23 14:04 Zzzz1zzz

运行环境:pytorch2.0 python=3.8

Zzzz1zzz avatar Apr 24 '23 14:04 Zzzz1zzz

yolov7在训练的时候是混合精度训练,测试或者验证的时候用的是半精度推理,生成位置掩码position那个变量在验证的时候是float32的,此时卷积权重是float16的,你position转成float16就好了。另外pytorch里面nn.conv是可以兼容float32和float16的,但是yolo里test.py里面写了model=model.half(),这个时候就只能算输入为float16的tensor。 最后说一下这个模块加到yolov7里面效果一般,计算量挺大的,推理速度有点慢。

YOSHIKII666 avatar Sep 22 '23 02:09 YOSHIKII666