windows 安装 PyTorch3D 详细指南
psiQAQ
2024年02月29日 16:33

在深度学习和3D图形处理领域,PyTorch3D 提供了强大的工具和接口。以下是安装 PyTorch3D 0.7.4 版本的详细步骤,适用于需要特定配置的项目。

准备工作:

  1. Visual Studio 2019 C++桌面开发环境

    安装 VS2019 并确保包含了C++桌面开发工具集。

  2、Anaconda 环境

    推荐在 Anaconda 中创建新的虚拟环境,例如 Python 3.10 版本:  

    ```

    conda create -n yourenvname python=3.10

    ```

  3、CUDA 11.7.1 

    根据项目需求选择CUDA版本,确保版本与显卡驱动兼容。

    当安装有多个 cuda 版本时,环境变量中的 `CUDA_PATH` 应指向所使用的CUDA版本,例如11.7。

    CUDA Toolkit 可在[NVIDIA官方网站](https://developer.nvidia.com/cuda-toolkit-archive)下载。

  4、安装 PyTorch

    从[PyTorch官方网站](https://pytorch.org/get-started/previous-versions/)选择合适的版本进行安装。

    对于CUDA 11.7,使用以下命令(如果用pip安装可能会torch.cuda.is_available()返回False):

    ```

    conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia

    ```

     

  5、安装额外必需库

    ```

    conda install -c fvcore -c iopath -c conda-forge fvcore iopath

    ```

安装步骤:

一、PyTorch3D 本地安装

  1. 下载指定版本的PyTorch3D

  [PyTorch3D的发布页面](https://github.com/facebookresearch/pytorch3d/releases)

  (如0.7.4,支持PyTorch 2.0.1。版本选择参考发布页面。

  2. 修改 `pyTorch3d\setup.py` 文件:  

    - 注释掉 `extra_compile_args = {"cxx": ["-std=c++14"]}`  

    - 替换为 `extra_compile_args = {"cxx": []}`

二、CUB 安装

  由于使用 CUDA toolkit 自带的CUB可能会导致编译错误:

    C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8/include\cub/device/dispatch/dispatch_segmented_sort.cuh(338): error: invalid combination of type specifiers

    C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8/include\cub/device/dispatch/dispatch_segmented_sort.cuh(338): error: expected an identifier

    C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8/include\cub/device/dispatch/dispatch_segmented_sort.cuh(379): error: expected a member name

    3 errors detected in the compilation of "D:/yourpath/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.backward.gpu.cu".

    renderer.backward.gpu.cu

  建议下载与CUDA toolkit版本对应的CUB版本,如使用cudatoolkit 11.7.1对应cub-1.16.0。

  [NVIDIA CUB Releases](https://github.com/NVIDIA/cub/releases)  

  下载后在系统环境变量中添加 变量名:CUB_HOME,指向CUB本地下载路径:\yourpath\cub-1.16.0。

三、使用 VS2019 终端安装

  在VS2019的"x64 Native Tools Command Prompt for VS 2019"终端中执行以下命令:

  ```

  set DISTUTILS_USE_SDK=1

  set PYTORCH3D_NO_NINJA=1

  cd \yourpath\pytorch3d

  activate 你的虚拟环境

  python setup.py install

  ```

四、CUB 版本不匹配问题处理

  如果遇到CUB版本不匹配的编译错误:

    CUB is now included in the CUDA Toolkit, so you no longer need to use your own checkout of CUB. 

  可以尝试修改CUDA Toolkit中的配置文件以忽略版本检查:

    - 打开并以管理员权限编辑文件:

      `C:\路径\NVIDIA GPU Computing Toolkit\CUDA\v11.7\include\thrust\system\cuda\config.h`

    - 搜索找到:

      `#ifndef THRUST_IGNORE_CUB_VERSION_CHECK`

      更改为

      `#ifdef THRUST_IGNORE_CUB_VERSION_CHECK`

    - 重新编译安装。

五、验证安装:

  安装成功后最后一行提示:

    Finished processing dependencies for pytorch3d==0.7.4

  可以通过以下命令验证PyTorch3D安装是否成功:

    ```

    pip list | findstr pytorch3d

    python

    import pytorch3d

    print(pytorch3d.__version__)

    ```

  如果安装正确,上述命令应输出PyTorch3D的版本号,例如 `0.7.4`。

希望这个指南能帮助你成功安装PyTorch3D,并为你的项目提供强大的3D视觉支持。

参考:

官方安装教程:https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md

https://blog.csdn.net/jacke121/article/details/136344356

https://blog.csdn.net/m0_61787307/article/details/129941897

https://blog.csdn.net/m0_70229101/article/details/127196699

https://blog.csdn.net/weixin_43357695/article/details/126063091

https://zhuanlan.zhihu.com/p/666359190