Skip to content

Instantly share code, notes, and snippets.

@kerikun11
Created September 27, 2020 06:38
Show Gist options
  • Save kerikun11/c12650913778ac581e3ea897f90ba608 to your computer and use it in GitHub Desktop.
Save kerikun11/c12650913778ac581e3ea897f90ba608 to your computer and use it in GitHub Desktop.
k210_mnist.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "k210_mnist.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMDQSo4urv5ctrvMyXr6WNN",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kerikun11/c12650913778ac581e3ea897f90ba608/k210_mnist.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "P64MMexPKfyv"
},
"source": [
"K210マイコン用のモデルを学習するコード \n",
"作成日: 2020.09.27"
]
},
{
"cell_type": "code",
"metadata": {
"id": "0q0OSmRJm11D",
"outputId": "35464731-30d1-476d-d889-d9c3e800f708",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
}
},
"source": [
"# prepare dataset\n",
"from tensorflow import keras\n",
"\n",
"# classes\n",
"num_classes = 10\n",
"# input image dimensions\n",
"img_rows, img_cols = 28, 28\n",
"input_shape = (img_rows, img_cols, 1)\n",
"\n",
"# the data, split between train and test sets\n",
"(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
"\n",
"# channel_last\n",
"x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n",
"x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n",
"\n",
"x_train = x_train.astype('float32')\n",
"x_test = x_test.astype('float32')\n",
"x_train /= 255\n",
"x_test /= 255\n",
"print('x_train shape:', x_train.shape)\n",
"print('x_test shape:', x_test.shape)\n",
"\n",
"# convert class vectors to binary class matrices\n",
"y_train = keras.utils.to_categorical(y_train, num_classes)\n",
"y_test = keras.utils.to_categorical(y_test, num_classes)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"x_train shape: (60000, 28, 28, 1)\n",
"x_test shape: (10000, 28, 28, 1)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "QHbnPFxgy104"
},
"source": [
"# CNN parts definition\n",
"from tensorflow.keras import layers\n",
"\n",
"def conv_bn_act(x, filters, kernel_size=(3, 3), strides=(1, 1)):\n",
" # convolution, batch normalization, activation (ReLU)\n",
" x = layers.Conv2D(filters, kernel_size, strides, padding='same')(x)\n",
" x = layers.BatchNormalization()(x)\n",
" x = layers.Activation('relu')(x)\n",
" return x\n",
"\n",
"def inverted_residual_block(x, filters, t):\n",
" x1 = x\n",
" x = conv_bn_act(x, filters * t, (1, 1))\n",
" x = layers.SeparableConv2D(filters, (3, 3), padding='same')(x)\n",
" x = layers.BatchNormalization()(x)\n",
" x = layers.Activation('relu')(x)\n",
" return x1 + x\n",
"\n",
"# input tensor\n",
"img = keras.layers.Input(shape=input_shape)"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "JLZY2N0fug7x",
"outputId": "3b2d49cc-5a14-459a-8ae5-26499193103f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 417
}
},
"source": [
"# model definition\n",
"n = 2\n",
"x = img\n",
"x = conv_bn_act(x, n)\n",
"x = inverted_residual_block(x, n, 6)\n",
"n *= 2\n",
"x = conv_bn_act(x, n, strides=(2, 2)) # 28x28 -> 14x14\n",
"x = inverted_residual_block(x, n, 6)\n",
"x = inverted_residual_block(x, n, 6)\n",
"n *= 2\n",
"x = conv_bn_act(x, n, strides=(2, 2)) # 14x14 -> 7x7\n",
"x = inverted_residual_block(x, n, 6)\n",
"x = inverted_residual_block(x, n, 6)\n",
"x = inverted_residual_block(x, n, 6)\n",
"x = layers.Dense(num_classes, activation='softmax')(layers.Flatten()(x))\n",
"# make model\n",
"model = keras.models.Model(img, x)\n",
"# compile\n",
"model.compile(loss='categorical_crossentropy', optimizer='sgd',\n",
" metrics=['accuracy'])\n",
"# train\n",
"batch_size = 32\n",
"epochs = 10\n",
"model.fit(x_train, y_train,\n",
" batch_size=batch_size,\n",
" epochs=epochs,\n",
" verbose=1,\n",
" validation_data=(x_test, y_test))\n",
"score = model.evaluate(x_test, y_test, verbose=0)\n",
"print('Test loss:', score[0])\n",
"print('Test accuracy:', score[1])"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.2799 - accuracy: 0.9129 - val_loss: 0.1415 - val_accuracy: 0.9530\n",
"Epoch 2/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.1258 - accuracy: 0.9606 - val_loss: 0.1159 - val_accuracy: 0.9620\n",
"Epoch 3/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.0939 - accuracy: 0.9700 - val_loss: 0.0826 - val_accuracy: 0.9734\n",
"Epoch 4/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.0763 - accuracy: 0.9763 - val_loss: 0.0762 - val_accuracy: 0.9747\n",
"Epoch 5/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.0659 - accuracy: 0.9791 - val_loss: 0.0713 - val_accuracy: 0.9750\n",
"Epoch 6/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.0586 - accuracy: 0.9816 - val_loss: 0.0690 - val_accuracy: 0.9772\n",
"Epoch 7/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.0525 - accuracy: 0.9832 - val_loss: 0.0602 - val_accuracy: 0.9795\n",
"Epoch 8/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.0483 - accuracy: 0.9850 - val_loss: 0.0555 - val_accuracy: 0.9807\n",
"Epoch 9/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.0448 - accuracy: 0.9856 - val_loss: 0.0577 - val_accuracy: 0.9805\n",
"Epoch 10/10\n",
"1875/1875 [==============================] - 12s 6ms/step - loss: 0.0412 - accuracy: 0.9869 - val_loss: 0.0576 - val_accuracy: 0.9816\n",
"Test loss: 0.05757155269384384\n",
"Test accuracy: 0.9815999865531921\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "p-27WVTQmlWp",
"outputId": "b79eea47-612c-48c9-9fd3-034f71cd66d9",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
}
},
"source": [
"model.summary()"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"Model: \"functional_1\"\n",
"__________________________________________________________________________________________________\n",
"Layer (type) Output Shape Param # Connected to \n",
"==================================================================================================\n",
"input_1 (InputLayer) [(None, 28, 28, 1)] 0 \n",
"__________________________________________________________________________________________________\n",
"conv2d (Conv2D) (None, 28, 28, 2) 20 input_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization (BatchNorma (None, 28, 28, 2) 8 conv2d[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation (Activation) (None, 28, 28, 2) 0 batch_normalization[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv2d_1 (Conv2D) (None, 28, 28, 12) 36 activation[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_1 (BatchNor (None, 28, 28, 12) 48 conv2d_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_1 (Activation) (None, 28, 28, 12) 0 batch_normalization_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"separable_conv2d (SeparableConv (None, 28, 28, 2) 134 activation_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_2 (BatchNor (None, 28, 28, 2) 8 separable_conv2d[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_2 (Activation) (None, 28, 28, 2) 0 batch_normalization_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"tf_op_layer_AddV2 (TensorFlowOp [(None, 28, 28, 2)] 0 activation[0][0] \n",
" activation_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv2d_2 (Conv2D) (None, 14, 14, 4) 76 tf_op_layer_AddV2[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_3 (BatchNor (None, 14, 14, 4) 16 conv2d_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_3 (Activation) (None, 14, 14, 4) 0 batch_normalization_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv2d_3 (Conv2D) (None, 14, 14, 24) 120 activation_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_4 (BatchNor (None, 14, 14, 24) 96 conv2d_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_4 (Activation) (None, 14, 14, 24) 0 batch_normalization_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"separable_conv2d_1 (SeparableCo (None, 14, 14, 4) 316 activation_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_5 (BatchNor (None, 14, 14, 4) 16 separable_conv2d_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_5 (Activation) (None, 14, 14, 4) 0 batch_normalization_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"tf_op_layer_AddV2_1 (TensorFlow [(None, 14, 14, 4)] 0 activation_3[0][0] \n",
" activation_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv2d_4 (Conv2D) (None, 14, 14, 24) 120 tf_op_layer_AddV2_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_6 (BatchNor (None, 14, 14, 24) 96 conv2d_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_6 (Activation) (None, 14, 14, 24) 0 batch_normalization_6[0][0] \n",
"__________________________________________________________________________________________________\n",
"separable_conv2d_2 (SeparableCo (None, 14, 14, 4) 316 activation_6[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_7 (BatchNor (None, 14, 14, 4) 16 separable_conv2d_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_7 (Activation) (None, 14, 14, 4) 0 batch_normalization_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"tf_op_layer_AddV2_2 (TensorFlow [(None, 14, 14, 4)] 0 tf_op_layer_AddV2_1[0][0] \n",
" activation_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv2d_5 (Conv2D) (None, 7, 7, 8) 296 tf_op_layer_AddV2_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_8 (BatchNor (None, 7, 7, 8) 32 conv2d_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_8 (Activation) (None, 7, 7, 8) 0 batch_normalization_8[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv2d_6 (Conv2D) (None, 7, 7, 48) 432 activation_8[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_9 (BatchNor (None, 7, 7, 48) 192 conv2d_6[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_9 (Activation) (None, 7, 7, 48) 0 batch_normalization_9[0][0] \n",
"__________________________________________________________________________________________________\n",
"separable_conv2d_3 (SeparableCo (None, 7, 7, 8) 824 activation_9[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_10 (BatchNo (None, 7, 7, 8) 32 separable_conv2d_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_10 (Activation) (None, 7, 7, 8) 0 batch_normalization_10[0][0] \n",
"__________________________________________________________________________________________________\n",
"tf_op_layer_AddV2_3 (TensorFlow [(None, 7, 7, 8)] 0 activation_8[0][0] \n",
" activation_10[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv2d_7 (Conv2D) (None, 7, 7, 48) 432 tf_op_layer_AddV2_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_11 (BatchNo (None, 7, 7, 48) 192 conv2d_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_11 (Activation) (None, 7, 7, 48) 0 batch_normalization_11[0][0] \n",
"__________________________________________________________________________________________________\n",
"separable_conv2d_4 (SeparableCo (None, 7, 7, 8) 824 activation_11[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_12 (BatchNo (None, 7, 7, 8) 32 separable_conv2d_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_12 (Activation) (None, 7, 7, 8) 0 batch_normalization_12[0][0] \n",
"__________________________________________________________________________________________________\n",
"tf_op_layer_AddV2_4 (TensorFlow [(None, 7, 7, 8)] 0 tf_op_layer_AddV2_3[0][0] \n",
" activation_12[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv2d_8 (Conv2D) (None, 7, 7, 48) 432 tf_op_layer_AddV2_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_13 (BatchNo (None, 7, 7, 48) 192 conv2d_8[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_13 (Activation) (None, 7, 7, 48) 0 batch_normalization_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"separable_conv2d_5 (SeparableCo (None, 7, 7, 8) 824 activation_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"batch_normalization_14 (BatchNo (None, 7, 7, 8) 32 separable_conv2d_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_14 (Activation) (None, 7, 7, 8) 0 batch_normalization_14[0][0] \n",
"__________________________________________________________________________________________________\n",
"tf_op_layer_AddV2_5 (TensorFlow [(None, 7, 7, 8)] 0 tf_op_layer_AddV2_4[0][0] \n",
" activation_14[0][0] \n",
"__________________________________________________________________________________________________\n",
"flatten (Flatten) (None, 392) 0 tf_op_layer_AddV2_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"dense (Dense) (None, 10) 3930 flatten[0][0] \n",
"==================================================================================================\n",
"Total params: 10,140\n",
"Trainable params: 9,636\n",
"Non-trainable params: 504\n",
"__________________________________________________________________________________________________\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "-tKNhBiVAeGt",
"outputId": "a4425171-1d16-44af-fc08-a77098546035",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"# prepare dataset for ncc\n",
"%cd /content\n",
"!mkdir images\n",
"import numpy as np\n",
"import cv2\n",
"batch_num = 100\n",
"batch = x_train[0:batch_num]\n",
"imgs = batch.reshape((batch_num,28,28)) * 255\n",
"imgs = imgs.astype(np.uint8)\n",
"for i,img in enumerate(imgs):\n",
" cv2.imwrite(\"images/%03d.jpg\"%i, img)\n",
"# prepare config file for kmodel\n",
"jsontext=\"\"\"\n",
"{\n",
" \"version\": \"0.2.0\",\n",
" \"files\": [\n",
" {\n",
" \"address\": 0x00300000,\n",
" \"bin\": \"model.kmodel\",\n",
" \"sha256Prefix\": false\n",
" }\n",
" ]\n",
"}\n",
"\"\"\"\n",
"with open(\"flash-list.json\",\"w\") as f:\n",
" f.write(jsontext)"
],
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"/content\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Y951f0O7_zaz",
"outputId": "ad9d96a4-02c6-43eb-975d-c83b1c8cb509",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"# Download NCC binary\n",
"%cd /content\n",
"!wget -q https://github.com/kendryte/nncase/releases/download/v0.2.0-beta4/ncc_linux_x86_64.tar.xz\n",
"!tar Jxf ncc_linux_x86_64.tar.xz"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"/content\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "zuRtALjR_43v",
"outputId": "0b2acb39-07f9-49c0-864a-087102dcd5cf",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 619
}
},
"source": [
"# save model for K210\n",
"%cd /content\n",
"model.save(\"model.h5\")\n",
"# model -> tflite\n",
"import tensorflow as tf\n",
"converter = tf.lite.TFLiteConverter.from_keras_model(model)\n",
"tflite_model = converter.convert()\n",
"open(\"model.tflite\", \"wb\").write(tflite_model)\n",
"# tflite -> kmodel\n",
"!/content/ncc compile /content/model.tflite /content/model.kmodel -i tflite --dataset /content/images\n",
"# (kmodel, json) -> kfpkg\n",
"!zip -r kmodel_at_0x300000.kfpkg model.kmodel flash-list.json"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"/content\n",
"WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n",
"WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n",
"INFO:tensorflow:Assets written to: /tmp/tmp3w1qbn44/assets\n",
"1. Import graph...\n",
"2. Optimize Pass 1...\n",
"3. Optimize Pass 2...\n",
"4. Quantize...\n",
" 4.1. Add quantization checkpoints...\n",
" 4.2. Get activation ranges...\n",
" Plan buffers...\n",
" Run calibration...\n",
" [==================================================] 100% 0.206s\n",
" 4.5. Quantize graph...\n",
"5. Lowering...\n",
"6. Optimize Pass 3...\n",
"7. Generate code...\n",
" Plan buffers...\n",
" Emit code...\n",
"Working memory usage: 31360 B\n",
"\n",
"SUMMARY\n",
"INPUTS\n",
"0\tInput_0\t1x1x28x28\n",
"OUTPUTS\n",
"0\tIdentity\t1x10\n",
" adding: model.kmodel (deflated 55%)\n",
" adding: flash-list.json (deflated 26%)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "-OwpRRnjnfXg",
"outputId": "38eaa9b8-d736-4dab-d0f3-17f0b739bb8e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 17
}
},
"source": [
"# download kfpkg\n",
"from google.colab import files\n",
"files.download('kmodel_at_0x300000.kfpkg')"
],
"execution_count": 8,
"outputs": [
{
"output_type": "display_data",
"data": {
"application/javascript": [
"\n",
" async function download(id, filename, size) {\n",
" if (!google.colab.kernel.accessAllowed) {\n",
" return;\n",
" }\n",
" const div = document.createElement('div');\n",
" const label = document.createElement('label');\n",
" label.textContent = `Downloading \"${filename}\": `;\n",
" div.appendChild(label);\n",
" const progress = document.createElement('progress');\n",
" progress.max = size;\n",
" div.appendChild(progress);\n",
" document.body.appendChild(div);\n",
"\n",
" const buffers = [];\n",
" let downloaded = 0;\n",
"\n",
" const channel = await google.colab.kernel.comms.open(id);\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
"\n",
" for await (const message of channel.messages) {\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
" if (message.buffers) {\n",
" for (const buffer of message.buffers) {\n",
" buffers.push(buffer);\n",
" downloaded += buffer.byteLength;\n",
" progress.value = downloaded;\n",
" }\n",
" }\n",
" }\n",
" const blob = new Blob(buffers, {type: 'application/binary'});\n",
" const a = document.createElement('a');\n",
" a.href = window.URL.createObjectURL(blob);\n",
" a.download = filename;\n",
" div.appendChild(a);\n",
" a.click();\n",
" div.remove();\n",
" }\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {
"tags": []
}
},
{
"output_type": "display_data",
"data": {
"application/javascript": [
"download(\"download_a0513e6a-1b45-429b-9017-e0e93a3b7cb6\", \"kmodel_at_0x300000.kfpkg\", 14149)"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "C1SfYHIILuOy"
},
"source": [
"モデルをフラッシュに書き込み\n",
"\n",
"```sh\n",
"kflash -p /dev/ttyUSB0 -b 3000000 kmodel_at_0x300000.kfpkg\n",
"```\n",
"\n",
"MaixPy IDE で以下のコードを実行する.\n",
"\n",
"```py\n",
"import sensor, lcd, image, time\n",
"import KPU as kpu\n",
"lcd.init()\n",
"sensor.reset()\n",
"sensor.set_auto_gain(0,24) # auto gain disable and 24dB\n",
"sensor.set_pixformat(sensor.RGB565)\n",
"sensor.set_framesize(sensor.QVGA)\n",
"sensor.set_windowing((224, 224)) #set to 224x224 input\n",
"sensor.set_vflip(True) #flip camera\n",
"sensor.set_hmirror(True) #flip camera\n",
"task = kpu.load(0x300000) #load model from flash address 0x300000\n",
"kpu.set_outputs(task, 0, 10, 1, 1)\n",
"clock = time.clock() # Create a clock object to track the FPS.\n",
"sensor.run(1)\n",
"while True:\n",
" clock.tick()\n",
" img = sensor.snapshot()\n",
" lcd.display(img,oft=(0,0)) #display large picture\n",
" img1 = img.to_grayscale(1) #convert to gray\n",
" img2 = img1.resize(28,28) #resize to mnist input 28x28\n",
" a = img2.invert() #invert picture as mnist need\n",
" a = img2.strech_char(1) #preprocessing pictures, eliminate dark corner\n",
" lcd.display(img2,oft=(224,32)) #display small 28x28 picture\n",
" a = img2.pix_to_ai(); #generate data for ai\n",
" fmap = kpu.forward(task,img2) #run neural network model\n",
" plist = fmap[:] #get result (10 digit's probability)\n",
" pmax = max(plist) #get max probability\n",
" fps = clock.fps()\n",
" max_index = plist.index(pmax) #get the digit\n",
" print(\"%d %d fps\" % (max_index, fps))\n",
" print(plist)\n",
" lcd.draw_string(224,0,\"%d: %.3f\"%(max_index,pmax),lcd.WHITE,lcd.BLACK) #show result\n",
"\n",
"```"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment