TensorFlow Hubとは
事前学習済みモデルを簡単に再利用できるライブラリです。
画像分類(ResNet, MobileNet)、テキスト埋め込み(BERT, Universal Sentence Encoder)、
音声・動画処理などのモデルをゼロから学習することなく、
すぐに自分のタスクに適用できます。
主なメリット
-
転移学習の簡易化:
モジュールを固定して特徴抽出器として使い、上位層だけ再学習することで、
少ないデータでも高精度を実現
基本的な使い方
import tensorflow as tf
import tensorflow_hub as hub
# モジュールのロード
hub_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/5"
feature_extractor = hub.KerasLayer(hub_url, input_shape=(224, 224, 3), trainable=False)
# Kerasモデルへの組み込み
model = tf.keras.Sequential([
feature_extractor, # 事前学習済みの特徴抽出層
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(num_classes, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
重要パラメータの解説
hub.KerasLayer()
の主要パラメータ:
- hub_url: TensorFlow Hub上のモデルを指すURL
- input_shape: 入力テンソルの形状(高さ, 幅, チャンネル数)
-
trainable:
-
False
: モジュール内の重みを凍結(更新なし)、少量データに有効 -
True
: ファインチューニング時に使用、全体を再学習
-