1.启用虚拟机功能
在Windows功能中,启用“适用于Linux的Windows子系统”和“虚拟机平台/Virtual Machine Platform”
2.安装和更新WSL
wsl --install
wsl --update
3.查看可安装的系统版本
wsl --list --online
4.安装Ubuntu24.04 LTS版本
wsl --install Ubuntu-24.04
安装完成后输入wsl进入系统创建用户。
5.迁移Ubuntu子系统到其他文件目录(个人进行修改)
在其他文件目录(此处为D盘根目录)新建文件夹WSL-Ubuntu,关闭Ubuntu系统
wsl --shutdown
wsl -l -v
确认关闭后将系统导出到目标目录。
wsl --export Ubuntu-24.04 D:\WSL-Ubuntu\Ubuntu.tar
在文件目录确认已经创建Ubuntu.tar后,注销原有的wsl。
wsl --unregister Ubuntu-24.04
接着将备份文件恢复到D:\WSL-Ubuntu中。
wsl --import Ubuntu-24.04 D:\WSL-Ubuntu D:\WSL-Ubuntu\Ubuntu.tar
6.进入WSL,检查NVIDIA驱动是否支持cuda
nvidia-smi
7.在Ubuntu中安装miniconda
sudo apt update && sudo apt install wget -y
cd
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
此处第三步需要cd到根目录,若在/mnt/c/Users/Arno(用户名)目录下进行操作,会有权限问题无法安装
8. 添加推荐的channels顺序
source ~/.bashrc
然后输入:
conda config --add channels defaults
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
conda config --add channels nvidia
conda config --set show_channel_urls yes
此处遇到问题
arno@Arno82JQ:~$ conda config --add channels defaults
conda: command not found
系统找不到conda命令,推测是没有被添加到系统环境变量。
对conda进行初始化
source ~/miniconda3/etc/profile.d/conda.sh
然后运行
conda init
重启powershell后执行以下命令使配置生效,然后就能正常运行添加channels命令了。
source ~/.bashrc
9.安装PyTorch
conda create -n yolo python=3.10 -y
conda activate yolo
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
10.验证cuda是否可用
在终端中输入python进入python交互环境,输入以下代码:
import torch
print("PyTorch 版本:", torch.__version__)
print("CUDA 是否可用:", torch.cuda.is_available())
print("CUDA 版本:", torch.version.cuda)
print("GPU 数量:", torch.cuda.device_count())
print("当前 GPU 名称:", torch.cuda.get_device_name(0) if torch.cuda.is_available() else "无")
11.安装 YOLOv8
安装
pip install ultralytics
终端窗口输入python,回车, 打开 Python 交互环境,接着输入以下代码来验证YOLOv8是否检测到GPU:
from ultralytics import YOLO
import torch
print("YOLOv8 是否检测到 CUDA:", torch.cuda.is_available())
print("当前使用的设备:", torch.cuda.get_device_name(0) if torch.cuda.is_available() else "无 GPU")
12.模型训练
在官网下载yolov8n.pt预训练模型,放入老师提供的project01.v1i.yolov5pytorch文件夹中。将整个文件夹拷贝到Ubuntu系统内(我放到了/home/arno目录下,并且更改文件夹名为yolov8-light)。然后进入Ubuntu-24.04\home\arno.config\Ultralytics目录下,进入settings.json,修改datasets_dir的值为yolov8-light文件夹的目录/home/arno/yolov8-light。
回到WSL中,进入文件夹目录,
cd /home/arno/yolov8-light
开始训练
yolo task=detect mode=train model=yolov8n.pt data=data.yaml epochs=100 imgsz=640 batch=50
其中:
task用于指定任务类型,task=detect为目标检测任务。
mode用于指定运行模式,mode=train为训练模式。
model=yolov8n.pt是指定使用的模型权重文件。
data=data.yaml指定数据集配置文件。
epochs指定训练的轮次,更多轮次会有更好模型性能,但有过拟合风险。
Imgsz用于指定输入图像尺寸。
batch指定批量大小,越大显存占用越大,训练越快。
观察GPU和显存占用
watch -n 1 nvidia-smi
若观察有一定占用,说明在用GPU进行训练。
13.模型使用
训练完成后,进入Ubuntu-24.04\home\arno\yolov8-light\runs\detect\train2\weights可以看到best.pt模型。将其复制出来,与待检测的图片防止到同一目录,在该目录下打开终端,进入wsl后运行
yolo task=detect mode=predict model=best.pt source=test.jpg
命令中best.pt为模型名称,test.jpg为待检测图片名称。
等待运行结束后即可在runs文件夹内查看运行结果。