How to create a model easily convertible to TensorFlow Lite?

谁都会走 提交于 2020-06-23 14:27:28

问题


How to create a TensorFlow model which can be converted to TensorFlow Lite (tflite) and can be used in Android application?

Following the examples in Google ML Crash Course I've created a classifier and trained a model. I've exported the model as saved model. I wanted to convert the model to .tflite file and use it to infer on Android.

Soon (actually later) I understand that my model uses unsupported operation - ParseExampleV2.

Here is the classifier I'm using for training the model:

classifier = tf.estimator.DNNClassifier(
        feature_columns=[tf.feature_column.numeric_column('pixels', shape=WIDTH * HEIGHT)],
        n_classes=NUMBER_OF_CLASSES,
        hidden_units=[40, 40],
        optimizer=my_optimizer,
        config=tf.estimator.RunConfig(keep_checkpoint_max=1),
        model_dir=MODEL_DIR)

Is there a way to train a model which doesn't use this tf.ParseExampleV2 operator?


回答1:


Use Keras Sequential API instead of Estimator API.

If your model is more complex try Keras functional API.

The Estimator is a high-level API which adds additional complexity to the model.

Here is a sequential model:

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1024, input_dim=WIDTH*HEIGHT, activation='relu'))
model.add(tf.keras.layers.Dense(1024, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

optimizer = tf.keras.optimizers.Adam(learning_rate=rate)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])

And its schema. Compare it with the one in the question:

For full example how to convert the model to tflite see my project for classifying slashed-zeros and eights.



来源:https://stackoverflow.com/questions/60622834/how-to-create-a-model-easily-convertible-to-tensorflow-lite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!