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."
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: result = so.run(g, inputs=large_input, outputs=['result']) # Print the result if result[0]: print("The container in the large image is empty.") else: print("The container in the large image is filled.") # Store the merged graph g = so.graph_to_file(g, "onnx/check-container-resize.onnx")
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)
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]) g3 = so.add_node(g3, n3) # so.display(g3) # data = { # "in_3_1": np.array([2]).astype(np.float32), # "in_3_2": np.array([5]).astype(np.float32)} # print(so.run(g3, inputs=data, outputs=["out_3_1"])) # # MERGE: # # Merge takes two complete graphs and links the output of the parent to the inputs of the child. # # Merge assumes the result is complete. g_merge = so.merge(sg1=g1, sg2=g2, io_match=[("out_1_1", "in_2_1")]) # so.display(g_merge) # data = {"in_1_1": np.array([2]).astype(np.float32)} # print(so.run(g_merge, inputs=data, outputs=["out_2_1"])) # # JOIN: # # Join takes two parents and links their outputs to one child # # Join assumes the result is complete. g_join = so.join(pg1=g1, pg2=g2, cg=g3, pg1_match=[("out_1_1", "in_3_1")], pg2_match=[("out_2_1", "in_3_2")]) # so.display(g_join) # data = { # "in_1_1": np.array([2]).astype(np.float32),