{"id":3379,"date":"2021-06-26T16:13:25","date_gmt":"2021-06-26T07:13:25","guid":{"rendered":"http:\/\/oohito.com\/nqthm\/?p=3379"},"modified":"2022-08-29T10:55:46","modified_gmt":"2022-08-29T01:55:46","slug":"m5stickv-m5unitv-%e3%81%ae-model-kmodel-%e3%82%92-windows-10-%e3%81%a7%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b%ef%bc%88%e8%bb%a2%e7%a7%bb%e5%ad%a6%e7%bf%92%e7%b7%a8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/oohito.com\/nqthm\/archives\/3379","title":{"rendered":"M5StickV\/M5UnitV \u306e model.kmodel \u3092 Windows 10 \u3067\u4f5c\u6210\u3059\u308b\uff08\u8ee2\u79fb\u5b66\u7fd2\u7de8\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u307e\u3048\u304c\u304d<\/h2>\n\n\n\n<p><a href=\"http:\/\/oohito.com\/nqthm\/archives\/3365\" data-type=\"post\" data-id=\"3365\">\u524d\u56de\u306e\u8a18\u4e8b<\/a>\u307e\u3067\u306e\u6e96\u5099\u304c\u6574\u3063\u305f\u3068\u3053\u308d\u3067\u3001\u5b9f\u969b\u306b model.kmodel \u3092\u4f5c\u6210\u3059\u308b\u624b\u9806\u306b\u3064\u3044\u3066\u8aac\u660e\u3059\u308b\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001MobileNet.v1 \u3092\u7528\u3044\u305f\u8ee2\u79fb\u5b66\u7fd2\u306b\u3088\u308a\u4f5c\u6210\u3057\u305f\u30e2\u30c7\u30eb\u3092\u3082\u3068\u306b kmodel \u5f62\u5f0f\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u751f\u6210\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u3092\u7d39\u4ecb\u3059\u308b\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u3092\u53c2\u8003\u306b\u8a18\u4e8b\u3092\u4f5c\u6210\u3057\u305f\u3002\u5143\u8a18\u4e8b\u306f Google Colab \u3092\u4f7f\u3063\u3066\u3044\u308b\u304c\u3001Windows 10 \u4e0a\u306e Jupyter Notebook \u3092\u7528\u3044\u308b\u3088\u3046\u306b\u4fee\u6b63\u3057\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"https:\/\/www.lancard.com\/blog\/2019\/09\/24\/m5stickv%E3%81%A7%E7%8B%AC%E8%87%AA%E3%81%AE%E3%83%A2%E3%83%87%E3%83%AB%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E7%94%BB%E5%83%8F%E8%AA%8D%E8%AD%98\/\">https:\/\/www.lancard.com\/blog\/2019\/09\/24\/m5stickv%E3%81%A7%E7%8B%AC%E8%87%AA%E3%81%AE%E3%83%A2%E3%83%87%E3%83%AB%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E7%94%BB%E5%83%8F%E8%AA%8D%E8%AD%98\/<\/a><\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u306e\u6e96\u5099<\/h2>\n\n\n\n<p>\u753b\u50cf\u30c7\u30fc\u30bf\u306f\u524d\u3005\u56de\u306e\u8a18\u4e8b\u3067\u7528\u610f\u3057\u305f k210 \u30d5\u30a9\u30eb\u30c0\u5185\u306b\u7528\u610f\u3059\u308b\u3002images \u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u306b\u30d5\u30a9\u30eb\u30c0\u3054\u3068\u306b\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u5206\u985e\u3057\u3066\u5165\u308c\u3066\u304a\u304f\u3082\u306e\u3068\u3059\u308b\u3002\u4f8b\u3048\u3070\u30d5\u30a9\u30eb\u30c0\u69cb\u9020\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u306b\u306a\u308b\u3002\uff08\u4ee5\u4e0b\u3067\u306f\u3001images\u30d5\u30a9\u30eb\u30c0\u306e\u4e0b\u306b\u3001salad, sushi, tofu \u306e\u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u6210\u3057\u3001\u305d\u308c\u305e\u308c\u306b\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">C:\\USERS\\EIICHIRO ITO\\DOCUMENTS\\K210\n \u251c\u2500.ipynb_checkpoints\n \u251c\u2500images\n \u2502  \u251c\u2500salad\n \u2502  \u251c\u2500sushi\n \u2502  \u2514\u2500tofu\n \u251c\u2500Maix_Toolbox\n \u2502  \u251c\u2500images\n \u2502  \u251c\u2500log\n \u2502  \u251c\u2500ncc\n \u2502  \u2502  \u251c\u2500bin\n \u2502  \u2502  \u2514\u2500refs\n \u2502  \u2514\u2500workspace\n \u2514\u2500transfer_learning_sipeed\n     \u251c\u2500images\n     \u2502  \u251c\u2500your_class1\n     \u2502  \u2514\u2500your_class2\n     \u251c\u2500mobilenet_sipeed\n     \u2502  \u2514\u2500<strong>pycache<\/strong>\n     \u251c\u2500mobilenet_v1_transfer_learning\n     \u2514\u2500model_labels<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Jupyter Notebook \u306e\u8d77\u52d5<\/h2>\n\n\n\n<p>\u3053\u308c\u4ee5\u964d\u306e\u4f5c\u696d\u3067\u306f miniconda3 \u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f Jupyter Notebook \u3092\u5229\u7528\u3059\u308b\u3002\u30b9\u30bf\u30fc\u30c8\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u3066\u300cAnaconda3(64 bit)\u300d\u304b\u3089\u300cJupyter Notebook (k210)\u300d\u3092\u9078\u3093\u3067 Jupyter Notebook \u3092\u8d77\u52d5\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u3059\u308b\u3068\u30a6\u30a7\u30d6\u30d6\u30e9\u30a6\u30b6\u304c\u958b\u3044\u3066\u30d5\u30a9\u30eb\u30c0\u4e00\u89a7\u304c\u8868\u793a\u3055\u308c\u308b\u3002Documents \u3092\u30af\u30ea\u30c3\u30af\u3057\u3001\u3055\u3089\u306b k210 \u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u753b\u9762\u306b\u306a\u308b\u3002\uff08sample1.ipynb \u306f\u3053\u308c\u304b\u3089\u4f5c\u6210\u3059\u308b\u306e\u3067\u3001\u307e\u3060\u5b58\u5728\u3057\u306a\u3044\uff09<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/oohito.com\/nqthm\/wp-content\/uploads\/2021\/06\/5ea0a3f2685fbe2bc9c36e140672bfa1-1-1.png\"><img decoding=\"async\" src=\"http:\/\/oohito.com\/nqthm\/wp-content\/uploads\/2021\/06\/5ea0a3f2685fbe2bc9c36e140672bfa1-1.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p>\u53f3\u5074\u306e\u300cNew\u300d\u3068\u3044\u3046\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u300cPython 3\u300d\u3068\u3044\u3046\u9805\u76ee\u3092\u9078\u3076\u3068\u3001\u30d6\u30e9\u30a6\u30b6\u306e\u65b0\u3057\u3044\u30bf\u30d6\u306b Notebook \u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/oohito.com\/nqthm\/wp-content\/uploads\/2021\/06\/38344fc5924ef78413a29ccd8a36a82f-1-1.png\"><img decoding=\"async\" src=\"http:\/\/oohito.com\/nqthm\/wp-content\/uploads\/2021\/06\/38344fc5924ef78413a29ccd8a36a82f-1.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p>In [ ] \u306e\u53f3\u306b\u3042\u308b\u6b04\u306b\u3001\u4ee5\u4e0b\u306e\u5185\u5bb9\u3092\u5165\u529b\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!cd<\/pre>\n\n\n\n<p>\u5165\u529b\u3057\u305f\u5f8c\u3067\u3001Shift \u30ad\u30fc\u3092\u62bc\u3057\u306a\u304c\u3089 Enter \u30ad\u30fc\u3092\u62bc\u3059\u3068\u3001\u5165\u529b\u3057\u305f\u5185\u5bb9\u304c\u5b9f\u884c\u3055\u308c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u7d50\u679c\uff08\u4f5c\u6210\u3057\u305f Notebook \u304c\u3042\u308b\u30d5\u30a9\u30eb\u30c0\u540d\uff09\u304c\u8868\u793a\u3055\u308c\u308b\u3002\uff08\u753b\u9762\u4e0a\u90e8\u306e Run \u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u3082\u3088\u3044\uff09<\/p>\n\n\n\n<p>miniconda \u306e\u74b0\u5883\u540d\u304c\u8868\u793a\u3055\u308c\u308c\u3070 OK \u3067\u3042\u308b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/oohito.com\/nqthm\/wp-content\/uploads\/2021\/06\/b37080fca09cfbb0bb52a73e043c116e-1-1.png\"><img decoding=\"async\" src=\"http:\/\/oohito.com\/nqthm\/wp-content\/uploads\/2021\/06\/b37080fca09cfbb0bb52a73e043c116e-1.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MobileNet.v1 \u306b\u3088\u308b\u8ee2\u79fb\u5b66\u7fd2<\/h2>\n\n\n\n<p>\u3042\u3068\u306f Windows 10 \u4e0a\u3067\u3042\u308b\u3053\u3068\u306b\u6c17\u3092\u3064\u3051\u306a\u304c\u3089\u3001\u30b3\u30d4\u30da\u3057\u306a\u304c\u3089 Notebook \u3092\u52d5\u304b\u3057\u3066\u3044\u3051\u3070\u826f\u3044\u3002\u307e\u305a\u3001\u5404\u7a2e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import keras\nimport numpy as np\nfrom keras import backend as K\nfrom keras.optimizers import Adam\nfrom keras.metrics import categorical_crossentropy\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing import image\nfrom keras.models import Model\nfrom keras.applications import imagenet_utils\nfrom keras.layers import Dense, GlobalAveragePooling2D, Dropout\nimport sys\nsys.path.append('.\/transfer_learning_sipeed')\nfrom mobilenet_sipeed.mobilenet import MobileNet\nfrom keras.applications.mobilenet import preprocess_input\nimport tensorflow<\/pre>\n\n\n\n<p>\u30ad\u30e2\u306f\u3001\u5f8c\u534a\u306b\u3042\u308b\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u30fb\u5c55\u958b\u3057\u305fMobileNet \u3092\u4f7f\u3046\u90e8\u5206\u3067\u3042\u308b\u3002\uff08import sys\u304b\u3089\u306e3\u884c\uff09<\/p>\n\n\n\n<p>\u7d9a\u3044\u3066 MobileNet \u3092\u4f7f\u3063\u3066\u57fa\u672c\u3068\u306a\u308b\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3002\u5165\u529b\u3068\u3057\u3066 224&#215;224 \u30d4\u30af\u30bb\u30eb\u306e\u753b\u50cf\u3092\u4f7f\u3046\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">IMAGE_SIZE = 224\nALPHA = 0.75\nbase_model=MobileNet(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), alpha = ALPHA, depth_multiplier = 1, dropout = 0.001, include_top = False, weights = \"imagenet\", classes = 1000, backend=keras.backend, layers=keras.layers,models=keras.models,utils=tensorflow.keras.utils)<\/pre>\n\n\n\n<p>\u6700\u7d42\u6bb5\u3092\u8ffd\u52a0\u3057\u3066\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">x = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(100,activation='relu')(x)\nx = Dropout(0.5)(x)\nx = Dense(50,activation='relu')(x)\npreds = Dense(3,activation='softmax')(x)\nmodel = Model(inputs=base_model.input, outputs=preds)<\/pre>\n\n\n\n<p>\u5ff5\u306e\u70ba\u30e2\u30c7\u30eb\u306e\u5168\u5c64\u3092\u8868\u793a\u3057\u305f\u5f8c\u3001\u30e2\u30c7\u30eb\u306e\u57fa\u672c\u90e8\u5206\u306f\u5b66\u7fd2\u3055\u305b\u305a\u306b\u8ffd\u52a0\u3057\u305f\u5f8c\u534a\u306e\u5c64\u3060\u3051\u5b66\u7fd2\u3055\u305b\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i,layer in enumerate(model.layers):\n    print(i,layer.name)\nfor layer in model.layers[:86]:\n    layer.trainable=False\nfor layer in model.layers[86:]:\n    layer.trainable=True<\/pre>\n\n\n\n<p>\u6e96\u5099\u3057\u305f\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u8ee2\u79fb\u5b66\u7fd2\u3055\u305b\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\ntrain_generator = train_datagen.flow_from_directory('.\/images', target_size=(IMAGE_SIZE,IMAGE_SIZE), color_mode='rgb', batch_size=32, class_mode='categorical', shuffle=True)<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u753b\u50cf\u30d5\u30a9\u30eb\u30c0\u3068\u5206\u985e\u7d50\u679c\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u306e\u5bfe\u5fdc\u304c\u308f\u304b\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">train_generator.class_indices<\/pre>\n\n\n\n<p>\u30e2\u30c7\u30eb\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])<\/pre>\n\n\n\n<p>\u5b66\u7fd2\u3092\u5b9f\u884c\u3055\u305b\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">step_size_train = train_generator.n \/\/ train_generator.batch_size\nmodel.fit_generator(generator=train_generator, steps_per_epoch=step_size_train, epochs=10)<\/pre>\n\n\n\n<p>\u5b66\u7fd2\u3057\u305f\u30e2\u30c7\u30eb\u3092 model.h5 \u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">model.save('model.h5')<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">model.kmodel \u30d5\u30a1\u30a4\u30eb\u306b\u5909\u63db\u3059\u308b<\/h2>\n\n\n\n<p>\u307e\u305a\u306f TensorFlow Lite \u5f62\u5f0f\u306b\u5909\u63db\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!tflite_convert --output_file=.\/model.tflite --keras_model_file=.\/model.h5<\/pre>\n\n\n\n<p>\u6b21\u306b kmodel \u5f62\u5f0f\u306b\u5909\u63db\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">%cd Maix_Toolbox\n!ncc\\ncc -i tflite -o k210model --dataset ..\/images ..\/model.tflite ..\/model.kmodel\n%cd ..<\/pre>\n\n\n\n<p>\u3053\u308c\u3067\u51fa\u6765\u4e0a\u304c\u3063\u305f\uff08\u306f\u305a\uff09\u3067\u3042\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!dir<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u30c9\u30e9\u30a4\u30d6 C \u306e\u30dc\u30ea\u30e5\u30fc\u30e0 \u30e9\u30d9\u30eb\u306f OS \u3067\u3059\n  \u30dc\u30ea\u30e5\u30fc\u30e0 \u30b7\u30ea\u30a2\u30eb\u756a\u53f7\u306f 9A9C-C56D \u3067\u3059\n C:\\Users\\Eiichiro Ito\\working \u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\n 2021\/06\/22  12:15\n          .\n 2021\/06\/22  12:15   &lt;DIR&gt;      ..\n 2021\/06\/22  09:46   &lt;DIR&gt;      .ipynb_checkpoints\n 2021\/06\/22  10:49   &lt;DIR&gt;      images\n 2021\/06\/22  09:39   &lt;DIR&gt;      Maix_Toolbox\n 2021\/06\/22  12:08    8,596,944 model.h5\n 2021\/06\/22  12:15    1,964,232 <span class=\"has-inline-color has-vivid-red-color\">model.kmodel<\/span>\n 2021\/06\/22  12:09    7,575,316 model.tflite\n 2021\/06\/22  09:44   &lt;DIR&gt;      transfer_learning_sipeed\n 2021\/06\/22  12:10       17,293 Untitled.ipynb\n                4 \u500b\u306e\u30d5\u30a1\u30a4\u30eb          18,153,785 \u30d0\u30a4\u30c8\n                6 \u500b\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea  73,775,235,072 \u30d0\u30a4\u30c8\u306e\u7a7a\u304d\u9818\u57df<\/pre>\n\n\n\n<p>\u3053\u3053\u307e\u3067\u30a8\u30e9\u30fc\u306a\u3057\u3067\u5230\u9054\u3067\u304d\u308c\u3070\u5b8c\u4e86\u3068\u306a\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u307e\u3048\u304c\u304d \u524d\u56de\u306e\u8a18\u4e8b\u307e\u3067\u306e\u6e96\u5099\u304c\u6574\u3063\u305f\u3068\u3053\u308d\u3067\u3001\u5b9f\u969b\u306b model.kmodel \u3092\u4f5c\u6210\u3059\u308b\u624b\u9806\u306b\u3064\u3044\u3066\u8aac\u660e\u3059\u308b\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001MobileNet.v1 \u3092\u7528\u3044\u305f\u8ee2\u79fb\u5b66\u7fd2\u306b\u3088\u308a\u4f5c\u6210\u3057\u305f\u30e2\u30c7\u30eb\u3092\u3082\u3068\u306b kmodel \u5f62\u5f0f\u306e&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-3379","post","type-post","status-publish","format-standard","hentry","category-k210"],"_links":{"self":[{"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/posts\/3379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/comments?post=3379"}],"version-history":[{"count":1,"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/posts\/3379\/revisions"}],"predecessor-version":[{"id":3564,"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/posts\/3379\/revisions\/3564"}],"wp:attachment":[{"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/media?parent=3379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/categories?post=3379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oohito.com\/nqthm\/wp-json\/wp\/v2\/tags?post=3379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}