intel-analytics / analytics-zoo

Distributed Tensorflow, Keras and PyTorch on Apache Spark/Flink & Ray
https://analytics-zoo.readthedocs.io/
Apache License 2.0
17 stars 4 forks source link

When I use the http API of cluster-serving, how do I parse the results of non-standard json file? #401

Open gtfaiwxm opened 3 years ago

gtfaiwxm commented 3 years ago

When I use the http API of cluster-serving, I obtain the json response: {'predictions': ['{value=data=[0.0,1.0,0.99930465,0.2667401,0.2137495,0.51740575,0.7415706,...,0.0,0.0,0.0,0.0],shape=[1,200,7];}']}. How do I parse the results of non-standard json?

qiuxin2012 commented 3 years ago

Could you try com.intel.analytics.zoo.serving.http.JsonUtil.fromJson[Predictions]?

gtfaiwxm commented 3 years ago

@qiuxin2012 Where can I find this API of com.intel.analytics.zoo.serving.http.JsonUtil.fromJson?

gtfaiwxm commented 3 years ago

Moreover, I want to know that how to use the com.intel.analytics.zoo.serving.http.JsonUtil.fromJson[Predictions]. I enter this command directly on the command ? Thanks.

qiuxin2012 commented 3 years ago

https://github.com/intel-analytics/analytics-zoo/blob/master/zoo/src/main/scala/com/intel/analytics/zoo/serving/http/serializers.scala AZ use this JacksonJsonSerializer to serialize predition result. BTW, which language is your client using?

gtfaiwxm commented 3 years ago

Thanks. I use python

glorysdj commented 3 years ago

The response is standard json format, you can use any json lib in python to parse it:

{
  'predictions': [
    '{value=data=[0.0,1.0,0.99930465,0.2667401,0.2137495,0.51740575,0.7415706,...,0.0,0.0,0.0,0.0],shape=[1,200,7];}'
  ]
}

and you will get a array of predictions, each item of the array is also a json object, you can also parse it,

data=[
  0.0,
  1.0,
  0.99930465,
  0.2667401,
  0.2137495,
  0.51740575,
  0.7415706,
  ...,
  0.0,
  0.0,
  0.0,
  0.0
],
shape=[
  1,
  200,
  7
];
gtfaiwxm commented 3 years ago

@glorysdj Hi, I use the code to parse json file, the error is occured. `import json

data = { 'predictions': [ '{value=data=[0.0,1.0,0.99930465,0.2667401,0.2137495,0.51740575,0.7415706,0.0,1.0,0.99930465,0.2667401,0.2137495,0.51740575,0.7415706],shape=[1,2,7];}' ] }

data_temp = data['predictions'][0] print(data_temp) result = json.loads(data_temp)

Traceback (most recent call last): File "d:/code/json_test.py", line 11, in result = json.loads(data_temp) File "D:\SoftwareInatall\anaconda\envs\tensorflow2.3\lib\json__init__.py", line 354, in loads return _default_decoder.decode(s) File "D:\SoftwareInatall\anaconda\envs\tensorflow2.3\lib\json\decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "D:\SoftwareInatall\anaconda\envs\tensorflow2.3\lib\json\decoder.py", line 355, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)`

glorysdj commented 3 years ago

Hi @gtfaiwxm it seems like still has ' before and after the string, could you please try to remove ' in the data_temp ? data_temp = data_temp .replace("'","")

gtfaiwxm commented 3 years ago

Hi @glorysdj The ' before and after the string is normal, because string needs ' before and after in python. data_temp = data_temp .replace("'","") This command has no effect.

glorysdj commented 3 years ago

please try this:

import json
str = " { 'predictions': [ '{value=data=[0.0,1.0,0.99930465,0.2667401,0.2137495,0.51740575,0.7415706,0.0,1.0,0.99930465,0.2667401,0.2137495,0.51740575,0.7415706],shape=[1,2,7];}' ]}"
str = str.replace("\'", "\"")
result = json.loads(str)
predictions = result['predictions']
for prediction in predictions:
    prediction = prediction.replace("value=","").replace(";","")
    prediction = prediction.replace('data=','"data":').replace('shape=','"shape":')
    prediction_result = json.loads(prediction)
    print(prediction_result)
glorysdj commented 3 years ago

@Litchilitchy we should add a python api to parse response to tensor results.

gtfaiwxm commented 3 years ago

Hi. Does you add a python api to parse response to tensor results in cluster-serving?

glorysdj commented 3 years ago

not yet, we are working on notebook tutorials which will show a example of json processing, after that we will add a pyton api for this.

gtfaiwxm commented 3 years ago

Ok, If you haved add the python api in cluster-serving,I hope you can tell me. Thanks

Litchilitchy commented 3 years ago

@gtfaiwxm we used to store just the string value e.g. [[1,2],[3,3]] but it turns out to be too slow.

you can get the data array and shape array and just use np.array(data, shape=shape) to construct the array.

Although this seems not user friendly but it increase the performance.

Any better idea please share with us, thanks.

Litchilitchy commented 3 years ago

The result of http api is now value=a_json_string, you could parse a_json_string by a json parser.

helenlly commented 2 years ago

@gtfaiwxm is the issue fixed on your side? or any more questions? if no more questions, we will close the issue soon.