Last active
March 8, 2019 11:37
-
-
Save IAmSuyogJadhav/f9e42606b78dadb9088eeff6fecf8b0f to your computer and use it in GitHub Desktop.
Keras Model on Android.ipynb
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": "Keras Model on Android.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"collapsed_sections": [ | |
"es2FzIe2eY60", | |
"aSNST5yZAh2n", | |
"HULogxxjA1-R" | |
], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/IAmSuyogJadhav/f9e42606b78dadb9088eeff6fecf8b0f/keras-model-on-android.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "WU928POSBZMc", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# Run a keras model on android\n", | |
"\n", | |
"Simplest way to deploy an already created keras model on android" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "es2FzIe2eY60", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# Build Your Model (or upload one)" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "LJ4jf--jBuX5", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"#@title Do you want to upload a model? { display-mode: \"form\" }\n", | |
"upload = False #@param {type:\"boolean\"}" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "rt6X21GOeq2Z", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"if not upload:\n", | |
" from keras.models import Model\n", | |
" from keras.layers import *\n", | |
" from keras import backend as k\n", | |
" import tensorflow as tf\n", | |
"\n", | |
" # b = tf.constant([1, 1], dtype=tf.float32)\n", | |
" # <Design any model here>\n", | |
" inp = Input((3,), name='I')\n", | |
" out = Dense(2, name='O', use_bias=True)(inp)\n", | |
"\n", | |
" model = Model(inp, out)\n", | |
" model.summary()\n", | |
" \n", | |
" # Save the model\n", | |
" model.save('model.h5')\n", | |
"else:\n", | |
" from google.colab import files\n", | |
" files.upload()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "aSNST5yZAh2n", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# Convert to `.pb`" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "8-8bFVSgxeF8", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!wget https://github.com/amir-abdi/keras_to_tensorflow/raw/master/keras_to_tensorflow.py" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "FXKhDBbbDVfa", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"_If you uploaded your own model, change the filename in following code cell_" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "qp_oBJ8Fx9Jf", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!python keras_to_tensorflow.py \\\n", | |
" --input_model=\"model.h5\" \\ # Change this if your model has a different name\n", | |
" --output_model=\"optimized_model.pb\"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "HULogxxjA1-R", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# Download" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "fDy9Ha59Auru", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"Get output and input layer names (to use in the android app)" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "uwSdvi3P2a2O", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"[model.outputs[0].name, model.inputs[0].name]" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "gkSIWVUI4Msx", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from google.colab import files\n", | |
"files.download('optimized_model.pb')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment