为了账号安全,请及时绑定邮箱和手机立即绑定

如何在ml引擎中构建用于对象检测预测的uint8 numpy数组输入张量

如何在ml引擎中构建用于对象检测预测的uint8 numpy数组输入张量

慕工程0101907 2021-07-15 14:02:39
我想根据 Google ML 引擎中现有的模型进行在线对象检测预测(或推理)。但我无法构建 json 请求。模型是来自TF模型动物园的faster_rcnn_inception_resnet_v2_atrous_coco_2017_11_08。输入是图像输出类,bb,分数等...所需的输入是(来自 saved_model_cli show )inputs['inputs'] tensor_info:dtype: DT_UINT8shape: (-1, -1, -1, 3)name: image_tensor:0因为它需要一个 uint8 数组,所以我将图像加载到一个 numpy 数组中encoded_contents = np.array(image.getdata()).reshape(        (im_height, im_width, 3)).astype(np.uint8)调整图像大小 image_np_expanded = np.expand_dims(encoded_contents, axis=0)尝试构建 json 请求instance = {"input":encoded_contents}row = json.dumps(instance,sort_keys=True)但我无法构建它,因为TypeError(repr(o) + " is not JSON serializable")TypeError: array([[[164, 191, 220],[190, 157, 114],[190, 157, 114]]], dtype=uint8) is not JSON serializable如果我使用 tolist() 方法将 numpy 数组转换为列表,则 json 文件需要 3 兆字节并且 ML 引擎拒绝它“消息”:“请求有效负载大小超过限制:1572864 字节。”,我会将此 json 作为 json 文件发送到 ml-engine predict。gcloud ml-engine predict --model=pellaires --version=pellaires14 --json- instances=request.json > response.yaml
查看完整描述

2 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

我正在处理的图像被标准化为 [0,1]。当转换为列表时,图像的每个像素都具有不必要的大量精度:


[[[0.4, 0.41568627450980394, 0.4117647058823529],

  [0.39215686274509803, 0.403921568627451, 0.403921568627451],

  [0.38823529411764707, 0.4, 0.4],

  [0.3803921568627451, 0.39215686274509803, 0.3843137254901961],

  [0.3803921568627451, 0.38823529411764707, 0.38823529411764707],

  ...

  [0.11764705882352941, 0.12941176470588237, 0.12549019607843137],

  [0.11764705882352941, 0.12941176470588237, 0.12549019607843137],

  [0.11764705882352941, 0.12941176470588237, 0.12549019607843137]]]

一个快速而肮脏的解决方法是使用 np.around():


img_list = np.around(img_np, 4).tolist()

结果低于有效载荷大小限制。


查看完整回答
反对 回复 2021-07-21
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

发送如此大的整数数组通常效率不高(您将花费大量时间编码和解码这些数组、网络延迟等)。这篇文章提供了一些选项(包括tolist您尝试过的)。


我会推荐“打包为字节字符串的张量”或“压缩图像数据”。


更新 10/19/2018


为了使用这些方法,您需要修改您的图表。如果您能够重新导出模型,那是最简单的。代替:


images = tf.placeholder(dtype=tf.uint8, shape=[None, None, None, 3])

你会使用:


raw_byte_strings = tf.placeholder(dtype=tf.string, shape=[None])

decode = lambda raw_byte_str: tf.decode_raw(raw_byte_str, tf.uint8)

images = tf.map_fn(decode, raw_byte_strings, dtype=tf.uint8)


查看完整回答
反对 回复 2021-07-21
  • 2 回答
  • 0 关注
  • 173 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信