1. 安装WSL

微软目前提供了单个指令一键安装WSL,使用管理员模式打开PowerShell,然后输入wsk --install,此命令安装的是Linux默认发行版,通过添加--distribution指定发行版,可以通过运行 wsl --list --online来查找可用的发行版,也可以加载本地发行版文件wsl --import <名称> <解压位置> <镜像文件>
在这里插入图片描述
本文选择安装20.04

wsl --install --distribution Ubuntu-20.04

在安装完成后会要求设置用户名和密码,设置完成后会有以下三个变化:

  • windows命令行即变为linux格式
  • 打开MobaXterm会自动生成此linux端口
  • VS Code安装WSL插件后也可以直连
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    【补】WSL默认安装在C盘,为了后续开发方便,将其移植到D盘
  • 首先将当前的Linux发行版导出wsl --export Ubuntu D:\xxx\\Ubuntu.tar
  • 注销当前的发行版:wsl --unregister Ubuntu
  • 重新导入安装发行版:wsl --import Ubuntu D:\xxx\Ubuntu D:\xxx\Ubuntu.tar
  • 进入Linux,并将默认启动用户设置为非root
    • 在/etc/目录下创建wsl.conf
    [user]
    default=user_name
    

2. 安装anaconda

  • Conda是一个开源的包管理器和环境管理器,它被设计用来解决Python包的依赖问题,并且可以创建、保存、加载和切换环境,使项目可以在不同的环境中独立运行而不会相互干扰;
  • anaconda是一个面向科学计算的Python发行版,它包含了Conda、Python以及一系列预安装的库和工具;
  • 下载anaconda
mkdir download && cd download
wget https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh
  • 执行脚本安装
sh Anaconda3-2024.02-1-Linux-x86_64.sh

3. 安装cuda、cudnn

  • CUDA(Compute Unified Device Architecture)是由NVIDIA开发的并行计算平台和编程模型,允许使用GPU进行高性能计算;
  • 在pytorch中,使用device = torch.device("cuda")指定CUDA,所有的模型和数据均可以使用xxx.to(device)移动到GPU自动使用CUDA加速计算;
  • CUDNN是在CUDA基础上对深度学习进行进一步优化的库(CUDA只是并行运算,还可以用于图形计算、游戏引擎等)
  • 可能涉及换源的问题,打开/etc/apt/sources.list,注释原本的源,将下述源复制进去
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
  • 下载cuda11.8
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run
  • 编辑环境变量~/.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.8/lib64
export PATH=$PATH:/usr/local/cuda-11.8/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-11.8
  • 安装CUDNN
    • 下载cudnn,从Windows目录下拷贝到Linux目录下
    • 解压并复制文件
    sudo tar -xvf cudnn-linux-x86_64-8.9.7.29_cuda11-archive.tar.xz
    cd cudnn-linux-x86_64-8.9.7.29_cuda11-archive/
    sudo cp -r lib/* /usr/local/cuda-11.8/lib64/
    sudo cp -r include/* /usr/local/cuda-11.8/include/
    sudo chmod a+r /usr/local/cuda-11.8/lib64/libcudnn*
    sudo chmod a+r /usr/local/cuda-11.8/include/cudnn*
    

4. 安装torch

conda install pytorch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 pytorch-cuda=11.8 -c pytorch -c nvidia

验证:

python
------
import torch
torch.cuda.is_available()
True

5. YOLO Demo

pip install ultralytics
from ultralytics import YOLO
from PIL import Image

# Create a new YOLO model from scratch
model = YOLO("yolov8n.yaml")

# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolov8n.pt")

# # Train the model using the 'coco8.yaml' dataset for 3 epochs
# results = model.train(data="coco8.yaml", epochs=3)

# # Evaluate the model's performance on the validation set
# results = model.val()

# Perform object detection on an image using the model
img = Image.open()
results = model(source=img, save=True)

# # Export the model to ONNX format
# success = model.export(format="onnx")

在这里插入图片描述

6. 参考文档

  1. 微软官方教程
  2. 修改WSL安装包的位置
  3. WSL2安装深度学习环境
  4. YOLO
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐