とある大学生の勉強メモ

Python, C#, UWP, WPF, 心理実験関連の開発備忘録

Pytorchで学習・保存したonnx(format version6,opset=11)をVisual Studio2019(C#)のUWPで読み込む

備忘録.
Pytorchで学習したモデルをtorch.onnx.exportで保存後,Visual Studioでmodel Outputが返ってくるまでのお話.

環境:

Windows 10 2004 (OSビルド 19041.1052)
onnx opset =11
onnx format version = 6
Visual Studio 2019(UWP)
CUDA : 10.0
cuDNN : 7.4.1
torch 1.8.1 + cu102
torchvision 0.9.1+cu102

onnxの保存

色々とハマる点がある.
・ModelのInput
・opsetの指定
・Model,InputをCPU上で扱うかGPU上で扱うか
今回モデルの入力にはカラー画像を用いたので,縦横(width*heightピクセル)の画像をinputにし,modelはcpu()上に乗せ,opsetは11でexportした.cuda()にすればGPUで使えるようになるが,Inputとmodelは同じハードウェア上で用いなければErrorを吐かれるので注意.

import torch
input = torch.rand(1, 3, width, height)
torch.onnx.export(model.cpu(), input, "model_name.onnx", opset_version = 11)

ひとまずこのonnxをNetronで確認してみよう. f:id:amakazeryu:20210706120748p:plain f:id:amakazeryu:20210706120730p:plain ここまで確認できればonnx出力はお終い.

モデルのラッパーファイル作成

色々と沼るポイントにモデルのインターフェースを合わせる(InputとOutputを合わせる)作業がある.こちらをショートカットさせてくれるのがWindows Machine Learning Code Generatorである.
ツールバー拡張機能から,インストールして用いるのが手っ取り早い.
f:id:amakazeryu:20210706121355p:plain f:id:amakazeryu:20210706121440p:plain

自分が半日死んだポイントはVisual StudioのプロジェクトファイルのPath問題である.一応参考リンクを.

amakazeryu.hatenablog.com

さて,ラッパーファイルであるmodel_name.csがVisual Studio上に無事出来上がったら覗こう.
とりあえずInputとOutputを見る.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Media;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.AI.MachineLearning;
namespace MNIST_Demo
{
    
    public sealed class mrcnn_0702Input
    {
        public TensorFloat images; // shape(1,3,width, height)
    }
    
    public sealed class mrcnn_0702Output
    {
        public TensorFloat output3550; // shape(-1,4)
        public TensorInt64Bit output3213; // shape(-1)
        public TensorFloat output3211; // shape(-1)
        public TensorFloat output3788; // shape(0,1,width, height)
    }

変更ポイントは主に二つ.Inputの型をイメージに,opset=11を読み込めるようにMicrosoft.AI version≧1.8.0を使うこと.

//using Windows.AI.MachineLearning;
using Microsoft.AI.MachineLearning;
public sealed class mrcnn_0702Input
{
    //public TensorFloat images; // shape(1,3,2448,1920)
    public ImageFeatureValue images; // shape(1,3,2448,1920)
}

とまあ公開できる情報は現状ここまで.具体的なコードはオープンにできないが,時間が経ったらできるかもしれない. 引っかかるポイントをクリアさせられていたら嬉しい.