TIPCIFAR-100を使ったtensorflowの多クラス分類のチュートリアル記事。 CIFAR-10を使った実験記事だとよくあるので、CIFAR-100でやってみました。 スーパークラスの20クラス分類にしたからCIFAR-10の分類と変わらないんだけれども。
私の環境
- GPU
のBTOパソコン - pyenvでインストールしたpython=3.7.11
- jupyter notebook
必要ライブラリ
vscodeを使ってGPUパソコンにSSH接続してjupyter notebookを起動!
requirements.txtを以下に示した。
absl-py==0.8.1appnope==0.1.0astor==0.8.0astunparse==1.6.3attrs==19.2.0backcall==0.1.0bleach==3.1.0cachetools==4.1.1certifi==2020.6.20chardet==3.0.4cycler==0.10.0decorator==4.4.0defusedxml==0.6.0entrypoints==0.3gast==0.3.3google-auth==1.18.0google-auth-oauthlib==0.4.1google-pasta==0.2.0grpcio==1.44.0h5py==2.10.0idna==2.10imageio==2.6.1importlib-metadata==0.23ipykernel==5.1.2ipython==7.8.0ipython-genutils==0.2.0ipywidgets==7.5.1jedi==0.15.1Jinja2==2.10.3jsonschema==3.1.1jupyter==1.0.0jupyter-client==5.3.4jupyter-console==6.0.0jupyter-core==4.6.0Keras==2.3.1Keras-Applications==1.0.8Keras-Preprocessing==1.1.0kiwisolver==1.1.0Markdown==3.1.1MarkupSafe==1.1.1matplotlib==3.1.1mistune==0.8.4more-itertools==7.2.0music21==5.7.0nbconvert==5.6.0nbformat==4.4.0networkx==2.3notebook==6.0.1numpy==1.21.5oauthlib==3.1.0opt-einsum==3.1.0pandas==0.25.1pandocfilters==1.4.2parso==0.5.1pexpect==4.7.0pickleshare==0.7.5Pillow==6.2.0prometheus-client==0.7.1prompt-toolkit==2.0.10protobuf==3.10.0ptyprocess==0.6.0pyasn1==0.4.8pyasn1-modules==0.2.8pydot==1.4.1pydotplus==2.0.2Pygments==2.4.2pyparsing==2.4.2pyrsistent==0.15.4python-dateutil==2.8.0pytz==2019.3PyWavelets==1.3.0PyYAML==5.1.2pyzmq==18.1.0qtconsole==4.5.5requests==2.24.0requests-oauthlib==1.3.0rsa==4.6scikit-image==0.17.2scipy==1.4.1Send2Trash==1.5.0six==1.12.0tensorboard==2.2.2tensorboard-plugin-wit==1.7.0tensorflow==2.2.0tensorflow-addons==0.10.0tensorflow-estimator==2.2.0termcolor==1.1.0terminado==0.8.2testpath==0.4.2tifffile==2021.11.2tornado==6.0.3traitlets==4.3.3typeguard==2.9.1urllib3==1.25.9wcwidth==0.1.7webencodings==0.5.1Werkzeug==0.16.0widgetsnbextension==3.5.1wrapt==1.11.2zipp==0.6.0再現したい人がいたら、python=3.7.11環境で `pip install -r requirements.txt` すると環境が構築できるよ。
CUDAは11.5が入ってます。
ソースコード解説
ライブラリのインポート
トレーニングまでに必要なライブラリをインポートする。jupoyter notebookを使う場合はユニットごとにセルにコピペするといい感じになる。
import numpy as np
from tensorflow.keras.layers import Input, Flatten, Dense, Conv2D, BatchNormalization, LeakyReLU, Dropout, Activationfrom tensorflow.keras.models import Modelfrom tensorflow.keras.optimizers import Adamfrom tensorflow.keras.utils import to_categoricalimport tensorflow.keras.backend as K
from tensorflow.keras.datasets import cifar100クラス数の設定
# 学習させるデータセットによって変わるNUM_CLASSES = 20データ読み込み
(x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='coarse')ちょいとデータの形状を確認してみる。
x_train[0].shape
データの前処理
多クラス分類を学習させるために、yデータのone hot ベクトル化を行う。keras.utilsの関数が使えるので楽。
# データの正規化x_train = x_train.astype('float32') / 255.0x_test = x_test.astype('float32') / 255.0# 正解データのone_hotデータ化y_train = to_categorical(y_train, NUM_CLASSES)y_test = to_categorical(y_test, NUM_CLASSES)レイヤーの構築
input_layer = Input(shape=(32,32,3))# 畳み込み層(入力)conv_layer_1 = Conv2D( filters = 10 , kernel_size = (4,4) , strides = 2 , padding = 'same' )(input_layer)# 畳み込み層conv_layer_2 = Conv2D( filters = 20 , kernel_size = (3,3) , strides = 2 , padding = 'same' )(conv_layer_1)# 平坦化層flatten_layer = Flatten()(conv_layer_2)# 全結合層(出力)output_layer = Dense(units=20, activation = 'softmax')(flatten_layer)
model = Model(input_layer, output_layer)モデル情報をちょいと確認する。
model.summary()
モデルの構築
各レイヤーの後に、正規化を行うBatchNormalizationとLeakyReLu層を挟むことによって精度向上を図るという試み。
input_layer = Input((32,32,3))
x = Conv2D(filters = 32, kernel_size = 3, strides = 1, padding = 'same')(input_layer)x = BatchNormalization()(x)x = LeakyReLU()(x)
x = Conv2D(filters = 32, kernel_size = 3, strides = 2, padding = 'same')(x)x = BatchNormalization()(x)x = LeakyReLU()(x)
x = Conv2D(filters = 64, kernel_size = 3, strides = 1, padding = 'same')(x)x = BatchNormalization()(x)x = LeakyReLU()(x)
x = Conv2D(filters = 64, kernel_size = 3, strides = 1, padding = 'same')(x)x = BatchNormalization()(x)x = LeakyReLU()(x)
x = Conv2D(filters = 64, kernel_size = 3, strides = 2, padding = 'same')(x)x = BatchNormalization()(x)x = LeakyReLU()(x)
x = Flatten()(x)
x = Dense(128)(x)x = BatchNormalization()(x)x = LeakyReLU()(x)x = Dropout(rate = 0.5)(x)
x = Dense(NUM_CLASSES)(x)output_layer = Activation('softmax')(x)
model = Model(input_layer, output_layer)ちょいとモデルを確認。
model.summary()
最適化アルゴリズムを設定し、コンパイル
最適化アルゴリズムは有名なAdamを用いる。そしてコンパイル。モデルを構成してコンパイル。モデルを構成してコンパイル。逃げちゃダメだ、逃げちゃダメだ。
opt = Adam(lr=0.0005)model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])モデルのトレーニング
何回か試してみて、結構学習するほど正解率が上がっていったので、 今回は100エポック学習させてみる。
model.fit(x_train , y_train , batch_size=32 , epochs=100 , shuffle=True , validation_data = (x_test, y_test))モデルの評価
model.evaluate(x_test, y_test, batch_size=1000)結果は以下の通りとなった。 loss: 2.8621 - accuracy: 0.5325
うーん。結構低い。 100エポックも学習させる意味はあまりないのかも。
モデル構成をいじったり、パラメータやエポック数を変えることで精度はだいぶん変わるので、 気になる方は試してみてくだされ。
推論
# 推論のためのラベル設定super_class_names = [ 'aquatic mammals', # 0:水生哺乳類 'fish', # 1:魚 'flowers', # :花 'food containers', # 3:食品容器 'fruit and vegetables', # 4:果物と野菜 'household electrical devices', # 5:家電 'household furniture', # 6:家具 'insects', # 7:昆虫 'large carnivores', # 8:大型の肉食動物 'large man-made outdoor things', # 9:大型の建造物 'large natural outdoor scenes', # 10:大自然の風景 'large omnivores and herbivores', # 11:大型の雑食動物と草食動物 'medium-sized mammals', # 12:中型の哺乳類 'non-insect invertebrates', # 13:昆虫ではない無脊椎動物 'people', # 14:人 'reptiles', # 15:爬虫類 'small mammals', # 16:小型の哺乳類 'trees', # 17:木 'vehicles 1', # 18:車両1 'vehicles 2', # 19:車両2]
# 推論CLASSES = np.array(super_class_names)
preds = model.predict(x_test)preds_single = CLASSES[np.argmax(preds, axis = -1)]actual_single = CLASSES[np.argmax(y_test, axis = -1)]結果を確認する
import matplotlib.pyplot as plt# 日本語を表示させるためのフォントの設定plt.rcParams['font.family'] = "Noto Serif CJK JP"
n_to_show = 10indices = np.random.choice(range(len(x_test)), n_to_show)
fig = plt.figure(figsize=(25, 5))fig.subplots_adjust(hspace=1, wspace=0.8)
for i, idx in enumerate(indices): img = x_test[idx] ax = fig.add_subplot(1, n_to_show, i+1) ax.axis('off') ax.text(0.5, -0.3, '==推論結果==', fontsize=12, ha='center', transform=ax.transAxes) ax.text(0.5, -0.6, super_class_dic[str(preds_single[idx])], fontsize=12, ha='center', transform=ax.transAxes) ax.text(0.5, -0.9, '==正解ラベル==', fontsize=12, ha='center', transform=ax.transAxes) ax.text(0.5, -1.2, super_class_dic[str(actual_single[idx])], fontsize=12, ha='center', transform=ax.transAxes) ax.imshow(img)少し解像度が低いが、以下に結果を示した。 正解率半分程度とあり、打倒な結果のように思える。

まとめ
- tensorflowを使って多クラス分類を行う工程を若干の説明と共に掲載した。
- tensorflowの環境を作ってさえしまえば、パラメータやレイヤーをいじくり回して結構遊べるのでおすすめです。
というわけで、今回はここまで。
おまけ
フォントを “Noto Serif CJK JP”に決定したのは、以下のコードで日本語と英語が両方ちゃんと表示されているものを選んだから。
import matplotlib
fonts = set([f.name for f in matplotlib.font_manager.fontManager.ttflist])
plt.figure(figsize=(10,len(fonts)/4))
for i, font in enumerate(fonts): plt.text(0, i, f"日本語:{font}", fontname=font)
plt.ylim(0, len(fonts))plt.axis("off")
plt.show()