はじめに
akihiroさん(@akihiro01051)のHololens上でのwindowsML画像認識デモを
RS5で動かす時のメモです。
akihiro-document.azurewebsites.net
RS5への変更に伴い、windows MLの名前空間は以下のようにPreviewが除かれました。
Windows.AI.MachineLearning.Preview → Windows.AI.MachineLearning
そして、コード面でもいくつか変更点があったようです。
実行環境
- Windows 10 Insider Preview 17763.1
- Windows 10 SDK Insider Preview 17763
- Visual Studio 2017
- Unity 2017.4.12f1
- Hololens RS5 preview 17720
事前準備
まず、以下のリンクのリポジトリからWindows-Machine-LearningのサンプルをCloneします。
https://github.com/Microsoft/Windows-Machine-Learning
また、Windows-Machine-Learning\Samples\SqueezeNetObjectDetection\UWP\cs\Assets 以下にある
- model.onnx
- Labels.json
の二つのファイルを確保しておいてください。
次に、本記事冒頭で述べたakihiroさんのプロジェクトをCloneします。
GitHub - akihiro0105/WindowsMLDemo_HoloLens_Unity: This Repository is WindowsML demo(ObjectDetection) with Unity in HoloLens
その後、確保しておいた二つのファイルを、Assets\StreamingAssets 以下に置いてください。
(Labels.jsonは上書きで構いません。また、元のSqueezeNet.onnxは削除して大丈夫です。)
そして、Unityからプロジェクトをビルドし、生成されたvisual studioプロジェクトの
WindowsML_Demo.csを変更していきます
コード変更
・ 10行目 using参照を変更します。
//using Windows.AI.MachineLearning.Preview; //削除 //追加 using Windows.AI.MachineLearning;
・ 28行目ファイル名を変更します。
SqueezeNet.onnx → model.onnx
・67行目 Previewを外します。
//private LearningModelPreview _model = null; //削除 //追加 private LearningModel _model = null;
・68, 69行目 この辺がsessionに統一され、使いやすくなった感じはあります。
//private ImageVariableDescriptorPreview _inputImageDescription; //削除 //private TensorVariableDescriptorPreview _outputTensorDescription; //削除 //追加 private LearningModelSession _session = null;
・97行目 SqueezeNet.onnx → model.onnx
・98行目Previewを外します。
//_model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); //削除 //追加 _model = await LearningModel.LoadFromStorageFileAsync(modelFile);
・100~103行目 sessionに統一され、コードもすっきり書くことができます。
//削除 //List<ILearningModelVariableDescriptorPreview> inputFeatures = _model.Description.InputFeatures.ToList(); //List<ILearningModelVariableDescriptorPreview> outputFeatures = _model.Description.OutputFeatures.ToList(); //_inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; //_outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; //追加 _session = new LearningModelSession(_model, new LearningModelDevice(LearningModelDeviceKind.Default));
・146~148行目
// WindowsMLに入力,出力形式を設定する //削除 //LearningModelBindingPreview binding = new LearningModelBindingPreview(_model as LearningModelPreview); //binding.Bind(_inputImageDescription.Name, inputFrame); //binding.Bind(_outputTensorDescription.Name, _outputVariableList); //追加 LearningModelBinding binding = new LearningModelBinding(_session); ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputFrame); binding.Bind("data_0", imageTensor);
・151~152行目
// Process the frame with the model //削除 //LearningModelEvaluationResultPreview results = await _model.EvaluateAsync(binding, "test"); //List<float> resultProbabilities = results.Outputs[_outputTensorDescription.Name] as List<float>; //追加 var results = await _session.EvaluateAsync(binding, ""); var resultTensor = results.Outputs["softmaxout_1"] as TensorFloat; var resultVector = resultTensor.GetAsVectorView();
・157, 164行目 resultProbabilities → resultVector
結果
結果としては以下のような動画になります。
画面に近づいた時には猫が、 ノートPCを写している時はノートPCとして認識されているのがわかるかと思います。
最後にブルームしたときのUIは、RS5ならではのUIです。
youtu.be
また、猫の画像はこちらを使用させていただきました。
まとめ
・Hololens RS5でakihiroさん(@akihiro01051)のwindowsML画像認識デモを動かした
・その際の変更点などを示した