CoreMLTools入門
1. CoreMLToolsとは
「CoreMLTools」は、よく使われる「学習済みモデル」を「CoreML形式のモデル」に変換するPythonパッケージです。CoreML形式のモデルの検証のために、CoreMLフレームワークを使用して予測することもできます。
サポートされている「学習済みのモデル」は次の通りです。
・Keras (1.2.2, 2.0.4+) with TensorFlow (1.0+)
・XGBoost (0.6+)
・scikit-learn (0.15+)
・LIBSVM
2. CoreMLToolsのインストール
AnacondaでPython 3.6の仮想環境を作成し、以下のコマンドを入力します。
$ pip install -U coremltools
さらに、変換元の学習済みモデルの形式に応じた依存関係もインストールします。
「Keras」の学習済みモデル(*.h5)をCoreML形式のモデルに変換するには、TensorFlowとKerasをインストールします。
$ pip install tensorflow
$ pip install keras
3. モデル変換
◎MNISTのモデルの変換例
MNISTの変換例は次の通りです。
【convert.py】
import coremltools
coremltools.converters.keras.convert('image_classification.h5',
class_labels='labels.txt').save('image_classification.mlmodel')
【labels.txt】
0
1
2
3
4
5
6
7
8
9
◎画像分類の変換例
画像分類の変換例は次の通りです。
【convert.py】
import coremltools
coremltools.converters.keras.convert('image_classification.h5',
input_names = 'image',
output_names = ['classLabelProbs', 'classLabel'],
image_input_names = 'image',
class_labels='labels.txt').save('image_classification.mlmodel')
【labels.txt】
cat
dog
4. API
coremltools.converters.keras.convert(model,
input_names=None, output_names=None, image_input_names=None,
input_name_shape_dict={}, is_bgr=False,
red_bias=0.0, green_bias=0.0, blue_bias=0.0, gray_bias=0.0,
image_scale=1.0, class_labels=None, predicted_feature_name=None,
model_precision='float32', predicted_probabilities_output='',
add_custom_layers=False, custom_conversion_functions=None,
input_shapes=None, output_shapes=None, respect_trainable=False)
KerasモデルをCore ML protobuf仕様(.mlmodel)に変換します。
【引数】
◎model: Keras model object | str | (str, str)
A trained Keras neural network model which can be one of the following:
・Kerasモデルオブジェクト
・Kerasモデルファイルへのパス(h5)
・文字列のタプル。1番目はモデルアーキテクチャのパス、2番目は重みのパス。
◎input_names: [str] | str
モデルの入力の名前。
MLモデルインターフェースで使用。
◎output_names: [str] | str
モデルの出力の名前。
MLモデルインターフェースで使用。
◎image_input_names: [str] | str
モデルのイメージの入力の名前
他の入力はMultiArrayとして扱われる。
◎is_bgr: bool | dict()
内部チャネルの順序がBGRの場合はTrue、そうでない場合はRGB。
◎red_bias: float | dict()
入力画像の赤チャネルに追加されるバイアス値。
デフォルトは0.0。
各画像入力に異なる値を指定する時は、キーとしてimage_input_nameを持つ辞書を指定。
◎blue_bias: float | dict()
入力画像の青チャネルに追加されるバイアス値。
デフォルトは0.0。
各画像入力に異なる値を指定する時は、キーとしてimage_input_nameを持つ辞書を指定。
◎green_bias: float | dict()
入力画像の緑チャネルに追加されるバイアス値。
デフォルトは0.0。
各画像入力に異なる値を指定する時は、キーとしてimage_input_nameを持つ辞書を指定。
◎gray_bias: float | dict()
グレースケールの時に、入力画像の追加されるバイアス値。
デフォルトは0.0。
各画像入力に異なる値を指定する時は、キーとしてimage_input_nameを持つ辞書を指定。
◎image_scale: float | dict()
モデルが予測を行う前に入力画像をスケーリングする値。
デフォルトは1.0。
各画像入力に異なる値を指定する時は、キーとしてimage_input_nameを持つ辞書を指定。
◎class_labels: list[int or str] | str
モデルの出力インデックスをラベルにマップする、クラスラベルのリスト。
引数が文字列の場合、改行で区切りのラベルを保持するファイルのパスと判定。
◎predicted_feature_name: str
クラスラベルの出力の名前。
デフォルトは「classLabel」。
◎model_precision: str
モデルの精度。
現在、完全精度(float)と半制度(float16)がサポートされている。
デフォルトは完全制度(_MLMODEL_FULL_PRECISION)。
◎predicted_probabilities_output: str
クラスの予測確率の出力の名前。
softmax関数の出力で、デフォルトは最初の出力blob。
◎add_custom_layers: bool
Trueの場合、不明なKerasレイヤータイプが「custom」レイヤーとしてモデルに追加。
後処理として入力する必要がある。
◎custom_conversion_functions: {str:(Layer -> (dict, [weights])) }
カスタムレイヤーを取得し、パラメータ辞書と重みリストを返す関数として、
カスタムレイヤーの名前と値に対応するキーを持つ辞書。
◎respect_trainable: bool
Trueの場合、「‘trainable’」とマークされたKerasレイヤーは、
Core MLモデルで自動的に更新可能とマークされる。
【戻り値】
◎model: MLModel
CoreMLモデル
この記事が気に入ったらサポートをしてみませんか?