LoginSignup
16
11

More than 3 years have passed since last update.

PyTorchを使ったアプリをHerokuにディプロイする際の注意点

Last updated at Posted at 2021-03-07

はじめに

PyTorchで予測モデルを作成し、WEBインターフェースを作成してHerokuにディプロイする際に軽くハマったのでメモ

環境

  • Heroku (無料枠)
  • PyTorch 1.7.1 (CPU版)

現象

ERROR: Could not find a version that satisfies...が発生

pytorch(GPU版)を使うため、requiments.txtにtorch==1.7.1+cpuを入れたのだが、以下のエラーが表示されビルドできない。


$git push heroku master
(中略)
remote:          Downloading tensorboard_plugin_wit-1.8.0-py3-none-any.whl (781 kB)
remote:        ERROR: Could not find a version that satisfies the requirement torch==1.7.1+cpu (from -r /tmp/build_5a7f1f2b/requirements.txt (line 37)) (from versions: 0.1.2, 0.1.2.post1, 0.1.2.post2, 1.4.0, 1.5.0, 1.5.1, 1.6.0, 1.7.0, 1.7.1, 1.8.0)
remote:        ERROR: No matching distribution found for torch==1.7.1+cpu (from -r /tmp/build_5a7f1f2b/requirements.txt (line 37))
remote:  !     Push rejected, failed to compile Python app.
・・・

恐らく+cpuという指定の仕方に対応していないものと思われる。

Compiled slug size: XXX M is too large (max is 500M). が発生

気をとりなおして、torch==1.7.1+cpu から +cpu を除外し、torch==1.7.1 にしてやってみる。すると、今度は圧縮時に以下のエラーが。

$git push heroku master
(中略)
remote: -----> Compressing...
remote:  !     Compiled slug size: 866M is too large (max is 500M).
remote:  !     See: http://devcenter.heroku.com/articles/slug-size
・・・

何がこんなに容量を食っているのかと思ってメッセージを遡ってみると...

(中略)
remote:        Collecting tensorboard==2.4.1
remote:          Downloading tensorboard-2.4.1-py3-none-any.whl (10.6 MB)
remote:        Collecting tensorboard-plugin-wit==1.8.0
remote:          Downloading tensorboard_plugin_wit-1.8.0-py3-none-any.whl (781 kB)
remote:        Collecting torch==1.7.1
remote:          Downloading torch-1.7.1-cp38-cp38-manylinux1_x86_64.whl (776.8 MB)
remote:        Collecting torchaudio==0.7.2
remote:          Downloading torchaudio-0.7.2-cp38-cp38-manylinux1_x86_64.whl (7.6 MB)
remote:        Collecting torchvision==0.8.2
remote:          Downloading torchvision-0.8.2-cp38-cp38-manylinux1_x86_64.whl (12.8 MB)
remote:        Collecting tqdm==4.59.0
・・・

なんとPyTorchが776.8MBも使っているではないか。

対策

恐らくGPU版だからデカいのだろうと思い、CPU版を指定する方法を色々調べたところ、requiments.txtのPyTorchの行に前に以下のような記載をすることでCPU版がインストールされ、サイズも600M近く縮小され、無事ディプロイされた。

requirements.txt
--find-links https://download.pytorch.org/whl/torch_stable.html
torch==1.7.1+cpu

つーか、Herokuについてもっと勉強せなあかん! サイズ制限なんて知らなかったよ。

参考

16
11
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
16
11