当我们在训练模型的时候,发现模型训练十分缓慢,使用下面代码测试我们是否利用上gpu
import torch
# 检查 CUDA 是否可用
if torch.cuda.is_available():
print("CUDA is available! Training on GPU ...")
# ... 其余的代码 ...
else:
print("CUDA is not available. Training on CPU ...")
经过检查,发现cuda不可用。
所以我们重装环境
# 创建虚拟环境rtDetr
conda create -n rtDetr python==3.8
#激活环境
conda activate rtDetr
#安装cuda版本的torch以及torchvision
pip install torch==2.0.1 torchvision==0.15.2 --index-url https://download.pytorch.org/whl/cu118
#安装好cuda版本的torch再装其他环境
pip install -r requirements.txt //requirement.txt
# 运行训练脚本
python tools/train.py
其中要重视的主要是我们需要的torch版本以及其配套的torchvision版本。不能分开装,否则会报错。还有就是不能直接pip install torch,否则装上的是cpu版本。
具体代码寻找:
在网址:https://pytorch.org/get-started/previous-versions/
在previous pytorch version里面:
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu118
至此成功安装