COTR项目迁移1:对pytorch和jittor的基本认识和配置基本环境

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 2022 Visual Studio 2019
CUDA 11.3

下面配基本环境的时候碰到的问题我会一一记录,希望给后面的人提供一些有用的参考。

3.安装原项目环境

COTR安装流程

在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

详细解释:

  1. conda create: 这是用于创建新的 Conda 虚拟环境的命令。

  2. -n cotr: 指定新环境的名称为 cotr。你可以将 cotr 替换为你想要的任何环境名称。

  3. -y: 自动确认提示,这意味着在创建环境时,不需要手动确认。

  4. python=3.8: 指定在新环境中安装 Python 3.8。

  5. 后续是要安装的一系列包:

    • 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

然后我们需要把PyCharmPython当前的解释器也变为cotr
方法1.点击左上角File里的Settings,找到Project:COTR_for_jiitor里的Python Interpreter,点击Add InterpreterAdd Local InterpreterConda EnvironmentUsing existing environment, 选择cotr
方法2.在终端右下角找:
在这里插入图片描述
点击Add New InterpreterAdd 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_jittorconda环境
在这里插入图片描述

在医学图像分割中,Transformer是一种用于替代传统卷积神经网络的方法。它能够有效地处理3D医学图像,提供更精确的分割结果。 一种基于Transformer的方法是CoTr,它结合了CNN和Transformer来进行3D医学图像分割。另一个方法是UNETR,它采用纯Transformer来进行3D医学图像分割。Swin-unet是另一种类UNET的方法,它也是基于Transformer的。还有一种名为TransBTS的方法,它使用Transformer进行多模态脑肿瘤分割。而TransUNet则是一种利用Transformer作为强大编码器的方法,用于医学图像分割。 这些方法的核心思想是利用Transformer的自注意力机制来捕捉图像中的长距离依赖性,从而提高分割的精确度。与传统的卷积神经网络不同,Transformer对于图像中的每个位置并不均等对待,而是通过引入可变形的自注意力机制来关注少数关键位置。这种方法大大降低了计算和空间复杂度,并且能够处理多尺度和高分辨率特征图,从而提高了医学图像分割的效果。 如果您对Transformer在医学图像分割中的具体实现感兴趣,您可以学习一些相关的Vis Transformer,并以博客的形式详细讲解。此外,U-Net的decoder部分也是医学图像分割中常用的一种技术,可以进一步加深对医学图像分割的理解。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [transformer进行医学图像分割文章](https://blog.csdn.net/weixin_43779199/article/details/119949622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [医学图像分割之TransUNet](https://blog.csdn.net/yjysunshine/article/details/130260556)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值