Created
July 21, 2019 15:56
-
-
Save gheesung/79a54ebec00abd6f9913ea7047721db8 to your computer and use it in GitHub Desktop.
Image Classification mobilenetv2 With Keras for edge TPU using TF2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Image Classification mobilenetv2 With Keras for edge TPU using TF2", | |
"version": "0.3.2", | |
"provenance": [], | |
"collapsed_sections": [], | |
"toc_visible": true, | |
"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/gheesung/79a54ebec00abd6f9913ea7047721db8/image-classification-mobilenetv2-with-keras-for-edge-tpu-using-tf2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "prOt_VofnYCo", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"#Image Classification/Quantisation/Transfer Learning/Edge TPU.\n", | |
"\n", | |
"." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "OakAxtbW2hbA", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Transfer Learning using Mobilenet V2\n", | |
"\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6IKHm_2YfTj-", | |
"colab_type": "code", | |
"outputId": "52d8494f-fe24-4cd4-eb85-0a722614395e", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 546 | |
} | |
}, | |
"source": [ | |
"!pip install tensorflow-gpu==2.0-beta1" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Collecting tensorflow-gpu==2.0-beta1\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/2b/53/e18c5e7a2263d3581a979645a185804782e59b8e13f42b9c3c3cfb5bb503/tensorflow_gpu-2.0.0b1-cp36-cp36m-manylinux1_x86_64.whl (348.9MB)\n", | |
"\u001b[K |████████████████████████████████| 348.9MB 52kB/s \n", | |
"\u001b[?25hCollecting tb-nightly<1.14.0a20190604,>=1.14.0a20190603 (from tensorflow-gpu==2.0-beta1)\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/a4/96/571b875cd81dda9d5dfa1422a4f9d749e67c0a8d4f4f0b33a4e5f5f35e27/tb_nightly-1.14.0a20190603-py3-none-any.whl (3.1MB)\n", | |
"\u001b[K |████████████████████████████████| 3.1MB 48.5MB/s \n", | |
"\u001b[?25hRequirement already satisfied: gast>=0.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (0.2.2)\n", | |
"Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (1.1.0)\n", | |
"Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (0.7.1)\n", | |
"Requirement already satisfied: protobuf>=3.6.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (3.7.1)\n", | |
"Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (1.12.0)\n", | |
"Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (1.1.0)\n", | |
"Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (0.33.4)\n", | |
"Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (1.15.0)\n", | |
"Requirement already satisfied: google-pasta>=0.1.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (0.1.7)\n", | |
"Requirement already satisfied: astor>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (0.8.0)\n", | |
"Collecting tf-estimator-nightly<1.14.0.dev2019060502,>=1.14.0.dev2019060501 (from tensorflow-gpu==2.0-beta1)\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/32/dd/99c47dd007dcf10d63fd895611b063732646f23059c618a373e85019eb0e/tf_estimator_nightly-1.14.0.dev2019060501-py2.py3-none-any.whl (496kB)\n", | |
"\u001b[K |████████████████████████████████| 501kB 50.7MB/s \n", | |
"\u001b[?25hRequirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (1.11.2)\n", | |
"Requirement already satisfied: keras-applications>=1.0.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (1.0.8)\n", | |
"Requirement already satisfied: numpy<2.0,>=1.14.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0-beta1) (1.16.4)\n", | |
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tb-nightly<1.14.0a20190604,>=1.14.0a20190603->tensorflow-gpu==2.0-beta1) (3.1.1)\n", | |
"Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.6/dist-packages (from tb-nightly<1.14.0a20190604,>=1.14.0a20190603->tensorflow-gpu==2.0-beta1) (41.0.1)\n", | |
"Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tb-nightly<1.14.0a20190604,>=1.14.0a20190603->tensorflow-gpu==2.0-beta1) (0.15.5)\n", | |
"Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras-applications>=1.0.6->tensorflow-gpu==2.0-beta1) (2.8.0)\n", | |
"Installing collected packages: tb-nightly, tf-estimator-nightly, tensorflow-gpu\n", | |
"Successfully installed tb-nightly-1.14.0a20190603 tensorflow-gpu-2.0.0b1 tf-estimator-nightly-1.14.0.dev2019060501\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "2Z4E3BNH5VUB", | |
"colab_type": "code", | |
"outputId": "ebadebdf-bf7c-4c6c-fd47-1fb7892ce126", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 54 | |
} | |
}, | |
"source": [ | |
"from __future__ import absolute_import, division, print_function, unicode_literals\n", | |
"\n", | |
"import tensorflow as tf\n", | |
"from tensorflow import keras\n", | |
"from tensorflow.keras import layers, applications\n", | |
"print(tf.version.VERSION)\n", | |
"print(tf.keras.__version__)" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"2.0.0-beta1\n", | |
"2.2.4-tf\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "qJKw0elEWY3x", | |
"colab_type": "code", | |
"outputId": "228ea312-7df8-4bcc-9eea-d34e549a53a3", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"from tensorflow.keras.applications import MobileNetV2\n", | |
"\n", | |
"from tensorflow.keras.optimizers import Adam\n", | |
"from tensorflow.keras.metrics import categorical_crossentropy\n", | |
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", | |
"from tensorflow.keras.preprocessing import image\n", | |
"from tensorflow.keras.models import Model\n", | |
"from keras.applications import imagenet_utils\n", | |
"from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout\n", | |
"#from keras.applications.mobilenet_v2 import preprocess_input\n", | |
"from tensorflow.keras.applications.mobilenet_v2 import preprocess_input" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Using TensorFlow backend.\n" | |
], | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "t-6gYty7R3u8", | |
"colab_type": "code", | |
"outputId": "5c520d4c-18b0-4f16-8dc1-ea2acfa47fe7", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 90 | |
} | |
}, | |
"source": [ | |
"# Download the flower photos\n", | |
"\n", | |
"%cd /content\n", | |
"!curl -LO http://download.tensorflow.org/example_images/flower_photos.tgz\n", | |
"!tar xzf flower_photos.tgz" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"/content\n", | |
" % Total % Received % Xferd Average Speed Time Time Time Current\n", | |
" Dload Upload Total Spent Left Speed\n", | |
"100 218M 100 218M 0 0 115M 0 0:00:01 0:00:01 --:--:-- 115M\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "qN8wA-GN3AJK", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# the parameters\n", | |
"IMAGE_SIZE = 224\n", | |
"ALPHA=0.75\n", | |
"EPOCHS=20" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "wsWFPaOcOyS-", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def prepare_image(file):\n", | |
" img_path = ''\n", | |
" img = image.load_img(img_path + file, target_size=(IMAGE_SIZE, IMAGE_SIZE))\n", | |
" img_array = image.img_to_array(img)\n", | |
" #img_array= img_array/255.0\n", | |
" img_array_expanded_dims = np.expand_dims(img_array, axis=0)\n", | |
" return preprocess_input(img_array_expanded_dims)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "S7uWGZLdE55g", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def build_keras_model():\n", | |
" base_model=MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), alpha = ALPHA, \n", | |
" include_top = False, weights = \"imagenet\", classes = 1000 )\n", | |
"\n", | |
" for layer in base_model.layers:\n", | |
" layer.trainable = False\n", | |
" \n", | |
" # the last few layers\n", | |
" # for the flowers dataset, we are predicting 5 types of flower which is reason for the final layer to have 5 outputs.\n", | |
"\n", | |
" x=base_model.output\n", | |
" x=GlobalAveragePooling2D()(x)\n", | |
" x=Dense(100,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.\n", | |
" x=Dropout(0.5)(x)\n", | |
" x=Dense(50,activation='relu')(x) #dense layer 3\n", | |
" preds=Dense(5,activation='softmax')(x) #final layer with softmax activation \n", | |
" model=Model(inputs=base_model.input,outputs=preds)\n", | |
" for layer in model.layers[86:]:\n", | |
" layer.trainable=True \n", | |
" return model" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BayNJNWYSbr0", | |
"colab_type": "code", | |
"outputId": "f5ade6b4-804d-4dbf-eeb7-56ee13b3be17", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"source": [ | |
"train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) #included in our dependencies\n", | |
"\n", | |
"train_generator=train_datagen.flow_from_directory('/content/flower_photos',\n", | |
" target_size=(IMAGE_SIZE,IMAGE_SIZE),\n", | |
" color_mode='rgb',\n", | |
" batch_size=32,\n", | |
" class_mode='categorical', shuffle=True)" | |
], | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Found 3670 images belonging to 5 classes.\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "R6hbts_Sclxu", | |
"colab_type": "code", | |
"outputId": "efb71939-0da4-44a8-82e7-a715a36aa8f9", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 1000 | |
} | |
}, | |
"source": [ | |
"#train\n", | |
"step_size_train=train_generator.n//train_generator.batch_size\n", | |
"model = build_keras_model()\n", | |
"model.summary()\n", | |
"model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])\n", | |
"model.fit_generator(generator=train_generator,steps_per_epoch=step_size_train,epochs=EPOCHS)\n", | |
"model.save('model.h5')" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Downloading data from https://github.com/JonathanCMitchell/mobilenet_v2_keras/releases/download/v1.1/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.75_224_no_top.h5\n", | |
"5906432/5903360 [==============================] - 2s 0us/step\n", | |
"Model: \"model\"\n", | |
"__________________________________________________________________________________________________\n", | |
"Layer (type) Output Shape Param # Connected to \n", | |
"==================================================================================================\n", | |
"input_1 (InputLayer) [(None, 224, 224, 3) 0 \n", | |
"__________________________________________________________________________________________________\n", | |
"Conv1_pad (ZeroPadding2D) (None, 225, 225, 3) 0 input_1[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"Conv1 (Conv2D) (None, 112, 112, 24) 648 Conv1_pad[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"bn_Conv1 (BatchNormalization) (None, 112, 112, 24) 96 Conv1[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"Conv1_relu (ReLU) (None, 112, 112, 24) 0 bn_Conv1[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"expanded_conv_depthwise (Depthw (None, 112, 112, 24) 216 Conv1_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"expanded_conv_depthwise_BN (Bat (None, 112, 112, 24) 96 expanded_conv_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"expanded_conv_depthwise_relu (R (None, 112, 112, 24) 0 expanded_conv_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"expanded_conv_project (Conv2D) (None, 112, 112, 16) 384 expanded_conv_depthwise_relu[0][0\n", | |
"__________________________________________________________________________________________________\n", | |
"expanded_conv_project_BN (Batch (None, 112, 112, 16) 64 expanded_conv_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_expand (Conv2D) (None, 112, 112, 96) 1536 expanded_conv_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_expand_BN (BatchNormali (None, 112, 112, 96) 384 block_1_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_expand_relu (ReLU) (None, 112, 112, 96) 0 block_1_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_pad (ZeroPadding2D) (None, 113, 113, 96) 0 block_1_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_depthwise (DepthwiseCon (None, 56, 56, 96) 864 block_1_pad[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_depthwise_BN (BatchNorm (None, 56, 56, 96) 384 block_1_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_depthwise_relu (ReLU) (None, 56, 56, 96) 0 block_1_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_project (Conv2D) (None, 56, 56, 24) 2304 block_1_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_1_project_BN (BatchNormal (None, 56, 56, 24) 96 block_1_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_expand (Conv2D) (None, 56, 56, 144) 3456 block_1_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_expand_BN (BatchNormali (None, 56, 56, 144) 576 block_2_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_expand_relu (ReLU) (None, 56, 56, 144) 0 block_2_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_depthwise (DepthwiseCon (None, 56, 56, 144) 1296 block_2_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_depthwise_BN (BatchNorm (None, 56, 56, 144) 576 block_2_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_depthwise_relu (ReLU) (None, 56, 56, 144) 0 block_2_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_project (Conv2D) (None, 56, 56, 24) 3456 block_2_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_project_BN (BatchNormal (None, 56, 56, 24) 96 block_2_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_2_add (Add) (None, 56, 56, 24) 0 block_1_project_BN[0][0] \n", | |
" block_2_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_expand (Conv2D) (None, 56, 56, 144) 3456 block_2_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_expand_BN (BatchNormali (None, 56, 56, 144) 576 block_3_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_expand_relu (ReLU) (None, 56, 56, 144) 0 block_3_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_pad (ZeroPadding2D) (None, 57, 57, 144) 0 block_3_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_depthwise (DepthwiseCon (None, 28, 28, 144) 1296 block_3_pad[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_depthwise_BN (BatchNorm (None, 28, 28, 144) 576 block_3_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_depthwise_relu (ReLU) (None, 28, 28, 144) 0 block_3_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_project (Conv2D) (None, 28, 28, 24) 3456 block_3_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_3_project_BN (BatchNormal (None, 28, 28, 24) 96 block_3_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_expand (Conv2D) (None, 28, 28, 144) 3456 block_3_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_expand_BN (BatchNormali (None, 28, 28, 144) 576 block_4_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_expand_relu (ReLU) (None, 28, 28, 144) 0 block_4_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_depthwise (DepthwiseCon (None, 28, 28, 144) 1296 block_4_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_depthwise_BN (BatchNorm (None, 28, 28, 144) 576 block_4_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_depthwise_relu (ReLU) (None, 28, 28, 144) 0 block_4_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_project (Conv2D) (None, 28, 28, 24) 3456 block_4_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_project_BN (BatchNormal (None, 28, 28, 24) 96 block_4_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_4_add (Add) (None, 28, 28, 24) 0 block_3_project_BN[0][0] \n", | |
" block_4_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_expand (Conv2D) (None, 28, 28, 144) 3456 block_4_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_expand_BN (BatchNormali (None, 28, 28, 144) 576 block_5_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_expand_relu (ReLU) (None, 28, 28, 144) 0 block_5_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_depthwise (DepthwiseCon (None, 28, 28, 144) 1296 block_5_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_depthwise_BN (BatchNorm (None, 28, 28, 144) 576 block_5_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_depthwise_relu (ReLU) (None, 28, 28, 144) 0 block_5_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_project (Conv2D) (None, 28, 28, 24) 3456 block_5_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_project_BN (BatchNormal (None, 28, 28, 24) 96 block_5_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_5_add (Add) (None, 28, 28, 24) 0 block_4_add[0][0] \n", | |
" block_5_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_expand (Conv2D) (None, 28, 28, 144) 3456 block_5_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_expand_BN (BatchNormali (None, 28, 28, 144) 576 block_6_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_expand_relu (ReLU) (None, 28, 28, 144) 0 block_6_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_pad (ZeroPadding2D) (None, 29, 29, 144) 0 block_6_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_depthwise (DepthwiseCon (None, 14, 14, 144) 1296 block_6_pad[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_depthwise_BN (BatchNorm (None, 14, 14, 144) 576 block_6_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_depthwise_relu (ReLU) (None, 14, 14, 144) 0 block_6_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_project (Conv2D) (None, 14, 14, 48) 6912 block_6_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_6_project_BN (BatchNormal (None, 14, 14, 48) 192 block_6_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_expand (Conv2D) (None, 14, 14, 288) 13824 block_6_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_expand_BN (BatchNormali (None, 14, 14, 288) 1152 block_7_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_expand_relu (ReLU) (None, 14, 14, 288) 0 block_7_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_depthwise (DepthwiseCon (None, 14, 14, 288) 2592 block_7_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_depthwise_BN (BatchNorm (None, 14, 14, 288) 1152 block_7_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_depthwise_relu (ReLU) (None, 14, 14, 288) 0 block_7_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_project (Conv2D) (None, 14, 14, 48) 13824 block_7_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_project_BN (BatchNormal (None, 14, 14, 48) 192 block_7_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_7_add (Add) (None, 14, 14, 48) 0 block_6_project_BN[0][0] \n", | |
" block_7_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_expand (Conv2D) (None, 14, 14, 288) 13824 block_7_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_expand_BN (BatchNormali (None, 14, 14, 288) 1152 block_8_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_expand_relu (ReLU) (None, 14, 14, 288) 0 block_8_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_depthwise (DepthwiseCon (None, 14, 14, 288) 2592 block_8_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_depthwise_BN (BatchNorm (None, 14, 14, 288) 1152 block_8_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_depthwise_relu (ReLU) (None, 14, 14, 288) 0 block_8_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_project (Conv2D) (None, 14, 14, 48) 13824 block_8_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_project_BN (BatchNormal (None, 14, 14, 48) 192 block_8_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_8_add (Add) (None, 14, 14, 48) 0 block_7_add[0][0] \n", | |
" block_8_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_expand (Conv2D) (None, 14, 14, 288) 13824 block_8_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_expand_BN (BatchNormali (None, 14, 14, 288) 1152 block_9_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_expand_relu (ReLU) (None, 14, 14, 288) 0 block_9_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_depthwise (DepthwiseCon (None, 14, 14, 288) 2592 block_9_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_depthwise_BN (BatchNorm (None, 14, 14, 288) 1152 block_9_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_depthwise_relu (ReLU) (None, 14, 14, 288) 0 block_9_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_project (Conv2D) (None, 14, 14, 48) 13824 block_9_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_project_BN (BatchNormal (None, 14, 14, 48) 192 block_9_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_9_add (Add) (None, 14, 14, 48) 0 block_8_add[0][0] \n", | |
" block_9_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_10_expand (Conv2D) (None, 14, 14, 288) 13824 block_9_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_10_expand_BN (BatchNormal (None, 14, 14, 288) 1152 block_10_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_10_expand_relu (ReLU) (None, 14, 14, 288) 0 block_10_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_10_depthwise (DepthwiseCo (None, 14, 14, 288) 2592 block_10_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_10_depthwise_BN (BatchNor (None, 14, 14, 288) 1152 block_10_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_10_depthwise_relu (ReLU) (None, 14, 14, 288) 0 block_10_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_10_project (Conv2D) (None, 14, 14, 72) 20736 block_10_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_10_project_BN (BatchNorma (None, 14, 14, 72) 288 block_10_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_expand (Conv2D) (None, 14, 14, 432) 31104 block_10_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_expand_BN (BatchNormal (None, 14, 14, 432) 1728 block_11_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_expand_relu (ReLU) (None, 14, 14, 432) 0 block_11_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_depthwise (DepthwiseCo (None, 14, 14, 432) 3888 block_11_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_depthwise_BN (BatchNor (None, 14, 14, 432) 1728 block_11_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_depthwise_relu (ReLU) (None, 14, 14, 432) 0 block_11_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_project (Conv2D) (None, 14, 14, 72) 31104 block_11_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_project_BN (BatchNorma (None, 14, 14, 72) 288 block_11_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_11_add (Add) (None, 14, 14, 72) 0 block_10_project_BN[0][0] \n", | |
" block_11_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_expand (Conv2D) (None, 14, 14, 432) 31104 block_11_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_expand_BN (BatchNormal (None, 14, 14, 432) 1728 block_12_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_expand_relu (ReLU) (None, 14, 14, 432) 0 block_12_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_depthwise (DepthwiseCo (None, 14, 14, 432) 3888 block_12_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_depthwise_BN (BatchNor (None, 14, 14, 432) 1728 block_12_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_depthwise_relu (ReLU) (None, 14, 14, 432) 0 block_12_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_project (Conv2D) (None, 14, 14, 72) 31104 block_12_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_project_BN (BatchNorma (None, 14, 14, 72) 288 block_12_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_12_add (Add) (None, 14, 14, 72) 0 block_11_add[0][0] \n", | |
" block_12_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_expand (Conv2D) (None, 14, 14, 432) 31104 block_12_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_expand_BN (BatchNormal (None, 14, 14, 432) 1728 block_13_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_expand_relu (ReLU) (None, 14, 14, 432) 0 block_13_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_pad (ZeroPadding2D) (None, 15, 15, 432) 0 block_13_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_depthwise (DepthwiseCo (None, 7, 7, 432) 3888 block_13_pad[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_depthwise_BN (BatchNor (None, 7, 7, 432) 1728 block_13_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_depthwise_relu (ReLU) (None, 7, 7, 432) 0 block_13_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_project (Conv2D) (None, 7, 7, 120) 51840 block_13_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_13_project_BN (BatchNorma (None, 7, 7, 120) 480 block_13_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_expand (Conv2D) (None, 7, 7, 720) 86400 block_13_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_expand_BN (BatchNormal (None, 7, 7, 720) 2880 block_14_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_expand_relu (ReLU) (None, 7, 7, 720) 0 block_14_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_depthwise (DepthwiseCo (None, 7, 7, 720) 6480 block_14_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_depthwise_BN (BatchNor (None, 7, 7, 720) 2880 block_14_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_depthwise_relu (ReLU) (None, 7, 7, 720) 0 block_14_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_project (Conv2D) (None, 7, 7, 120) 86400 block_14_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_project_BN (BatchNorma (None, 7, 7, 120) 480 block_14_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_14_add (Add) (None, 7, 7, 120) 0 block_13_project_BN[0][0] \n", | |
" block_14_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_expand (Conv2D) (None, 7, 7, 720) 86400 block_14_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_expand_BN (BatchNormal (None, 7, 7, 720) 2880 block_15_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_expand_relu (ReLU) (None, 7, 7, 720) 0 block_15_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_depthwise (DepthwiseCo (None, 7, 7, 720) 6480 block_15_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_depthwise_BN (BatchNor (None, 7, 7, 720) 2880 block_15_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_depthwise_relu (ReLU) (None, 7, 7, 720) 0 block_15_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_project (Conv2D) (None, 7, 7, 120) 86400 block_15_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_project_BN (BatchNorma (None, 7, 7, 120) 480 block_15_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_15_add (Add) (None, 7, 7, 120) 0 block_14_add[0][0] \n", | |
" block_15_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_16_expand (Conv2D) (None, 7, 7, 720) 86400 block_15_add[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_16_expand_BN (BatchNormal (None, 7, 7, 720) 2880 block_16_expand[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_16_expand_relu (ReLU) (None, 7, 7, 720) 0 block_16_expand_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_16_depthwise (DepthwiseCo (None, 7, 7, 720) 6480 block_16_expand_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_16_depthwise_BN (BatchNor (None, 7, 7, 720) 2880 block_16_depthwise[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_16_depthwise_relu (ReLU) (None, 7, 7, 720) 0 block_16_depthwise_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_16_project (Conv2D) (None, 7, 7, 240) 172800 block_16_depthwise_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"block_16_project_BN (BatchNorma (None, 7, 7, 240) 960 block_16_project[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"Conv_1 (Conv2D) (None, 7, 7, 1280) 307200 block_16_project_BN[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"Conv_1_bn (BatchNormalization) (None, 7, 7, 1280) 5120 Conv_1[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"out_relu (ReLU) (None, 7, 7, 1280) 0 Conv_1_bn[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"global_average_pooling2d (Globa (None, 1280) 0 out_relu[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dense (Dense) (None, 100) 128100 global_average_pooling2d[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dropout (Dropout) (None, 100) 0 dense[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dense_1 (Dense) (None, 50) 5050 dropout[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dense_2 (Dense) (None, 5) 255 dense_1[0][0] \n", | |
"==================================================================================================\n", | |
"Total params: 1,515,469\n", | |
"Trainable params: 1,354,685\n", | |
"Non-trainable params: 160,784\n", | |
"__________________________________________________________________________________________________\n", | |
"Epoch 1/20\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "stream", | |
"text": [ | |
"WARNING: Logging before flag parsing goes to stderr.\n", | |
"W0721 15:42:43.643319 140525150693248 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n", | |
"Instructions for updating:\n", | |
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n" | |
], | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "stream", | |
"text": [ | |
"114/114 [==============================] - 34s 296ms/step - loss: 0.6430 - accuracy: 0.7806\n", | |
"Epoch 2/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.4123 - accuracy: 0.8700\n", | |
"Epoch 3/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.3489 - accuracy: 0.8920\n", | |
"Epoch 4/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.2646 - accuracy: 0.9093\n", | |
"Epoch 5/20\n", | |
"114/114 [==============================] - 15s 133ms/step - loss: 0.1997 - accuracy: 0.9379\n", | |
"Epoch 6/20\n", | |
"114/114 [==============================] - 15s 132ms/step - loss: 0.2204 - accuracy: 0.9346\n", | |
"Epoch 7/20\n", | |
"114/114 [==============================] - 15s 132ms/step - loss: 0.2037 - accuracy: 0.9437\n", | |
"Epoch 8/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.1823 - accuracy: 0.9489\n", | |
"Epoch 9/20\n", | |
"114/114 [==============================] - 15s 130ms/step - loss: 0.1349 - accuracy: 0.9593\n", | |
"Epoch 10/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.1203 - accuracy: 0.9632\n", | |
"Epoch 11/20\n", | |
"114/114 [==============================] - 15s 132ms/step - loss: 0.1534 - accuracy: 0.9579\n", | |
"Epoch 12/20\n", | |
"114/114 [==============================] - 15s 133ms/step - loss: 0.1371 - accuracy: 0.9632\n", | |
"Epoch 13/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.1105 - accuracy: 0.9706\n", | |
"Epoch 14/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.1871 - accuracy: 0.9519\n", | |
"Epoch 15/20\n", | |
"114/114 [==============================] - 15s 132ms/step - loss: 0.0947 - accuracy: 0.9742\n", | |
"Epoch 16/20\n", | |
"114/114 [==============================] - 15s 132ms/step - loss: 0.0586 - accuracy: 0.9830\n", | |
"Epoch 17/20\n", | |
"114/114 [==============================] - 15s 133ms/step - loss: 0.0599 - accuracy: 0.9830\n", | |
"Epoch 18/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.0748 - accuracy: 0.9824\n", | |
"Epoch 19/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.0872 - accuracy: 0.9740\n", | |
"Epoch 20/20\n", | |
"114/114 [==============================] - 15s 131ms/step - loss: 0.0744 - accuracy: 0.9791\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "WMfI30oXiOU7", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def representative_dataset_gen():\n", | |
" for i in range(0,5):\n", | |
" x,y=train_generator.next()\n", | |
" image=x[i:i+1]\n", | |
" yield [image]\n", | |
"\n", | |
" \n", | |
" \n" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TYmVAmDKObSA", | |
"colab_type": "code", | |
"outputId": "70098d96-e27a-44e2-8500-39f704b056ab", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 54 | |
} | |
}, | |
"source": [ | |
"#https://www.tensorflow.org/lite/r2/convert/python_api\n", | |
"# Convert the model.\n", | |
"converter = tf.lite.TFLiteConverter.from_keras_model(model)\n", | |
"tflite_model = converter.convert()\n", | |
"\n", | |
"# Load TFLite model and allocate tensors.\n", | |
"interpreter = tf.lite.Interpreter(model_content=tflite_model)\n", | |
"interpreter.allocate_tensors()\n", | |
"\n", | |
"# Get input and output tensors.\n", | |
"input_details = interpreter.get_input_details()\n", | |
"output_details = interpreter.get_output_details()\n", | |
"\n", | |
"# Test the TensorFlow Lite model on random input data.\n", | |
"input_shape = input_details[0]['shape']\n", | |
"input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)\n", | |
"interpreter.set_tensor(input_details[0]['index'], input_data)\n", | |
"\n", | |
"interpreter.invoke()\n", | |
"\n", | |
"# The function `get_tensor()` returns a copy of the tensor data.\n", | |
"# Use `tensor()` in order to get a pointer to the tensor.\n", | |
"tflite_results = interpreter.get_tensor(output_details[0]['index'])\n", | |
"\n", | |
"# Test the TensorFlow model on random input data.\n", | |
"tf_results = model(tf.constant(input_data))\n", | |
"\n", | |
"# Compare the result.\n", | |
"for tf_result, tflite_result in zip(tf_results, tflite_results):\n", | |
" np.testing.assert_almost_equal(tf_result, tflite_result, decimal=5)\n", | |
" print('tfresult', tf_results)\n", | |
" print('tflite', tflite_result)" | |
], | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"tfresult tf.Tensor([[1.1081396e-05 9.9998212e-01 6.5825448e-06 2.2968095e-08 2.4740385e-07]], shape=(1, 5), dtype=float32)\n", | |
"tflite [1.1081059e-05 9.9998212e-01 6.5823874e-06 2.2967395e-08 2.4739606e-07]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "125-TnWAdfYD", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"\n", | |
"converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file('model.h5')\n", | |
"converter.representative_dataset = representative_dataset_gen\n", | |
"converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]\n", | |
"converter.inference_input_type = tf.uint8\n", | |
"converter.inference_output_type = tf.uint8\n", | |
"converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]\n", | |
"\n", | |
"tflite_model = converter.convert()\n", | |
"with open('model.tflite', 'wb') as o_:\n", | |
" o_.write(tflite_model) " | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "UcbRv2QP824h", | |
"colab_type": "code", | |
"outputId": "802d3a91-05c2-49c8-cb76-318fd8aca49b", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 54 | |
} | |
}, | |
"source": [ | |
"# do a random test to confirm the model is working\n", | |
"# compare the result of original model and the quantized model.\n", | |
"preprocessed_image = prepare_image('/content/flower_photos/tulips/122450705_9885fff3c4_n.jpg')\n", | |
"predictions_flower = model.predict(preprocessed_image) \n", | |
"print (predictions_flower)\n", | |
"\n", | |
"\n", | |
"import numpy as np\n", | |
"import tensorflow as tf\n", | |
"\n", | |
"# Load TFLite model and allocate tensors.\n", | |
"interpreter = tf.lite.Interpreter(model_path=\"model.tflite\")\n", | |
"interpreter.allocate_tensors()\n", | |
"\n", | |
"# Get input and output tensors.\n", | |
"input_details = interpreter.get_input_details()\n", | |
"output_details = interpreter.get_output_details()\n", | |
"\n", | |
"def quantize(real_value):\n", | |
" std, mean = input_details[0]['quantization']\n", | |
" return (real_value/std + mean).astype(np.uint8)\n", | |
"\n", | |
"input_shape = input_details[0]['shape']\n", | |
"\n", | |
"sample_input = quantize(preprocessed_image).reshape(input_shape)\n", | |
" \n", | |
"# Test model on random input data.\n", | |
"# change the following line to feed into your own data.\n", | |
"interpreter.set_tensor(input_details[0]['index'], sample_input)\n", | |
"\n", | |
"interpreter.invoke()\n", | |
"output_data = interpreter.get_tensor(output_details[0]['index'])\n", | |
"print(output_data/255)\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n" | |
], | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[[1.9199765e-23 2.8861805e-20 2.5664539e-20 3.2812895e-22 1.0000000e+00]]\n", | |
"[[0. 0. 0. 0. 1.]]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Yx-rWl1HIYEc", | |
"colab_type": "code", | |
"outputId": "eef07552-c997-4200-e26b-ce6b0a2f5a1a", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 363 | |
} | |
}, | |
"source": [ | |
"%%bash\n", | |
"# install edgetpu_compiler\n", | |
"echo \"deb https://packages.cloud.google.com/apt coral-edgetpu-stable main\" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list\n", | |
"sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6A030B21BA07F4FB\n", | |
"\n", | |
"sudo apt update > /dev/null\n", | |
"sudo apt install edgetpu > /dev/null\n" | |
], | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"deb https://packages.cloud.google.com/apt coral-edgetpu-stable main\n", | |
"Executing: /tmp/apt-key-gpghome.5GZDpm4isE/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys 6A030B21BA07F4FB\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Warning: apt-key output should not be parsed (stdout is not a terminal)\n", | |
"gpg: key 6A030B21BA07F4FB: public key \"Google Cloud Packages Automatic Signing Key <[email protected]>\" imported\n", | |
"gpg: Total number processed: 1\n", | |
"gpg: imported: 1\n", | |
"\n", | |
"WARNING: apt does not have a stable CLI interface. Use with caution in scripts.\n", | |
"\n", | |
"\n", | |
"WARNING: apt does not have a stable CLI interface. Use with caution in scripts.\n", | |
"\n", | |
"debconf: unable to initialize frontend: Dialog\n", | |
"debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, <> line 5.)\n", | |
"debconf: falling back to frontend: Readline\n", | |
"debconf: unable to initialize frontend: Readline\n", | |
"debconf: (This frontend requires a controlling tty.)\n", | |
"debconf: falling back to frontend: Teletype\n", | |
"dpkg-preconfigure: unable to re-open stdin: \n" | |
], | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "s4Ha_vF9Ipq8", | |
"colab_type": "code", | |
"outputId": "3e860671-adfd-4e68-9024-faae9dc95616", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 490 | |
} | |
}, | |
"source": [ | |
"!edgetpu_compiler --show_operations 'model.tflite'" | |
], | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Edge TPU Compiler version 1.0.249710469\n", | |
"INFO: Initialized TensorFlow Lite runtime.\n", | |
"\n", | |
"Model compiled successfully in 432 ms.\n", | |
"\n", | |
"Input model: model.tflite\n", | |
"Input size: 1.78MiB\n", | |
"Output model: model_edgetpu.tflite\n", | |
"Output size: 2.02MiB\n", | |
"On-chip memory available for caching model parameters: 6.90MiB\n", | |
"On-chip memory used for caching model parameters: 1.90MiB\n", | |
"Off-chip memory used for streaming uncached model parameters: 0.00B\n", | |
"Number of Edge TPU subgraphs: 1\n", | |
"Total number of operations: 74\n", | |
"Operation log: model_edgetpu.log\n", | |
"\n", | |
"Operator Count Status\n", | |
"\n", | |
"ADD 10 Mapped to Edge TPU\n", | |
"PAD 5 Mapped to Edge TPU\n", | |
"QUANTIZE 2 Mapped to Edge TPU\n", | |
"CONV_2D 35 Mapped to Edge TPU\n", | |
"DEPTHWISE_CONV_2D 17 Mapped to Edge TPU\n", | |
"MEAN 1 Mapped to Edge TPU\n", | |
"FULLY_CONNECTED 3 Mapped to Edge TPU\n", | |
"SOFTMAX 1 Mapped to Edge TPU\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "o72wHEGDKdau", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Download to local drive" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_aZP7V6OKdwG", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"from google.colab import files\n", | |
"\n", | |
"files.download('/content/model_edgetpu.tflite')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice