1.简介
这个暑假帮老师进行一个项目的代码迁移:从pytorch迁移到jittor
PyTorch是一个由Facebook的人工智能研究团队开发的开源深度学习框架。
Jittor (计图)是由清华大学开发的一个基于即时编译和元算子的高性能深度学习框架。
2.配置
我的电脑的硬件配置如下:
CPU:12th Gen Intel® Core™ i7-12700H
GPU 0:Intel® Iris® Xe Graphics
GPU 1:NVIDIA GeForce RTX 3050 Ti Laptop GPU
Platform & Version:Windows 11 家庭中文版 23H2
RAM: 16.0 GB
我的电脑的软件配置如下:
PyCharm Community Edition 2024.1
Anaconda 2.6.0
Visual Studio 2022Visual Studio 2019
CUDA 11.3
下面配基本环境的时候碰到的问题我会一一记录,希望给后面的人提供一些有用的参考。
3.安装原项目环境
在Windows的盘符里先找一个你可以记住的位置,新建一个文件夹并命名,我起的名字是cotr
打开Anaconda
,从里头启动PyCharm
,打开终端,一步一步运行下面的命令
3.1. 创建conda虚拟环境并激活它
conda create -n cotr -y python=3.8 conda-build pyyaml numpy ipython cython typing typing_extensions mkl mkl-include ninja
详细解释:
-
conda create
: 这是用于创建新的 Conda 虚拟环境的命令。 -
-n cotr
: 指定新环境的名称为cotr
。你可以将cotr
替换为你想要的任何环境名称。 -
-y
: 自动确认提示,这意味着在创建环境时,不需要手动确认。 -
python=3.8
: 指定在新环境中安装 Python 3.8。 -
后续是要安装的一系列包:
conda-build
: 用于构建 Conda 包的工具。pyyaml
: 一个 YAML 解析和生成库。numpy
: 一个用于科学计算的库,提供多维数组对象和各种数学函数。ipython
: 一个交互式 Python 解释器,提供丰富的工具和功能。cython
: 一个编程语言,可以将 Python 代码转换为 C 代码,从而提高执行速度。typing
: 提供类型提示的模块。typing_extensions
: 一些扩展类型提示的功能,适用于旧版本的 Python。mkl
: 英特尔数学核心函数库,用于加速数学运算。mkl-include
: 英特尔数学核心函数库的头文件。ninja
: 一个用于构建系统的工具,主要用于加速 C++ 等语言的编译过程。
环境创建好了之后激活这个环境
conda activate cotr
然后再终端里可以看见路径的最前头是(cotr)
不放心的运行下面的命令查看:
conda env list
然后我们需要把PyCharm
的Python
当前的解释器也变为cotr
方法1.点击左上角File
里的Settings
,找到Project:COTR_for_jiitor
里的Python Interpreter
,点击Add Interpreter
,Add Local Interpreter
,Conda Environment
,Using existing environment
, 选择cotr
方法2.在终端右下角找:
点击Add New Interpreter
,Add Local Interpreter
,后面跟方法1一样
3.2. 安装Pytorch
conda install pytorch==1.10.0 torchvision==0.11.0 cudatoolkit=11.3
首先,在终端中查看有没有添加清华源,
conda config --show channels
没有的话,执行如下命令:
#添加镜像源
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/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/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/cloud/pytorch
#显示检索路径
conda config --set show_channel_urls yes
#显示镜像通道
conda config --show channels
如果添加错误了就删掉重新加。
补充命令
查看所有虚拟环境 conda info --env 在显示的环境中,有*的表示当前所在的环境
关闭环境 conda deactivate envname
删除环境 conda remove -n envname --all
克隆环境 本地已有一个环境AAA,若需要创建一个同样的BBB conda create -n BBB --clone AAA
3.3. 安装 mmcv-full
pip install mmcv-full==1.5.3 -f https://download.openmmlab.com/mmcv/dist/cu113/torch1.10.0/index.html
问题1
这一步我碰到了一个问题:pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
参考2个博主的文章,链接如下:
文章1
文章2
主要问题是由于网络问题,可能是科学上网的网络代理出问题了,那就关闭它,就是使用国内的网
我推荐2种方法:
1.重试运行命令
2.增加超时时间。例如:pip install --default-timeout=100 package_name
pip install --default-timeout=100 mmcv-full==1.5.3 -f https://download.openmmlab.com/mmcv/dist/cu113/torch1.10.0/index.html
3.使用清华源:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package_name
包过的!!!
如果下面的命令也有网络问题都可以增加超时时间/使用清华源来解决
3.4. 安装 mmdet 和 mmsegmentation
pip install mmdet==2.25.1 mmsegmentation==0.25.0
3.5. 安装其他要求
pip install lyft_dataset_sdk networkx==2.2 numba==0.53.0 numpy nuscenes-devkit plyfile scikit-image tensorboard trimesh==2.35.39
3.6. 准备 cotr repo
git clone https://github.com/NotACracker/COTR.git
cd COTR
pip install -v -e.
问题2
截取到的关键问题
RuntimeError: Error compiling objects for extension
error: subprocess-exited-with-error
× python setup.py develop did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
#error: -- unsupported Microsoft Visual Studio version! Only the versions between 201
7 and 2019 (inclusive) are supported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.
Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)
D:\user\anaconda3\envs\cotr\lib\site-packages\torch\utils\cpp_extension.py:316: UserWarning: Error checking compiler version for cl: [WinError 2] 系统找不到指定的文件。
1 error detected in the compilation of "D:/Codes/Py/COTR_for_jittor/COTR/mmdet3d/ops/bev_pool_v2/src/bev_pool_cuda.cu".
bev_pool_cuda.cu
ninja: build stopped: subcommand failed.
这个问题就是VS2022的问题,软件太新了!项目需要的微软C++编译器(MSVC)是老版本VS2017-2019的,没办法,只有卸载重装这一条路😅
我重装为VS2019后就好了。
下面是成功例子中截取的关键部分:
ninja: no work to do.
正在创建库 D:\Codes\Py\cotr\COTR\build\temp.win-amd64-cpython-38\Release\mmdet3d\ops\bev_pool_v2\src\bev_pool_v2_ext.cp38-win_amd64.lib 和对象 D:\Codes\Py\cotr\COTR\build\temp.win-amd64-cpython-38\Release\mmdet3d\ops\bev_pool_v2\src\bev_pool_v2_ext.cp38-win_amd64.exp
正在生成代码
已完成代码的生成
copying build\lib.win-amd64-cpython-38\mmdet3d\ops\bev_pool_v2\bev_pool_v2_ext.cp38-win_amd64.pyd -> mmdet3d\ops\bev_pool_v2
Creating d:\user\anaconda3\envs\cotr\lib\site-packages\mmdet3d.egg-link (link to .)
Adding mmdet3d 1.0.0rc4 to easy-install.pth file
Installed d:\codes\py\cotr\cotr
Successfully installed mmdet3d-1.0.0rc4
ninja: no work to do提示通常表示没有需要构建的文件或已经构建完成。
4.安装jittor环境
Jittor官网
按照步骤就可以了,这一部分直接顺利过了,Windows环境看来主要是依赖VS的版本啊!认准VS2019!
因为要代码迁移,所以我建议重新创建一个新的虚拟环境,比如我重新起名一个cotr_for_jittor
的conda
环境