如何在 Google 机器学习中将 jpeg 图像转换为 json 文件 [英] How convert a jpeg image into json file in Google machine learning

查看:35
本文介绍了如何在 Google 机器学习中将 jpeg 图像转换为 json 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Google Cloud ML,我想对 jpeg 图像进行预测.为此,我想使用:

I'm working on Google cloud ML, and I want to get prediction on jpeg image. To do this, I would like to use:

gcloud beta ml predict --instances=INSTANCES --model=MODEL [--version=VERSION]

gcloud beta ml predict --instances=INSTANCES --model=MODEL [--version=VERSION]

(https://cloud.google.com/ml/reference/commandline/预测)

Instances 是 json 文件的路径,其中包含有关图像的所有信息.如何从我的 jpeg 图像创建 json 文件?

Instances is the path to a json file with all info about image. How can I create the json file from my jpeg image?

非常感谢!!

推荐答案

第一步是确保您导出的图形具有可以接受 JPEG 数据的占位符和操作.请注意,CloudML 假设您正在发送一批图像.我们必须使用 tf.map_fn 来解码和调整一批图像的大小.根据模型的不同,可能需要对数据进行额外的预处理以对数据进行标准化等.如下所示:

The first step is to make sure that the graph you export has a placeholder and ops that can accept JPEG data. Note that CloudML assumes you are sending a batch of images. We have to use a tf.map_fn to decode and resize a batch of images. Depending on the model, extra preprocessing of the data may be required to normalize the data, etc. This is shown below:

# Number of channels in the input image
CHANNELS = 3

# Dimensions of resized images (input to the neural net)
HEIGHT = 200
WIDTH = 200

# A placeholder for a batch of images
images_placeholder = tf.placeholder(dtype=tf.string, shape=(None,))

# The CloudML Prediction API always "feeds" the Tensorflow graph with
# dynamic batch sizes e.g. (?,).  decode_jpeg only processes scalar
# strings because it cannot guarantee a batch of images would have
# the same output size.  We use tf.map_fn to give decode_jpeg a scalar
# string from dynamic batches.
def decode_and_resize(image_str_tensor):
  """Decodes jpeg string, resizes it and returns a uint8 tensor."""

  image = tf.image.decode_jpeg(image_str_tensor, channels=CHANNELS)

  # Note resize expects a batch_size, but tf_map supresses that index,
  # thus we have to expand then squeeze.  Resize returns float32 in the
  # range [0, uint8_max]
  image = tf.expand_dims(image, 0)
  image = tf.image.resize_bilinear(
      image, [HEIGHT, WIDTH], align_corners=False)
  image = tf.squeeze(image, squeeze_dims=[0])
  image = tf.cast(image, dtype=tf.uint8)
  return image

decoded_images = tf.map_fn(
    decode_and_resize, images_placeholder, back_prop=False, dtype=tf.uint8)

# convert_image_dtype, also scales [0, uint8_max] -> [0, 1).
images = tf.image.convert_image_dtype(decoded_images, dtype=tf.float32)

# Then shift images to [-1, 1) (useful for some models such as Inception)
images = tf.sub(images, 0.5)
images = tf.mul(images, 2.0)

# ...

此外,我们需要确保正确标记输入,在这种情况下,输入的名称(映射中的键)必须以 _bytes 结尾.当发送 base64 编码的数据时,它会让 CloudML 预测服务知道它需要对数据进行解码:

Also, we need to be sure to properly mark the inputs, in this case, it's essential that the name of the input (the key in the map) end in _bytes. When sending base64 encoded data, it will let the CloudML prediction service know it needs to decode the data:

inputs = {"image_bytes": images_placeholder.name}
tf.add_to_collection("inputs", json.dumps(inputs))

gcloud 命令期望的数据格式为:

The data format that the gcloud command is expecting will be of the form:

{"image_bytes": {"b64": "dGVzdAo="}}

(注意,如果 image_bytes 是您模型的唯一输入,您可以简化为仅 {"b64": "dGVzdAo="}).

(Note, if image_bytes is the only input to your model you can simplify to just {"b64": "dGVzdAo="}).

例如,要从磁盘上的文件创建此文件,您可以尝试以下操作:

For example, to create this from a file on disk, you could try something like:

echo "{"image_bytes": {"b64": "`base64 image.jpg`"}}" > instances

然后像这样将其发送到服务:

And then send it to the service like so:

gcloud beta ml predict --instances=instances --model=my_model

请注意,直接向服务发送数据时,您发送的请求正文需要包含在实例"列表中.因此,上面的 gcloud 命令实际上将以下内容发送到 HTTP 请求正文中的服务:

Please note that when sending data directly to the service, the body of the request you send needs to be wrapped in an "instances" list. So the gcloud command above actually sends the following to the service in the body of the HTTP request:

{"instances" : [{"image_bytes": {"b64": "dGVzdAo="}}]}

这篇关于如何在 Google 机器学习中将 jpeg 图像转换为 json 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆