【发布时间】:2021-04-06 02:28:27
【问题描述】:
我正在尝试在其中构建 conda 环境的服务器上构建 Docker 容器。除了启用 CUDA 的 PyTorch 之外,所有其他要求都得到了满足(我可以在没有 CUDA 的情况下让 PyTorch 工作,但是没问题)。如何确保 PyTorch 正在使用 CUDA?
这是Dockerfile:
# Use nvidia/cuda image
FROM nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04
# set bash as current shell
RUN chsh -s /bin/bash
# install anaconda
RUN apt-get update
RUN apt-get install -y wget bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 git mercurial subversion && \
apt-get clean
RUN wget --quiet https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh -O ~/anaconda.sh && \
/bin/bash ~/anaconda.sh -b -p /opt/conda && \
rm ~/anaconda.sh && \
ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
find /opt/conda/ -follow -type f -name '*.a' -delete && \
find /opt/conda/ -follow -type f -name '*.js.map' -delete && \
/opt/conda/bin/conda clean -afy
# set path to conda
ENV PATH /opt/conda/bin:$PATH
# setup conda virtual environment
COPY ./requirements.yaml /tmp/requirements.yaml
RUN conda update conda \
&& conda env create --name camera-seg -f /tmp/requirements.yaml \
&& conda install -y -c conda-forge -n camera-seg flake8
# From the pythonspeed tutorial; Make RUN commands use the new environment
SHELL ["conda", "run", "-n", "camera-seg", "/bin/bash", "-c"]
# PyTorch with CUDA 10.2
RUN conda activate camera-seg && conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
RUN echo "conda activate camera-seg" > ~/.bashrc
ENV PATH /opt/conda/envs/camera-seg/bin:$PATH
当我尝试构建此容器 (docker build -t camera-seg .) 时,这给了我以下错误:
.....
Step 10/12 : RUN conda activate camera-seg && conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
---> Running in e0dd3e648f7b
ERROR conda.cli.main_run:execute(34): Subprocess for 'conda run ['/bin/bash', '-c', 'conda activate camera-seg && conda install pytorch torchvision cudatoolkit=10.2 -c pytorch']' command failed. (See above for error)
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
The command 'conda run -n camera-seg /bin/bash -c conda activate camera-seg && conda install pytorch torchvision cudatoolkit=10.2 -c pytorch' returned a non-zero code: 1
这是requirements.yaml:
name: camera-seg
channels:
- defaults
- conda-forge
dependencies:
- python=3.6
- numpy
- pillow
- yaml
- pyyaml
- matplotlib
- jupyter
- notebook
- tensorboardx
- tensorboard
- protobuf
- tqdm
当我将pytorch、torchvision 和cudatoolkit=10.2 放在requirements.yaml 中时,PyTorch 安装成功,但它无法识别 CUDA(torch.cuda.is_available() 返回False)。
我尝试了各种解决方案,例如this、this 和this 以及它们的一些不同组合,但都无济于事。
非常感谢任何帮助。谢谢。
【问题讨论】:
-
我也有同样的问题。关于如何解决这个问题的任何想法没有使用pip安装torch?
-
@rubencart 您可以在需求文件中使用 conda 安装所有依赖项,对于 torch 和 torchvision,请在 Dockerfile 中明确输入。例如,在 Dockerfile 中安装
requirements.yaml依赖项之后添加这一行:RUN conda install -y -n ${CONDA_ENV_NAME} -c pytorch pytorch torchvision cudatoolkit=10.2 ipython
标签: python-3.x docker anaconda pytorch