【问题标题】:Is there a way to send multiple image to tf serving model?有没有办法将多个图像发送到 tf 服务模型?
【发布时间】:2022-12-01 18:04:28
【问题描述】:

我正在尝试下面的代码,但出现意外错误

这是我获取输入并将其传递给模型的代码。

def get_instances(dir = '/test_data'):

        instances = list()
        file_names = [file.split('/')[-1] for file in os.listdir(dir)]
        for file in file_names :
          image = nv.imread(os.path.join(dir ,file), resize = (300,300), color_mode='rgb',normalize=True)
          image = combine_rgb_xyz(image)
          #image = nv.expand_dims(image,axis=0)
          instances.append(image)
        return np.array(instances) ,file_names 

在我使用以下代码将这些数据发送到模型后:

def make_prediction(instances):
   url = get_url()
   data = json.dumps({"signature_name": "serving_default", "instances": instances.tolist()})
   headers = {"content-type": "application/json"}
   json_response = requests.post(url, data=data, headers=headers)
   predictions = json.loads(json_response.text)['predictons']
   return predictions

但我得到了意外的输出:

'predictons'

【问题讨论】:

  • 那是完整的输出吗?看起来您期望响应对象中有一个 predictons 键。但这是“predictions”的拼写错误,如果响应来自 TF Serving,我不认为 TF Serving 会拼错“predictions”。

标签: python json tensorflow tensorflow-serving


【解决方案1】:

您可以使用 tf.make_tensor_prototf.make_ndarray 进行图像 numpy 数组与张量的转换。然后,您可以使用“serving_default”签名进行预测并将多张图像传递给 serving_default 请求以获得更快的结果。 'serving_default' 签名支持一次处理多个图像,因为它具有 4d 输入(批次、高度、宽度、通道)。 请参考guide 将多张图片传递给 TF 服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    相关资源
    最近更新 更多