【问题标题】:Error in saving and serving a model in tensorflow-serving在 tensorflow-serving 中保存和服务模型时出错
【发布时间】:2019-06-11 14:42:42
【问题描述】:

我应该如何使用 tf.saved_model.simple_save 保存我训练的模型,以便我可以使用 tensorflow-serving 发出请求

x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
values = tf.placeholder(tf.float32, [None, 1])

layer = tf.add(tf.matmul(x, w), b)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=layer))
optimize = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
correct_pred = tf.equal(tf.argmax(layer, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

with tf.Session() as sess:
  sess.run(init)
  for _ in range(10000):
    batch = mnist.train.next_batch(100)
    sess.run(accuracy, feed_dict={x:batch[0],y:batch[1]})

  !rm -rf "/model"
  export_dir = "/model/1"

  #Problem here
  tf.saved_model.simple_save(
      sess,
      export_dir=export_dir,
      inputs={"x":x},
      outputs={"accuracy":accuracy}
  )

当我跑步时:

!saved_model_cli show --dir {export_dir} --all

I get:
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['x'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 784)
        name: Placeholder:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['accuracy'] tensor_info:
        dtype: DT_FLOAT
        shape: ()
        name: Mean_1:0
  Method name is: tensorflow/serving/predict

我的输出是 shape() 而不是 (-1,x) 或那种格式。 当我发送请求时,我没有得到任何响应。由于准确性是一项操作,我没有得到任何回应。如何将其更改为变量或如何使用 keras 中使用的 {t.name for t in model.outputs}?

【问题讨论】:

    标签: tensorflow tensorflow-serving


    【解决方案1】:

    simple_save 中的输出似乎不正确。它应该是层,而不是准确性。

    【讨论】:

      【解决方案2】:

      问题出在代码的最后一行outputs={"accuracy":accuracy}。如果将accuracy 替换为“层”,该问题将得到解决。所以,代码可以如下所示:

      tf.saved_model.simple_save(sess, export_dir=export_dir, inputs={"x":x},
      outputs={"Predicted_Output":layer})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-01
        • 2018-02-26
        • 2018-09-29
        • 2017-09-24
        • 2018-02-13
        • 2021-09-10
        • 2019-04-21
        • 2018-05-16
        相关资源
        最近更新 更多