使用Yolo检测图像时指定了device=0报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Ultralytics YOLOv8.1.34 🚀 Python-3.9.19 torch-2.0.1+cpu
Traceback (most recent call last):
File "E:\python-project\yolov10\test.py", line 6, in <module>
result = model.track(source=source, show=True, device=0)
File "E:\python-project\yolov10\ultralytics\engine\model.py", line 481, in track
return self.predict(source=source, stream=stream, **kwargs)
File "E:\python-project\yolov10\ultralytics\engine\model.py", line 434, in predict
self.predictor.setup_model(model=self.model, verbose=is_cli)
File "E:\python-project\yolov10\ultralytics\engine\predictor.py", line 299, in setup_model
device=select_device(self.args.device, verbose=verbose),
File "E:\python-project\yolov10\ultralytics\utils\torch_utils.py", line 128, in select_device
raise ValueError(
ValueError: Invalid CUDA 'device=0' requested. Use 'device=cpu' or pass valid CUDA device(s) if available, i.e. 'device=0' or 'device=0,1,2,3' for Multi-GPU.

torch.cuda.is_available(): False
torch.cuda.device_count(): 0
os.environ['CUDA_VISIBLE_DEVICES']: None
See https://pytorch.org/get-started/locally/ for up-to-date torch install instructions if no CUDA devices are seen by torch.

从错误来看是使用了cpu版本的torch,从官方仓库内我看是存在GPU版本的torch,后面发现是我使用了清华源进行加速,而清华源上面只找到cpu的版本,所以去掉清华源重新安装即可!

这是原来我手动指定清华源:

1
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 -i https://pypi.tuna.tsinghua.edu.cn/simple

去掉清华源重新安装即可:

1
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121