{"id":19760210,"url":"https://github.com/younatics/deeplearningtomobile","last_synced_at":"2025-04-30T13:32:43.923Z","repository":{"id":198221636,"uuid":"151357113","full_name":"younatics/DeepLearningToMobile","owner":"younatics","description":"Curated way to convert deep learning model to mobile⚡️","archived":false,"fork":false,"pushed_at":"2018-10-10T05:53:48.000Z","size":1794,"stargazers_count":48,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2023-10-04T21:49:19.807Z","etag":null,"topics":["coreml","coremltools","keras","tensorflow","tensorflow-lite","tensorflow-mobile","tf-coreml","toco"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/younatics.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-10-03T03:43:18.000Z","updated_at":"2023-10-04T21:53:44.688Z","dependencies_parsed_at":"2023-10-04T21:53:44.635Z","dependency_job_id":"d05a07bc-4161-4b5c-9d2a-9554db81e859","html_url":"https://github.com/younatics/DeepLearningToMobile","commit_stats":null,"previous_names":["younatics/deeplearningtomobile"],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/younatics%2FDeepLearningToMobile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/younatics%2FDeepLearningToMobile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/younatics%2FDeepLearningToMobile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/younatics%2FDeepLearningToMobile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/younatics","download_url":"https://codeload.github.com/younatics/DeepLearningToMobile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224214289,"owners_count":17274524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coreml","coremltools","keras","tensorflow","tensorflow-lite","tensorflow-mobile","tf-coreml","toco"],"created_at":"2024-11-12T03:36:00.406Z","updated_at":"2024-11-12T03:36:02.918Z","avatar_url":"https://github.com/younatics.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deep Learning To Mobile ⚡️\n### Curated way to convert deep learning model to mobile. \n\nThis repository will show you how to put your own model directly into mobile(iOS/Android) with basic example. First part is about **deep learning model to mobile machine learning framework**, and second part is about **deep learning framework to mobile machine learning framework**\n\n## Intro\n\n#### Part 1. Deep learning model to mobile machine learning framework\n\n| Neural Network | CoreML | TensorFlow Mobile | Tensorflow Lite |\n| :-: | :---: | :---------------: | :-------------: |\n| Feedforward NN | ✔️ | ✔️ | ✔️ |\n| Convolutional NN | ✔️ | ✔️ | ✔️ |\n| Recurrent NN | ✔️ | ✔️ | ❗️ |\n\n#### Part 2. Deep learning framework to mobile machine learning framework\n| Framework | CoreML | TensorFlow Mobile | Tensorflow Lite |\n| :-------: | :----: | :---------------: | :-------------: |\n| Tensorflow | `tf-coreml` | `tensorflow` | `tensorflow` |\n| Pytorch | `onnx` | ← | ← |\n| Keras | `coremltools` | `tensorflow backend` | ← |\n| Caffe | `coremltools` | `caffe-tensorflow` | ←  |\n\n\n# Part 0. \n### Basic FFNN example\nI'll use Golbin code in this [TensorFlow-Tutorials](https://github.com/golbin/TensorFlow-Tutorials/blob/master/04%20-%20Neural%20Network%20Basic/02%20-%20Deep%20NN.py), and simple Keras code to convert. I use two examples because there are different limits.\n\n#### TensorFlow\n```python\nimport tensorflow as tf\nimport numpy as np\n\nx_data = np.array(\n    [[0, 0], [1, 0], [1, 1], [0, 0], [0, 0], [0, 1]])\n\ny_data = np.array([\n    [1, 0, 0], \n    [0, 1, 0],  \n    [0, 0, 1],  \n    [1, 0, 0],\n    [1, 0, 0],\n    [0, 0, 1]\n])\n\nglobal_step = tf.Variable(0, trainable=False, name='global_step')\nX = tf.placeholder(tf.float32, name='Input')\nY = tf.placeholder(tf.float32, name='Output')\n\nwith tf.name_scope('layer1'):\n    W1 = tf.Variable(tf.random_uniform([2, 10], -1., 1.), name='W1')\n    b1 = tf.Variable(tf.zeros([10]), name='b1')\n    L1 = tf.add(tf.matmul(X, W1), b1, name='L1')\n    L1 = tf.nn.relu(L1)\n    \nwith tf.name_scope('layer2'):\n    W2 = tf.Variable(tf.random_uniform([10, 3], -1., 1.), name='W2')\n    b2 = tf.Variable(tf.zeros([3]), name='b2')\n    model = tf.add(tf.matmul(L1, W2), b2, name='model')\n    prediction = tf.argmax(model, 1, name='prediction')\n\ncost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=model), name='cost')\noptimizer = tf.train.AdamOptimizer(learning_rate=0.01, name='optimizer')\ntrain_op = optimizer.minimize(cost, global_step=global_step)\n\ninit = tf.global_variables_initializer()\nsess = tf.Session()\nsess.run(init)\n\nsaver = tf.train.Saver(tf.global_variables())\nmerged = tf.summary.merge_all()\nwriter = tf.summary.FileWriter('./logs', sess.graph)\n\nfor step in range(30):\n    sess.run(train_op, feed_dict={X: x_data, Y: y_data})\n\n    if (step + 1) % 30 == 0:\n        print(step + 1, sess.run(cost, feed_dict={X: x_data, Y: y_data}))\n        tf.train.write_graph(sess.graph_def, '.', './model/FFNN.pbtxt')  \n        saver.save(sess, './model/FFNN.ckpt', global_step=global_step)\n        break\n        \ntarget = tf.argmax(Y, 1)\nis_correct = tf.equal(prediction, target)\naccuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))\nprint('Accuracy: %.2f' % sess.run(accuracy * 100, feed_dict={X: x_data, Y: y_data}))\n```\n\n#### Keras\n```python\nimport numpy as np\nimport tensorflow as tf\n\nx_data = np.array(\n    [[0, 0], [1, 0], [1, 1], [0, 0], [0, 0], [0, 1]])\n\ny_data = np.array([\n    [1, 0, 0], \n    [0, 1, 0],  \n    [0, 0, 1],  \n    [1, 0, 0],\n    [1, 0, 0],\n    [0, 0, 1]\n])\n\nmodel = tf.keras.models.Sequential()\nmodel.add(tf.keras.layers.Dense(10, input_dim=2,activation=\"relu\"))\nmodel.add(tf.keras.layers.Dense(2, activation=\"relu\", kernel_initializer=\"uniform\"))\nmodel.add(tf.keras.layers.Dense(3))\nmodel.add(tf.keras.layers.Activation(\"softmax\"))\n\nadam = tf.keras.optimizers.Adam(lr=0.01)\n\nmodel.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])\nmodel.summary()\n\nreult = model.fit(x_data, y_data, shuffle=True, epochs=10, batch_size=2, validation_data=(x_data, y_data))\n```\n\n# Part 1. \n### Deep learning model to mobile machine learning framework\n## CoreML\n\n![CoreML](https://github.com/younatics/DeepLearningToMobile/blob/master/img/coreml.png)\n\n- ML Framework supported by Apple, using `.mlmodel` extension\n- Automatically generated wrapper for iOS(Swift or Objective-C)\n- | Neural Network | CoreML |\n  | :-: | :---: |\n  | Feedforward NN | ✔️ |\n  | Convolutional NN | ✔️ |\n  | Recurrent NN | ✔️ |\n\n### REFERENCE\n- [Core ML](https://developer.apple.com/documentation/coreml)\n- [Converting Trained Models to Core ML](https://developer.apple.com/documentation/coreml/converting_trained_models_to_core_ml)\n\n## TensorFlow Mobile🔒\n#### TensorFlow Mobile is now deprecated\n![tensorflowmobile](https://github.com/younatics/DeepLearningToMobile/blob/master/img/tensorflowmobile.png)\n\n- ML Framework supported by Google, using `.pb` extension\n- Support Java for Android, Objective-C++ for iOS\n- | Neural Network | TensorFlow Mobile |\n  | :-: | :---: |\n  | Feedforward NN | ✔️ |\n  | Convolutional NN | ✔️ |\n  | Recurrent NN | ✔️ |\n  \n### Reference\n- [TensorFlow Mobile](https://www.tensorflow.org/lite/tfmobile/)\n- [TensorFlow on Mobile: Tutorial](https://towardsdatascience.com/tensorflow-on-mobile-tutorial-1-744703297267)\n\n## TensorFlow Lite\n\n- ML Framework supported by Google, using `.tflite` extension\n- Support Java for Android, Objective-C++ for iOS\n- **Recommand way** by Google to use tensorflow in Mobile\n- | Neural Network | TensorFlow Mobile |\n  | :-: | :---: |\n  | Feedforward NN | ✔️ |\n  | Convolutional NN | ✔️ |\n  | Recurrent NN | RNN is not supported see more information in [this link](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/tf_ops_compatibility.md) |\n\n### Reference\n- [TensorFlow Lite](https://www.tensorflow.org/lite/)\n- [TensorFlow Lite \u0026 TensorFlow Compatibility Guide](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/tf_ops_compatibility.md)\n\n# Part 2. \n### Deep learning framework to mobile machine learning framework\n\n## TensorFlow to Tensorflow Mobile\nWe can get `FFNN.pbtxt`and `FFNN.ckpt-90` in Part 0 code.\n#### Freeze graph using `freeze_graph` from `tensorflow.python.tools`\n\n```python\nfrom tensorflow.python.tools import freeze_graph\n\nfreeze_graph.freeze_graph(\"model/FFNN.pbtxt\", \"\",\n                          \"\", \"model/FFNN.ckpt-90\", \"Output\",\n                          \"\", \"\",\n                          \"FFNN_frozen_graph.pb\", True, \"\")\n```\nNow you can use `FFNN_frozen_graph.pb` in TensorFlow Mobile!\n\n| Neural Network | `freeze_graph` |\n| :-: | :---: |\n| Feedforward NN | ✔️ |\n| Convolutional NN | ✔️ |\n| Recurrent NN | ✔️ |\n\n## Check your Tensor graph\nYou have to check frozen tensor graph\n\n```python\ndef load_graph(frozen_graph_filename):\n    with tf.gfile.GFile(frozen_graph_filename, \"rb\") as f:\n        graph_def = tf.GraphDef()\n        graph_def.ParseFromString(f.read())\n\n    with tf.Graph().as_default() as graph:\n        tf.import_graph_def(graph_def, name=\"\")\n\n    return graph\n    \ngraph = load_graph('FFNN_frozen_graph.pb')\n\nfor op in graph.get_operations():\n    print(op.name)\n```\n\n### Reference\n- [Graphs and Sessions](https://www.tensorflow.org/guide/graphs)\n\n## TensorFlow to CoreML (iOS)\n`tf-coreml` is the recommended way from Apple to convert tensorflow to CoreML\n#### `tf-coreml` currently could not convert cycled graph like RNN... etc [#124](https://github.com/tf-coreml/tf-coreml/issues/124)\n\n```python\nimport tfcoreml\n\nmlmodel = tfcoreml.convert(\n        tf_model_path = 'FFNN_frozen_graph.pb',\n        mlmodel_path = 'FFNN.mlmodel',\n        output_feature_names = ['layer2/prediction:0'],\n        input_name_shape_dict = {'Input:0': [1, 2]})\n```\nNow you can use `FFNN.mlmodel` in iOS project! \n\n| Neural Network | `tf-coreml` |\n| :-: | :---: |\n| Feedforward NN | ✔️ |\n| Convolutional NN | ✔️ |\n| Recurrent NN | ✖️ |\n\n### Reference\n- [tf-coreml](https://github.com/tf-coreml/tf-coreml)\n\n## TensorFlow to TensorFlow Lite (Android)\n`toco` is the recommended way from Google to convert TensorFlow to TensorFlow Lite\n\n```python\nimport tensorflow as tf\n\ngraph_def_file = \"FFNN_frozen_graph.pb\"\ninput_arrays = [\"Input\"]\noutput_arrays = [\"layer2/prediction\"]\n\nconverter = tf.contrib.lite.TocoConverter.from_frozen_graph(\n  graph_def_file, input_arrays, output_arrays)\ntflite_model = converter.convert()\nopen(\"FFNN.tflite\", \"wb\").write(tflite_model)\n```\nNow you can use `FFNN.tflite` in Android project! \n\n| Neural Network | `toco` |\n| :-: | :---: |\n| Feedforward NN | ✔️ |\n| Convolutional NN | ✔️ |\n| Recurrent NN | ✖️ |\n\n### Reference\n- [Toco](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite/toco)\n- [Intro to Machine Learning on Android — How to convert a custom model to TensorFlow Lite](https://heartbeat.fritz.ai/intro-to-machine-learning-on-android-how-to-convert-a-custom-model-to-tensorflow-lite-e07d2d9d50e3)\n\n## Keras to CoreML (iOS)\n`coremltools` is the recommended way from Apple to convert Keras to CoreML\n\n```python\nimport coremltools\n\ncoreml_model = coremltools.converters.keras.convert(model)\ncoreml_model.save('FFNN.mlmodel')\n```\n\nNow you can use `FFNN.mlmodel` in Android project! \n\n| Neural Network | `coremltools` |\n| :-: | :---: |\n| Feedforward NN | ✔️ |\n| Convolutional NN | ✔️ |\n| Recurrent NN | ✔️ |\n\n### Reference\n- [coremltools](https://github.com/apple/coremltools)\n\n## Keras to TensorFlow Lite (Android)\n`toco` is the recommended way from Google to convert Keras to TensorFlow Lite\n\nMake `.h5` Keras extension and then convert it to `.tflie` extension\n\n```python\nkeras_file = \"FFNN.h5\"\ntf.keras.models.save_model(model, keras_file)\n\nconverter = tf.contrib.lite.TocoConverter.from_keras_model_file(keras_file)\ntflite_model = converter.convert()\nopen(\"FFNN.tflite\", \"wb\").write(tflite_model)\n```\n\nNow you can use `FFNN.tflite` in Android project! \n\n| Neural Network | `toco` |\n| :-: | :---: |\n| Feedforward NN | ✔️ |\n| Convolutional NN | ✔️ |\n| Recurrent NN | ✖️ |\n\n### Reference\n- [TensorFlow Lite Optimizing Converter \u0026 Interpreter Python API reference](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/python_api.md)\n\n## Author\n[younatics](https://twitter.com/younatics)\n\u003ca href=\"http://twitter.com/younatics\" target=\"_blank\"\u003e\u003cimg alt=\"Twitter\" src=\"https://img.shields.io/twitter/follow/younatics.svg?style=social\u0026label=Follow\"\u003e\u003c/a\u003e\n\n## License\nDeepLearningToMobile is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyounatics%2Fdeeplearningtomobile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyounatics%2Fdeeplearningtomobile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyounatics%2Fdeeplearningtomobile/lists"}