29
22

More than 3 years have passed since last update.

TensorFlow 2.0のDockerコンテナをGPU+Python3+Jupyterで使う

Posted at

先日、TensorFlow 2.0が正式にリリースされました。
この記事では、GPUサポートを有効にしたTensorFlow 2.0のDockerコンテナでJupyterを起動し、チュートリアルを動かすまでの手順を紹介します。

主な手順:

  1. Dockerのインストール
  2. NVIDIAドライバのインストール
  3. NVIDIA Container Toolkitのインストール
  4. TensorFlowコンテナの起動
  5. Jupyterにアクセス
  6. チュートリアルで動作確認

主な環境:

  • OS: Ubuntu 18.04.3 LTS
  • GPU: GeForce GTX 1050 Ti

やり方

Dockerのインストール

NVIDIA Container ToolkitがDocker 19.03を求めているので、バージョンが古い場合はアップデートしてください。
新規インストールする場合は次のコマンドを実行します。

$ sudo apt-get update
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo apt-key fingerprint 0EBFCD88
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

参考:

NVIDIAドライバのインストール

次のコマンドを実行します。

$ sudo ubuntu-drivers autoinstall

インストール後、再起動します。

$ sudo reboot

NVIDIA Container Toolkitのインストール

次のコマンドを実行します。

# Add the package repositories
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
$ 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-container-toolkit
$ sudo systemctl restart docker

参考:

TensorFlow Dockerコンテナの起動

次のコマンドでイメージのプルとコンテナの起動を同時に行います。

$ sudo docker run --gpus all -u $(id -u):$(id -g) -d -p 8888:8888 -v ~/workspace:/tf tensorflow/tensorflow:latest-gpu-py3-jupyter

プルするイメージはtensorflow/tensorflow:latest-gpu-py3-jupyterです。
いろいろ試しましたがタグはこの順番でないとダメです。
またpy3を除いてもコンテナは起動できますが、Python 2になってしまうので除かないでください。(TensorFlow 2.0はPython 2では動かないはずですが…)

オプションの説明:

  • --gpus all: すべてのGPUを使う
  • -u $(id -u):$(id -g): 現在のユーザで起動する
  • -d: detachedモードで起動する
  • -p 8888:8888: Jupyterのポートを公開する
  • -v ~/workspace:/tf: ホストのボリュームをマウントする

参考:

Jupyterにアクセス

JupyterにアクセスするにはJupyter起動時に表示されるURLが必要ですが、コンテナをdetachedモードで起動したため、標準出力が表示されませんでした。
そこでコンテナのログを表示します。

次のコマンドでコンテナIDを取得します。(標準出力の一番左)

$ sudo docker ps
CONTAINER ID        IMAGE                                          COMMAND                  CREATED             STATUS              PORTS                    NAMES
989625a7fc2c        tensorflow/tensorflow:latest-gpu-py3-jupyter   "bash -c 'source /et…"   12 minutes ago      Up 12 minutes       0.0.0.0:8888->8888/tcp   pedantic_banzai

取得したコンテナIDを使って、次のコマンドでコンテナのログを表示し、JupyterのURLを取得します。

$ sudo docker logs 989625a7fc2c

________                               _______________
___  __/__________________________________  ____/__  /________      __
__  /  _  _ \_  __ \_  ___/  __ \_  ___/_  /_   __  /_  __ \_ | /| / /
_  /   /  __/  / / /(__  )/ /_/ /  /   _  __/   _  / / /_/ /_ |/ |/ /
/_/    \___//_/ /_//____/ \____//_/    /_/      /_/  \____/____/|__/


You are running this container as user with ID 1000 and group 1000,
which should map to the ID and group for your user on the Docker host. Great!

[I 08:59:18.372 NotebookApp] Writing notebook server cookie secret to /.local/share/jupyter/runtime/notebook_cookie_secret
/usr/local/lib/python3.6/dist-packages/IPython/paths.py:68: UserWarning: IPython parent '/' is not a writable location, using a temp directory.
  " using a temp directory.".format(parent))
[I 08:59:19.467 NotebookApp] Serving notebooks from local directory: /tf
[I 08:59:19.467 NotebookApp] The Jupyter Notebook is running at:
[I 08:59:19.467 NotebookApp] http://989625a7fc2c:8888/?token=c4a0d0593ce61b28570b5719ddad9a3759cf8623306c1e08
[I 08:59:19.467 NotebookApp]  or http://127.0.0.1:8888/?token=c4a0d0593ce61b28570b5719ddad9a3759cf8623306c1e08
[I 08:59:19.467 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 08:59:19.485 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///.local/share/jupyter/runtime/nbserver-1-open.html
    Or copy and paste one of these URLs:
        http://989625a7fc2c:8888/?token=c4a0d0593ce61b28570b5719ddad9a3759cf8623306c1e08
     or http://127.0.0.1:8888/?token=c4a0d0593ce61b28570b5719ddad9a3759cf8623306c1e08

ログにあるとおり、http://127.0.0.1:8888/?token=c4a0d0593ce61b28570b5719ddad9a3759cf8623306c1e08を使ってJupyterにアクセスします。

チュートリアルで動作確認

TensorFlow 2 quickstart for beginnersからチュートリアル用のNotebookをダウンロードします。

image.png

ダウンロードしたNotebookを実行します。

image.png

5セル目の実行中にGPUの使用状況を見てみます。

image.png

$ nvidia-smi
Tue Oct 29 19:16:43 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.26       Driver Version: 430.26       CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 105...  Off  | 00000000:01:00.0 Off |                  N/A |
| 43%   31C    P0    N/A /  75W |   3867MiB /  4038MiB |     10%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX 105...  Off  | 00000000:02:00.0 Off |                  N/A |
| 43%   32C    P0    N/A /  75W |     57MiB /  4040MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1387      G   /usr/lib/xorg/Xorg                            13MiB |
|    0      4252      C   /usr/bin/python3                            3841MiB |
|    1      4252      C   /usr/bin/python3                              45MiB |
+-----------------------------------------------------------------------------+

ちゃんとGPUを使ってくれました。

終わり

ちょうど今日からKaggleでTensorFlow 2.0 Question Answeringというコンペが始まりました。
TensorFlow 2.0を使ってくれた誰かにTensorFlow 2.0賞(総額$25,000)が贈られるそうです。
これを機に私も勉強してみようと思います。

29
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
22