Пример #1
0
def test_graph_to_file():
    g = empty_graph()
    check1 = graph_to_file(g, "")
    assert not check1, "Graph to file failed should have failed."
    check2 = graph_to_file(g, "files/test_graph_to_file.onnx")
    assert check2, "Graph to file failed to write file."
    os.remove("files/test_graph_to_file.onnx")
Пример #2
0
            pass
    for cn in n.outputs:
        if cn.name[-1:] != 'b':
            cn.name = f'{cn.name}b'
        else:
            pass
graph2_inputs = [i.name for i in graph2.inputs]
print(f'graph2 inputs: {graph2_inputs}')
onnx.save(gs.export_onnx(graph2), "graph2.onnx")
"""
graph1 outputs: [
    '317a',
    '852a',
    '870a',
    '897a',
    '836a'
]
graph2 inputs: [
    '0b',
    'input.1b',
    'input.13b',
    'input.25b',
    'input.37b'
]
"""
sg1 = so.graph_from_file('graph1.onnx')
sg2 = so.graph_from_file('graph2.onnx')
sg3 = so.merge(sg1, sg2, outputs=graph1_outputs, inputs=graph2_inputs)

so.graph_to_file(sg3, MODEL3)
Пример #3
0
# Let's clean:
g = so.clean(g)

# Let's try it out for the first image:
img_data = np.array(Image.open("images/1.JPG"), dtype=np.int32)
example = {"in": img_data.astype(np.int32)}
result = so.run(g, inputs=example, outputs=['result'])

# Print the result
if result[0]:
    print("The container is empty.")
else:
    print("The container is filled.")

# Store the graph
so.graph_to_file(g, "onnx/check-container.onnx")
'''
Additional usage of sclblpy for upload and evaluation:

# Import sclblpy
import sclblpy as sp

# Upload model
sp.upload_onnx("onnx/check-container.onnx", docs={"name": "Example_02: Image", "documentation": "None provided."})

# Example input for a Scailable runtime:
input_str = so.sclbl_input(example, _verbose=False)

# Run
sp.run("5622645a-a10f-11eb-9acc-9600004e79cc", input_str)
'''
Пример #4
0
# Check and clean
sg1 = so.clean(sg1)
so.check(sg1)

# Test the resize graph:
large_input = {"large_image": large_img.astype(np.int32)}
result = so.run(sg1, inputs=large_input, outputs=['small_image'])

# Round values in array and cast as 8-bit integer to store back as JPG:
img_arr = np.array(np.round(result[0]), dtype=np.uint8)
out = Image.fromarray(img_arr, mode="RGB")
out.save("images/1-Resized.JPG")  # Yes, this works.

# Store the resize onnx:
so.graph_to_file(sg1, "onnx/resize-image-450x600-300x400.onnx")

# So, now we have a working (sub)graph that resizes an image (which obviously we can just load next time)
# Now, we open up the original image processing graph
sg2 = so.graph_from_file("onnx/check-container.onnx")

# The outputs of sg1 and the inputs of sg2 need to match; lets examine them
so.list_outputs(sg1)
so.list_inputs(sg2)

# Merge the two graphs, the outputs will be merged with the inputs in order of appearance:
g = so.merge(sg1, sg2, outputs=["small_image"], inputs=["in"])
so.check(g)
so.display(g)

# And now it works with the large image:
Пример #5
0
# so.display() tries to open the graph using Netron to inspect it. This worsk on most systems if Netron is installed.
# Get Netron at https://github.com/onnx/onnx/blob/master/docs/Operators.md
so.display(g)

# Now, use the default ONNX runtime to do a test run of the graph.
# Note that the inputs dimensions and types need to match the specification of the graph.
# The outputs returns all the outputs named in the list.
example = {
    "x1": np.array([1.2]).astype(np.float32),
    "x2": np.array([2.5]).astype(np.float32)
}
result = so.run(g, inputs=example, outputs=["sum"])
print(result)

# We can easily store the graph to a file for upload at http://admin.sclbl.net:
so.graph_to_file(g, "onnx/add-scalars.onnx")

# Or, we can upload it to Scailable using the sclblpy package,
# See the sclblpy package docs for more details. https://pypi.org/project/sclblpy/
# sp.upload_onnx("onnx/add-scalars.onnx", docs={"name": "Example_01: Add", "documentation": "Empty.."})

# so.sclbl_input(inputs) converts an example input to the input that can be used on the device:
example_input = so.sclbl_input(example, "pb")
print(example_input)


# You can use the example to setup your own REST call:
def do_REST_call(cfid: str, data: str):
    """ Do rest call calls a REST endpoint on the Scailable cloud"""
    # This does work
    url = "https://taskmanager.sclbl.net:8080/task/" + cfid
Пример #6
0
    return img


# Open the image and execute the graph:
img_data = process_image("images/horse5.png").astype(np.float32)
example = {"input": img_data}
out = so.run(g, inputs=example, outputs=['output'])

# Pretty printing
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
           'ship', 'truck')
print("The ONNX model predicts the image is a",
      classes[np.argmax(out[0])] + ".")

# And store the file (since we did clean it):
so.graph_to_file(g, "onnx/cifar10-resnet20-clean.onnx")
'''
Additional usage of sclblpy for upload and evaluation:

# Import sclblpy
import sclblpy as sp

# Upload model
sp.upload_onnx("onnx/cifar10-resnet20-clean.onnx", docs={"name": "Example_03: Cifar", "documentation": "None provided."})

# Example input for a Scailable runtime:
input_str = so.sclbl_input(example, _verbose=False)

# Run
sp.run("11928b2a-a110-11eb-9acc-9600004e79cc", input_str)
'''
Пример #7
0
so.display(g)

# However, we might not like the tf default input and output names:
g = so.rename_input(g, "input_1", "in")
g = so.rename_output(g, "output_1", "result")
so.display(g)

# And now we can call it locally:
input_example = np.array([X[1, ]]).astype(
    np.float32)  # Note the extra brackets to create 1x10
example = {"in": input_example}
result = so.run(g, inputs=example, outputs=["result"])
print(result)

# Finally, we can store the changed graph:
so.graph_to_file(g, "onnx/tf-keras-static.onnx")
'''
Additional usage of sclblpy for upload and evaluation:

# Import sclblpy
import sclblpy as sp

# Upload model
sp.upload_onnx("onnx/tf-keras-static.onnx", docs={"name": "Example_04: TF-Keras-static", "documentation": "None provided."})

# Example input for a Scailable runtime:
input_str = so.sclbl_input(example, _verbose=False)

# Run
sp.run("0d7db3c7-a111-11eb-9acc-9600004e79cc", input_str)
'''
Пример #8
0
# two graphs (which can obviously be much more complex). We start with the if:
then_graph = so.empty_graph("then-graph")
then_graph = so.add_constant(then_graph, "then_value", np.array([100]),
                             "FLOAT")
then_graph = so.add_output(then_graph, "then_value", "FLOAT", [1])
so.display(
    then_graph)  # See, this is a very small graph, no input, only output

# Same for else
else_graph = so.empty_graph("else-graph")
else_graph = so.add_constant(else_graph, "iff_value", np.array([0]), "FLOAT")
else_graph = so.add_output(else_graph, "iff_value", "FLOAT", [1])

# Now, the If node which switches between the if and the else graph
n3 = so.node("If",
             inputs=['seven'],
             outputs=['horse'],
             then_branch=then_graph,
             else_branch=else_graph)
g = so.add_node(g, n3)

# Add the output
g = so.add_output(g, "horse", "FLOAT", [1])
result = so.run(g, inputs={"input": img_input}, outputs=["horse"])
print(result)  # Prints 100!

# Store the augmented graph
g = so.clean(g)
so.check(g)
so.graph_to_file(g, "onnx/cifar10-resnet20-augmented.onnx")