I have a numpy array (X_test) for testing my model in cloud-ml. For online prediction it is necessary to convert it to JSON format.
My numpy array has the next format:
[[ 0 0 0 ... 7464 1951 2861]
[ 0 0 0 ... 3395 1996 4999]
[ 0 0 0 ... 5294 9202 17867]
...
[ 0 0 0 ... 3506 977 7818]
[ 0 0 0 ... 1421 75 137]
[ 0 0 0 ... 12857 12686 2928]]
I use the next code for converting it to JSON:
import json
b = X_test.tolist()
json_file = "file.json"
json.dump(b, codecs.open(json_file, 'w', encoding='utf-8'), sort_keys=True, indent=4)
After this I use Google Cloud SDK Shell for cloud prediction and enter the next command:
gcloud ml-engine predict --model keras_model --version v1 --json-instances file.json
However, I get the next error:
ERROR: (gcloud.ml-engine.predict) Input instances are not in JSON format. See "gcloud ml-engine predict --help" for details.
As I understood, I incorrectly converted numpy to JSON for cloud-ml.
How correctly to convert numpy into JSON for avoiding this error?
UPD: Here's the code that helped me solve this problem:
import json
b = X_test.tolist()
json_file = "file.json"
with open(json_file, 'w', encoding='utf-8') as f:
for i in b:
instance = {"input": i}
json.dump(instance, f , sort_keys=True)
f.write("\n")