LoginSignup
32
23

More than 3 years have passed since last update.

EfficientNetを最速で試す方法

Last updated at Posted at 2020-01-27

TL;DR

はじめに

「EfficientNet」については素晴らしい記事がありますのでこちらをご覧ください。
Qiita - 2019年最強の画像認識モデルEfficientNet解説

今回の記事はEfficientNetを最速で試す方法を紹介することに重きをおきます。

早速デモを動かしてみる

動かすデモのGoogleColabratoryは用意しておきましたのでこちらをお使いください。

GoogleColabratory - transfer_learning_EfficientNet

上記GoogleColabratoryより肝になるコード、モデルの定義部分だけ抽出してみました。これだけでEfficientNetを利用できるようになります。
あとの学習部分をどうするか気になる方は上のデモを動かしてみてください。

EfficientNet.py
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers

# クラス数はデータに合わせて適当に変えてください
num_classes = 10

# URLはこちらのページ https://tfhub.dev/google/collections/efficientnet/1
# 末尾に記載されているURLから使いたいモデル(B0-B7)までのどれかを選んでください
# 今回はB0を使います
feature_extractor_url = "https://tfhub.dev/google/efficientnet/b0/feature-vector/1"

# width/heightについてはB0は(224, 224)が推奨とされているのでそうしています
# 推奨のwidth/heightについてはこちらのページをご覧ください https://tfhub.dev/google/collections/efficientnet/1
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
                                         input_shape=(224,224,3))
# 学習済み重みは固定
feature_extractor_layer.trainable = False

# Keras functional APIで動くかなと試したのですがうまく動かず
# 公式Tutorialに倣って以下の通りにしています
model = tf.keras.Sequential([
  feature_extractor_layer,
  layers.Dense(num_classes, activation='softmax')
])

参考

32
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
23