Exemple #1
0
def test_merge():
    """
    Functional test of merge().
    """
    # Subgraph 1
    sg1 = empty_graph("Graph 1")
    n1 = node('Add', inputs=['x1', 'x2'], outputs=['sum'])
    sg1 = add_node(sg1, n1)
    sg1 = add_input(sg1, 'x1', "FLOAT", [1])
    sg1 = add_input(sg1, 'x2', "FLOAT", [1])
    sg1 = add_output(sg1, 'sum', "FLOAT", [1])

    # Subgraph 2
    sg2 = empty_graph("Graph 2")
    sg2 = add_constant(sg2, "const", np.array([7]), "FLOAT")
    n2 = node("Equal", inputs=['sum', 'const'], outputs=['equal'])
    sg2 = add_node(sg2, n2)

    sg2 = add_input(sg2, 'sum', "FLOAT", [1])
    sg2 = add_output(sg2, 'equal', "BOOL", [1])

    g = merge(sg1, sg2, outputs=["sum"], inputs=["sum"])

    data = {
        "x1": np.array([2]).astype(np.float32),
        "x2": np.array([5]).astype(np.float32)
    }
    result = run(g, inputs=data, outputs=["equal"])
    assert result[
        0], "Sum of 2 and 5 should be equal to constant 7. Merged failed."
Exemple #2
0
def test_add_constant():

    # Simple add graph
    g = empty_graph()
    n1 = node('Add', inputs=['x1', 'x2'], outputs=['sum'])
    g = add_node(g, n1)

    # Add input and constant
    g = add_constant(g, 'x1', np.array([1]), "INT64")
    g = add_constant(g, 'x2', np.array([5]), "INT64")

    # Output:
    g = add_output(g, 'sum', "INT64", [1])

    # This works, but seems to fail for other data types...
    result = run(g, inputs={}, outputs=["sum"])
    assert result[0] == 6, "Add constant failed."
    # todo(McK): Does not work for INT16 / INT8, check?
Exemple #3
0
- Given an image of an object (for example 1.JPG in the source/images/ folder
- Subtract the empty-image (which is encoded as a constant in the ONNX graph)
- Compute absolute values
- Sum all elements of the result into a single scalar
- Compare the scalar to a threshold (another constant)
If the threshold is reached, we conclude that the container is filled.
"""

# Start with the empty graph:
g = so.empty_graph()

# Create the constant node encoding the empty image and add it to the graph:
# Note the type encoding as np.int64.
reference_image = np.array(Image.open("images/empty-average.JPG"),
                           dtype=np.int32)
g = so.add_constant(g, "c1", reference_image, "INT32")

# Add the first input (note, same shape):
g = so.add_input(g, 'in', "INT32", reference_image.shape)

# Add the Subtract, Absolute, ReduceSum, and Less nodes
# Node how the names again enforce the topology of the graph
n1 = so.node("Sub", inputs=['in', 'c1'], outputs=['sub'])
n2 = so.node("Abs", inputs=['sub'], outputs=['abs'])
n3 = so.node("ReduceSum", inputs=['abs'], outputs=['sum'],
             keepdims=0)  # Note the keepdims additional parameter.
g = so.add_nodes(g, [n1, n2, n3])

# And, we need to add the threshold (constant c2):
threshold = np.array([3000000]).astype(np.int32)
g = so.add_constant(g, "c2", threshold, "INT32")
Exemple #4
0
n1 = so.node('Abs', inputs=['in_1_1'], outputs=['out_1_1'], name="node_1_1")
g1 = so.add_input(g1, 'in_1_1', "FLOAT", [1])
g1 = so.add_output(g1, 'out_1_1', "FLOAT", [1])
g1 = so.add_node(g1, n1)
# so.display(g1)
# data = {"in_1_1": np.array([2]).astype(np.float32)}
# print(so.run(g1, inputs=data, outputs=["out_1_1"]))

# # Simple max value graph:
g2 = so.empty_graph("g_2")
n2 = so.node('Max',
             inputs=['in_2_1', 'in_2_2'],
             outputs=['out_2_1'],
             name="node_2_1")
g2 = so.add_input(g2, 'in_2_1', "FLOAT", [1])
g2 = so.add_constant(g2, "in_2_2", np.array([10]), "FLOAT")
g2 = so.add_output(g2, 'out_2_1', "FLOAT", [1])
g2 = so.add_node(g2, n2)
# so.display(g2)
# data = {"in_2_1": np.array([2]).astype(np.float32)}
# print(so.run(g2, inputs=data, outputs=["out_2_1"]))

# # Simple add two values graph:
g3 = so.empty_graph("g_3")
n3 = so.node('Add',
             inputs=['in_3_1', 'in_3_2'],
             outputs=['out_3_1'],
             name="node_3_1")
g3 = so.add_input(g3, 'in_3_1', "FLOAT", [1])
g3 = so.add_input(g3, 'in_3_2', "FLOAT", [1])
g3 = so.add_output(g3, 'out_3_1', "FLOAT", [1])
Exemple #5
0
g = so.add_node(
    g, n1)  # Note, this adds the node, but the output is still "output"
g = so.delete_output(g, "output")  # Remove the old output
g = so.add_output(g, 'argmax', "INT64",
                  [1])  # Add the new output (for testing only)

# Test:
result = so.run(g, inputs={"input": img_input}, outputs=["argmax"])
print(result)

# So, this works. Let's remove the output argmax again before we continue:
g = so.delete_output(g, 'argmax')

# Because the if statement to switch between values of 100 and 0 requires a boolean input condition, we add
# a constant node with the value of 7, and add an equals node:
g = so.add_constant(g, "cut", np.array([7]), "INT64")
n2 = so.node("Equal", inputs=['argmax', 'cut'], outputs=['seven'])
g = so.add_node(g, n2)

# Lets again test:
g = so.add_output(g, 'seven', "BOOL", [1])
result = so.run(g, inputs={"input": img_input}, outputs=["seven"])
print(result)  # Prints true... we are getting closer!
g = so.delete_output(g, 'seven')

# Here we build an if statement. Note that the if "switches" between two graphs, so let's first create the
# 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])