Error trying to convert from saved model to tflite format

吃可爱长大的小学妹 提交于 2019-12-06 06:41:08

问题


While trying to convert a saved model to tflite file I get the following error:

F tensorflow/contrib/lite/toco/tflite/export.cc:363] Some of the operators in the model are not supported by the standard TensorFlow Lite runtime. If you have a custom implementation for them you can disable this error with --allow_custom_ops, or by setting allow_custom_ops=True when calling tf.contrib.lite.toco_convert(). Here is a list of operators for which you will need custom implementations: AsString, ParseExample.\nAborted (core dumped)\n' None

I am using the DNN premade Estimator.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"
INPUT_TENSOR_NAME = 'inputs'

def main():
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
    filename=IRIS_TRAINING,
    target_dtype=np.int,
    features_dtype=np.float32)

feature_columns = [tf.feature_column.numeric_column(INPUT_TENSOR_NAME, shape=[4])]

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                           hidden_units=[10, 20, 10],
                           n_classes=3,
                           model_dir="/tmp/iris_model")


# Define the training inputs
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={INPUT_TENSOR_NAME: np.array(training_set.data)},
    y=np.array(training_set.target),
    num_epochs=None,
    shuffle=True)

# Train model.
classifier.train(input_fn=train_input_fn, steps=2000)

inputs = {'x': tf.placeholder(tf.float32, [4])}
tf.estimator.export.ServingInputReceiver(inputs, inputs)

saved_model=classifier.export_savedmodel(export_dir_base="/tmp/iris_model", serving_input_receiver_fn=serving_input_receiver_fn)

print(saved_model)

converter = tf.contrib.lite.TocoConverter.from_saved_model(saved_model)
tflite_model = converter.convert()

def serving_input_receiver_fn():
    feature_spec = {INPUT_TENSOR_NAME: tf.FixedLenFeature(dtype=tf.float32, shape=[4])}
    return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()

if __name__ == "__main__":
    main()

Iris files can be downloaded form the following links:

IRIS_TRAINING FILE: "http://download.tensorflow.org/data/iris_training.csv"

IRIS_TEST FILE: "http://download.tensorflow.org/data/iris_test.csv"


回答1:


ParseExample is used in the tf.estimator.export.build_parsing_serving_input_receiver_fn method.

If you want to avoid it you should use tf.estimator.export.build_raw_serving_input_receiver_fn.

Keep in mind that when you want to predict on the resulting SavedModel you should set the signature_def_key="predict".

So it will look like this predict_fn = predictor.from_saved_model(export_dir='tmp/...', signature_def_key="predict")



来源:https://stackoverflow.com/questions/51845395/error-trying-to-convert-from-saved-model-to-tflite-format

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