Python 如何在cloud ml中正确预测jpeg图像

Python 如何在cloud ml中正确预测jpeg图像,python,google-cloud-ml,Python,Google Cloud Ml,我想在cloud-ml中预测jpeg图像 我的培训模型是inception模型,我想将输入发送到图形的第一层:'DecodeJpeg/contents:0'(在这里我必须发送jpeg图像)。我通过添加以下内容将此层设置为可能的输入: 然后,我将培训结果保存在两个文件(export和export.meta)中,其中包括: 我使用这些文件在CloudML中创建了一个模型 正如一些帖子(以及谷歌云官方博客)所建议的那样,我正试图用 gcloud beta ml predict --json-insta

我想在cloud-ml中预测jpeg图像

我的培训模型是inception模型,我想将输入发送到图形的第一层:
'DecodeJpeg/contents:0'
(在这里我必须发送jpeg图像)。我通过添加以下内容将此层设置为可能的输入:

然后,我将培训结果保存在两个文件(export和export.meta)中,其中包括:

我使用这些文件在CloudML中创建了一个模型

正如一些帖子(以及谷歌云官方博客)所建议的那样,我正试图用

gcloud beta ml predict --json-instances=request.json --model=MODEL
其中,实例是以base64格式解码的jpeg图像,具有:

python -c 'import base64, sys, json; img = base64.b64encode(open(sys.argv[1], "rb").read()); print json.dumps({"key":"0", "image_bytes": {"b64": img}})' image.jpg &> request.json
但是,请求返回给我:

error: 'Prediction failed: '
我的程序有什么问题?你有什么建议吗?
我在文章中假设cloud ml在读取带有图像字节的请求时自动将base64图像转换为jpeg格式。对吗?否则我该怎么办?

CloudML要求您向图形提供一批图像


我很确定这就是重新使用retain.py的问题所在。看到代码的;它一次传送一个图像。与中的批处理jpeg占位符比较。

注意,需要构建三个稍有不同的TF图:训练、评估和预测。有关详细信息,请参阅。训练图和预测图直接使用预处理中的嵌入,因此它们不包含初始图。对于预测,我们需要将图像字节作为输入,并使用Inception来提取嵌入

对于在线预测,您需要导出预测图。还应指定输出和输入键

要构建预测图():

要导出精度图,请执行以下操作:

def export(self, last_checkpoint, output_dir):
  # Build and save prediction meta graph and trained variable values.
  with tf.Session(graph=tf.Graph()) as sess:        
    self.build_prediction_graph()
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    self.restore_from_checkpoint(sess, self.inception_checkpoint_file,
                                 last_checkpoint)
    saver = tf.train.Saver()
    saver.export_meta_graph(filename=os.path.join(output_dir, 'export.meta'))
    saver.save(sess, os.path.join(output_dir, 'export'), write_meta_graph=False)
最后一个检查点必须指向培训中的最新检查点文件:

self.model.export(tf.train.latest_checkpoint(self.train_path), self.model_path)

在您的帖子中,您指出您的输入集合只有“image_bytes”张量别名。但是,在构建请求的代码中,包含两个输入:一个是“key”,另一个是“image_bytes”。因此,我的建议是从请求中删除“key”,或者在输入集合中添加“key”

第二个问题是DecodeJpeg/contents:0'的形状是()。对于CloudML,您需要有一个类似于(None,)的形状,以便可以输入它

在你的问题的其他答案中有一些建议,关于你如何能够跟随公共帖子修改你的图表,但我可以告诉你这两个问题


如果您遇到任何进一步的问题,请告诉我们。

我可能需要更多信息来更好地帮助您。您介意将您的项目编号、型号和版本名发送给cloudml吗-feedback@google.com?
def build_prediction_graph(self):
   """Builds prediction graph and registers appropriate endpoints."""
   tensors = self.build_graph(None, 1, GraphMod.PREDICT)
   keys_placeholder = tf.placeholder(tf.string, shape=[None])
   inputs = {
      'key': keys_placeholder.name,
      'image_bytes': tensors.input_jpeg.name
   }

   tf.add_to_collection('inputs', json.dumps(inputs))

   # To extract the id, we need to add the identity function.
   keys = tf.identity(keys_placeholder)
   outputs = {
       'key': keys.name,
       'prediction': tensors.predictions[0].name,
       'scores': tensors.predictions[1].name
   }
   tf.add_to_collection('outputs', json.dumps(outputs))
def export(self, last_checkpoint, output_dir):
  # Build and save prediction meta graph and trained variable values.
  with tf.Session(graph=tf.Graph()) as sess:        
    self.build_prediction_graph()
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    self.restore_from_checkpoint(sess, self.inception_checkpoint_file,
                                 last_checkpoint)
    saver = tf.train.Saver()
    saver.export_meta_graph(filename=os.path.join(output_dir, 'export.meta'))
    saver.save(sess, os.path.join(output_dir, 'export'), write_meta_graph=False)
self.model.export(tf.train.latest_checkpoint(self.train_path), self.model_path)