如何创建可轻松转换为 TensorFlow Lite 的模型? [英] How to create a model easily convertible to TensorFlow Lite?

查看:32
本文介绍了如何创建可轻松转换为 TensorFlow Lite 的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个可以转换为TensorFlow Lite(tflite)并且可以在Android应用中使用的TensorFlow模型?

按照 Google ML Crash Course 中的示例,我创建了一个分类器并训练了一个模型.我已将模型导出为保存的模型.我想将模型转换为 .tflite 文件并使用它在 Android 上进行推断.

很快(实际上是稍后)我了解到我的模型使用了

解决方案

使用

有关如何将模型转换为 tflite 的完整示例,请参阅我的 分类斜线项目-0 和 8.

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?

解决方案

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.

这篇关于如何创建可轻松转换为 TensorFlow Lite 的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆