运行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",
)
CONVERT MODEL:
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 (most recent call last)
<ipython-input-14-ab1c0339a10b> in <module>()
1 converter = 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 reorder_across_fake_quant=self.reorder_across_fake_quant,
308 change_concat_input_ranges=self.change_concat_input_ranges,
--> 309 allow_custom_ops=self.allow_custom_ops)
310 return result
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 else:
205 raise ValueError("Tensors %s not known type %r" % (input_tensor.name,
--> 206 input_tensor.dtype))
207
208 input_array = model.input_arrays.add()
ValueError: Tensors input_example_tensor:0 not known type tf.string
train_df
和test_df
是由单个输入文本列和二进制目标变量组成的pandas数据帧。我使用的是Python 3.6.5和TensorFlow r1.9。
答案 0 :(得分:1)
此问题已在TensorFlow的master
分支上(在提交d3931c8中修复)。请参考TensorFlow网站上的以下文档以从GitHub构建pip安装:https://www.tensorflow.org/install/install_sources。