WongKinYiu / yolov7

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
GNU General Public License v3.0
13.4k stars 4.22k forks source link

Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! #556

Open rdwEngineering opened 2 years ago

rdwEngineering commented 2 years ago

running python detect.py --weights yolov7-mask.pt --img-size 640 --source /var/wmt/externaldisk/TRAINDATA/ --device 0 gives me the following error:

Fusing layers... 
Model Summary: 399 layers, 45413366 parameters, 0 gradients
 Convert model to Traced-model... 
/var/wmt/miniconda/envs/yolov7_mask/lib/python3.8/site-packages/torch/functional.py:478: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  ../aten/src/ATen/native/TensorShape.cpp:2894.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
<class 'torch.Tensor'>  --  <class 'list'>  --  <class 'torch.Tensor'>
Traceback (most recent call last):
  File "detect.py", line 195, in <module>
    detect()
  File "detect.py", line 39, in detect
    model = TracedModel(model, device, opt.img_size)
  File "/var/wmt/yolov7_2/yolov7/utils/torch_utils.py", line 362, in __init__
    traced_script_module = torch.jit.trace(self.model, rand_example, strict=False)
  File "/var/wmt/miniconda/envs/yolov7_mask/lib/python3.8/site-packages/torch/jit/_trace.py", line 750, in trace
    return trace_module(
  File "/var/wmt/miniconda/envs/yolov7_mask/lib/python3.8/site-packages/torch/jit/_trace.py", line 967, in trace_module
    module._c._create_method_from_trace(
  File "/var/wmt/miniconda/envs/yolov7_mask/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/var/wmt/miniconda/envs/yolov7_mask/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1118, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/var/wmt/yolov7_2/yolov7/models/yolo.py", line 700, in forward
    return self.forward_once(x, profile)  # single-scale inference, train
  File "/var/wmt/yolov7_2/yolov7/models/yolo.py", line 726, in forward_once
    x = m(x)  # run
  File "/var/wmt/miniconda/envs/yolov7_mask/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/var/wmt/miniconda/envs/yolov7_mask/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1118, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/var/wmt/yolov7_2/yolov7/models/yolo.py", line 374, in forward
    y[..., 0:2] = (y[..., 0:2] * 3. - 1.0 + self.grid[i]) * self.stride[i]  # xy
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

What should i do. I already looked to tensors being both on cpu and gpu, however, i did no manage to fix it yet.

Thnx!

Krestovik89 commented 2 years ago

exactly the same problem(

cairuil commented 2 years ago

I am also experiencing this, is there any solution?

afzixhaikh commented 2 years ago

Same problem here, am i missing something?

xmayyy commented 2 years ago

Same problem here, am i missing something?

afzixhaikh commented 2 years ago

I was able to successfully use mask branch by following instance.ipynb file.

KingRedMan commented 7 months ago

使用参数--no-trace 例如python detect.py --no-trace