0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TensorFlowの動作確認環境をDockerで構築する

Posted at

やりたいこと

TensorFlowのDockerイメージをダウンロード
https://www.tensorflow.org/install/docker?hl=ja
して、コンテナを起動し、tensorflowの動作を確認したい。

前提

  • 任意のホストにDockerがインストール済であること。
  • Dockerコマンドが使えること。

Dockerのインストール手順は
https://qiita.com/kenichiro-yamato/items/7e3cb21613784a27409d

事前確認

バージョン

[root@docker ~]# docker -v
Docker version 19.03.7, build 7141c199a2

稼働中のコンテナ

[root@docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

docker pull でイメージを取得する

docker pull tensorflow/tensorflow

結果

[root@docker ~]# docker pull tensorflow/tensorflow
Using default tag: latest
latest: Pulling from tensorflow/tensorflow
171857c49d0f: Pull complete
419640447d26: Pull complete
61e52f862619: Pull complete
40085aa86d3c: Pull complete
b827fdfa00c7: Pull complete
134f84527676: Pull complete
e1f30e7788ed: Pull complete
a13925316d82: Pull complete
5decca4d86ff: Pull complete
70c56a3cd1fa: Pull complete
Digest: sha256:c57fb9628d80872ece8640a22bd0153b2cc8d62d21d79f4d99c3b237a728b62e
Status: Downloaded newer image for tensorflow/tensorflow:latest
docker.io/tensorflow/tensorflow:latest

イメージが取得できたことを確認

[root@docker ~]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
tensorflow/tensorflow                  latest              623195db36df        7 weeks ago         1.46GB

docker run でコンテナを起動する

メモリ指定、ポート指定、ホスト指定、共有ディレクトリ指定など、オプション指定は任意で変えてください。

docker run -m 4096m -p 80:80 -d --privileged -h yamato.host -i -t -v /root/share:/share:rw --name tensorflow_container1 tensorflow/tensorflow

コンテナの起動を確認して接続する

[root@docker ~]# docker ps
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS                NAMES
62f578fc0279        tensorflow/tensorflow   "/bin/bash"         11 seconds ago      Up 10 seconds       0.0.0.0:80->80/tcp   tensorflow_container1

docker exec -it tensorflow_container1 /bin/bash

でtensorflow_container1に接続する。

[root@docker ~]# docker exec -it tensorflow_container1 /bin/bash

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

接続成功。

初期状態の確認

既にpython3とpipが使える状態になっている。

root@yamato:~# python -V
Python 3.6.9
root@yamato:~# pip

Usage:
  pip <command> [options]

vimが入っていないのでインストールする。

apt-get update

してから

apt-get install vim

で、vimが使えるようになる。

pyファイルを作成して試す

vim test.py


print ("Hello World! I am Kenichiro Yamato")

実行する。

python test.py

Hello World! I am Kenichiro Yamato

が表示されたらOK。

tensorflow のサンプルコードを試す

vi sample.py

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(512, activation=tf.nn.relu),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

実行する。

python sample.py

ワーニングやインフォメーションがいくつか出ているが、動作している模様。

2020-11-18 10:00:09.352555: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-11-18 10:00:09.352579: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-11-18 10:00:10.743602: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-11-18 10:00:10.743630: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
2020-11-18 10:00:10.743654: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (yamato.host): /proc/driver/nvidia/version does not exist
2020-11-18 10:00:10.743940: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-11-18 10:00:10.752038: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 3599995000 Hz
2020-11-18 10:00:10.752747: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4e64770 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-11-18 10:00:10.752763: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Epoch 1/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2193 - accuracy: 0.9347
Epoch 2/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0972 - accuracy: 0.9705
Epoch 3/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0676 - accuracy: 0.9789
Epoch 4/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0525 - accuracy: 0.9831
Epoch 5/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0423 - accuracy: 0.9862
313/313 [==============================] - 0s 1ms/step - loss: 0.0640 - accuracy: 0.9801
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?