覚書：Windows版TensorFlowでGPUを使ってアヤメデータの分類をやってみた

本記事は、[こちらのページ](https://www.tensorflow.org/install/gpu?hl=ja)を元に書かれています。

#まえおき
いまさらですが、Windows上でTensorflowを利用してアヤメデータの分類を行うまでの結果をまとめました。

#資料について
[Tensorflowの元ネタはこちら](https://www.tensorflow.org/install/gpu?hl=ja)を参照してください。
[CUDA環境構築の元ネタはこちら](https://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows)を参照してください。
[Eclipse日本語版はこちらから](https://mergedoc.osdn.jp/)ダウンロードできます。
[pip公式サイトはこちら](https://bootstrap.pypa.io/get-pip.py)を参照してください。
[kerasについてはこちら](https://keras.io/ja/)を参照してください。

#その他のネタ
おぼえがきなので特にネタはないです。

#セットアップ
使用する機材は下記の実行環境です。

・AMD:Ryzen 3700x
・GPU:RTX3090
・OS:Windows10
・NVIDIA GPU Computing Toolkit:CUDA11.1
・CUDANN:CUDA11.1
・IDE:Eclipse Ultimate Version: 2020-06 (4.16.0)


###EclipseでPython
EclipseでUltimateをダウンロードしている場合、Pythonの環境は最初から入っているのですが、初期設定がおかしく、そのままではインタープリターのManage pipが動きません。Command to executeにコマンドを入力後、実行をおしても、何も起きないまま終了してしまいます。Eclipseで使用しているPythonでget-pip.pyを実行することで動くようになります。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/732297/66a117fa-6364-fa18-d673-d7d1abd5de6e.png)

Eclipseで使用しているPython3は、<Eclipseのインストールディレクトリ>/python/3の配下にあります。[get-pip.pyはこちら](https://pip.pypa.io/en/stable/installing/)にあるので、内容をファイルに保存して実行してください。[pip公式サイトはこちら](https://bootstrap.pypa.io/get-pip.py)を参照してください。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/732297/bc04798f-78b1-60b5-d243-072100671c84.png)

tesorflow-gpuをインストールします。と言っても、install tenserflow-gpuと打って実行ボタンを押すだけです。その他、keras、pandas、sklearnと使用するであろうライブラリを導入していきます。とりあえず、既出の3つをインストールしました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/732297/50709ae5-cf6a-5cfa-ee3d-a8b56613c87c.png)

アヤメデータを読み込み分類する実装を行います。iris.pyとして下記のように実装しました。

```python:iris.py
from sklearn import datasets
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.utils import np_utils
from sklearn import preprocessing
from sklearn.model_selection import train_test_split

# アヤメデータを読み込み
iris = datasets.load_iris()

# x_irisのデータは下記のようになっています。
# がくの長さ、がくの幅、花弁の長さ、花弁の幅
x_iris = iris.data
# print(x_iris[:1])
# [[5.1 3.5 1.4 0.2]]

# y_irisは下記のようになっています。
# 0: Setosa, 1: Versicolor, 2: Virginica
y_iris = iris.target
# print(y_iris[:1])
# [0]

# アヤメデータの正規化
x_scale = preprocessing.scale(x_iris)
# print(x_scale[:1])
# [[-0.90068117  1.01900435 -1.34022653 -1.3154443 ]]

# カテゴリデータをOne-Hot形式に変換
y_categorical = np_utils.to_categorical(y_iris)
# print(y_categorical[:1])
# [[1. 0. 0.]]

# 学習用データ70%とテスト用データ30%に分割
x_scale_train, x_scale_test, y_categorical_train, y_categorical_test = train_test_split(x_scale, y_categorical, test_size=0.3, random_state=True)
# print(x_scale_train[:1])
# [[ 2.24968346 -1.05276654  1.78583195  1.44883158]]
# print(x_scale_test[:1])
# [[-0.05250608  2.16998818 -1.45390138 -1.3154443 ]]
# print(y_categorical_train[:1])
# [[0. 0. 1.]]
# print(y_categorical_test[:1])
# [[1. 0. 0.]]

# モデル
model = Sequential()

# 第一引数:隠れ層のニューロン数
# 第二引数:入力層の指定(がくの長さ、幅、花弁の長さ、幅)
model.add(Dense(32, input_shape=(4,)))

# 出力層の指定(ヒオウギアヤメ、バージカラー、バージニカ)
model.add(Dense(3))

# 活性化関数の指定
model.add(Activation('softmax'))

# optimizer、lossなどはkerasのページ参照
# https://keras.io/api/optimizers/
# https://keras.io/api/losses/
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(x_scale_train, y_categorical_train, epochs=50, batch_size=10, verbose=1)

loss, accuracy = model.evaluate(x_scale_test, y_categorical_test, verbose=0)

# 正解率
print('正解率', '{:.2f}'.format(accuracy))
# 損失率
print('損失率', '{:.2f}'.format(loss))

# テストデータ
print('入力データ', x_scale_test[:1])

# テストデータのカテゴリ Setosa:0, Versicolor:1, Virginica:2
print('分類データ', y_categorical_test[:1])

# 予測
print('予測', model.predict_classes(x_scale_test[:1], batch_size=1))



```

#実行
iris.pyを実行した結果が下記になります。

```test:実行ログ
pydev debugger: starting (pid: 10160)
2020-10-05 15:00:02.688675: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-10-05 15:00:05.113446: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-10-05 15:00:05.136723: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: 
pciBusID: 0000:09:00.0 name: GeForce RTX 3090 computeCapability: 8.6
coreClock: 1.695GHz coreCount: 82 deviceMemorySize: 24.00GiB deviceMemoryBandwidth: 871.81GiB/s
2020-10-05 15:00:05.136997: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-10-05 15:00:05.140151: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2020-10-05 15:00:05.143597: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2020-10-05 15:00:05.144763: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2020-10-05 15:00:05.148446: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll
2020-10-05 15:00:05.151001: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll
2020-10-05 15:00:05.157317: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-10-05 15:00:05.157522: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-10-05 15:00:05.158211: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-10-05 15:00:05.166391: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1a79ea9c6d0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-10-05 15:00:05.166542: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-10-05 15:00:05.166817: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: 
pciBusID: 0000:09:00.0 name: GeForce RTX 3090 computeCapability: 8.6
coreClock: 1.695GHz coreCount: 82 deviceMemorySize: 24.00GiB deviceMemoryBandwidth: 871.81GiB/s
2020-10-05 15:00:05.167066: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-10-05 15:00:05.167186: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2020-10-05 15:00:05.167340: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2020-10-05 15:00:05.167459: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2020-10-05 15:00:05.167578: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll
2020-10-05 15:00:05.167710: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll
2020-10-05 15:00:05.167824: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-10-05 15:00:05.167988: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-10-05 15:03:48.113686: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-10-05 15:03:48.113823: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]      0 
2020-10-05 15:03:48.113877: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1276] 0:   N 
2020-10-05 15:03:48.114144: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 21823 MB memory) -> physical GPU (device: 0, name: GeForce RTX 3090, pci bus id: 0000:09:00.0, compute capability: 8.6)
2020-10-05 15:03:48.117147: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1a803843cc0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-10-05 15:03:48.117264: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce RTX 3090, Compute Capability 8.6
Epoch 1/50
2020-10-05 15:03:49.552709: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
pydev debugger: Unable to find module to reload: "benchmark.benchmark".

 1/11 [=>............................] - ETA: 0s - loss: 1.2367 - accuracy: 0.1000
11/11 [==============================] - 0s 2ms/step - loss: 1.0737 - accuracy: 0.2952
Epoch 2/50

 1/11 [=>............................] - ETA: 0s - loss: 1.1483 - accuracy: 0.1000
11/11 [==============================] - 0s 2ms/step - loss: 0.9888 - accuracy: 0.4476
Epoch 3/50

 1/11 [=>............................] - ETA: 0s - loss: 0.9464 - accuracy: 0.5000
11/11 [==============================] - 0s 2ms/step - loss: 0.9078 - accuracy: 0.5619
Epoch 4/50

 1/11 [=>............................] - ETA: 0s - loss: 0.7831 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.8374 - accuracy: 0.7333
Epoch 5/50

 1/11 [=>............................] - ETA: 0s - loss: 0.7165 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.7740 - accuracy: 0.8095
Epoch 6/50

 1/11 [=>............................] - ETA: 0s - loss: 0.6278 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.7159 - accuracy: 0.8476
Epoch 7/50

 1/11 [=>............................] - ETA: 0s - loss: 0.7164 - accuracy: 0.7000
11/11 [==============================] - 0s 2ms/step - loss: 0.6646 - accuracy: 0.8476
Epoch 8/50

 1/11 [=>............................] - ETA: 0s - loss: 0.5403 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.6175 - accuracy: 0.8571
Epoch 9/50

 1/11 [=>............................] - ETA: 0s - loss: 0.5426 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.5751 - accuracy: 0.8571
Epoch 10/50

 1/11 [=>............................] - ETA: 0s - loss: 0.4794 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.5366 - accuracy: 0.8571
Epoch 11/50

 1/11 [=>............................] - ETA: 0s - loss: 0.5594 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.5022 - accuracy: 0.8571
Epoch 12/50

 1/11 [=>............................] - ETA: 0s - loss: 0.5516 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.4724 - accuracy: 0.8571
Epoch 13/50

 1/11 [=>............................] - ETA: 0s - loss: 0.5247 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.4459 - accuracy: 0.8571
Epoch 14/50

 1/11 [=>............................] - ETA: 0s - loss: 0.4824 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.4215 - accuracy: 0.8667
Epoch 15/50

 1/11 [=>............................] - ETA: 0s - loss: 0.4286 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.4007 - accuracy: 0.8762
Epoch 16/50

 1/11 [=>............................] - ETA: 0s - loss: 0.5303 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.3819 - accuracy: 0.8762
Epoch 17/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3834 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.3660 - accuracy: 0.8857
Epoch 18/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3036 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.3505 - accuracy: 0.8952
Epoch 19/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2763 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.3379 - accuracy: 0.8952
Epoch 20/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2408 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.3260 - accuracy: 0.8952
Epoch 21/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3472 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.3152 - accuracy: 0.8952
Epoch 22/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1487 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.3053 - accuracy: 0.8952
Epoch 23/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1621 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.2962 - accuracy: 0.8952
Epoch 24/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3064 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.2881 - accuracy: 0.8952
Epoch 25/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3553 - accuracy: 0.7000
11/11 [==============================] - 0s 2ms/step - loss: 0.2800 - accuracy: 0.8952
Epoch 26/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3569 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.2729 - accuracy: 0.9048
Epoch 27/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2459 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.2659 - accuracy: 0.9048
Epoch 28/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3193 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.2593 - accuracy: 0.9048
Epoch 29/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1197 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.2532 - accuracy: 0.9048
Epoch 30/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3457 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.2466 - accuracy: 0.9048
Epoch 31/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2704 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.2413 - accuracy: 0.9238
Epoch 32/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1812 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.2361 - accuracy: 0.9143
Epoch 33/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1858 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.2309 - accuracy: 0.9143
Epoch 34/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2704 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.2260 - accuracy: 0.9143
Epoch 35/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3128 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.2219 - accuracy: 0.9143
Epoch 36/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1038 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.2172 - accuracy: 0.9143
Epoch 37/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2709 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.2133 - accuracy: 0.9143
Epoch 38/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1376 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.2086 - accuracy: 0.9238
Epoch 39/50

 1/11 [=>............................] - ETA: 0s - loss: 0.3896 - accuracy: 0.7000
11/11 [==============================] - 0s 2ms/step - loss: 0.2047 - accuracy: 0.9238
Epoch 40/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2730 - accuracy: 0.8000
11/11 [==============================] - 0s 2ms/step - loss: 0.2014 - accuracy: 0.9333
Epoch 41/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1550 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.1970 - accuracy: 0.9333
Epoch 42/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1230 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.1928 - accuracy: 0.9333
Epoch 43/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2601 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.1897 - accuracy: 0.9429
Epoch 44/50

 1/11 [=>............................] - ETA: 0s - loss: 0.0503 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.1868 - accuracy: 0.9524
Epoch 45/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2105 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.1831 - accuracy: 0.9524
Epoch 46/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1636 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.1799 - accuracy: 0.9429
Epoch 47/50

 1/11 [=>............................] - ETA: 0s - loss: 0.2297 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.1777 - accuracy: 0.9619
Epoch 48/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1763 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.1734 - accuracy: 0.9619
Epoch 49/50

 1/11 [=>............................] - ETA: 0s - loss: 0.1937 - accuracy: 0.9000
11/11 [==============================] - 0s 2ms/step - loss: 0.1708 - accuracy: 0.9524
Epoch 50/50

 1/11 [=>............................] - ETA: 0s - loss: 0.0477 - accuracy: 1.0000
11/11 [==============================] - 0s 2ms/step - loss: 0.1677 - accuracy: 0.9619
正解率 0.87
損失率 0.25
テストデータ [[-0.05250608  2.16998818 -1.45390138 -1.3154443 ]]
テストデータ分類 [[1. 0. 0.]]
WARNING:tensorflow:From C:\tools\pleiades-2020-06\pleiades\workspace\benchmark\benchmark\benchmark.py:77: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.
Instructions for updating:
Please use instead:* `np.argmax(model.predict(x), axis=-1)`,   if your model does multi-class classification   (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,   if your model does binary classification   (e.g. if it uses a `sigmoid` last-layer activation).
予想値 [0]

```

#内容の説明
覚書なので、特に踏み込んだ説明はここでは書かないです。ワタシより詳しい人がいろいろ書いているので、そちらを参照していただいたほうがよいかと思われます。

#感想
Kerasは処理速度遅です。対応策は調べればでてきますが、それでも遅いと思うのはワタシだけでしょうか。
CPUだいぶ余力あるのに帰ってこないです。