LoginSignup
8
6

More than 5 years have passed since last update.

Custom Vision Serviceから出力したモデルを使ってWindowsML上で物体認識をする(その2 モデルデータの変換とクラスの生成)

Last updated at Posted at 2018-03-22

前回はCustom VisionのAIモデルデータをCoreML形式で出力しました。
WindowsMLはonnx形式対応なのでモデルデータの変換が必要になります。
マイクロソフトではPython上で変換できるツールモジュール(Winmltools)を提供しています。このモジュールを導入して変換プログラムを作成します。

CoreMLモデルをonnxモデルに変換する

・変換に必要なモジュールをインストールします。

-------追記 2018/05/05
coremltoolsのインストールですがPython3.6ではインストールできません。
Python2.7の環境でインストールしてください。
-------追記終了

pip install winmltools
pip install coremltools

・Pythonで次のプログラムを作成します。ダウンロードしたモデルファイルは「vegetables.mlmodel」とします。

coreml2onnx.py
from coremltools.models.utils import load_spec

# mlmodel load
model_coreml=load_spec('vegetables.mlmodel')  

from winmltools import convert_coreml

# convert coreml to onnx
model_onnx=convert_coreml(model_coreml,name='vegetables')

from winmltools.utils import save_model

# save onnx file
save_model(model_onnx,'vegetables.onnx')

from winmltools.utils import save_text

# save text file
save_text(model_onnx,'vegetables.txt')

・作成したプログラムとモデルデータを同じフォルダにおいてから実行してください。

python coreml2onnx.py

・実行するとフォルダには「vegetables.onnx」、「vegetables.txt」2つのファイルが出力されます。

これでWindowsMLで実行するためのonnx形式のAIモデルファイルができました。

onnxモデルからモデル用CSファイルを生成する

Windows10 SDK InsiderPreview(Build 17110以降)にはonnxファイルからC#及びC++用のクラスファイルを生成するツールが含まれています。
このツールを使ってクラスファイルを作成します。

・コマンドプロンプトを開き、onnxファイルのあるフォルダで次のコマンドを実行します。

"c:\Program Files (x86)\Windows Kits\10\bin\10.0.17110.0\x64\mlgen.exe" -i vegetables.onnx -l CS -n vegetables -o vegetables.cs

・次のようなC#クラスファイルが生成されます。

vegetables.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Media;
using Windows.Storage;
using Windows.AI.MachineLearning.Preview;

// vegetables

namespace vegetables
{
    public sealed class VegetablesModelInput
    {
        public VideoFrame data { get; set; }
    }

    public sealed class VegetablesModelOutput
    {
        public IList<string> classLabel { get; set; }
        public IDictionary<string, float> loss { get; set; }
        public VegetablesModelOutput()
        {
            this.classLabel = new List<string>();
            this.loss = new Dictionary<string, float>();
        }
    }

    public sealed class VegetablesModel
    {
        private LearningModelPreview learningModel;
        public static async Task<VegetablesModel> CreateVegetablesModel(StorageFile file)
        {
            LearningModelPreview learningModel = await LearningModelPreview.LoadModelFromStorageFileAsync(file);
            VegetablesModel model = new VegetablesModel();
            model.learningModel = learningModel;
            return model;
        }
        public async Task<VegetablesModelOutput> EvaluateAsync(VegetablesModelInput input) {
            VegetablesModelOutput output = new VegetablesModelOutput();
            LearningModelBindingPreview binding = new LearningModelBindingPreview(learningModel);
            binding.Bind("data", input.data);
            binding.Bind("classLabel", output.classLabel);
            binding.Bind("loss", output.loss);
            LearningModelEvaluationResultPreview evalResult = await learningModel.EvaluateAsync(binding, string.Empty);
            return output;
        }
    }
}

このファイルとonnxファイルをUWPのプロジェクトに追加してアプリケーション作成していきます。
今生成したCSファイルはこのままでは正しく動作しません。
UWPプロジェクトに追加してからファイルを修正します。

次はいよいよUWPアプリの作成になります。

Custom Vision Serviceから出力したモデルを使ってWindowsML上で物体認識をする(その3 UWPアプリの作成)に続きます。

8
6
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
8
6