尝试从保存的模型转换为 tflite 格式时出错 [英] Error trying to convert from saved model to tflite format

查看:33
本文介绍了尝试从保存的模型转换为 tflite 格式时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将保存的模型转换为 tflite 文件时,出现以下错误:

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

我正在使用 DNN 预制估算器.

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 文件:"http://download.tensorflow.org/data/iris_training.csv"

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

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

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

推荐答案

ParseExample 用于 tf.estimator.export.build_parsing_serving_input_receiver_fn 方法.

如果你想避免它,你应该使用 tf.estimator.export.build_raw_serving_input_receiver_fn.

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

请记住,当您想对结果 SavedModel 进行预测时,您应该设置 signature_def_key="predict".

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

所以它看起来像这样predict_fn = predictor.from_saved_model(export_dir='tmp/...', signature_def_key="predict")

这篇关于尝试从保存的模型转换为 tflite 格式时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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