在這篇文章中沒有直接使用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