CentOS Stream 9
Sponsored Link

TensorFlow : Docker イメージ (CPU) を利用する2022/08/10

 
機械学習ライブラリー TensorFlow をインストールします。
当例では、TensorFlow 公式の Docker イメージをダウンロードして、コンテナーから TensorFlow を利用します。
Docker イメージは GPU サポート無しの CPU オンリーのイメージを使用します。
[1]
[2] TensorFlow Docker (CPU) の利用例です。
# TensorFlow イメージを pull

[cent@dlp ~]$
podman pull docker.io/tensorflow/tensorflow:latest
[cent@dlp ~]$
podman images

REPOSITORY                       TAG         IMAGE ID      CREATED       SIZE
docker.io/tensorflow/tensorflow  latest      976c17ec6daa  36 hours ago  1.48 GB

# コンテナー実行

[cent@dlp ~]$
podman run --rm docker.io/tensorflow/tensorflow:latest \
python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

2022-09-08 07:38:22.779379: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-09-08 07:38:24.244232: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
tf.Tensor(-483.35004, shape=(), dtype=float32)

# Hello World テストスクリプトを作成してコンテナーから実行

[cent@dlp ~]$
vi hello_tensorflow.py
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow World!')
tf.print(hello)

[cent@dlp ~]$
podman run --rm -v $PWD:/tmp -w /tmp docker.io/tensorflow/tensorflow:latest python3 ./hello_tensorflow.py

2022-09-08 07:40:32.431019: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-09-08 07:40:34.001372: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Hello, TensorFlow World!
[3] SELinux を有効にしている場合は、ポリシーの変更が必要です。
[root@dlp ~]#
vi my-python.te
# 以下の内容で新規作成

module my-python 1.0;

require {
        type user_home_t;
        type container_t;
        type user_home_dir_t;
        class file { create ioctl open read unlink write };
        class dir { add_name remove_name write };
}

#============= container_t ==============
allow container_t user_home_dir_t:dir { add_name remove_name write };
allow container_t user_home_dir_t:file { create ioctl open read unlink write };
allow container_t user_home_t:file { ioctl open read };

[root@dlp ~]#
checkmodule -m -M -o my-python.mod my-python.te

[root@dlp ~]#
semodule_package --outfile my-python.pp --module my-python.mod

[root@dlp ~]#
semodule -i my-python.pp

[4] Jupyter Notebook を含むイメージの利用例です。
[cent@dlp ~]$
podman pull docker.io/tensorflow/tensorflow:latest-jupyter
[cent@dlp ~]$
podman images

REPOSITORY                       TAG             IMAGE ID      CREATED       SIZE
docker.io/tensorflow/tensorflow  latest-jupyter  c94342dbd1e8  36 hours ago  1.72 GB
docker.io/tensorflow/tensorflow  latest          976c17ec6daa  36 hours ago  1.48 GB

# コンテナーをデーモンとして実行

[cent@dlp ~]$
podman run -dt -p 8888:8888 docker.io/tensorflow/tensorflow:latest-jupyter

1aee64df6f7f75dd2afbdf7cce77e05315e31435c1ebc14f31c1258799347ad8

[cent@dlp ~]$
podman ps

CONTAINER ID  IMAGE                                           COMMAND               CREATED         STATUS             PORTS                   NAMES
1aee64df6f7f  docker.io/tensorflow/tensorflow:latest-jupyter  bash -c source /e...  23 seconds ago  Up 23 seconds ago  0.0.0.0:8888->8888/tcp  gallant_mahavira

# URL 確認

[cent@dlp ~]$
podman exec 1aee64df6f7f bash -c "jupyter notebook list"

Currently running servers:
http://0.0.0.0:8888/?token=5d4929bd5b94509d010c54540427fd6ac2b9a89565461668 :: /tf
  表示された URL にアクセスすると Jupyter Notebook が利用できます。
関連コンテンツ