Singularity容器CUDA/PyTorch环境下Detectron2的pip依赖冲突问题
Detectron2 Singularity容器构建的依赖冲突问题
问题背景
使用如下Singularity def文件构建包含Detectron2的Python库容器:
Bootstrap: docker From: nvcr.io/nvidia/pytorch:21.06-py3 %post export DEBIAN_FRONTEND=noninteractive apt-get update apt-get -y install pip wget git vim pip install opencv-python-headless pip install 'git+https://github.com/facebookresearch/fvcore' cd /opt git clone https://github.com/facebookresearch/detectron2.git detectron2_repo cd detectron2_repo pip install .
执行到pip install .时出现依赖冲突错误,日志如下:
Successfully built detectron2 fvcore antlr4-python3-runtime pycocotools fairscale Installing collected packages: zipp, numpy, antlr4-python3-runtime, tomli, platformdirs, pathspec, omegaconf, mypy-extensions, iopath, importlib-resources, huggingface-hub, click, timm, pycocotools, hydra-core, fvcore, fairscale, cloudpickle, black, detectron2 Attempting uninstall: numpy Found existing installation: numpy 1.20.3 Uninstalling numpy-1.20.3: Successfully uninstalled numpy-1.20.3 Attempting uninstall: iopath Found existing installation: iopath 0.1.10 Uninstalling iopath-0.1.10: Successfully uninstalled iopath-0.1.10 Attempting uninstall: click Found existing installation: click 7.1.2 Uninstalling click-7.1.2: Successfully uninstalled click-7.1.2 Attempting uninstall: pycocotools Found existing installation: pycocotools 2.0+nv0.5.1 Uninstalling pycocotools-2.0+nv0.5.1: Successfully uninstalled pycocotools-2.0+nv0.5.1 Attempting uninstall: fvcore Found existing installation: fvcore 0.1.6 Uninstalling fvcore-0.1.6: Successfully uninstalled fvcore-0.1.6 ERROR: pips dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. typer 0.3.2 requires click<7.2.0,>=7.1.1, but you have click 8.1.3 which is incompatible. scipy 1.6.3 requires numpy<1.23.0,>=1.16.5, but you have numpy 1.24.2 which is incompatible.
问题表现:pip卸载了原有兼容版本的click 7.1.2和numpy 1.20.3,安装了不兼容的新版本后抛出冲突错误。
已尝试的方法:
- 在
setup.py中添加指定版本的代码,问题依旧; - 更换NVIDIA NGC的其他PyTorch镜像,仍出现其他包的依赖冲突;
- 使用纯Ubuntu镜像构建无CUDA环境,无错误但无法满足GPU需求;
- 跳过def文件中最后一行,进入容器后执行
pip install --user -e .可解决,但需在容器构建阶段完成安装。
原因分析
- Detectron2依赖与基础镜像预装包版本不匹配:Detectron2的依赖链要求更高版本的
numpy和click,但NVIDIA PyTorch 21.06-py3镜像预装了低版本的这些包,同时还预装了依赖低版本的typer和scipy,版本要求形成冲突。 - 旧版pip解析逻辑缺陷:镜像自带的pip版本较低,依赖解析时优先满足Detectron2的直接依赖,不会全局校验所有已安装包的兼容性,导致强制卸载旧版本后触发冲突。
- 重复安装fvcore打乱依赖链:def文件中单独安装了fvcore,而Detectron2本身也依赖fvcore,两次安装的版本要求可能不一致,进一步加剧依赖混乱。
解决方案
方案1:用--constraint约束版本范围
在安装Detectron2前创建版本约束文件,限制numpy和click在基础镜像兼容的范围内:
# 在%post阶段添加 echo "numpy<=1.22.3" > /opt/constraints.txt echo "click<=7.1.2" >> /opt/constraints.txt pip install . --constraint /opt/constraints.txt
这样pip会优先选择符合约束的版本,避免卸载原有兼容包。
方案2:--no-deps跳过自动依赖,手动指定兼容版本
先卸载单独安装的fvcore,再手动安装与基础镜像兼容的依赖版本,最后安装Detectron2时跳过自动依赖处理:
# 在%post阶段替换原有安装步骤 pip uninstall -y fvcore pip install numpy==1.20.3 click==7.1.2 fvcore==0.1.6 iopath==0.1.10 pycocotools==2.0+nv0.5.1 pip install . --no-deps
方案3:容器内创建虚拟环境隔离依赖
在容器构建阶段创建Python虚拟环境,将Detectron2及其依赖安装在虚拟环境中,完全隔离基础镜像的预装包:
# 在%post阶段添加 apt-get -y install python3-venv python3 -m venv /opt/detectron2_env source /opt/detectron2_env/bin/activate pip install opencv-python-headless pip install 'git+https://github.com/facebookresearch/fvcore' cd /opt/detectron2_repo pip install . # 配置容器启动自动激活虚拟环境 echo "source /opt/detectron2_env/bin/activate" >> /root/.bashrc
方案4:使用Detectron2官方适配的wheel包安装
替换原有的fvcore安装和Detectron2克隆步骤,使用官方针对对应CUDA和PyTorch版本的wheel包,自动处理版本兼容性:
# 在%post阶段替换原有安装步骤 pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.10/index.html
内容的提问来源于stack exchange,提问作者Parisa Khateri
相关产品推荐
相关产品推荐

