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

我想根据 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_UINT8

shape: (-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


慕工程0101907
浏览 200回答 2
2回答

牧羊人nacy

我正在处理的图像被标准化为 [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()结果低于有效载荷大小限制。

繁华开满天机

发送如此大的整数数组通常效率不高(您将花费大量时间编码和解码这些数组、网络延迟等)。这篇文章提供了一些选项(包括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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python