-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathcreate_scoring_datafiles.py
47 lines (39 loc) · 1.66 KB
/
create_scoring_datafiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Generate files for scoring input to the MLflow scoring server and TensorFlow Serving server.
Each server expects a JSON file with its own schema.
Data is obtained from the standard Keras MNIST 'x_test' field from ~/.keras/datasets/mnist.npz.
"""
import os
import json
import utils
def write_json(data, opath):
with open(opath, "w") as f:
f.write(json.dumps(data,indent=2)+"\n")
def to_json_mlflow(data, opath):
data = data.tolist()
line = data[0]
print("Number of columns:",len(line))
columns = [ f"col_{j}" for j in range(0,len(line)) ]
dct = { "columns": columns, "data": data }
write_json(dct, opath)
def to_json_tensorflow_serving(data, opath):
dct = { "instances": data.tolist() }
write_json(dct, opath)
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--rows", dest="rows", help="Number of rows", default=None, type=int)
parser.add_argument("--base_name", dest="base_name", help="Base name", default="mnist", type=str)
parser.add_argument("--output_dir", dest="output_dir", help="Output directory", default=".")
args = parser.parse_args()
print("Arguments:")
for arg in vars(args):
print(f" {arg}: {getattr(args, arg)}")
x_test = utils.get_prediction_data()
print("x_test.type:",type(x_test))
print("x_test.shape:",x_test.shape)
if args.rows:
x_test = x_test[:args.rows]
print("x_test.shape:",x_test.shape)
to_json_mlflow(x_test, os.path.join(args.output_dir,f"{args.base_name}-mlflow.json"))
to_json_tensorflow_serving(x_test, os.path.join(args.output_dir,f"{args.base_name}-tf_serving.json"))