LoginSignup
2
2

More than 3 years have passed since last update.

EfficientnetをCore MLに変換する【変換済みモデルあり】

Last updated at Posted at 2020-08-11

スクリーンショット 2020-08-11 12.40.11.png
画像分類の最新モデルをCore ML形式に変換します。

変換済みCoreMLModel(GitHub)

TensorFlow Hubのモデルコレクションからダウンロードしてモデルを構築。

m = tf.keras.Sequential([
    hub.KerasLayer("https://tfhub.dev/tensorflow/efficientnet/b0/classification/1")
])
m.build([1, 224, 224, 3])
m.summary()

クラスラベルを読み込んでおきます。

(今回はImageNetの1000クラスでそのまま使用。TensorFlowHubには転移学習のできるバージョンも公開されています。)

import urllib
label_url = 'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt'
class_labels = urllib.request.urlopen(label_url).read().splitlines()
class_labels = class_labels[1:] # remove the first class which is background
assert len(class_labels) == 1000

# make sure entries of class_labels are strings
for i, label in enumerate(class_labels):
  if isinstance(label, bytes):
    class_labels[i] = label.decode("utf8")

CoreMLTools4.0で変換します。

import coremltools as ct

image_input = ct.ImageType(shape=(1, 224, 224, 3,),
                           scale=1/255)

mlmodel = ct.convert(m,
                     inputs=[image_input],
                     classifier_config=classifier_config,
                     )
mlmodel.save('./efficientnet.mlmodel')

スクリーンショット 2020-08-11 12.40.58.png

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
MLBoysチャンネル
Medium

相棒
note

2
2
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
2
2