えんじにあのじゆうちょう

勉強したことを中心にアウトプットしていきます。

MAiX Dock上でMNISTの手書き数字識別モデルを動かしてみる

はじめに

MAiX Dockは価格も安く、高性能と言われていますが、ドキュメント*1が少しわかりにくい!!
独自モデルを動かしたくても、どうしたものか、意外とわかりません。

そこで、画像認識のHello WorldというべきMNISTの手書き数字識別モデルをMAiX Dock上で動かしてみて全体像を掴んでみようと思います。

0.5.0から実装されたnumpyも(やや強引に)使っているので、更新がまだの方は以下記事も参考にしてください。
t.marufeuille.dev


手順

作業環境

あまり意識する必要ないかもしれませんが、私の場合はcolaboratory*2上で実施しています。
最終的なコードは以下に公開しています。
(下記にある、Sipeed社公式のコードも含みます)
colab.research.google.com

モデル構築

コードは公式のこれを一旦丸パクリします。
Maix-TF-workspace/mnist.py at master · sipeed/Maix-TF-workspace · GitHub

生tensorflowですね。
あと、今後の工程でinput, outputの名前が重要になるので、それぞれ覚えておきましょう。

テスト用の画像(データセット)用意

mnistなので簡単にkerasの標準データセットてして取ってこれるのですが、後述するkmodel形式への変換時に、以下形式でデータセットを用意する必要があります。

dataset/
  正解ラベル1/
    正解ラベル1の画像01
    正解ラベル1の画像02
    ...
  正解ラベル2/
    正解ラベル2の画像01
    ...
  ...

今回は以下のようにします。

images/
  0/
    xxx.png
    xxx.png
  1/
    xxx.png
  ...
  9/
    xxx.png

変換用のとても単純なスクリプトです。

import os
from PIL import Image
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

for idx in range(x_test.shape[0]):
    x = x_test[idx,:]
    y = y_test[idx]
    dirname = "images/" + str(y)
    if not os.path.isdir(dirname):
        os.mkdir(dirname)
    
    img = Image.new("L", (28, 28))
    pix = img.load()
    for i in range(28):
        for j in range(28):
            pix[i, j] = int(x[i,j])
    filename = "images/" + str(y) + "/test" + "{0:04d}".format(idx) + ".png"
    img.save(filename)

モデルの変換

MAiX Dockに搭載されているkendryte-k210というCNN専用のプロセッサがあるわけですが、このプロセッサはtensorflowとか、一般的なフレームワークで生成したモデルは食べられないので、変換する必要があります。
pb(tensorflow)形式は直接kmodel形式に変換できないようなので、一旦tflite形式に変換してからkmodel形式に変換します。

Toolboxの取得

Sipeed社はコンバートのためのツールも公開していますので、基本はこれを使います。
まずはクローンしておきましょう。

$ git clone https://github.com/sipeed/Maix_Toolbox
pb形式からtflite形式への変換

ツールを使えば一発です。

$ ./Maix_Toolbox/pb2tflite.sh

色々聞かれるので、必要なことを入力していきましょう。
2,3は上で控えておいたinput, outputの名前ですね。

This script help you generate cmd to convert *.pb to *.tflite
Please put your pb into workspace dir
1. pb file name: (don't include workspace)
mnist.pb
2. input_arrays name:
inputs
3. output_arrays name:
output
4. input width:
28
5. input height:
28
6. input channel:
1
-----------------------------------
The command you need is:
-----------------------------------
toco --graph_def_file=workspace/mnist.pb --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --output_file=workspace/mnist.tflite --inference_type=FLOAT --input_type=FLOAT --input_arrays=inputs --output_arrays=output --input_shapes=1,28,28,1
2019-12-30 04:17:19.998517: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2019-12-30 04:17:20.001119: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.001599: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: Tesla P100-PCIE-16GB major: 6 minor: 0 memoryClockRate(GHz): 1.3285
pciBusID: 0000:00:04.0
2019-12-30 04:17:20.001855: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2019-12-30 04:17:20.003927: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2019-12-30 04:17:20.005699: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2019-12-30 04:17:20.006058: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2019-12-30 04:17:20.020933: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2019-12-30 04:17:20.044229: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2019-12-30 04:17:20.056034: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2019-12-30 04:17:20.056273: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.056940: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.057422: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-12-30 04:17:20.057949: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX512F
2019-12-30 04:17:20.105341: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2000165000 Hz
2019-12-30 04:17:20.105573: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55bb0eae8f40 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2019-12-30 04:17:20.105604: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2019-12-30 04:17:20.210044: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.210748: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55bb0eae9d40 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2019-12-30 04:17:20.210777: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Tesla P100-PCIE-16GB, Compute Capability 6.0
2019-12-30 04:17:20.210959: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.211401: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: Tesla P100-PCIE-16GB major: 6 minor: 0 memoryClockRate(GHz): 1.3285
pciBusID: 0000:00:04.0
2019-12-30 04:17:20.211451: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2019-12-30 04:17:20.211470: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2019-12-30 04:17:20.211489: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2019-12-30 04:17:20.211506: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2019-12-30 04:17:20.211536: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2019-12-30 04:17:20.211559: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2019-12-30 04:17:20.211578: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2019-12-30 04:17:20.211633: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.212157: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.212757: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-12-30 04:17:20.219155: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2019-12-30 04:17:20.220142: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-12-30 04:17:20.220172: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0 
2019-12-30 04:17:20.220182: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N 
2019-12-30 04:17:20.223048: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.223595: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.224043: W tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:39] Overriding allow_growth setting because the TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original config value was 0.
2019-12-30 04:17:20.224090: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10745 MB memory) -> physical GPU (device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0)
2019-12-30 04:17:20.255984: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.256489: I tensorflow/core/grappler/devices.cc:55] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 1
2019-12-30 04:17:20.256629: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
2019-12-30 04:17:20.257178: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.257677: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: Tesla P100-PCIE-16GB major: 6 minor: 0 memoryClockRate(GHz): 1.3285
pciBusID: 0000:00:04.0
2019-12-30 04:17:20.257743: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2019-12-30 04:17:20.257767: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2019-12-30 04:17:20.257791: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2019-12-30 04:17:20.257811: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2019-12-30 04:17:20.257832: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2019-12-30 04:17:20.257855: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2019-12-30 04:17:20.257878: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2019-12-30 04:17:20.257939: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.258392: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.258835: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-12-30 04:17:20.258878: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-12-30 04:17:20.258893: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0 
2019-12-30 04:17:20.258909: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N 
2019-12-30 04:17:20.258995: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.259452: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-12-30 04:17:20.259889: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10745 MB memory) -> physical GPU (device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0)
2019-12-30 04:17:20.279775: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:786] Optimization results for grappler item: graph_to_optimize
2019-12-30 04:17:20.279808: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:788]   constant_folding: Graph size after: 31 nodes (-1), 31 edges (0), time = 7.647ms.
2019-12-30 04:17:20.279820: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:788]   constant_folding: Graph size after: 31 nodes (0), 31 edges (0), time = 0.504ms.
tflite形式からkmodel形式への変換

まずは単純にツールを実行してみましょう。

$ ./tflite2kmodel.sh ../workspace/mnist.tflite
uasge: ./tflite2kmodel.sh xxx.tflite
./Maix_Toolbox/tflite2kmodel.sh: line 5: ./ncc/ncc: No such file or directory

ncc?nccってなんぞや、と思ってlsを打ってみると、「get_nncase.sh」というファイルがあることに気づきます。
これだ!と思い実行してみます。
(実はgithubのREADMEを読むと、小さく「!!!Please run it to init toolbox」と書いてあります)

$ ./get_nncase.sh
--2019-12-30 03:04:01--  https://github.com/kendryte/nncase/releases/download/v0.1.0-rc5/ncc-linux-x86_64.tar.xz
Resolving github.com (github.com)... 52.69.186.44
Connecting to github.com (github.com)|52.69.186.44|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/128056991/86526300-8233-11e9-91ac-884e08be60de?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20191230%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20191230T030401Z&X-Amz-Expires=300&X-Amz-Signature=4bc8115d293117c4246e4f2b1cc52e7ad8b010446f7d7e6004cc16d3d3fc9284&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dncc-linux-x86_64.tar.xz&response-content-type=application%2Foctet-stream [following]
--2019-12-30 03:04:01--  https://github-production-release-asset-2e65be.s3.amazonaws.com/128056991/86526300-8233-11e9-91ac-884e08be60de?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20191230%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20191230T030401Z&X-Amz-Expires=300&X-Amz-Signature=4bc8115d293117c4246e4f2b1cc52e7ad8b010446f7d7e6004cc16d3d3fc9284&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dncc-linux-x86_64.tar.xz&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.8.180
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.8.180|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 54220128 (52M) [application/octet-stream]
Saving to: ‘ncc-linux-x86_64.tar.xz’

ncc-linux-x86_64.tar.xz            100%[===============================================================>]  51.71M  14.4MB/s    in 4.4s

2019-12-30 03:04:06 (11.7 MB/s) - ‘ncc-linux-x86_64.tar.xz’ saved [54220128/54220128]

tar (child): ncc-linux-x86_64.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
rm: cannot remove 'ncc-linux-x86_64.tar.gz': No such file or directory
download nncase ok!

良し!と思ってよく見ると「Cannot open: No such file or directory」等たくさんエラーが出ております。
更に良く見ると、ダウンロードされているファイルが「ncc-linux-x86_64.tar.xz」で、解凍しようとしているファイルが「ncc-linux-x86_64.tar.gz」となっており、どうもスクリプトが間違っているようなので直します。。

更新後スクリプト置いておきます。

#!/bin/bash
mkdir -p ncc
mkdir -p workspace
mkdir -p images
mkdir -p log
cd ncc
wget https://github.com/kendryte/nncase/releases/download/v0.1.0-rc5/ncc-linux-x86_64.tar.xz
xz -dc ncc-linux-x86_64.tar.xz | tar xfv -
rm ncc-linux-x86_64.tar.xz
echo "download nncase ok!"

プルリクは出ているようですけど、取り込まれてないみたいですね。。。
https://github.com/sipeed/Maix_Toolbox/pull/10


さて気を取り直して、kmodelへ変換します。
ちなみにさっきのスクリプトは内部的にはnccを呼んでいるだけで、細かい調整のことを考えると、nccを直接使ったほうがよいです。
というわけで、ncc直接使います。

$ cd Maix_Toolbox
$ ./ncc/ncc -i tflite -o k210model --dataset ../images ../workspace/mnist.tflite ../workspace/mnist.kmodel
0: InputLayer -> 1x1x28x28
1: K210Conv2d 1x1x28x28 -> 1x16x14x14
2: K210Conv2d 1x16x14x14 -> 1x32x7x7
3: Dequantize 1x32x7x7 -> 1x32x7x7
4: TensorflowFlatten 1x32x7x7 -> 1x1568
5: FullyConnected 1x1568 -> 1x32
6: Quantize 1x32 -> 1x32
7: K210AddPadding 1x32 -> 1x32x4x4
8: K210Conv2d 1x32x4x4 -> 1x10x4x4
9: K210RemovePadding 1x10x4x4 -> 1x10
10: Dequantize 1x10 -> 1x10
11: Softmax 1x10 -> 1x10
12: OutputLayer 1x10
KPU memory usage: 2097152 B
Main memory usage: 14112 B
2019-12-30 05:37:14.426984: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA

おっ!ようやくうまくできました。

モデルをcolabからダウンロード

colaboratoryからのダウンロードはこんな感じで簡単に実行できます。

from google.colab import files
files.download('/content/workspace/mnist.kmodel')

適当なわかりやすいところに置いておいてください。

モデルのアップロード

SDカードがあればSDカードにコピーして読み込ませればいいのですが、手元にmicro SDはあるものの、変換アダプター等が見当たらずMacBookから書き込めないので、uPyLoaderを使います。
以下から適切なバージョンをダウンロードしておいてください。
Releases · BetaRavener/uPyLoader · GitHub

なお、執筆当時の最新版はMac版がなかったので、一つ古いバージョンを使っています。

起動して接続したら、まずは初期化が必要です。
以下のように「init trasnfer file」をクリックするとuPyLoaderを利用する上での前提として必要なスクリプトが作成されます。
f:id:marufeuillex:20191230153427p:plain


その後、モデルをアップロードします。
ダウンロードしてきたモデルを選択し、転送してください。少し時間がかかります。

実行

ではモデルを実行します。
コード全体は以下のような感じです。

import sensor
import image
import lcd
import KPU as kpu
import ulab as np

lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
sensor.set_hmirror(0)

task = kpu.load("/flash/mnist.kmodel")

while(True):
    img = sensor.snapshot()
    a = lcd.display(img)
    gray_img = img.to_grayscale().resize(28,28)
    gray_img.pix_to_ai()
    fmap = kpu.forward(task, gray_img)
    max_index = np.argmax(fmap[:])
    a = lcd.draw_string(0, 224, str(max_index))

上記はほぼサンプルのようなコードなのであまり解説はいらないと思いますが、あまりサンプルに書いてないところだけ少し解説します。

画像の左右反転を補正

なぜかデフォルトだと撮影した画像が左右反転しています。
以下コードを挿入することで左右を正しく認識させられます。

sensor.set_hmirror(0)
モデルのロード

モデルのロードは以下のように実施します。

task = kpu.load("/flash/mnist.kmodel")

引数は番地かPath指定かが出行えますが、uPyLoaderを使った場合、デフォルトで/flash/配下にモデルはアップロードされるので、/flash/mnist.kmodelを指定します。

推論の仕方

モデルを使って推論を実施するときはforwardを使います。

fmap = kpu.forward(task, gray)

この部分です。結果が少しわかりにくいですが、featuremapという形式で返ってきます。
featuremapにはclassificationの結果が「リスト」のような形で入っています。
softmaxの結果なので、確率っぽい形で0 の確率はxx, 1の確率はxxという具合です。
そこから、最も確率の高い数字の抽出は以下のように実施します。

max_index = np.argmax(fmap[:])

fmapはそのままではリストとして扱えないのですが[:]としてやると、リスト(正確にはタプルかな)として扱えるようになります。
そして、np.argmaxを使って最大値の添字を取ってくることで、書かれた数値が何と推論されたかを取得できます。

最後にそれを表示して終了です。

試してみる

ちょっとわかりにくいですが、左下に小さく認識結果が表示されています。正しく認識できてそうですね。
f:id:marufeuillex:20191231063845j:plain

頑張れば認識させられますが、モデルの問題なのかなんなのか、精度は結構悪い気がしますね。

まとめ

今回はMNISTのモデル作成から変換、MAiX Dock上への実装を行いました。
これで独自画像認識モデルをMAiX Dock上でどう使えばよいかわかりました。

結構簡単にやれそうな気がしてきたので色々試してみようと思います。