首页 > 其他分享 >【玩转jetson orin nano(三)PyTorch深度学习环境安装】

【玩转jetson orin nano(三)PyTorch深度学习环境安装】

时间:2024-10-19 18:22:25浏览次数:3  
标签:https nano -- torch aarch64 PyTorch conda orin 安装

文章目录

一、PyTorch深度学习环境安装

1. Anaconda安装

1.1 下载

官网下载地址,选择aarch64版本,无法访问时可以选择清华镜像源下载

1.2 安装

# 安装 Anaconda3-2023.07-2-Linux-aarch64 版本
cd /home/nano/Downloads/
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2022.10-Linux-aarch64.sh
chmod +x Anaconda3-2022.10-Linux-aarch64.sh
./Anaconda3-2022.10-Linux-aarch64.sh        # 出现一大段licence时直接按空格跳到最后,不用一直enter

# 安装 Anaconda3-2023.07-2-Linux-aarch64 版本
cd /home/nx/Downloads/
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2023.07-2-Linux-aarch64.sh
chmod +x Anaconda3-2023.07-2-Linux-aarch64.sh
./Anaconda3-2023.07-2-Linux-aarch64.sh      # 出现一大段licence时直接按空格跳到最后,不用一直enter

设置环境变量

# 感觉好像不用添加环境变量
echo 'export PATH="~/anaconda3/bin:$PATH"' >> ~/.bashrc	# 安装路径若不同,需换成自己的安装路径
source ~/.bashrc

1.3 换源

conda换源

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
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 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

pip 换源

# 中科大源
pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple
# 清华源感觉最近很慢
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
# 阿里源和豆瓣源有些包不全

1.4 创建环境

# 建议创建python3.8的环境,ubuntu20.04本身自带python3.8,各种库比较容易找,jetson官网公布的torch也是3.8版本的
conda create -n pytorch python=3.8
conda activate pytorch
conda env remove --name pytorch 
conda remove -n pytorch --all
conda activate pytorch

1.5 设置默认启动环境

# 非自动启动base环境
conda config --set auto_activate_base false

# 指定默认启动环境
vim ~/.bashrc
conda activate pytorch	# 在.bashrc文件末尾添加
source ~/.bashrc

1.6 卸载

# 删除整个 anaconda 安装目录。
rm -rf anaconda3

注释掉.bashrc中的conda init语句(下图中还是miniconda,未进行更新,不过原理相同)

请添加图片描述

# 删除’.condarc’文件
rm -rf ~/.condarc
# 重新激活 .bashrc
source .bashrc

2.安装torch+torchvisiion

安装依赖项

sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev libavcodec-dev libavformat-dev libopenblas-dev

2.1 安装torch

  1. 下载torch

目前PyTorch for Jetson官方已经更新 PyTorch v2.1.0的whl包,可以将whl下载到本地,直接安装即可
请添加图片描述

cd ~/Downloads
wget https://developer.download.nvidia.cn/compute/redist/jp/v512/pytorch/torch-2.1.0a0+41361538.nv23.06-cp38-cp38-linux_aarch64.whl
pip install torch-2.1.0a0+41361538.nv23.06-cp38-cp38-linux_aarch64.whl

2.2 安装torchvisiion

torchvision暂未发布直接能pip安装的whl版本,因此直接从源码编译。

  1. 下载torchvisiion

确定torch2.1.0对应的torchvisiion版本,为0.16.x。torch与torchvision版本对应,因此选择v0.16.2版本的torchvisiion,从github clone到本地

git clone --branch v0.16.2 https://github.com/pytorch/vision/tree/v0.16.2  # clone[慢的话直接下载到本地即可]v0.16.2
export BUILD_VERSION=0.16.2  # 将BUILD_VERSION环境变量设置为值 0.16.2  
python3 setup.py install --user

编译的时间比较久,可能需要个把小时,需要耐心等待。

2.3 验证

查看pytorch运行时真正调用的cuda、cudnn、tensorrt版本:

import torch
import torchvision
import tensorrt as trt

torch.cuda.is_available()		# 检查cuda是否可用
torch.version.cuda				# 查看Pytorch运行时使用的cuda版本
torch.backends.cudnn.enabled	# 查看cudnn是否可用
torch.backends.cudnn.version()  # 查看cudnn版本
print(trt.__version__)
# '/usr/local/cuda'

pip3 show tensorrt		# 查看tensorrt版本

2.4 备注(可直接跳过)

torch与torchvision版本不匹配run v8报错

RuntimeError: Couldn't load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompatible, or if you had errors while compiling torchvision from source. For further information on the compatible versions, check https://github.com/pytorch/vision#installation for the compatibility matrix. Please check your PyTorch version with torch.__version__ and your torchvision version with torchvision.__version__ and verify if they are compatible, and if not please reinstall torchvision so that it matches your PyTorch install.

torchvision下载网址(不适用于orin nano)

标签:https,nano,--,torch,aarch64,PyTorch,conda,orin,安装
From: https://blog.csdn.net/python_yjys/article/details/143081967

相关文章