来自Google ml引擎的无效JSON

问题描述 投票:0回答:1

我已经在AI平台上部署了TensorFlow对象检测模型。模型配置如下:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['image_bytes'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: encoded_image_string_tensor:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['detection_boxes'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 300, 4)
        name: detection_boxes:0
    outputs['detection_classes'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 300)
        name: detection_classes:0
    outputs['detection_features'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1, -1, -1, -1)
        name: detection_features:0
    outputs['detection_multiclass_scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 300, 2)
        name: detection_multiclass_scores:0
    outputs['detection_scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 300)
        name: detection_scores:0
    outputs['num_detections'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: num_detections:0
    outputs['raw_detection_boxes'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 300, 4)
        name: raw_detection_boxes:0
    outputs['raw_detection_scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 300, 2)
        name: raw_detection_scores:0
  Method name is: tensorflow/serving/predict

我正在使用以下代码来生成预测

    def predict_json(project, model, request, version=None):
        """Send json data to a deployed model for prediction.

        Args:
            project (str): project where the Cloud ML Engine Model is deployed.
            model (str): model name.
            instances ([Mapping[str: Any]]): Keys should be the names of Tensors
                your deployed model expects as inputs. Values should be datatypes
                convertible to Tensors, or (potentially nested) lists of datatypes
                convertible to tensors.
            version: str, version of the model to target.
        Returns:
            Mapping[str: any]: dictionary of prediction results defined by the
                model.
        """
        # Create the ML Engine service object.
        # To authenticate set the environment variable
        # GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
        os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = <service-account-file>
        service = build('ml', 'v1')
        name = 'projects/{}/models/{}'.format(project, model)
        if version is not None:
            name += '/versions/{}'.format(version)
        response = service.projects().predict(
            name=name,
            body=request
        )
        response.execute()
        if 'error' in response:
            raise RuntimeError(response['error'])

        return response['predictions']

img = Image.open(image)
output_str = io.BytesIO()
img.save(output_str, "JPEG")
image_byte_array = output_str.getvalue()
image_base64 = base64.b64encode(image_byte_array).decode()
request = {"instances": [{"image_bytes": {"b64": image_base64}}]}

prediction = predict_json('handdetector', 'fastercnn', request)

预测返回,但缺少一个标记'detection_scores'。此外,响应不是正确的JSON,这会导致以下错误:

Traceback (most recent call last):
  File "/Users/syedmustufainabbasrizvi/PycharmProjects/sign-language/Sign-Language-Translation/detection/predict.py", line 60, in <module>
    prediction = predict_json('handdetector', 'fastercnn', request)
  File "/Users/syedmustufainabbasrizvi/PycharmProjects/sign-language/Sign-Language-Translation/detection/predict.py", line 36, in predict_json
    response.execute()
  File "/Users/syedmustufainabbasrizvi/.pyenv/versions/sign-language/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Users/syedmustufainabbasrizvi/.pyenv/versions/sign-language/lib/python3.6/site-packages/googleapiclient/http.py", line 857, in execute
    return self.postproc(resp, content)
  File "/Users/syedmustufainabbasrizvi/.pyenv/versions/sign-language/lib/python3.6/site-packages/googleapiclient/model.py", line 216, in response
    return self.deserialize(content)
  File "/Users/syedmustufainabbasrizvi/.pyenv/versions/sign-language/lib/python3.6/site-packages/googleapiclient/model.py", line 274, in deserialize
    body = json.loads(content)
  File "/Users/syedmustufainabbasrizvi/.pyenv/versions/3.6.5/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/Users/syedmustufainabbasrizvi/.pyenv/versions/3.6.5/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/syedmustufainabbasrizvi/.pyenv/versions/3.6.5/lib/python3.6/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 9 column 1976399 (char 1999826)

我手动检查了响应的正文,发现它没有返回有效的json,因为某些括号也丢失了,因此当它尝试在内部加载回json时会导致json错误。是否有人遇到过类似的经历。

tensorflow object-detection google-cloud-ml
1个回答
0
投票

对此有任何解决方法吗? @gogasca我遇到同样的问题。

[我可以猜到,AI平台上有一个大小阈值。我有一个设置为预测100个类的对象检测模型,它的save_model.pb为〜55MB,它很好用...我有另一个模型,可以预测近1000个类,其save_model.pb为〜160MB,那个模型阻止了上面的错误。

[也许找到一种方法来使用tf服务而不是AI平台,或者制作一个较小的模型。但这只是一个猜测,因为AI平台对此问题的文档很少。

在我的情况下较小的模型是答案->,这很不幸,因为1000个类更适合我的特定用例。但是,尽管如此,还是可以解决您发布的问题。

© www.soinside.com 2019 - 2024. All rights reserved.