ディープラーニングモデルを自動で構築するAutoKerasを試してみる

前回の記事で、Minisforum B550にGTX1650のグラフィックボードを外付けして、CUDAやCuDNNをインストールするところまで作業を進めました。

ここからはTensorFlowを導入して、最終的な目的であるAutoKerasを動作させます。(普段はM1 Max 24コアのMacBook Pro 14インチで作業しているのですが、TensorFlowは問題なく動作するものの、どうもAutoKerasが導入できなくてWindows+WSL2 Ubuntu環境を使うことにしたのでした。)

Python

WSL2上のPythonは、anyenv+pyenvを使って導入しています。使っているのは、素のPython 3.10.5です。(Anacondaやminicondaではありません。)
anyenv+pyenvを使ったPythonの環境構築は、こちらを参考に。

できあがったPython環境にJupyter Notebookをインストールしておきます。

pip install jupyter notebook

この後の操作ではJupyter Notebookを使うので、起動しておきます。

jupyter notebook

TensorFlow

次に、TensorFlowのインストールです。操作は、これだけ。

pip install tensorflow

Jupyter Notebookで下記のプログラムを実行します。

from  tensorflow.python.client import device_lib
device_lib.list_local_devices()

きちんとGPU(GTX1650)を認識していることが分かります。

[name: "/device:CPU:0"
 device_type: "CPU"
 memory_limit: 268435456
 locality {
 }
 incarnation: 7168474680457350950
 xla_global_id: -1,
 name: "/device:GPU:0"
 device_type: "GPU"
 memory_limit: 2210083636
 locality {
   bus_id: 1
   links {
   }
 }
 incarnation: 4388315488285514043
 physical_device_desc: "device: 0, name: NVIDIA GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5"
 xla_global_id: 416903419]

AutoKerasのインストール

AutoKerasは、ディープラーニングモデルを自動で構築してくれるツールです。

下記のようなタスクに対応しています。

  • Image Classification
  • Image Regression
  • Text Classification
  • Text Regression
  • Structured Data Classification
  • Structured Data Regression

では、インストールです。先にkeras-tunerを導入し、その後で、autokerasを導入します。

pip install git+https://github.com/keras-team/keras-tuner.git
pip install autokeras

AutoKerasを使う

ここからは、Image Classification(画像分類)のサンプルどおりにやってみます。

まず、必要なライブラリをインポートします。

import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import mnist

import autokeras as ak

使用するデータセットをロードします。MNISTですね。

(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape)  # (60000, 28, 28)
print(y_train.shape)  # (60000,)
print(y_train[:3])  # array([7, 2, 1], dtype=uint8)

AutoKerasのImageClassifierを使ってモデルのトレーニングを行い、続けてテストデータでの予測の実行と、評価を行います。
ImageClassifier()の引数にmax_trialsがありますが、ここに指定した数だけのモデル作成を行ってくれます。その中で最も良好な結果を残したモデルを使えば良いわけですね。

# Initialize the image classifier.
clf = ak.ImageClassifier(overwrite=True, max_trials=1)
# Feed the image classifier with training data.
clf.fit(x_train, y_train, epochs=10)

# Predict with the best model.
predicted_y = clf.predict(x_test)
print(predicted_y)

# Evaluate the best model with testing data.
print(clf.evaluate(x_test, y_test))

実行すると、下記のような出力が行われます。

Trial 1 Complete [00h 01m 46s]
val_loss: 0.04240993410348892

Best val_loss So Far: 0.04240993410348892
Total elapsed time: 00h 01m 46s
INFO:tensorflow:Oracle triggered exit
Epoch 1/10
1875/1875 [==============================] - 11s 6ms/step - loss: 0.1651 - accuracy: 0.9494
(省略)
Epoch 10/10
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0307 - accuracy: 0.9897
WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 2 of 2). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ./image_classifier/best_model/assets
INFO:tensorflow:Assets written to: ./image_classifier/best_model/assets
313/313 [==============================] - 1s 2ms/step
313/313 [==============================] - 0s 2ms/step
[['7']
 ['2']
 ['1']
 ...
 ['4']
 ['5']
 ['6']]
313/313 [==============================] - 1s 4ms/step - loss: 0.0400 - accuracy: 0.9876
[0.04003762826323509, 0.9876000285148621]

テストデータを用いた評価では、0.9876という正解率(Accuracy)が出ています。

作成したモデルは、image_classifier/best_modelにpbファイルとして保存されているので、これを使って実際の分類処理を行うことができるわけです。

今回構築したAutoKerasの環境を使って、実際のお仕事のデータでモデルを作ってみようと思います。そこで分かったTipsがあったら、また書くことにします。

この記事を書いた人

井上 研一

株式会社ビビンコ代表取締役、ITエンジニア/経済産業省推進資格ITコーディネータ。AI・IoTに強いITコーディネータとして活動。2018年、株式会社ビビンコを北九州市に創業。IoTソリューションの開発・導入や、画像認識モデルを活用したアプリの開発などを行っている。近著に「使ってわかった AWSのAI」、「ワトソンで体感する人工知能」。日本全国でセミナー・研修講師としての登壇も多数。