【问题标题】:how make correct predictions of jpeg image in cloud-ml如何在 cloud-ml 中正确预测 jpeg 图像
【发布时间】:2017-05-06 19:31:23
【问题描述】:

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

我的训练模型是 inception 模型,我想将输入发送到图的第一层:'DecodeJpeg/contents:0'(我必须发送 jpeg 图像)。我已通过添加retrain.py 将这一层设置为可能的输入:

inputs = {'image_bytes': 'DecodeJpeg/contents:0'}
tf.add_to_collection('inputs', json.dumps(inputs))

然后我将训练结果保存在两个文件(export 和 export.meta)中:

saver.save(sess, os.path.join(output_directory,'export'))

然后我使用这些文件在 cloud-ml 中创建了一个模型。

正如一些帖子(来自谷歌云官方博客的hereherehere)中所建议的那样,我正在尝试使用

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: '

我的程序有什么问题?你有什么建议吗? 我特别从this 帖子我假设cloud-ml 在读取带有image_bytes 的请求时会自动将base64 图像转换为jpeg 格式。这是正确的吗?不然怎么办?

【问题讨论】:

  • 我可能需要更多信息来更好地帮助您。您介意将您的项目编号、型号和版本名称发送到 cloudml-feedback@google.com 吗?

标签: python google-cloud-ml


【解决方案1】:

CloudML 要求您向图表提供 batch 图像。

我很确定这是重新使用 retrain.py 的问题。查看该代码的sess.run line;它一次只输入一个图像。与 flowers sample 中的批处理 jpeg 占位符进行比较。

【讨论】:

    【解决方案2】:

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

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

    构建预测图 (the code):

    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)
    

    last_checkpoint 必须指向训练中最新的检查点文件:

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

    【讨论】:

      【解决方案3】:

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

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

      在此处对您的问题的其他答案中有一些建议,关于您如何能够关注公开帖子来修改您的图表,但我可以告诉这两个问题。

      如果您遇到任何其他问题,请告诉我们。

      【讨论】:

        猜你喜欢
        • 2019-01-19
        • 2019-04-20
        • 1970-01-01
        • 2018-06-23
        • 1970-01-01
        • 2019-01-09
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        相关资源
        最近更新 更多