AutoKeras 1.0 : Tutorials : 画像分類 (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 03/21/2020
* 本ページは、AutoKeras の以下のページを翻訳した上で適宜、補足説明したものです:
- Getting Started : Image Classification
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
Tutorials : 画像分類
単純なサンプル
最初のステップはデータを準備することです。ここではサンプルとして MNIST データセットを使用します。
from tensorflow.keras.datasets import 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)
2 番目のステップは ImageClassifier を実行します。
import autokeras as ak # Initialize the image classifier. clf = ak.ImageClassifier(max_trials=1) # It tries 10 different models. # Feed the image classifier with training data. clf.fit(x_train, y_train,epochs=3)
# 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))
検証データ
デフォルトでは、AutoKeras は訓練データの最後の 20% を検証データとして使用します。下のサンプルで示されるように、パーセンテージを指定するために validation_split を使用できます。
clf.fit(x_train, y_train, # Split the training data and use the last 15% as validation data. validation_split=0.15,epochs=3)
それを訓練データから分割する代わりに、validation_data で貴方自身の検証セットを使用することもできます。
split = 50000 x_val = x_train[split:] y_val = y_train[split:] x_train = x_train[:split] y_train = y_train[:split] clf.fit(x_train, y_train, # Use your own validation set. validation_data=(x_val, y_val),epochs=3)
カスタマイズされた探索空間
上級ユーザのために、ImageClassifier の代わりに AutoModel を使用して探索空間をカスタマイズしても良いです。幾つかの高位設定のために ImageBlock を設定することができます、e.g., 探索するニューラルネットワークのタイプのための block_type、データ正規化を行なうか否かのための normlize、データ増強を行なうか否かのための augment。これらの引数を指定しないこともできます、これは異なる選択が自動的に調整されるようにするでしょう。詳細のために次のサンプルを見てください。
import autokeras as ak input_node = ak.ImageInput() output_node = ak.ImageBlock( # Only search ResNet architectures. block_type='resnet', # Normalize the dataset. normalize=True, # Do not do data augmentation. augment=False)(input_node) output_node = ak.ClassificationHead()(output_node) clf = ak.AutoModel(inputs=input_node, outputs=output_node, max_trials=10) clf.fit(x_train, y_train,epochs=3)
AutoModel の利用方法は Keras の functional API に類似しています。基本的には、グラフを構築しています、そのエッジはブロックでノードはブロックの中間出力です。output_node = ak.some_block(input_node) で input_node から output_node へのエッジを追加します。
更に探索空間をカスタマイズするためにより極め細かいブロックを利用することもまた可能です。次のサンプルを見てください。
import autokeras as ak input_node = ak.ImageInput() output_node = ak.Normalization()(input_node) output_node = ak.ImageAugmentation(percentage=0.3)(output_node) output_node = ak.ResNetBlock(version='v2')(output_node) output_node = ak.ClassificationHead()(output_node) clf = ak.AutoModel(inputs=input_node, outputs=output_node, max_trials=10) clf.fit(x_train, y_train,epochs=3)
データ形式
AutoKeras ImageClassifier はデータ形式について非常に柔軟です。画像については、それはチャネル次元ありとなしの両者のデータ形式を受け取ります。MNIST データセットの画像はチャネル次元を持ちません。各画像は shape (28, 28) の行列です。AutoKeras はまたチャネル次元を最後に持つ, e.g., (32, 32, 3), (28, 28, 1)、3 つの次元の画像も受け取ります。
分類ラベルについては、AutoKeras は plain ラベル, i.e. 文字列か整数、そして one-hot エンコードラベル, i.e. 0 と 1 のベクトルの両者を受け取ります。
そのため次の方法で貴方のデータを準備すれば、ImageClassifier は依然として動作するはずです。
from tensorflow.keras.datasets import mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() # Reshape the images to have the channel dimension. x_train = x_train.reshape(x_train.shape + (1,)) x_test = x_test.reshape(x_test.shape + (1,)) # One-hot encode the labels. import numpy as np eye = np.eye(10) y_train = eye[y_train] y_test = eye[y_test] print(x_train.shape) # (60000, 28, 28, 1) print(y_train.shape) # (60000, 10) print(y_train[:3]) # array([[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], # [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], # [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
訓練データのために tf.data.Dataset 形式を使用することもサポートします。この場合、画像は 3-次元でなければならないでしょう。ラベルは tensorflow Dataset 内にラップされるためマルチクラス分類のために one-hot エンコードされなければなりません。
import tensorflow as tf from tensorflow.python.keras.utils.data_utils import Sequence train_set = tf.data.Dataset.from_tensor_slices(((x_train, ), (y_train, ))) test_set = tf.data.Dataset.from_tensor_slices(((x_test, ), (y_test, ))) clf = ak.ImageClassifier(max_trials=10) # Feed the tensorflow Dataset to the classifier. clf.fit(train_set) # Predict with the best model. predicted_y = clf.predict(test_set) # Evaluate the best model with testing data. print(clf.evaluate(test_set))
以上