{
  "id": 390182,
  "title": "Converting Models to TensorFlow Lite",
  "url": "/competitions/asl-signs/discussion/390182",
  "author_name": "",
  "post_date": "2023-02-24T15:03:46.861866600Z",
  "votes": 48,
  "comment_count": 3,
  "views": 0,
  "content": "<p>According the the rules for this competition, our submission must be a TFLite model<br>\nTensorFlow Lite is TensorFlow's lite weight framework that allows people to develop models to run on mobile, embedded, and edge devices. <br>\nHowever, most of our training likely will not be done starting in TFLite. Instead, we will likely create and train our models in TensorFlow's core frameworks and convert it to TFLite. TensorFlow has a conversion system (in the sections below) that show how to convert any TensorFlow model to TFLite.</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F6537187%2F4facc48ef6b2b1100e3b16302151a088%2Ftflite_convert_img.PNG?generation=1677250077226192&amp;alt=media\" alt=\"\"></p>\n<p>Information and code from:<br>\n<a href=\"https://www.tensorflow.org/lite/models/convert/\" target=\"_blank\">https://www.tensorflow.org/lite/models/convert/</a><br>\n<a href=\"https://www.tensorflow.org/lite/models/convert/convert_models\" target=\"_blank\">https://www.tensorflow.org/lite/models/convert/convert_models</a></p>\n<h1>Convert Saved Model</h1>\n<p>This is the method they recommend people take in the documentation. The TFLiteConverter.from_saved_model function allows you to convert any saved model to TFLite. <a href=\"https://www.tensorflow.org/guide/saved_model\" target=\"_blank\">Here is info about saving a model</a>. Below is the code to convert a saved model to TFLite.</p>\n<pre><code> tensorflow  tf\n\n\nconverter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) \ntflite_model = converter.convert()\n\n\n (, )  f:\n  f.write(tflite_model)\n</code></pre>\n<h1>Convert Keras Model</h1>\n<p>Keras is a very popular high level framework built on TensorFlow. The API makes designing your neural network  easy to use and sill very powerful. Below is how to convert a keras model to TFLite.</p>\n<pre><code> tensorflow  tf\n\n\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Dense(units=, input_shape=[]),\n    tf.keras.layers.Dense(units=, activation=),\n    tf.keras.layers.Dense(units=)\n])\nmodel.(optimizer=, loss=) \nmodel.fit(x=[-, , ], y=[-, -, ], epochs=) \n\n\n\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n\n (, )  f:\n  f.write(tflite_model)\n</code></pre>\n<h1>Convert Concrete Functions</h1>\n<p>If you are familiar with TensorFlow's low level API, this could be useful. Below show how to convert a tf.Module class to a TFLite model.</p>\n<pre><code> tensorflow  tf\n\n\n (tf.Module):\n\n   ():\n     tf.square(x)\nmodel = Squared()\n\n\nconcrete_func = model.__call__.get_concrete_function()\n\n\n\nconverter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func],\n                                                            model)\ntflite_model = converter.convert()\n\n\n (, )  f:\n  f.write(tflite_model)\n</code></pre>",
  "messages": [
    {
      "id": "2158027",
      "postDate": "02/24/2023 15:03:46",
      "content": "<p>According the the rules for this competition, our submission must be a TFLite model<br>\nTensorFlow Lite is TensorFlow's lite weight framework that allows people to develop models to run on mobile, embedded, and edge devices. <br>\nHowever, most of our training likely will not be done starting in TFLite. Instead, we will likely create and train our models in TensorFlow's core frameworks and convert it to TFLite. TensorFlow has a conversion system (in the sections below) that show how to convert any TensorFlow model to TFLite.</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F6537187%2F4facc48ef6b2b1100e3b16302151a088%2Ftflite_convert_img.PNG?generation=1677250077226192&amp;alt=media\" alt=\"\"></p>\n<p>Information and code from:<br>\n<a href=\"https://www.tensorflow.org/lite/models/convert/\" target=\"_blank\">https://www.tensorflow.org/lite/models/convert/</a><br>\n<a href=\"https://www.tensorflow.org/lite/models/convert/convert_models\" target=\"_blank\">https://www.tensorflow.org/lite/models/convert/convert_models</a></p>\n<h1>Convert Saved Model</h1>\n<p>This is the method they recommend people take in the documentation. The TFLiteConverter.from_saved_model function allows you to convert any saved model to TFLite. <a href=\"https://www.tensorflow.org/guide/saved_model\" target=\"_blank\">Here is info about saving a model</a>. Below is the code to convert a saved model to TFLite.</p>\n<pre><code> tensorflow  tf\n\n\nconverter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) \ntflite_model = converter.convert()\n\n\n (, )  f:\n  f.write(tflite_model)\n</code></pre>\n<h1>Convert Keras Model</h1>\n<p>Keras is a very popular high level framework built on TensorFlow. The API makes designing your neural network  easy to use and sill very powerful. Below is how to convert a keras model to TFLite.</p>\n<pre><code> tensorflow  tf\n\n\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Dense(units=, input_shape=[]),\n    tf.keras.layers.Dense(units=, activation=),\n    tf.keras.layers.Dense(units=)\n])\nmodel.(optimizer=, loss=) \nmodel.fit(x=[-, , ], y=[-, -, ], epochs=) \n\n\n\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n\n (, )  f:\n  f.write(tflite_model)\n</code></pre>\n<h1>Convert Concrete Functions</h1>\n<p>If you are familiar with TensorFlow's low level API, this could be useful. Below show how to convert a tf.Module class to a TFLite model.</p>\n<pre><code> tensorflow  tf\n\n\n (tf.Module):\n\n   ():\n     tf.square(x)\nmodel = Squared()\n\n\nconcrete_func = model.__call__.get_concrete_function()\n\n\n\nconverter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func],\n                                                            model)\ntflite_model = converter.convert()\n\n\n (, )  f:\n  f.write(tflite_model)\n</code></pre>",
      "rawMarkdown": "According the the rules for this competition, our submission must be a TFLite model\nTensorFlow Lite is TensorFlow's lite weight framework that allows people to develop models to run on mobile, embedded, and edge devices. \nHowever, most of our training likely will not be done starting in TFLite. Instead, we will likely create and train our models in TensorFlow's core frameworks and convert it to TFLite. TensorFlow has a conversion system (in the sections below) that show how to convert any TensorFlow model to TFLite.\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F6537187%2F4facc48ef6b2b1100e3b16302151a088%2Ftflite_convert_img.PNG?generation=1677250077226192&alt=media)\n\nInformation and code from:\nhttps://www.tensorflow.org/lite/models/convert/\nhttps://www.tensorflow.org/lite/models/convert/convert_models\n\n# Convert Saved Model\n\nThis is the method they recommend people take in the documentation. The TFLiteConverter.from_saved_model function allows you to convert any saved model to TFLite. [Here is info about saving a model](https://www.tensorflow.org/guide/saved_model). Below is the code to convert a saved model to TFLite.\n\n```python\nimport tensorflow as tf\n\n# Convert the model\nconverter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) # path to the SavedModel directory\ntflite_model = converter.convert()\n\n# Save the model.\nwith open('model.tflite', 'wb') as f:\n  f.write(tflite_model)\n```\n\n# Convert Keras Model\n\nKeras is a very popular high level framework built on TensorFlow. The API makes designing your neural network  easy to use and sill very powerful. Below is how to convert a keras model to TFLite.\n\n```python\nimport tensorflow as tf\n\n# Create a model using high-level tf.keras.* APIs\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Dense(units=1, input_shape=[1]),\n    tf.keras.layers.Dense(units=16, activation='relu'),\n    tf.keras.layers.Dense(units=1)\n])\nmodel.compile(optimizer='sgd', loss='mean_squared_error') # compile the model\nmodel.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model\n# (to generate a SavedModel) tf.saved_model.save(model, \"saved_model_keras_dir\")\n\n# Convert the model.\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n# Save the model.\nwith open('model.tflite', 'wb') as f:\n  f.write(tflite_model)\n```\n\n# Convert Concrete Functions \n\nIf you are familiar with TensorFlow's low level API, this could be useful. Below show how to convert a tf.Module class to a TFLite model.\n\n```python\nimport tensorflow as tf\n\n# Create a model using low-level tf.* APIs\nclass Squared(tf.Module):\n  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)])\n  def __call__(self, x):\n    return tf.square(x)\nmodel = Squared()\n# (to run your model) result = Squared(5.0) # This prints \"25.0\"\n# (to generate a SavedModel) tf.saved_model.save(model, \"saved_model_tf_dir\")\nconcrete_func = model.__call__.get_concrete_function()\n\n# Convert the model.\n\nconverter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func],\n                                                            model)\ntflite_model = converter.convert()\n\n# Save the model.\nwith open('model.tflite', 'wb') as f:\n  f.write(tflite_model)\n```",
      "votes": null
    },
    {
      "id": "2158177",
      "postDate": "02/24/2023 17:09:50",
      "content": "<p>Thanks for share👍</p>",
      "rawMarkdown": "Thanks for share👍",
      "votes": null
    },
    {
      "id": "2161306",
      "postDate": "02/27/2023 11:38:51",
      "content": "<p>That was really informative Thankyou <a href=\"https://www.kaggle.com/ravishah1\" target=\"_blank\">@ravishah1</a> </p>",
      "rawMarkdown": "That was really informative Thankyou @ravishah1",
      "votes": null
    },
    {
      "id": "2231531",
      "postDate": "04/23/2023 12:00:09",
      "content": "<p>Thanks for the informative notebook! <a href=\"https://www.kaggle.com/ravishah1\" target=\"_blank\">@ravishah1</a> </p>",
      "rawMarkdown": "Thanks for the informative notebook! @ravishah1",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2158177,
      "author_name": "sanikamal",
      "author_url": "",
      "post_date": "02/24/2023 17:09:50",
      "content": "<p>Thanks for share👍</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 2161306,
      "author_name": "ayushsingh05",
      "author_url": "",
      "post_date": "02/27/2023 11:38:51",
      "content": "<p>That was really informative Thankyou <a href=\"https://www.kaggle.com/ravishah1\" target=\"_blank\">@ravishah1</a> </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 2231531,
      "author_name": "pmohankrishna",
      "author_url": "",
      "post_date": "04/23/2023 12:00:09",
      "content": "<p>Thanks for the informative notebook! <a href=\"https://www.kaggle.com/ravishah1\" target=\"_blank\">@ravishah1</a> </p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2158027": "According the the rules for this competition, our submission must be a TFLite model\nTensorFlow Lite is TensorFlow's lite weight framework that allows people to develop models to run on mobile, embedded, and edge devices. \nHowever, most of our training likely will not be done starting in TFLite. Instead, we will likely create and train our models in TensorFlow's core frameworks and convert it to TFLite. TensorFlow has a conversion system (in the sections below) that show how to convert any TensorFlow model to TFLite.\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F6537187%2F4facc48ef6b2b1100e3b16302151a088%2Ftflite_convert_img.PNG?generation=1677250077226192&alt=media)\n\nInformation and code from:\nhttps://www.tensorflow.org/lite/models/convert/\nhttps://www.tensorflow.org/lite/models/convert/convert_models\n\n# Convert Saved Model\n\nThis is the method they recommend people take in the documentation. The TFLiteConverter.from_saved_model function allows you to convert any saved model to TFLite. [Here is info about saving a model](https://www.tensorflow.org/guide/saved_model). Below is the code to convert a saved model to TFLite.\n\n```python\nimport tensorflow as tf\n\n# Convert the model\nconverter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) # path to the SavedModel directory\ntflite_model = converter.convert()\n\n# Save the model.\nwith open('model.tflite', 'wb') as f:\n  f.write(tflite_model)\n```\n\n# Convert Keras Model\n\nKeras is a very popular high level framework built on TensorFlow. The API makes designing your neural network  easy to use and sill very powerful. Below is how to convert a keras model to TFLite.\n\n```python\nimport tensorflow as tf\n\n# Create a model using high-level tf.keras.* APIs\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Dense(units=1, input_shape=[1]),\n    tf.keras.layers.Dense(units=16, activation='relu'),\n    tf.keras.layers.Dense(units=1)\n])\nmodel.compile(optimizer='sgd', loss='mean_squared_error') # compile the model\nmodel.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model\n# (to generate a SavedModel) tf.saved_model.save(model, \"saved_model_keras_dir\")\n\n# Convert the model.\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n# Save the model.\nwith open('model.tflite', 'wb') as f:\n  f.write(tflite_model)\n```\n\n# Convert Concrete Functions \n\nIf you are familiar with TensorFlow's low level API, this could be useful. Below show how to convert a tf.Module class to a TFLite model.\n\n```python\nimport tensorflow as tf\n\n# Create a model using low-level tf.* APIs\nclass Squared(tf.Module):\n  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)])\n  def __call__(self, x):\n    return tf.square(x)\nmodel = Squared()\n# (to run your model) result = Squared(5.0) # This prints \"25.0\"\n# (to generate a SavedModel) tf.saved_model.save(model, \"saved_model_tf_dir\")\nconcrete_func = model.__call__.get_concrete_function()\n\n# Convert the model.\n\nconverter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func],\n                                                            model)\ntflite_model = converter.convert()\n\n# Save the model.\nwith open('model.tflite', 'wb') as f:\n  f.write(tflite_model)\n```",
    "2158177": "Thanks for share👍",
    "2161306": "That was really informative Thankyou @ravishah1",
    "2231531": "Thanks for the informative notebook! @ravishah1"
  },
  "source": "meta"
}