【问题标题】:How to build request json correctly in Object Detection with Tensorflow REST API?如何使用 Tensorflow REST API 在对象检测中正确构建请求 json?
【发布时间】:2019-10-07 03:25:21
【问题描述】:

我正在尝试将基于对象检测的模型与 Tensorflow REST API 结合使用。我已经使用 GRPC 模式做到了。我输入了一个解析为 numpy 数组的 ba​​se64 图像,如下代码:

            #Converting to byte array
            image = base64.b64decode(input)

            #Now converting to numpy array
            nparr = np.fromstring(image, np.uint8)

            #Decode into image object
            image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

            #Expandind the array
            image_expanded = np.expand_dims(image, axis=0)

            #Making the predict of image
            result = self._predict(image_expanded, label_map_string, num_classes, image)

所以我调用了 predict 方法,这就是 grpc 调用发生的地方:

        stub = self._connection()
        result = None
        request = predict_pb2.PredictRequest()
        request.model_spec.name = self.lst_model_names[0]
        request.model_spec.signature_name = self.signature_name

        label_map = label_map_util.load_labelmap(label_map_string)
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=num_classes,
                                                                    use_display_name=True)
        category_index = label_map_util.create_category_index(categories)

        request.inputs['inputs'].CopyFrom(
            tf.contrib.util.make_tensor_proto(data))

        result = stub.Predict.future(request, self.timeout)

在请求对象中我有类似的东西:

model_spec {
name: "object_detection"
signature_name: "serving_default"
}
inputs {
  key: "inputs"
  value {
    dtype: DT_UINT8
    tensor_shape {
      dim {
        size: 1
      }
      dim {
        size: 720
      }
      dim {
        size: 1280
      }
      dim {
        size: 3
      }
    }
    tensor_content: "&/3 )-\037%*!\',#)..."
}

它在 grpc 中完美运行。

对于 Tensorflow REST API,我不知道它应该如何工作。 我已经构建了这样的有效载荷:

{
"signature_name": "serving_default",
"inputs":{
    "inputs":[
        {
            "b64":""
        }
    ]
}

}

将图像输入作为 base64 格式传递。

我做错了什么?

每次我发送请求时都会出错:

Type: Object is not of expected type: uint8"

这个结构有问题吗?

我应该如何构建正确的 json 来预测对象检测模型?

非常感谢!

【问题讨论】:

    标签: rest tensorflow request tensorflow-serving object-detection-api


    【解决方案1】:

    我认为结构应该是这样的。 "b64" 用于 base64 编码图像,但您的输入是 uint8 类型。

    body = {
        "signature_name": "serving_default",
        "inputs": [
            image 
            ]
        }
    

    【讨论】:

    • 效果也不错!我覆盖了我的 4 维数组的“图像”。我使用的另一种方法是:{ "signature_name": "serving_default", "inputs":{ "inputs":[ { "b64": image_array } ] } } 但现在我正试图找到一种方法来使请求不那么重,因为 4 个 dim 数组太重了。
    猜你喜欢
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2018-01-14
    • 2019-02-18
    • 2012-01-16
    相关资源
    最近更新 更多