Skip to content

Commit 95a825b

Browse files
committed
All examples working, on to writing tests. However, still need to fix sclblpy.run.
1 parent edcb452 commit 95a825b

9 files changed

+81
-67
lines changed

examples/.temp.onnx

17 Bytes
Binary file not shown.

examples/example_06.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
sg1 = so.add_input(sg1, "large_image", "INT32", [450, 600, 3]) # Add the input
2525

2626
# The resize node:
27+
e1 = so.constant("roi", np.array([]), "FLOAT") # Note the empty fields for roi and scales.
28+
e2 = so.constant("scales", np.array([]), "FLOAT")
2729
c1 = so.constant("size", np.array([300, 400, 3]), "INT64")
28-
n1 = so.node("Resize", inputs=['large_image', '', '', 'size'], outputs=['small_image'])
29-
sg1 = so.add_nodes(sg1, [c1, n1])
30+
n1 = so.node("Resize", inputs=['large_image', 'roi', 'scales', 'size'], outputs=['small_image'])
31+
sg1 = so.add_nodes(sg1, [e1, e2, c1, n1])
3032
sg1 = so.add_output(sg1, "small_image", "INT32", [300, 400, 3])
3133

3234
# Check and clean
@@ -67,6 +69,5 @@
6769
print("The container in the large image is filled.")
6870

6971
# Store the merged graph
70-
# Todo(McK): Check this merged graph; it does not compile...
7172
g = so.graph_to_file(g, "onnx/check-container-resize.onnx")
7273

17 Bytes
Binary file not shown.
17 Bytes
Binary file not shown.

examples/onnx/tf-keras-static.onnx

20.3 KB
Binary file not shown.

sclblonnx/main.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22
import json
33
import os
44
import subprocess
5-
65
import onnxruntime as xrt
76
from onnx import ModelProto as xmp
87
from onnx import helper as xhelp
98
from onnx import onnx_ml_pb2 as xpb2
109
from onnx import save as xsave
1110
from onnx import numpy_helper as xnp
12-
1311
import sclblonnx._globals as glob
14-
15-
16-
# empty_graph creates an empty graph
1712
from sclblonnx.utils import _print
1813

1914

15+
# empty_graph creates an empty graph
2016
def empty_graph(
2117
_default_name: str = "sclblgraph"):
2218
""" empty_graph returns an empty graph
@@ -32,7 +28,7 @@ def empty_graph(
3228
try:
3329
graph = xpb2.GraphProto(name=_default_name)
3430
except Exception as e:
35-
print("Unable to create graph: " + str(e))
31+
_print("Unable to create graph: " + str(e))
3632
return False
3733
return graph
3834

@@ -58,7 +54,7 @@ def graph_from_file(
5854
mod_temp.ParseFromString(content)
5955
graph = mod_temp.graph
6056
except Exception as e:
61-
print("Unable to open the file: " + str(e))
57+
_print("Unable to open the file: " + str(e))
6258
return False
6359
return graph
6460

@@ -81,11 +77,11 @@ def graph_to_file(
8177
True if successful, False otherwise.
8278
"""
8379
if not filename:
84-
print("Please specify a filename.")
80+
_print("Unable to save: Please specify a filename.")
8581
return False
8682

8783
if type(graph) is not xpb2.GraphProto:
88-
print("graph is not an ONNX graph")
84+
_print("Unable to save: Graph is not an ONNX graph")
8985

9086
try:
9187
mod = xhelp.make_model(graph, producer_name=_producer)
@@ -122,20 +118,20 @@ def run(
122118

123119
store = graph_to_file(graph, _tmpfile)
124120
if not store:
125-
print("Unable to store model for evaluation.")
121+
_print("Unable to store model for evaluation.")
126122
return False
127123

128124
try:
129125
sess = xrt.InferenceSession(_tmpfile)
130126
out = sess.run(outputs, inputs)
131127
except Exception as e:
132-
print("Failed to run the model: " + str(e))
128+
_print("Failed to run the model: " + str(e))
133129
return False
134130

135131
try:
136132
os.remove(_tmpfile)
137133
except Exception as e:
138-
print("We were unable to delete the file " + _tmpfile)
134+
print("We were unable to delete the file " + _tmpfile, "MSG")
139135

140136
return out
141137

@@ -162,7 +158,7 @@ def display(
162158
SclblONNXError
163159
"""
164160
if type(graph) is not xpb2.GraphProto:
165-
print("graph is not a valid ONNX graph.")
161+
_print("graph is not a valid ONNX graph.")
166162
return False
167163

168164
# store as tmpfile

test/.tmp.onnx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
 sclblonnx:i
2+
$
3+
x1
4+
x2sumsclbl-onnx-node1"Add
5+
sclblgraphZ
6+
x1
7+
8+

9+
Z
10+
x2
11+
12+

13+
b
14+
sum
15+
16+

17+
B

test/files/add.onnx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
 sclblonnx:i
2+
$
3+
x1
4+
x2sumsclbl-onnx-node1"Add
5+
sclblgraphZ
6+
x1
7+
8+

9+
Z
10+
x2
11+
12+

13+
b
14+
sum
15+
16+

17+
B

test/test_main.py

+34-51
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,49 @@
11
import os
2-
import onnx
3-
import pytest
2+
import numpy as np
3+
from onnx import onnx_ml_pb2 as xpb2
4+
from sclblonnx import empty_graph, graph_from_file, graph_to_file, run
45

5-
from sclblonnx.main import display, graph_from_file, graph_to_file, check, clean
6+
7+
def test_empty_graph():
8+
g = empty_graph()
9+
assert type(g) is xpb2.GraphProto, "Failed to create empty graph."
610

711

8-
# Test graph from file:
912
def test_graph_from_file():
10-
# Test non-existing file
11-
g = graph_from_file('not_existing.onnx')
12-
assert not g, "This should be false"
13-
# Test existing file
14-
g = graph_from_file("source/example01.onnx")
15-
assert type(g) is onnx.onnx_ml_pb2.GraphProto
13+
g = graph_from_file("files/non-existing-file.onnx")
14+
assert not g, "Graph from file failed to check emtpy file."
15+
g = graph_from_file("files/example01.onnx")
16+
assert type(g) is xpb2.GraphProto, "Graph from file failed to open file."
1617

1718

18-
# Test graph storage:
1919
def test_graph_to_file():
20-
# Test not a proper graph
21-
with pytest.raises(SclblONNXError) as excinfo:
22-
graph_to_file({}, 'name.onnx')
23-
assert "valid ONNX graph" in str(excinfo.value)
24-
# Test existing file
25-
g = graph_from_file("source/example01.onnx")
26-
result = graph_to_file(g, '_tmp.onnx')
27-
assert result is True
28-
# Remove the file
29-
os.remove("_tmp.onnx")
30-
31-
32-
# Test alle examples in test/source
33-
def test_check():
34-
dir = "files"
35-
for fname in os.listdir(dir):
36-
if fname.endswith(".onnx"):
37-
print(fname)
38-
fpath = dir + "/" + fname
39-
g = graph_from_file(fpath)
40-
check(g, False)
41-
20+
g = empty_graph()
21+
check1 = graph_to_file(g, "")
22+
assert not check1, "Graph to file failed should have failed."
23+
check2 = graph_to_file(g, "files/test_graph_to_file.onnx")
24+
assert check2, "Graph to file failed to write file."
25+
os.remove("files/test_graph_to_file.onnx")
4226

43-
# Functional test off all bits of the package:
44-
def test_functional():
45-
# Open an existing graph:
46-
g = graph_from_file("source/example07.onnx")
4727

48-
# Display the graph:
49-
display(g)
28+
def test_run():
29+
g = graph_from_file("files/add.onnx")
30+
example = {"x1": np.array([2]).astype(np.float32), "x2": np.array([5]).astype(np.float32)}
31+
result = run(g,
32+
inputs=example,
33+
outputs=["sum"]
34+
)
35+
assert result[0] == 7, "Add output not correct."
36+
result = run(g, inputs="", outputs="sum")
37+
assert not result, "Model with this input should not run."
5038

51-
# Clean up:
52-
g = clean(g, _optimize=True, _simplify=True, _check=False)
53-
display(g)
5439

40+
def test_display():
41+
from onnx import TensorProto
42+
print(TensorProto.DOUBLE)
5543

56-
# Check the graph:
57-
#check(g)
58-
#print(g)
44+
return True # No test for display
5945

60-
#test_graph_from_file()
61-
#test_graph_to_file()
62-
#test_functional()
63-
#test_check()
6446

65-
g = graph_from_file("source/example01.onnx")
66-
display(g)
47+
# def test_scblbl_input():
48+
# def test_list_data_types
49+
# def test_list_operators

0 commit comments

Comments
 (0)