0
2

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.

UbuntuにTensorFlow+Docker+NVIDIA GPUで機械学習する準備メモ

Posted at

Ubuntuでtensorflow2系でGPUを使った機械学習の準備です。

#Dockerインストール
【参考】
https://docs.docker.com/get-docker/
https://docs.docker.com/engine/install/ubuntu/

#旧バージョンがインストールされていればUninstallする
#$ sudo apt-get remove docker docker-engine docker.io containerd runc
#リポジトリ使ってインストール
$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
#Docker's official GPG keyを追加
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

$ sudo apt-key fingerprint 0EBFCD88
#Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.だそうです

#x86_64/amd64の場合はこれ、armとかは別のコマンドのようです
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

#Dockerエンジンインストール
#今回は19.03以降でnvidia-container-toolkitを使うつもりなので最新版をインストールします(19.03.8が最新でした)
 $ sudo apt-get update
 $ sudo apt-get install docker-ce docker-ce-cli containerd.io

##動作確認
sudo docker run --rm hello-worldを実行して

↓こんなメッセージが出ればOKらしいです。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the![Screenshot from 2020-05-09 17-35-12.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/241416/0ea9d1e3-e1d5-568e-7c0b-b577bfbae162.png)

    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

#NVIDIA GPUドライバインストール

gccなんかが必要になるので事前に入れておきましょう
sudo apt install build-essentiialで入れてみました

【参考】
https://qiita.com/kawazu191128/items/8a46308be6949f5bda57

 NVIDIA-Linux-x86_64-440.82.runの選択肢

Screenshot from 2020-05-09 17-32-44.png

Screenshot from 2020-05-09 17-35-12.png

#NVIDIA Dockerサポートのインストール

https://github.com/NVIDIA/nvidia-docker
のQuickStartに従って

# 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

Usageの

sudo docker run --gpus all nvidia/cuda:10.0-base nvidia-smi

を実行したら何か動いてるっぽい!!

#tensorflowのGPU対応イメージを使った動作確認

https://www.tensorflow.org/install/docker?hl=ja
に戻ってGPUサポートの動作確認

sudo docker run --gpus all -it --rm tensorflow/tensorflow:latest-gpu \
       python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

ちょっと時間かかりましたが無事完了!
どうやら準備ができた模様です。

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?