代码改变世界

在Docker容器中搭建MXNet/Gluon开发环境

2018-03-02 16:38  ☆Ronny丶  阅读(3474)  评论(3编辑  收藏  举报

在这篇文章中没有直接使用MXNet官方提供的docker image,而是从一个干净的nvidia/cuda镜像开始,一步一步部署mxnet需要的相关软件环境,这样做是为了更加细致的了解mxnet的运行环境,方便后续我们更加灵活的去修改相关的配置。

1. 通过docker创建干净的系统环境

docker run -itd --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=2,3 --name=mxnet-cu90 -p 9999:8888 -p 1234:22 -v /docker_share/:/root/share nvidia/cuda:9.0-cudnn7-devel bash
docker exec -it mxnet-cu90  bash

这里镜像使用了nvida最新的cuda9.0的镜像,使用--runtime=nvidia是为了使用nvidia-docker在容器中使用GPU资源, 环境变量NVIDIA_VISIBLE_DEVICES用来控制在容器中可见的GPU的id,这里选择2和3,是因为我的机器上一共有4块GPU卡,容器中只使用后两块。-v选项是为了方便主机与容器之间进行数据交换而挂载的一块空间,使用-p选择进行主机与容器之间的端口映射,这里分别映身了容器中的22号端口是为了使用ssh进行远程访问,以及8888端口是jupyter-notebook默认使用的端口号。

2. 替换阿里云的源

容器中默认的软件包的更新源都是国外的,在国外使用起来速度较慢,这里更新为阿里云的即可。也可以直接在/etc/apt/sources.list文件中将http://archive.ubuntu.com/ubuntu/直接替换为http://cn.archive.ubuntu.com/ubuntu/。

apt-get update
apt-get install -y vim
mv /etc/apt/sources.list /etc/apt/sources.list.bark #备份
vim /etc/apt/sources.list #修改
apt-get update #更新

阿里云的源:

deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

3. 安装必要的软件工具

sudo apt-get update && sudo apt-get install -y build-essential git libgfortran3
apt-get install -y vim git openssh-server # 安装 vim git ssh远程登录
apt-get install -y bash-completion man # 安装命令行的自动提示的功能,以及帮助手册的查看工具
adduser username #创建新用户
adduser username sudo #把用户加入到sudo权限
service ssh start # 启动ssh服务
exit #退出容器 
docker exec mxnet-cu90  /usr/sbin/sshd # 在host上开启容器的sshd服务

这样我们后续就可以通过ssh -p 1234 username@172.17.2.50访问容器了。

4. 安装Miniconda

wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh #下载
sh Miniconda3-latest-Linux-x86_64.sh #安装
conda config --prepend channels http://mirrors.ustc.edu.cn/anaconda/pkgs/free/ # 配置国内科大的源

进一步阅读:

5. 替换pip的源

mkdir ~/.pip
vim ~/.pip/pip.conf

粘贴如下内容,添加阿里云的源

[global]
trusted-host=mirrors.aliyun.com
index-url=https://mirrors.aliyun.com/pypi/simple/

6. MXNet/Gluon开发环境的配置

使用conda提供的虚拟环境机制在容器里创建一个虚拟环境配置文件environment.yml,文件内容如下:

name: mxnet
dependencies:
- python=3
- jupyter
- matplotlib
- pandas
- pip:
  - requests
  - mxnet-cu90>=1.0.1b20180125

然后通过下面的命令,创建虚拟环境mxnet并激活使用。

conda env create -f environment.yml # 安装运行mxnet所需要的Python包
source activate mxnet # 激活虚拟环境,Windows下不需要 source

使用notedown

source activate mxnet # 激活mxnet环境
pip install https://github.com/mli/notedown/tarball/master # 安装插件
jupyter notebook --generate-config # 生成jupyter notebook的配置文件
echo "c.NotebookApp.contents_manager_class = 'notedown.NotedownContentsManager' " >> ~/.jupyter/jupyter_notebook_config.py # 在配置文件最后增加一行

安装notebook每个cell的计时程序

source activate mxnet # 激活gluon环境
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable execute_time/ExecuteTime

7. 开发环境验证

import mxnet as mx
import mxnet.ndarray as nd
a = nd.ones((2,3), ctx=mx.gpu())
a.asnumpy()

如果整个环境安装正确,则运行上面的代码,不会报错,且有如下的输出:

array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)

8. 通过源码安装

在步骤6中,我们演示了如何使用虚拟环境工具conda以及python的包管理工具pip来安装mxnet最新的GPU版本,我们可以得到一个mxnet的python开发环境。我们也可以从源码开始,编译安装。

安装MXNet的依赖库

apt-get install -y libopenblas-dev liblapack-dev libopencv-dev

下载mxnet源码并编译

$ git clone --recursive https://github.com/apache/incubator-mxnet
$ cd incubator-mxnet
$ make -j $(nproc) USE_OPENCV=1 USE_BLAS=openblas USE_CUDA=1 USE_CUDA_PATH=/usr/local/cuda USE_CUDNN=1

编译python接口

sudo apt-get install -y python-dev python-setuptools python-pip libgfortran3 # 安装python相关的环境
cd python
pip install --upgrade pip
pip install -e . #通过目录下的requirement.txt来管理pip安装的包
sudo apt-get install graphviz #安装graphviz用来计算图的显示
pip install graphviz