问题
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