-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonnx_predict.py
executable file
·50 lines (35 loc) · 1.28 KB
/
onnx_predict.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
48
49
50
#!/usr/bin/env python3
import sys
import argparse
import itertools
import numpy as np
import onnx
import onnxruntime
def main():
parser = argparse.ArgumentParser(
description='Inference using ONNX model',
)
parser.add_argument('--model', default="model.onnx", help="path to the ONNX model")
args = parser.parse_args()
# Load the ONNX model
model = onnx.load(args.model)
# Check that the IR is well formed
onnx.checker.check_model(model)
# A human readable representation of the graph
graph = onnx.helper.printable_graph(model.graph)
print(graph)
# Inference using ONNX runtime in Python
ort_session = onnxruntime.InferenceSession(args.model)
# compute ONNX Runtime output prediction from STDIN features in libsvm format
for line in sys.stdin:
x = list(map(lambda x: float(x.split(":")[1]), line.strip().split(" ")))
x = np.array([x]).astype(np.float32)
print(f"input: {x.shape}")
ort_inputs = {ort_session.get_inputs()[0].name: x}
ort_outs = ort_session.run(None, ort_inputs)
print(f"output: {ort_outs[0]}")
p = np.argwhere(ort_outs[0][0] > 0)
pp = list(itertools.chain(*p))
print(f"\nPredicted classes \w positive prob: {pp}")
if __name__ == "__main__":
main()