ラベル cuDNN の投稿を表示しています。 すべての投稿を表示
ラベル cuDNN の投稿を表示しています。 すべての投稿を表示

2018年2月28日水曜日

Windows10環境にTensorflow+Kerasをインストール

で、Windows10環境にTensorflow+Kerasをインストールする。
Ubuntu16とデュアルブートするやり方もあったのだけれど、漫画自炊などはWindows環境でやっているので切り替えがめんどくさいと思った。
それから、仮想環境だとDeepLearningでGPUを叩けなくなり、それはそれで困るから。
しょぼいけどGeForce GTX970が付いているので、CPU計算だけよりはマシだろう。

で、まずやったこと。

1)Windows版のPython3.6をインストール
→これはネット上に参考資料があるから割愛。
 https://www.python.org/downloads/windows/
 から64ビット版をダウンロードしてインストール。
 コツは、
 ・インストール時にCustomize Installationを選択すること。
 ・Add Python 3.6 to Pathにチェックを入れること。
 くらい。インストール先を変えたいときなんかは適宜変更するといいね。

2)PowerShellでVimを使えるようにする
→まずはVimをダウンロードして圧縮ファイルを展開。
 https://www.kaoriya.net/software/vim/
 僕はディレクトリをProgram Filesの下に移動させた。
 エイリアスの設定に手間取り、今もちょっと動きが微妙……。
 ・Pathを通す。
 ・PowerShellを立ち上げて$PROFILEの有無を確認。
  PS > $PROFILE
  C:\Users\MyUserName\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  環境によっては設定ファイルどころかディレクトリすらない。
  僕の場合もWindowsPowerShellのディレクトリがなかった。

  で、Microsoft.PowerShell_profile.ps1に下記の行を追加した。

  New-Alias vi "C:\Program Files\vim\vim.exe"
  New-Alias vim "C:\Program Files\vim\vim.exe"
  function view() {"C:\Program Files\vim\vim.exe" -R $args}

  ディレクトリの部分は、Vimを置いた場所によって変わる。

 ・スクリプトの実行権限を変更する。
  PS > Get-ExecutionPolicy
  Restricted
  と表示されたら制限がかかっている。
  PowerShellを管理者モードで立ち上げて、次のコマンドを実行。

  PS > Set-ExecutionPolicy RemoteSigned

 PowerShellを立ち上げなおして、Vimが起動するならばとりあえずは成功かな。


3)個人的な理由でPowerShell上で動くSSHクライアントをインストール
 Windows10の「設定」から「更新とセキュリティ」を選択。
 「開発者向け」で「開発者モード」のボタンを選択。
 「設定」に戻って「アプリと機能」の中の「オプション機能の管理」を選択。
 「OpenSSH Client(Beta)」を選択してインストール。

 これでPowerShell上でSSH機能が利用可能になる。
 それにしても、いつBetaが取れるんだろうか……。


4)個人的な理由で公開鍵暗号を作成
 Mac、Linux勢と同じく、まずは.sshディレクトリを作るところから。
 作ったディレクトリはユーザディレクトリの直下にできて、かつ丸見えなのに軽い違和感。

 PS > ssh-keygen

 としたらEd25519方式の鍵が生成された。
 RSA鍵にしようとすると弾かれたので、Ed25519の公開鍵をサーバ側に送って設定。
 Windows10環境だとEd25519方式がデフォルトになっているんだなっと。


5)SSHサーバに楽に接続できるようにconfigファイルを作って設置
→これをやりたいためにVimをインストールしたんだが、別にnotepadで作っても良かったのな。


6)TnsorFlowをインストールする
 GPUを使いたいので、CUDAとcuDNNのインストールは絶対必要。
 どういうわけだか自分のPCにはCUDA9.0がインストール済みだった。
 cuDNNをNvidiaからダウンロード。展開したファイルをCUDAのディレクトリに移動。

 で、TensorFlowをインストールする。

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS > pip3 install --upgrade tensorflow-gpu
Collecting tensorflow-gpu
  Downloading tensorflow_gpu-1.5.0-cp36-cp36m-win_amd64.whl (82.1MB)
    100% |████████████████████████████████| 82.1MB 17kB/s
Collecting tensorflow-tensorboard<1.6.0,>=1.5.0 (from tensorflow-gpu)
  Downloading tensorflow_tensorboard-1.5.1-py3-none-any.whl (3.0MB)
    100% |████████████████████████████████| 3.0MB 467kB/s
Collecting absl-py>=0.1.6 (from tensorflow-gpu)
  Downloading absl-py-0.1.10.tar.gz (79kB)
    100% |████████████████████████████████| 81kB 7.1MB/s
Collecting protobuf>=3.4.0 (from tensorflow-gpu)
  Downloading protobuf-3.5.1-py2.py3-none-any.whl (388kB)
    100% |████████████████████████████████| 389kB 3.2MB/s
Collecting numpy>=1.12.1 (from tensorflow-gpu)
  Downloading numpy-1.14.1-cp36-none-win_amd64.whl (13.4MB)
    100% |████████████████████████████████| 13.4MB 108kB/s
Collecting wheel>=0.26 (from tensorflow-gpu)
  Downloading wheel-0.30.0-py2.py3-none-any.whl (49kB)
    100% |████████████████████████████████| 51kB 6.4MB/s
Collecting six>=1.10.0 (from tensorflow-gpu)
  Downloading six-1.11.0-py2.py3-none-any.whl
Collecting html5lib==0.9999999 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow-gpu)
  Downloading html5lib-0.9999999.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 1.4MB/s
Collecting markdown>=2.6.8 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow-gpu)
  Downloading Markdown-2.6.11-py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 6.6MB/s
Collecting werkzeug>=0.11.10 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow-gpu)
  Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
    100% |████████████████████████████████| 327kB 3.8MB/s
Collecting bleach==1.5.0 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow-gpu)
  Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting setuptools (from protobuf>=3.4.0->tensorflow-gpu)
  Downloading setuptools-38.5.1-py2.py3-none-any.whl (489kB)
    100% |████████████████████████████████| 491kB 2.7MB/s
Installing collected packages: six, html5lib, wheel, numpy, markdown, setuptools, protobuf, werkzeug, bleach, tensorflow-tensorboard, absl-py, tensorflow-gpu
  Running setup.py install for html5lib ... done
  Found existing installation: setuptools 28.8.0
    Uninstalling setuptools-28.8.0:
      Successfully uninstalled setuptools-28.8.0
  Running setup.py install for absl-py ... done
Successfully installed absl-py-0.1.10 bleach-1.5.0 html5lib-0.9999999 markdown-2.6.11 numpy-1.14.1 protobuf-3.5.1 setuptools-38.5.1 six-1.11.0 tensorflow-gpu-1.5.0 tensorflow-tensorboard-1.5.1 werkzeug-0.14.1 wh
eel-0.30.0
PS >

 まあ誰にでもできる簡単インストールだった。
 では、TensorFlowが動作するのか、GPUを認識しているのかテストしてみよう。
 まずはPowerShell上でPythonを起動する。

PS > Python
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow')
>>> sess = tf.Session()
2018-02-28 11:07:39.474339: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to
 use: AVX AVX2
2018-02-28 11:07:39.829653: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1105] Found device 0 with properties:
name: GeForce GTX 970 major: 5 minor: 2 memoryClockRate(GHz): 1.2155
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.31GiB
2018-02-28 11:07:39.835622: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1195] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GT
X 970, pci bus id: 0000:01:00.0, compute capability: 5.2)
>>> print(sess.run(hello))
b'Hello, TensorFlow'
>>>

 動作している模様。
 GeForce GTX970を認識していることもわかる。


7)Kerasをインストールする

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS > pip install keras
Collecting keras
  Downloading Keras-2.1.4-py2.py3-none-any.whl (322kB)
    100% |████████████████████████████████| 327kB 1.8MB/s
Requirement already satisfied: numpy>=1.9.1 in c:\users\hogehoge\appdata\local\programs\python\python36\lib\site-packages (from keras)
Collecting scipy>=0.14 (from keras)
  Downloading scipy-1.0.0-cp36-none-win_amd64.whl (30.8MB)
    100% |████████████████████████████████| 30.8MB 46kB/s
Requirement already satisfied: six>=1.9.0 in c:\users\hogehoge\appdata\local\programs\python\python36\lib\site-packages (from keras)
Collecting pyyaml (from keras)
  Downloading PyYAML-3.12.tar.gz (253kB)
    100% |████████████████████████████████| 256kB 4.6MB/s
Building wheels for collected packages: pyyaml
  Running setup.py bdist_wheel for pyyaml ... done
  Stored in directory: C:\Users\hogehoge\AppData\Local\pip\Cache\wheels\2c\f7\79\13f3a12cd723892437c0cfbde1230ab4d82947ff7b3839a4fc
Successfully built pyyaml
Installing collected packages: scipy, pyyaml, keras
Successfully installed keras-2.1.4 pyyaml-3.12 scipy-1.0.0
PS >

なんかしれっとインストールは終わったみたいだ。
僕の使っているWindows10が、あらかじめPythonのモジュール等が入っていて、インストーラーが吐くかもしれないエラーが回避されていた可能性はある。

それにしても、Bash on Ubuntu on Windowsやら、PowerShellやら、コマンドプロンプトやら、WindowsとUnixの操作体系の混じったようなシェルを使っていると、インストールから含めてわけわかんなくなってくる。

2016年7月2日土曜日

やっとcuDNNが動いたけれど

どうやって動いたのか、未だによくわからない。
色々とシステムをいじくりまわした結果なので。

Macで、Chainer初心者が定番にやるtrain_mnist.py。
CPUモードで動かしている限りはかなり楽勝。
だが、GPUを使う場合は、いろいろとうまく行かなかった。

Python3でサンプルを動かすと、まずエラーが出る。
CUDA、cuDNNをインストールガイドにしたがってインストールしてもエラーが出る。

で、やってみたこと。

1.シンボリックリンクを貼ってみた。
El CaptitanではDYLD_LIBRARY_PATHの上書きが有効にならないらしいため。
$ ln -s /usr/local/cuda/lib/* /usr/local/lib
$ ln -s /Developer/NVIDIA/CUDA-7.5/lib/* /usr/local/lib

出典:http://qiita.com/ayuma0913/items/82c03a8173d3c97bff10

で、とりあえずGPUというかCUDAは動き出した感じ。
でもエラーはまだ出てくる。

$ python train_mnist.py -g 0
GPU: 0
# unit: 1000
# Minibatch-size: 100
# epoch: 20
Network type: simple

load MNIST dataset
Downloading train-images-idx3-ubyte.gz...
Done
Downloading train-labels-idx1-ubyte.gz...
Done
Downloading t10k-images-idx3-ubyte.gz...
Done
Downloading t10k-labels-idx1-ubyte.gz...
Done
Converting training data...
Done
Converting test data...
Done
Save output...
Done
Convert completed
/Users/ほげほげ/.pyenv/versions/anaconda3-4.0.0/lib/python3.5/site-packages/chainer/cuda.py:87: UserWarning: cuDNN is not enabled.
Please reinstall chainer after you install cudnn
(see https://github.com/pfnet/chainer#installation).
  'cuDNN is not enabled.\n'
epoch 1
/Users/ほげほげ/.pyenv/versions/anaconda3-4.0.0/lib/python3.5/site-packages/chainer/cuda.py:87: UserWarning: cuDNN is not enabled.
Please reinstall chainer after you install cudnn
(see https://github.com/pfnet/chainer#installation).
  'cuDNN is not enabled.\n'
graph generated
train mean loss=0.1901665245865782, accuracy=0.942733337233464, throughput=19169.138552723725 images/sec
test  mean loss=0.09113417560467496, accuracy=0.9730000060796737
以下略

CPUモードに比べれば全然速いのだけど、cuDNNが無効というのが気に入らない。

2.OS X 10.11 El CapitanのSystem Integrity Protection(SIP)を無効にしてみる。
自分の環境がちょっと特殊(苦笑)なため、リカバリーモードに入るのに苦労した。

例のインストールUSBからブートして、GUIが起動したらユーティリティのターミナルに入る。
$ csrutil disable
$ reboot

これでSIPが無効になる。
セキュリティ的にSIPは効果的らしいんだけど、まずは切ってみた。
副次効果として、SIP環境下ではアプリがSandboxで動いていたのがそうではなくなるので、動作はサクサクになる。操作感は相当にいいので、Sandboxは重たいのだなって実感。

で、chainerをアンインストール、再インストールする。

で、再挑戦。
やっぱりcuDNN is not enabledと表示される。

3.cuDNNのバージョンを変える。
いままでcuDNN v5(May 12,2016),for CUDA 7.5を使っていたが、cuDNN v5.1 RC (June 16, 2016), for CUDA 7.5にバージョンアップしてみる。

ダウンロードしたフォルダを展開し、それぞれ/Developer/NVIDIA/CUDA-7.5下のフォルダにコピー。

で、chainerをアンインストール、再インストールする。

で、再挑戦。

$ python train_mnist.py -g 0
GPU: 0
# unit: 1000
# Minibatch-size: 100
# epoch: 20
Network type: simple

load MNIST dataset
Downloading train-images-idx3-ubyte.gz...
Done
Downloading train-labels-idx1-ubyte.gz...
Done
Downloading t10k-images-idx3-ubyte.gz...
Done
Downloading t10k-labels-idx1-ubyte.gz...
Done
Converting training data...
Done
Converting test data...
Done
Save output...
Done
Convert completed
epoch 1
graph generated
train mean loss=0.1888404919952154, accuracy=0.9433166711529096, throughput=16031.889196102731 images/sec
test  mean loss=0.09853773100476246, accuracy=0.9686000072956085
epoch 2
train mean loss=0.07550216538365931, accuracy=0.9763166766365369, throughput=22556.0344267401 images/sec
test  mean loss=0.07264450040020165, accuracy=0.9770000052452087
epoch 3
train mean loss=0.04877504045046711, accuracy=0.9843666765093804, throughput=22632.55122509905 images/sec
test  mean loss=0.08447756661909807, accuracy=0.9758000069856644
epoch 4
train mean loss=0.03617304402956506, accuracy=0.9882166763146718, throughput=22372.07331256839 images/sec
test  mean loss=0.07253925856392016, accuracy=0.9785000050067901
epoch 5
train mean loss=0.02990280262378898, accuracy=0.9904666745662689, throughput=22544.69441226119 images/sec
test  mean loss=0.07364016732306482, accuracy=0.9786000072956085
epoch 6
train mean loss=0.02051041631498568, accuracy=0.9932666728893916, throughput=22602.377604251567 images/sec
test  mean loss=0.08061152838608905, accuracy=0.9794000065326691
epoch 7
train mean loss=0.02313764386140974, accuracy=0.9924666731556256, throughput=22608.97704668778 images/sec
test  mean loss=0.07398538219076045, accuracy=0.9813000071048736
epoch 8
train mean loss=0.01743261187949732, accuracy=0.9944333384434382, throughput=22505.744075544564 images/sec
test  mean loss=0.06380779648055522, accuracy=0.9824000066518783
epoch 9
train mean loss=0.017964339632999326, accuracy=0.9940833389759064, throughput=22634.045328307027 images/sec
test  mean loss=0.07465034724052032, accuracy=0.9814000070095062
epoch 10
train mean loss=0.01577379300415032, accuracy=0.9950333380699158, throughput=22556.450902358458 images/sec
test  mean loss=0.07825881053931426, accuracy=0.9805000072717667
epoch 11
train mean loss=0.01135890639123924, accuracy=0.9965166698892911, throughput=22567.71775072012 images/sec
test  mean loss=0.0798807725448205, accuracy=0.9818000066280365
epoch 12
train mean loss=0.014168494007887299, accuracy=0.9959166704614957, throughput=22579.648188927462 images/sec
test  mean loss=0.08158497168318718, accuracy=0.9832000064849854
epoch 13
train mean loss=0.01492698869720698, accuracy=0.9956500040491422, throughput=22598.878417525055 images/sec
test  mean loss=0.08872715805148687, accuracy=0.980800005197525
epoch 14
train mean loss=0.009995754014827678, accuracy=0.9964000033338865, throughput=22575.31759377331 images/sec
test  mean loss=0.08891520491886695, accuracy=0.9828000068664551
epoch 15
train mean loss=0.010710552199502388, accuracy=0.9967833363016446, throughput=22601.20229250324 images/sec
test  mean loss=0.08941980777552216, accuracy=0.981700005531311
epoch 16
train mean loss=0.01189803228019097, accuracy=0.9964333367347717, throughput=22547.26976868952 images/sec
test  mean loss=0.09618509650914689, accuracy=0.982900008559227
epoch 17
train mean loss=0.007446224756765029, accuracy=0.9976333355903626, throughput=22305.55383457275 images/sec
test  mean loss=0.09916683364292737, accuracy=0.9818000072240829
epoch 18
train mean loss=0.008587048528358612, accuracy=0.9974333357810974, throughput=22575.74288268399 images/sec
test  mean loss=0.1162207845506302, accuracy=0.9780000072717666
epoch 19
train mean loss=0.012611796275693409, accuracy=0.9964500031868617, throughput=22599.16659265566 images/sec
test  mean loss=0.09134372303703306, accuracy=0.9832000058889389
epoch 20
train mean loss=0.005183243499375673, accuracy=0.9983000016212463, throughput=22576.352491400365 images/sec
test  mean loss=0.1039556429999165, accuracy=0.9821000063419342
save the model
save the optimizer

ああ、動いたなあ。
そして速いなあ…………。

結局1〜3のどれが効いたのかよくわからない。
2のSIP無効化は必要ないのかもしれない。
そのうち検証してみようと思う。