---
title: [DLHacks]pytorch - segmentation を TPU で実行してみた / pytorch - lightning で書き換えてみた
tags:  #deep learning  
author: [Deep Learning JP](https://docswell.com/user/DeepLearning2023)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/LELZXG4PJR.jpg?width=480
description: 2019/08/19 Deep Learning JP:  http://deeplearning.jp/hacks/
published: August 20, 19
canonical: https://docswell.com/s/DeepLearning2023/KJL6DM-dlhackspytorch-segmentation-tpu-pytorch-lightning
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/LELZXG4PJR.jpg)

pytorch-segmentationを
TPUで実行してみた/
pytorch-lightningで書き換えてみた
東京大学大学院 情報理工学系研究科
電子情報学専攻 坂井・入江研 D1
谷合 廣紀


# Page. 2

![Page Image](https://bcdn.docswell.com/page/4JMVLQK2JW.jpg)

今日の内容
•
セマンティックセグメンテーションを題材に
•
PyTorchでTPUを使ってみる
•
pytorch-lightningで学習コードを書き直す


# Page. 3

![Page Image](https://bcdn.docswell.com/page/PJR2K8X5E9.jpg)

pytorch-segmentation
•
https://github.com/nyoki-mtl/pytorch-segmentation
•
PyTorchで書いたセマンティックセグメンテーション用のコード
•
Deeplab V3+, MobileNet V2, Unetなどが使える


# Page. 4

![Page Image](https://bcdn.docswell.com/page/PEXDL86X7X.jpg)

TPUで実行してみた


# Page. 5

![Page Image](https://bcdn.docswell.com/page/3EKXLK197D.jpg)

TPUとは
•
Googleが開発したディープラーニング用のプロセッサ
•
Google ColaboratoryやGoogle Cloud Platformで使える
•
フレームワークとしえはKeras/Tensorflowが対応


# Page. 6

![Page Image](https://bcdn.docswell.com/page/L7353ZQ9E5.jpg)

PyTorchからもTPUを使ってみたい
•
去年10月くらいにPyTorchも今後TPUに対応していくことが発表された
•
•
https://cloud.google.com/blog /products/ai-machinelearning /introducing-pytorch-across-google-cloud
https://github.com/pytorch/xla で開発が進められている
•
XLA(Accelerated Linear Algebra)はTensorflow用のコンパイラ
•
XLAでコンパイルした中間言語はTPUで実行可能


# Page. 7

![Page Image](https://bcdn.docswell.com/page/87DD4R987G.jpg)

pytorch/xla
•
githubのページに行くとチュートリアルがある
•
実行してみよう！


# Page. 8

![Page Image](https://bcdn.docswell.com/page/VJPXMWZW78.jpg)

環境
•
Google Cloud Platformで実験
•
インスタンス
•
TPU v3x8 (第三世代のTPUが8台)
•
n1-standard-16 (vCPU x16, Memory 60GB)


# Page. 9

![Page Image](https://bcdn.docswell.com/page/2EVL98W9JQ.jpg)

TPUの起動 (コマンド例)
$ gcloud compute tpus create tpu-node1 \
--zone=us-central1-a \
--network=default \
--range=10.0.101.0 \
Pytorch用のソフトウェア
--version=pytorch-0.1 \
--accelerator-type=v3-8


# Page. 10

![Page Image](https://bcdn.docswell.com/page/57GXZ5D4EL.jpg)

CPUの起動 (コマンド例)
$ gcloud compute instances create master-vm \
--zone=us-central1-a \
--machine-type=n1-standard-16 \
--image=debian-9-torch-xla-v20190817 \
--image-project=ml-images \
--boot-disk-size=100GB \
Pytorch用のイメージ
--boot-disk-type=pd-ssd


# Page. 11

![Page Image](https://bcdn.docswell.com/page/4EQ8LZ1RJP.jpg)

MNIST実行
$ gcloud compute ssh master-vm
(vm)$ export XRT_TPU_CONFIG=&quot;tpu_worker;0;10.0.101.2:8470“
(vm)$ conda activate pytorch-0.1
(pytorch-0.1)$ cd /usr/share/torch-xla-0.1/pytorch/xla
(pytorch-0.1)$ python test/test_train_mnist.py


# Page. 12

![Page Image](https://bcdn.docswell.com/page/KJ42D3RQE1.jpg)

pytorch-segmentationを書き換える
•
もとのコード
https://github.com/nyoki-mtl/pytorch-segmentation/blob/master/src /train.py
•
TPU用のコード
https://github.com/nyoki-mtl/pytorch-segmentation/blob/tpu/src /train_tpu.py
•
UNetでcityscapesを学習させてみる


# Page. 13

![Page Image](https://bcdn.docswell.com/page/LE1MZ1QL7G.jpg)

ライブラリ
•
TPU用の環境変数
•
torch-xlaのインポート


# Page. 14

![Page Image](https://bcdn.docswell.com/page/GEW398MQE2.jpg)

TPUの設定
•
使用するデバイスの設定
•
max_devices: TPUをいくつ使うか指定でき、Noneなら全部使う


# Page. 15

![Page Image](https://bcdn.docswell.com/page/47ZV98Q3E3.jpg)

モデルの並列化
•
Train
•
Valid


# Page. 16

![Page Image](https://bcdn.docswell.com/page/YJ65KP6Z7V.jpg)

Train loop


# Page. 17

![Page Image](https://bcdn.docswell.com/page/GJ5YPK69J4.jpg)

所感
•
思ったより簡単にTPU実行可能なコードに書き換えることができた
•
まだできていない部分も多い
•
•
Dilated convが実行できなかったので、deeplab v3+は動かせなかった
•
model.eval()をするとBatchNormの挙動がおかしくなっているように感じた
学習に時間がかかる
•
NVIDIA TITAN Vで3分の学習がTPU v3x8で20分ほどかかった
•
TPUの強みを活かせていない or 別のところにボトルネックがあるかも


# Page. 18

![Page Image](https://bcdn.docswell.com/page/LE353ZQ2J5.jpg)

pytorch-lightningに
書き換えてみた


# Page. 19

![Page Image](https://bcdn.docswell.com/page/8EDD4R96EG.jpg)

pytorch-lightningとは
•
PyTorchのラッパーで効率的に実験コードを書くことができるライブラリ
•
Tensorflowにおけるkerasのような立ち位置
•
最近、Pytorch公式のエコシステムのひとつに


# Page. 20

![Page Image](https://bcdn.docswell.com/page/V7PXMW9ZE8.jpg)

pytorch-lightningの書き方
•
pl.LightningModuleを継承したモデルを書いて


# Page. 21

![Page Image](https://bcdn.docswell.com/page/2JVL98MM7Q.jpg)

pytorch-lightningの書き方
•
Trainerで学習を回すだけ


# Page. 22

![Page Image](https://bcdn.docswell.com/page/5EGXZ5GXJL.jpg)

pytorch-segmentationを書き換える
•
もとのコード
•
•
•
https://github.com/nyoki-mtl/pytorch-segmentation/blob/master/src/train.py
Pytorch-lightningのコード
•
https://github.com/nyoki-mtl/pytorch-segmentation/blob/pytorch-lightning/src/pl_model.py
•
https://github.com/nyoki-mtl/pytorch-segmentation/blob/pytorch-lightning/src/pl_train.py
Deeplabv3+でcityscapesを学習させてみる


# Page. 23

![Page Image](https://bcdn.docswell.com/page/4JQ8LZX57P.jpg)

所感
•
コードがきれいにまとまって可読性が向上した
•
機能が多い
•
•
簡単にマルチGPUや混合精度演算を実行できる
•
Tensorboardでログを出力できる
簡潔に書ける一方でカスタマイズ性も高い


