TFLite Converter missing functions in TF2.0 alpha

332 views
Skip to first unread message

Aakash Kumar Nain

unread,
Apr 24, 2019, 9:02:47 AM4/24/19
to TensorFlow Community Testing, TensorFlow Developers, Paige Bailey
Hi,

I was trying to convert a `tf.keras` model to `tflite` but to my surprise I found out that there is no `from_keras_model` function available in the API. The only functions available are `convert` and `from_concrete_function`. Here is the error log:

----> 1 converter = tf.lite.TFLiteConverter.from_keras_model(model)

AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_keras_model'

As per the documentation, it should be there but it isn't. Should I upgrade the version of TF2.0 to some nightly build? 

Regards,
Aakash Nain 

Paige Bailey

unread,
Apr 24, 2019, 9:15:26 AM4/24/19
to Aakash Kumar Nain, TensorFlow Community Testing, TensorFlow Developers, Tim Davis
Adding +Tim.

Margaret Maynard-Reid

unread,
Apr 24, 2019, 10:27:28 AM4/24/19
to Aakash Kumar Nain, Paige Bailey, tfl...@tensorflow.org, TensorFlow Community Testing, TensorFlow Developers, Tim Davis
[Adding tfl...@tensorflow.org to cc]

Hi Aakash,

You may find this helpful - look at "How do I get a concrete function for a tf.Keras model?" under "/Convert a TF 2.0 Model/Generate a concrete function"

Looks like the TFLite Converter has changed quite a bit Here is my compare/contrast of TFLite 1.13 vs TFLite 2.0:

TensorFlow Lite version

TFLite 1.13

TFLite 2.0

Class

TFLiteConverter

TFLiteConverterV2

Converter methods

4 Methods


# Converting a GraphDef from session.

from_session(sess, in_tensors, out_tensors)

# Converting a GraphDef from file.

from_frozen_graph(graph_def_file, input_arrays, output_arrays)

# Converting a SavedModel.

from_saved_model(saved_model_dir)

# Converting a tf.keras model

from_keras_model_file(keras_model)

2 methods:


# creates a TFLiteConverter class from a ConcreteFunction

from_concrete_function(cls, func)

#Converts a TensorFlow GraphDef based on instance variables

convert(self):

Source code

link

link

API doc

link

link

Guide on tensorflow.org

Convert a model/Python API (link)

Convert a TF 2.0 Model/Generate a concrete function (link)


Margaret

--
You received this message because you are subscribed to the Google Groups "TensorFlow Community Testing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testing+u...@tensorflow.org.

Aakash Kumar Nain

unread,
Apr 24, 2019, 10:53:15 AM4/24/19
to Margaret Maynard-Reid, Paige Bailey, tfl...@tensorflow.org, TensorFlow Community Testing, TensorFlow Developers, Tim Davis
Thanks Margaret. I will look into it. Though first look at this gives me an impression that this is more complicated than converting directly using `from_keras_model` method that was present earlier. 

Regards,
Aakash Nain

Margaret Maynard-Reid

unread,
Apr 24, 2019, 11:19:06 AM4/24/19
to Aakash Kumar Nain, Tim Davis, Paige Bailey, tfl...@tensorflow.org, TensorFlow Community Testing, TensorFlow Developers
Yes I agree. The tf.Keras model to TFLite conversion seems to be more complex with TF 2.0 now.
There is one more step added in the process: train tf.Keras model -> save tf.Keras model -> get concrete function from tf.Keras model --> convert to TFLite.

Tim - any plans to simply? Thanks.

Margaret

Nupur Garg

unread,
Apr 24, 2019, 11:29:59 AM4/24/19
to Margaret Maynard-Reid, Aakash Kumar Nain, Tim Davis, Paige Bailey, TensorFlow Lite, TensorFlow Community Testing, TensorFlow Developers
The 2.0 TFLiteConverter API has been updated since alpha release. `TFLiteConverter.from_keras_model` is available in the `tf-nightly-2.0-preview`. Documentation on the API is available here: https://www.tensorflow.org/lite/r2/convert/python_api.

Additionally, documentation on concrete functions is available here: https://www.tensorflow.org/lite/r2/convert/concrete_function.

Hope this helps!

Best,
Nupur

You received this message because you are subscribed to the Google Groups "TensorFlow Lite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tflite+un...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tflite/.

Margaret Maynard-Reid

unread,
Apr 24, 2019, 12:06:11 PM4/24/19
to Nupur Garg, Aakash Kumar Nain, Tim Davis, Paige Bailey, TensorFlow Lite, TensorFlow Community Testing, TensorFlow Developers
Thank you Nupur. I just verified that `TFLiteConverter.from_keras_model` is available in the `tf-nightly-2.0-preview` (which is great!), but not in TF 2.0 alpha. 

I have a suggestion and question:

Suggestion
It's super confusing that from_keras_model() is in 1.13 and TF 2.0 preview, but not in TF 2.0 alpha. As developers are still trying out TF 2.0 alpha, it may be helpful to point out in the guide that if they are trying TF 2.0 alpha they need to write the extra code to generate concrete function from tf.Keras model, before being able to converting to TFLite; or better yet just point them to try the preview instead of TF 2.0 Alpha. 

Question
If we can directly convert from a tf.Keras model to TFLite with from_keras_model(), under what scenario do we need to worry about generating a concrete function from a tf.Keras model?

Thanks.
Margaret

Nupur Garg

unread,
Apr 24, 2019, 1:40:50 PM4/24/19
to Margaret Maynard-Reid, Aakash Kumar Nain, Tim Davis, Paige Bailey, TensorFlow Lite, TensorFlow Community Testing, TensorFlow Developers
Hi Margaret,

Thanks for the feedback. I will add a note to at the top of the "Converter Python API guide" about the 2.0 alpha differing from the nightly.

When using from_keras_model you do not need to worry about generating a concrete function. The only caveat is when you want to convert only a subset of the model. In 2.0, the only way to convert a portion of the model currently is to wrap only the part of the model you want to convert in a tf.function and convert that function.

Best,
Nupur

Margaret Maynard-Reid

unread,
Apr 24, 2019, 1:49:22 PM4/24/19
to Nupur Garg, Aakash Kumar Nain, Tim Davis, Paige Bailey, TensorFlow Lite, TensorFlow Community Testing, TensorFlow Developers
Hi Nupur,

Sounds good. Thanks for the follow-up (note on 2.0 alpha differs from nightly) and clarification!

Margaret

Aakash Kumar Nain

unread,
Apr 25, 2019, 2:54:35 AM4/25/19
to Margaret Maynard-Reid, Nupur Garg, Tim Davis, Paige Bailey, TensorFlow Lite, TensorFlow Community Testing, TensorFlow Developers
Hi,

Thanks for the info. I tested the `from_keras_model` in the nightly version today and it works fine. I have one more question though. Now, I have the converted tflite model, how do I save it onto disk. In earlier versions, it was through the `.pb` file. What are the changes in tf2.0 for saving the model onto disk with `.tflite` format? 

Regards,
Aakash Nain

Aakash Kumar Nain

unread,
Apr 25, 2019, 7:27:47 AM4/25/19
to Margaret Maynard-Reid, Nupur Garg, Tim Davis, Paige Bailey, TensorFlow Lite, TensorFlow Community Testing, TensorFlow Developers
I found it. It's simple save and read thing like an ordinary file. Thanks anyways


Regards,
Aakash Nain

GetMotivated

unread,
Dec 18, 2019, 3:36:27 AM12/18/19
to TensorFlow Developers, marga...@gmail.com, nupu...@google.com, timd...@google.com, webp...@google.com, tfl...@tensorflow.org, tes...@tensorflow.org
Hi bro,
I am new to python, I am facing similar issue you were facing, can you kindly share your code, or atleast how you imported or installed nightly version because I have trouble with that also

Thanks
To unsubscribe from this group and stop receiving emails from it, send an email to tes...@tensorflow.org.

--
You received this message because you are subscribed to the Google Groups "TensorFlow Lite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfl...@tensorflow.org.

--
You received this message because you are subscribed to the Google Groups "TensorFlow Lite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfl...@tensorflow.org.
Reply all
Reply to author
Forward
0 new messages