关于人工智能:基于Docker的Python开发

作者|GUEST
编译|VK
起源|Analytics Vidhya

在PyCharm和Visual Studio代码上反对CUDA

介绍

如果你没有教训,建设一个开发环境是不容易的,特地是如果你想学习的技术很多。

本教程旨在向你展现如何在PyCharm或Visual Studio代码中设置一个基于Docker的Python开发环境,并反对CUDA。

免责申明

  • 在写这篇文章的时候,我无奈在Windows10 家庭版中应用Docker外部的CUDA(即便是外部版本),所以本教程是在思考Linux的状况下实现的,只管基本上没有什么是特定于平台的。
  • 只有在专业版上能力应用Docker作为PyCharm的近程Python解释器。
  • 我假如你曾经在你的机器上装置了Docker。
  • 我假如你曾经在你的机器上装置了CUDA。如果你仍在设置你的Linux机器,并且你不违心钻研太多,我通常举荐Pop操作系统(https://pop.system76.com/)。在该文中(https://support.system76.com/…),你能够找到如何在他们的平台上非常容易地设置CUDA和cuDNN。文章还提供了在Ubuntu上应用他们的包的阐明。

我的项目构造

在本教程中,我应用了一个只有3个文件的玩具我的项目:

生成容器的Dockerfile

requirements.txt蕴含我的项目依赖项的文件。

run.py蕴含一些要运行的代码的文件。显然,你的集体我的项目很可能更简单,你能够应用不同的办法来治理依赖关系,你也能够应用docker-compose.yaml但为了实现我的例子这会毫无意义引入复杂性。

Dockerfile文件

对于一篇更关注Docker和Dockerfiles的文章,我举荐Docker初学者指南:https://medium.com/codingthes…

上面是咱们的Dockerfile和一个简短的正文

FROM nvidia/cuda:10.2-devel

# 地址: https://github.com/ContinuumIO/docker-images/blob/master/miniconda3/debian/Dockerfile

ENV PATH /opt/conda/bin:$PATH

RUN apt-get update --fix-missing && \
    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/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
    /bin/bash ~/miniconda.sh -b -p /opt/conda && \
    rm ~/miniconda.sh && \
    /opt/conda/bin/conda clean -tipsy && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
    echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
    echo "conda activate base" >> ~/.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

# 我的项目设置

WORKDIR /code

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "./run.py"]

艰深地说,Dockerfile形容了生成Docker镜像的过程,该镜像随后用于创立Docker容器。这个Dockerfile文件建设在nvidia/cuda:10.2-devel,镜像由NVIDIA间接在DockerHub中提供:https://hub.docker.com/r/nvid…。

nvidia/cuda:10.2-devel是曾经装置了CUDA10.2工具包的开发镜像

当初你只须要装置Python开发所需的货色并设置咱们的我的项目。

在Dockerfile的两头局部有一个Miniconda3装置。我决定应用Miniconda而不是仅仅应用Python,因为它是我大多数我的项目的首选平台。

咱们没有利用Miniconda的任何性能,所以这有点过头了。将Miniconda替换成Dockerfile中的Python,这是留给读者的一个练习(不要惊恐,只需应用与新的Ubuntu设施雷同的命令)。

最初一节是对于我的项目设置的,咱们只是装置依赖项,复制镜像工作目录中的所有文件,并抉择在没有指定命令的状况下调用docker run时启动的命令。

要构建Docker镜像,只需应用你抉择的shell导航到蕴含Dockerfile的门路并运行:

docker build -t <image_name> .

这将生成配置形容的Docker镜像,并将其命名为image_name。如果在名称中没有指定标记,则应用最新的作为默认值。要指定标记,只需在冒号后写入。

在本教程的其余部分中,我将应用pytorch-development-box这个名称。

requirements.txt

我只应用Pytorch和Torchvision作为这个我的项目的依赖项。我常常应用这些包,我会应用他们的CUDA可用性办法来查看是否一切正常。所以我的requirements.txt是:

torch
torchvision

run.py

我的Python文件非常简单,我只是查看CUDA是否可用。

import torch.cuda

if torch.cuda.is_available():
    print("CUDA is available :D")
else:
    print("CUDA isn't available :(")

设置PyCharm

只有在PyCharm Professional上能力应用Docker的近程Python解释器。那么,让咱们看看如何设置它。

构建好Docker镜像并在PyCharm中关上我的项目文件夹后,导航到File > Settings > Project > Python Interpreter

你应该看到这样的画面:

当初单击右上角左近的小齿轮并增加一个新的Python解释器。

在这里,你须要抉择Docker并在名为image name的下拉菜单中抉择之前抉择的镜像名称,如下所示:

确认此配置后,请期待索引实现,而后尝试运行run.py.

CUDA isn't available :(

在这一点上,咱们没有配置让Docker应用GPU,但咱们能够疾速修复它。

关上主动生成的运行/调试配置,并在Docker容器设置的开端增加--gpus all

你应该失去这样的后果:

确认此配置并运行它。CUDA后果当初可用!

设置Visual Studio代码

我将依附新的Visual Studio代码的近程开发扩大来设置通过Docker的开发。

第一步是装置近程开发扩大包并关上我的项目文件夹:https://marketplace.visualstu…

应用VisualStudio palette中的“Add Development Container Configuration Files”命令。

抉择应用本人的Dockerfile。

此时devcontainer.json文件将被创立到一个.devcontainer目录中,如下所示:

// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.128.0/containers/docker-existing-dockerfile
{
    "name": "Existing Dockerfile",

    // Sets the run context to one level up instead of the .devcontainer folder.
    "context": "..",

    // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
    "dockerFile": "../Dockerfile",

    // Set *default* container specific settings.json values on container create.
    "settings": { 
        "terminal.integrated.shell.linux": null
    },

    // Add the IDs of extensions you want installed when the container is created.
    "extensions": []

    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    // "forwardPorts": [],

    // Uncomment the next line to run commands after the container is created - for example installing curl.
    // "postCreateCommand": "apt-get update && apt-get install -y curl",

    // Uncomment when using a ptrace-based debugger like C++, Go, and Rust
    // "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],

    // Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
    // "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],

    // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
    // "remoteUser": "vscode"
}

将弹出一个提醒,要求在容器中从新关上该文件夹。

在此之前,咱们只须要抉择一些在容器中开发时应用的扩大。

转到Extensions选项卡,浏览你须要的扩大,你能够右键单击并抉择Add to devcontainer。将它们增加到配置中。

当初咱们只须要增加一个runArgs键来启用GPU,咱们就能够开始开发了。

减去正文,你应该失去这样的后果:

{
    "name": "Existing Dockerfile",
    "context": "..",
    "dockerFile": "../Dockerfile",
    "settings": {
        "terminal.integrated.shell.linux": null
    },
    "extensions": [
        "ms-python.python"
    ],
    // This was added!
    "runArgs": [ 
        "--gpus=all"
    ]
}

当初从命令面板,咱们能够重建和从新关上容器

论断

当初你曾经在IDE中配置了一个十分根本的开发环境,它基于你本人的Docker镜像,所有这些都反对GPU。

原文链接:https://www.analyticsvidhya.c…

欢送关注磐创AI博客站:
http://panchuang.net/

sklearn机器学习中文官网文档:
http://sklearn123.com/

欢送关注磐创博客资源汇总站:
http://docs.panchuang.net/

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据