ハードウェア技術者のスキルアップ日誌

某家電メーカーの技術者がスキルアップのために勉強したことを記録するブログです

Keras - 学習済みモデルを流用する

KerasではImageNetのデータセットで学習した画像分類のモデルが準備されており、ダウンロードして使用することができます。
Applications - Keras Documentation

Mobilenetの学習済みモデルを使おうとしたときに少し手こずったところがあったので備忘録として使い方を書いておきます。

 

学習済みモデルのダウンロード方法

サンプルとしてMobilenetのモデルをダウンロードするコードを載せます。
keras.applicationsの下に様々なモデルがあるので、使いたいモデルを指定すればOKです。VGG16であれば、applications.vgg16.VGG16()となります。

from keras import applications

model = applications.mobilenet.MobileNet(input_shape=None, alpha=1.0,
depth_multiplier=1, dropout=1e-3,
include_top=True, weights='imagenet',
input_tensor=None, pooling=None, classes=1000)

また、引数の意味をいくつか抜粋して説明します。

  • weights : 重みを乱数とするか(None)、
                   imagenetで学習したものとするか('imagenet')
  • include_top : ネットワークに全結合層を含むかどうか
          Trueだと全結合層付きのモデル、Falseだと全結合層なしのモデル
  • pooling : 特徴量抽出後のPooling Layerの指定 (include_topがFalseの時に有効)
         - None : 畳み込み層
                   - 'avg' : global average pooling
                   - 'max' : global max pooling
  • classes : 何クラス分類のモデルとするか(include_topがTrue, weightがNoneの時)

詳細はこちら:Applications - Keras Documentation

 

次にモデルの使い方を含めた具体的なサンプルコードを乗せておきます。

①分類器として使用

ImageNetで学習したモデルを用い、1000クラスの画像分類を行う最もシンプルな例です。モデルは全結合層付きのものを使用します。

import tensorflow as tf
from tensorflow import keras
import numpy as np
from keras.applications import mobilenet

#モデルのロード model = mobilenet.MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1,
dropout=1e-3, include_top=True, weights='imagenet',
input_tensor=None, pooling=None, classes=1000) #model.summary()
#画像の前処理 height = 224 width = 224 img_pil = keras.preprocessing.image.load_img("./dog.jpg", target_size=(height, width)) img = keras.preprocessing.image.img_to_array(img_pil) img = img[tf.newaxis, ...] img_preprocessed = mobilenet.preprocess_input(img)

#推論実行
predict = model.predict(img_preprocessed) #出力クラスのtop5を表示 result = mobilenet.decode_predictions(predict, top=5) print(result)

 

②特徴抽出器として利用

全結合層前の特徴量を抽出する方法です。分類クラス数をカスタマイズしたり、ベースモデルの一部のみを追加学習するなど転移学習をしたい場合はこの方法を使います。

全結合層は不要なので引数のinclude_topはFalseとします。

import tensorflow as tf
from tensorflow import keras
import numpy as np
from keras.applications import mobilenet

#モデルのロード model = mobilenet.MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1,
dropout=1e-3, include_top=False, weights='imagenet',
input_tensor=None, pooling='avg', classes=1000) #model.summary()
#画像の前処理 height = 224 width = 224 img_pil = keras.preprocessing.image.load_img("./dog.jpg", target_size=(height, width)) img = keras.preprocessing.image.img_to_array(img_pil) img = img[tf.newaxis, ...] img_preprocessed = mobilenet.preprocess_input(img)
#特徴量抽出 predict = model.predict(img_preprocessed) print(predict.shape) #(1,1024)

転移学習の具体的な手順はこちらのサイトをご参照下さい。
https://note.nkmk.me/python-tensorflow-keras-transfer-learning-fine-tuning/

 

参考サイト

TensorFlow, KerasでVGG16などの学習済みモデルを利用 | note.nkmk.me