【发布时间】:2022-06-24 18:18:55
【问题描述】:
我想在 Visual Studio Code docker 容器中使用 GPU 来使用 TensorFlow 训练模型。要为我的容器构建映像,我使用下一个 Dockerfile:
FROM mcr.microsoft.com/vscode/devcontainers/anaconda:0-3
ARG PROJECT_NAME=fire_rec
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
COPY environment.yml* .devcontainer/noop.txt /tmp/conda-tmp/
RUN if [ -f "/tmp/conda-tmp/environment.yml" ]; then umask 0002 && /opt/conda/bin/conda env update -n base -f /tmp/conda-tmp/environment.yml; fi \
&& rm -rf /tmp/conda-tmp
WORKDIR /srv/${PROJECT_NAME}
COPY requirements.txt /srv/${PROJECT_NAME}
RUN apt-get update && apt-get install -y python3-opencv
RUN apt-get update && apt-get install -y pip
RUN python3 -m pip install --no-cache -r requirements.txt
RUN apt-get update && apt-get install -y nvidia-cuda-toolkit
“requirements.txt”包括:
opencv-python
tensorflow-gpu
numpy
matplotlib
albumentations
tensorflow_addons
我还有 .devcontainer.json 文件:
{
"name": "Anaconda (Python 3)",
"build": {
"context": "..",
"dockerfile": "Dockerfile",
"args": {
"NODE_VERSION": "none"
}
},
"settings": {
"python.defaultInterpreterPath": "/opt/conda/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.autopep8Path": "/opt/conda/bin/autopep8",
"python.formatting.yapfPath": "/opt/conda/bin/yapf",
"python.linting.flake8Path": "/opt/conda/bin/flake8",
"python.linting.pycodestylePath": "/opt/conda/bin/pycodestyle",
"python.linting.pydocstylePath": "/opt/conda/bin/pydocstyle",
"python.linting.pylintPath": "/opt/conda/bin/pylint"
},
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
],
"remoteUser": "vscode",
}
我成功构建了镜像并启动了容器。但是当我尝试在容器内的 jupyter-notebook 中启动此代码时:
import tensorflow as tf
tf.config.list_physical_devices('GPU')
我收到下一条消息:
2022-05-05 14:42:02.712454: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2022-05-05 14:42:02.712483: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:163] no NVIDIA GPU device is present: /dev/nvidia0 does not exist
所以这段代码无法使用 GPU。我该如何解决这个问题?
【问题讨论】:
-
在容器内使用CUDA(GPU),一般建议安装the NVIDIA container toolkit。
标签: python docker tensorflow visual-studio-code cuda