【问题标题】:TensorFlow saved model export conversion to tfliteTensorFlow 将模型导出转换保存到 tflite
【发布时间】:2018-06-18 13:08:00
【问题描述】:

TLDR: 运行时我得到一个ValueError:

tf.contrib.lite.TocoConverter.from_saved_model()

目标:我正在尝试将 TensorFlow 保存的模型转换为 tflite,以便通过 Firebase 在移动设备上进行部署。我可以训练模型并输出保存的模型,但我无法使用 python ToCo 接口将其转换为.tflite。任何帮助将不胜感激。另外,如果有人可以评论 tflite 转换是否会捕获我所依赖的 hub.text_embedding_column() 输入过程。移动部署会使用原始输入文本执行此操作,还是我需要单独部署其中的那一部分?

问题:这是我正在运行的代码:

输入:

train_input_fn = tf.estimator.inputs.pandas_input_fn(
    train_df, train_df["target_var"], num_epochs=None, shuffle=True
)

predict_train_input_fn = tf.estimator.inputs.pandas_input_fn(
    train_df, train_df["target_var"], shuffle=False
)

predict_test_input_fn = tf.estimator.inputs.pandas_input_fn(
    test_df, test_df["target_var"], shuffle=False)

embedded_text_feature_column = hub.text_embedding_column(
    key="text", 
    module_spec="https://tfhub.dev/google/nnlm-en-dim128/1"
)

训练和评估:

estimator = tf.estimator.DNNClassifier(
    hidden_units=[500, 100],
    feature_columns=[embedded_text_feature_column],
    n_classes=2,
    optimizer=tf.train.AdagradOptimizer(learning_rate=0.003),
    model_dir="my-model"
)

estimator.train(input_fn=train_input_fn, steps=1000)

train_eval_result = estimator.evaluate(input_fn=predict_train_input_fn)
test_eval_result = estimator.evaluate(input_fn=predict_test_input_fn)

保存模型:

feature_spec = tf.feature_column.make_parse_example_spec([embedded_text_feature_column])

serve_input_fun = tf.estimator.export.build_parsing_serving_input_receiver_fn(
    feature_spec,
    default_batch_size=None
)

estimator.export_savedmodel(
    export_dir_base = "my-model",
    serving_input_receiver_fn = serve_input_fun,
    as_text=False,
    checkpoint_path="my-model/model.ckpt-1000",
)

转换模型:

converter = tf.contrib.lite.TocoConverter.from_saved_model("my-model/1529320265/") 
tflite_model = converter.convert()

错误

运行最后一行时出现以下错误:

ValueError: Tensors input_example_tensor:0 not known type tf.string

完整的跟踪是:

ValueError Traceback(最近一次调用最后一次)
在 ()
1 个转换器 = tf.contrib.lite.TocoConverter.from_saved_model("my-model/1529320265/")
----> 2 tflite_model = converter.convert()

/media/rmn/data/projects/anaconda3/envs/monily_tf19/lib/python3.6/site-packages/tensorflow/contrib/lite/python/lite.py in convert(self)
第307章,
第308章,
--> 309 allow_custom_ops=self.allow_custom_ops)
310返回结果
311
/media/rmn/data/projects/anaconda3/envs/monily_tf19/lib/python3.6/site-packages/tensorflow/contrib/lite/python/convert.py in toco_convert(input_data, input_tensors, output_tensors, inference_type, inference_input_type, input_format , output_format, quantized_input_stats, default_ranges_stats, drop_control_dependency, reorder_across_fake_quant, allow_custom_ops, change_concat_input_ranges)
204 其他:
205 raise ValueError("张量 %s 未知类型 %r" % (input_tensor.name, --> 206 input_tensor.dtype))
207
208 input_array = 模型.input_arrays.add()

ValueError: Tensors input_example_tensor:0 not known type tf.string

详情

train_dftest_df 是 pandas 数据帧,由单个输入文本列和二进制目标变量组成。我正在使用 Python 3.6.5 和 TensorFlow r1.9。

【问题讨论】:

    标签: python tensorflow tensorflow-serving tensorflow-lite


    【解决方案1】:

    此问题已在 TensorFlow 的 master 分支上修复(在提交 d3931c8 中)。参考 TensorFlow 网站上的以下文档从 GitHub 构建 pip 安装:https://www.tensorflow.org/install/install_sources

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-17
      • 2019-05-06
      • 2019-06-10
      • 2020-12-03
      • 1970-01-01
      • 2020-09-10
      • 2023-01-29
      • 1970-01-01
      相关资源
      最近更新 更多