教你如何用docker快速搭建深度學習環境
阿新 • • 發佈:2018-12-15
本教程搭建集 Tensorflow、Keras、Coffe、PyTorch 等深度學習框架於一身的環境,及jupyter。
一、安裝依賴環境
1. 使用國內映象加速安裝
sudo mv /etc/apt/sources.list /etc/apt/sources.list.old
sudo vim /etc/apt/sources.list
然後將下面的內容寫入該檔案: 需要注意的是:不同版本的ubuntu映象源不一樣,可以在清華映象源查詢
# 預設註釋了原始碼映象以提高 apt update 速度,如有需要可自行取消註釋
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# 預釋出軟體源,不建議啟用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
使映象源生效
sudo apt-get update
2. 安裝 NVIDIA GPU驅動
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-384 nvidia-prime
檢視是否安裝成功
watch nvidia-smi # 該命令可檢視GPU使用情況
3. 安裝 Docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce
檢視是否安裝成功
docker -v
4. 安裝 nvidia-docker
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | \
sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-docker2
sudo pkill -SIGHUP dockerd
這裡的最後一步會重啟docker,並載入nvidia-docker的配置 檢視是否安裝成功
nvidia-docker -v
二、拉取映象並啟動
1. 拉取映象
這裡我們使用了deepo映象:https://hub.docker.com/r/ufoym/deepo/
其下的 ufoym/deepo:all-py36-jupyter
,該映象收集了大部分深度學習框架,執行在GPU環境,以及配有jupyter。
docker pull ufoym/deepo:all-py36-jupyter
2. 啟動映象
預設配置
nvidia-docker run -it -p 8888:8888 ufoym/deepo:all-py36-jupyter jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token= --notebook-dir='/root'
博主推薦配置(掛載外部目錄,方便移動資料)
# 這裡使用了-v選項用於掛載外部目錄
nvidia-docker run -it -p 8888:8888 --ipc=host -v /data:/data ufoym/deepo:all-jupyter-py36 jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token= --notebook-dir='/data'
後臺執行(需要注意的地方是要把引數-it
改成-i
,否則無法執行在後臺)
nohup nvidia-docker run -i -p 8888:8888 --ipc=host -v /data:/data ufoym/deepo:all-jupyter-py36 jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token= --notebook-dir='/data' &
引數說明
-v /data:/data
:左邊是外部路徑,右邊是內部路徑,例如我的檔案放在/home/ubuntu/data
下,需要掛載到docker內部的路徑是/data
,則引數配置應該是-v /home/ubuntu/data:/data
--notebook-dir
:jupyter工作目錄的預設路徑,推薦與上面的docker內部資料路徑相同,即/data
-p 8888:8888
:左邊是外部埠,右邊是docker映象埠。如果想將jupyter應用掛載在8080埠,只需修改引數-p 8080:8888
即可--NotebookApp.token
:進入jupyter的密碼,這裡設定的是空
3. 成功啟動
4. 其他
1. import tensorflow時遇到Future Warning解決方案
錯誤如下:
FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
解決方案: 進入jupyter terminal並輸入
pip install --upgrade numpy
pip install --upgrade h5py
問題解決。