保存要投放的模型,但“ ValueError:必须同时提供标签和登录信息”。尝试导出模型时

时间:2018-09-21 09:37:58

标签: tensorflow tensorflow-serving google-cloud-tpu

我想保存一个模型,以便对特定图片进行一些预测。这是我的服务功能:

def _serving_input_receiver_fn():
    # Note: only handles one image at a time
    feat = tf.placeholder(tf.float32, shape=[None, 120, 50, 1])
    return tf.estimator.export.TensorServingInputReceiver(features=feat, receiver_tensors=feat)

这是我导出模型的地方:

  export_dir_base = os.path.join(FLAGS.model_dir, 'export')
  export_dir = estimator.export_savedmodel(
    export_dir_base, _serving_input_receiver_fn)

但是出现以下错误:

ValueError: Both labels and logits must be provided.

现在我不明白该错误,因为“服务”对象应该只创建一个占位符,以便以后可以通过占位符放置一些图像以对保存的模型进行预测?

这是整个追溯:

Traceback (most recent call last):
  File "/home/cezary/models/official/mnist/mnist_tpu.py", line 222, in <module>
    tf.app.run()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/home/cezary/models/official/mnist/mnist_tpu.py", line 206, in main
    export_dir_base, _serving_input_receiver_fn)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 650, in export_savedmodel
    mode=model_fn_lib.ModeKeys.PREDICT)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 703, in _export_saved_model_for_mode
    strip_default_attrs=strip_default_attrs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 811, in _export_all_saved_models
    mode=model_fn_lib.ModeKeys.PREDICT)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 1971, in _add_meta_graph_for_mode
    mode=mode)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 879, in _add_meta_graph_for_mode
    config=self.config)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 1992, in _call_model_fn
    features, labels, mode, config)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 1107, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 2203, in _model_fn
    features, labels, is_export_mode=is_export_mode)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 1131, in call_without_tpu
    return self._call_model_fn(features, labels, is_export_mode=is_export_mode)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 1337, in _call_model_fn
    estimator_spec = self._model_fn(features=features, **kwargs)
  File "/home/cezary/models/official/mnist/mnist_tpu.py", line 95, in model_fn
    cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=logits)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_impl.py", line 156, in sigmoid_cross_entropy_with_logits
    labels, logits)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.py", line 1777, in _ensure_xent_args
    raise ValueError("Both labels and logits must be provided.")
ValueError: Both labels and logits must be provided.

不要紧记mnist命名,我只是使用了代码的结构,但没有重命名。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

(我无法使用全新的帐户发表评论。)通过将featuresreceiver_tensors设置为具有相同的值,我能够复制您的错误,但是我认为您的__serving_input_receiver_fn已正确实施。您可以遵循示例here吗?