Example #1
0
 def mk_model(opset_versions: List[Tuple[Text, int]]) -> ModelProto:
     graph = helper.make_graph([], "my graph", [], [])
     return helper.make_model_gen_version(graph,
                                          opset_imports=[
                                              helper.make_opsetid(*pair)
                                              for pair in opset_versions
                                          ])
Example #2
0
    def test_create_weight_matching_per_channel(self):

        # float model
        #         (input)
        #           |
        #          Add
        #       /   |   \
        #  MatMul MatMul MatMul
        #     |     |      |
        # (output)(output)(output)
        float_model_path = str(Path(self._tmp_model_dir.name) / "float_model4.onnx")
        initializers = []
        input_tensor = helper.make_tensor_value_info("input", TensorProto.FLOAT, [5, 5])
        output_tensor1 = helper.make_tensor_value_info("M", TensorProto.FLOAT, [5, 5])
        output_tensor2 = helper.make_tensor_value_info("N", TensorProto.FLOAT, [5, 5])
        output_tensor3 = helper.make_tensor_value_info("O", TensorProto.FLOAT, [5, 5])

        add_weight_data = np.random.normal(0, 0.1, [5, 5]).astype(np.float32)
        initializers.append(onnx.numpy_helper.from_array(add_weight_data, name="P"))
        matmul_weight_data_1 = np.random.normal(0, 0.1, [5, 5]).astype(np.float32)
        initializers.append(onnx.numpy_helper.from_array(matmul_weight_data_1, name="Q"))
        matmul_weight_data_2 = np.random.normal(0, 0.1, [5, 5]).astype(np.float32)
        initializers.append(onnx.numpy_helper.from_array(matmul_weight_data_2, name="R"))
        initializers.append(onnx.numpy_helper.from_array(matmul_weight_data_2, name="S"))

        add_node = onnx.helper.make_node("Add", ["input", "P"], ["T"], name="Add")
        matmul_node_1 = onnx.helper.make_node("MatMul", ["T", "Q"], ["M"], name="MatMul1")
        matmul_node_2 = onnx.helper.make_node("MatMul", ["T", "R"], ["N"], name="MatMul2")
        matmul_node_3 = onnx.helper.make_node("MatMul", ["T", "S"], ["O"], name="MatMul3")

        graph = helper.make_graph(
            [add_node, matmul_node_1, matmul_node_2, matmul_node_3],
            "QDQ_Test",
            [input_tensor],
            [output_tensor1, output_tensor2, output_tensor3],
            initializer=initializers,
        )
        model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)])
        onnx.save(model, float_model_path)

        # Setup: create qdq model:
        qdq_model_path = str(Path(self._tmp_model_dir.name) / "qdq_model4.onnx")
        quantize_static(
            float_model_path,
            qdq_model_path,
            TestDataReader([5, 5]),
            quant_format=QuantFormat.QDQ,
            per_channel=True,
            reduce_range=False,
            activation_type=QuantType.QInt8,
            weight_type=QuantType.QInt8,
        )

        # Call function under test and verify all weights are present
        matched_weights = create_weight_matching(float_model_path, qdq_model_path)
        weight_names = ["P", "Q", "R", "S"]
        for weight_name in weight_names:
            float_array = matched_weights[weight_name]["float"]
            dq_array = matched_weights[weight_name]["dequantized"]
            self.assertEqual(float_array.shape, dq_array.shape)
Example #3
0
def create_model(constant_node_name, output_file_name):
    dense_shape = [3,3]
    sparse_values = [1.764052391052246, 0.40015721321105957, 0.978738009929657]
    values_tensor = helper.make_tensor(name='Constant', data_type=TensorProto.FLOAT,
                                       dims=[len(sparse_values)],
                                       vals=np.array(sparse_values).astype(np.float32), raw=False)

    linear_indicies = [2, 3, 5]
    indicies_tensor = helper.make_tensor(name='indicies', data_type=TensorProto.INT64, 
                                       dims=[len(linear_indicies)],
                                       vals=np.array(linear_indicies).astype(np.int64), raw=False)
    sparse_tensor = helper.make_sparse_tensor(values_tensor, indicies_tensor, dense_shape)

    # Nodes
    #sparse_attribute = helper.make_attribute('value', sparse_tensor)
    constant_node = helper.make_node(constant_node_name, inputs=[], outputs=['values'],
                                     name='Constant', domain='', value=sparse_tensor)

     # Outputs, a square matrix
    Values_info = make_sparse_tensor_value_info('values', TensorProto.FLOAT, dense_shape)

    graph_def = helper.make_graph(nodes=[constant_node],
                                  name='ConstantNodeOutput',
                                  inputs=[],
                                  outputs=[Values_info])

    model_def = helper.make_model(graph_def, producer_name='dmitrism', 
      opset_imports=[make_opsetid('', 12)])

    onnx.save(model_def, output_file_name)
Example #4
0
        def create_onnx(self) -> onnx.ModelProto:
            node = make_node("Clip", inputs=["x"], outputs=["y"], min=0.0, max=6.0)

            inputs = [info("x", TensorProto.FLOAT, (1, 3, 4, 5))]
            outputs = [info("y", TensorProto.FLOAT, (1, 3, 4, 5))]
            graph = make_graph([node], "add_graph", inputs, outputs)
            return make_model(graph, opset_imports=[make_opsetid("", 6)])
Example #5
0
    def construct_model_Constant(self, model_path):
        #    (input)    Constant
        #       \         /
        #        \       /
        #         \     /
        #          \   /
        #           Add
        #            |
        #         (output)

        initializers = []
        input = helper.make_tensor_value_info('input', TensorProto.FLOAT,
                                              [4, 8, 12])
        output = helper.make_tensor_value_info('output', TensorProto.FLOAT,
                                               [4, 8, 12])

        # make nodes
        constant_node = onnx.helper.make_node('Constant', [], ['const_output'],
                                              value_float=42.0)
        add_node = onnx.helper.make_node('Add', ['input', 'const_output'],
                                         ['output'],
                                         name='Add')
        graph = helper.make_graph([add_node, constant_node],
                                  'onnx_model_test', [input], [output],
                                  initializer=initializers)
        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 13)])
        onnx.save(model, model_path)
    def construct_model_conv_avgpool(self, output_model_path,
                                     conv_input_shape, conv_weight_shape,
                                     avgpool_input_shape, avgpool_attributes,
                                     output_shape,
                                     ):
        #      (input)
        #          \
        #         Conv
        #        /    \
        #   Identity  AveragePool
        #    /            \
        # (identity_out)  (output)
        input_tensor = helper.make_tensor_value_info('input', TensorProto.FLOAT, conv_input_shape)

        conv_weight_arr = np.random.randint(-1, 2, conv_weight_shape).astype(np.float32)
        conv_weight_initializer = onnx.numpy_helper.from_array(conv_weight_arr, name='conv1_weight')
        conv_node = onnx.helper.make_node('Conv', ['input', 'conv1_weight'], ['conv_output'], name='conv_node')

        identity_out = helper.make_tensor_value_info('identity_out', TensorProto.FLOAT, avgpool_input_shape)
        identity_node = helper.make_node('Identity', ['conv_output'], ['identity_out'], name='IdentityNode')

        initializers = [conv_weight_initializer]

        output_tensor = helper.make_tensor_value_info('output', TensorProto.FLOAT, output_shape)
        avgpool_node = helper.make_node('AveragePool', ['conv_output'], ['output'], name='avgpool_node', **avgpool_attributes)

        graph = helper.make_graph([conv_node, identity_node, avgpool_node], 'TestOpQuantizerAveragePool_test_model',
                                  [input_tensor], [identity_out, output_tensor], initializer=initializers)
        model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 12)])
        model.ir_version = 7 # use stable onnx ir version
        onnx.save(model, output_model_path)
Example #7
0
    def export_nearest():  # type: () -> None
        node = onnx.helper.make_node(
            'Upsample',
            inputs=['X', 'scales'],
            outputs=['Y'],
            mode='nearest',
        )

        data = np.array([[[
            [1, 2],
            [3, 4],
        ]]], dtype=np.float32)

        scales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)

        output = np.array([[[
            [1, 1, 1, 2, 2, 2],
            [1, 1, 1, 2, 2, 2],
            [3, 3, 3, 4, 4, 4],
            [3, 3, 3, 4, 4, 4],
        ]]],
                          dtype=np.float32)

        expect(node,
               inputs=[data, scales],
               outputs=[output],
               name='test_upsample_nearest',
               opset_imports=[helper.make_opsetid("", 9)])
Example #8
0
    def test_onnx_to_caffe2_if(self):
        true_nodes = [helper.make_node("MatMul", ["X", "W"], ["Y"])]
        false_nodes = [
            helper.make_node("Slice", ["X"], ["Y"],
                             axes=[0, 1],
                             starts=[0, 0],
                             ends=[2, 2])
        ]
        nodes = self._make_fake_if_op(true_nodes, false_nodes,
                                      [(TensorProto.FLOAT, (2, 2), "Y")])
        X = np.random.rand(2, 3).astype(np.float32)
        W = np.random.rand(3, 2).flatten().astype(np.float32)
        graph_def = helper.make_graph(
            nodes,
            "test", [
                helper.make_tensor_value_info("X", TensorProto.FLOAT, (2, 3)),
                helper.make_tensor_value_info("W", TensorProto.FLOAT, (3, 2))
            ], [helper.make_tensor_value_info("Y", TensorProto.FLOAT, (2, 2))],
            initializer=[
                helper.make_tensor("W", TensorProto.FLOAT, [3, 2], W.tolist())
            ])
        onnx_id = helper.make_opsetid("", 9)
        model_def = helper.make_model(graph_def,
                                      producer_name='onnx-to-caffe2-test',
                                      opset_imports=[onnx_id])

        p = c2.prepare(model_def)
        Y = np.matmul(X, W.reshape(3, 2))
        out = p.run(X)
        np.testing.assert_allclose(out.Y, Y)
def create_model(output_file_name):
    matmul_node = helper.make_node("SparseToDenseMatMul",
                                   inputs=['sparse_A', 'dense_B'],
                                   outputs=['dense_Y'],
                                   name='SpMM',
                                   domain='com.microsoft')

    value_info_A = make_sparse_tensor_value_info('sparse_A', TensorProto.FLOAT,
                                                 [9, 9])
    value_info_B = helper.make_tensor_value_info('dense_B', TensorProto.FLOAT,
                                                 [9, 9])
    value_info_Y = helper.make_tensor_value_info('dense_Y', TensorProto.FLOAT,
                                                 [9, 9])

    graph_def = helper.make_graph(nodes=[matmul_node],
                                  name='SpMM',
                                  inputs=[value_info_A, value_info_B],
                                  outputs=[value_info_Y])

    model_def = helper.make_model(
        graph_def,
        producer_name='dmitrism',
        opset_imports=[make_opsetid('com.microsoft', 1)])

    onnx.save(model_def, output_file_name)
Example #10
0
def main():
    args = get_args()

    opset = tf2onnx.utils.find_opset(args.opset)
    print("using tensorflow={}, onnx={}, opset={}, tfonnx={}/{}".format(
        tf.__version__, onnx.__version__, opset, tf2onnx.__version__,
        tf2onnx.version.git_version[:6]))

    # override unknown dimensions from -1 to 1 (aka batchsize 1) since not every runtime does
    # support unknown dimensions.
    tf2onnx.utils.ONNX_UNKNOWN_DIMENSION = args.unknown_dim

    if args.custom_ops:
        # default custom ops for tensorflow-onnx are in the "tf" namespace
        custom_ops = {
            op: default_custom_op_handler
            for op in args.custom_ops.split(",")
        }
        extra_opset = [helper.make_opsetid(_TENSORFLOW_DOMAIN, 1)]
    else:
        custom_ops = {}
        extra_opset = None

    graph_def = tf.GraphDef()
    with tf.gfile.GFile(args.input, 'rb') as f:
        graph_def.ParseFromString(f.read())

    # todo: consider to enable const folding by default?
    graph_def = tf_optimize(args.inputs, args.outputs, graph_def,
                            args.fold_const)
    with tf.Graph().as_default() as tf_graph:
        tf.import_graph_def(graph_def, name='')
    with tf.Session(graph=tf_graph):
        g = process_tf_graph(tf_graph,
                             continue_on_error=args.continue_on_error,
                             verbose=args.verbose,
                             target=args.target,
                             opset=args.opset,
                             custom_op_handlers=custom_ops,
                             extra_opset=extra_opset,
                             shape_override=args.shape_override,
                             input_names=args.inputs,
                             output_names=args.outputs,
                             inputs_as_nchw=args.inputs_as_nchw)

    new_model_proto = GraphUtil.opt_transposes_with_graph(
        g,
        "converted from {}".format(args.input),
        optimize=not args.continue_on_error)
    if new_model_proto:
        model_proto = new_model_proto
    else:
        print("NON-CRITICAL, optimizers are not applied successfully")

    # write onnx graph
    if args.output:
        with open(args.output, "wb") as f:
            f.write(model_proto.SerializeToString())
            print("\nComplete successfully, the onnx model is generated at " +
                  args.output)
Example #11
0
    def perform_quantization(self, activations, weight, act_sym, wgt_sym):

        # One-layer convolution model
        act = helper.make_tensor_value_info("ACT", TensorProto.FLOAT,
                                            activations[0].shape)
        wgt = helper.make_tensor_value_info("WGT", TensorProto.FLOAT,
                                            weight.shape)
        res = helper.make_tensor_value_info("RES", TensorProto.FLOAT,
                                            [None, None, None, None])
        wgt_init = numpy_helper.from_array(weight, "WGT")
        conv_node = onnx.helper.make_node("Conv", ["ACT", "WGT"], ["RES"])
        graph = helper.make_graph([conv_node],
                                  "test", [act], [res],
                                  initializer=[wgt_init])
        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 11)])
        onnx.save(model, "model.onnx")

        # Quantize model
        class DummyDataReader(quantization.CalibrationDataReader):
            def __init__(self):
                self.iterator = ({"ACT": act} for act in activations)

            def get_next(self):
                return next(self.iterator, None)

        quantization.quantize_static(
            model_input="model.onnx",
            model_output="quantized-model.onnx",
            calibration_data_reader=DummyDataReader(),
            quant_format=quantization.QuantFormat.QOperator,
            activation_type=quantization.QuantType.QInt8,
            weight_type=quantization.QuantType.QInt8,
            op_types_to_quantize=["Conv", "MatMul"],
            extra_options={
                "WeightSymmetric": wgt_sym,
                "ActivationSymmetric": act_sym
            })

        # Extract quantization parameters: scales and zero points for activations, weights, and results
        model = onnx.load("quantized-model.onnx")
        act_zp = [
            init for init in model.graph.initializer
            if init.name == "ACT_zero_point"
        ][0].int32_data[0]
        act_sc = [
            init for init in model.graph.initializer
            if init.name == "ACT_scale"
        ][0].float_data[0]
        wgt_zp = [
            init for init in model.graph.initializer
            if init.name == "WGT_zero_point"
        ][0].int32_data[0]
        wgt_sc = [
            init for init in model.graph.initializer
            if init.name == "WGT_scale"
        ][0].float_data[0]

        # Return quantization parameters
        return act_zp, act_sc, wgt_zp, wgt_sc
Example #12
0
    def construct_model_conv_clip(self, output_model_path, input_shape,
                                  weight_shape, output_shape):
        #    (input)
        #      |
        #     Conv
        #      |
        #     Clip
        #      |
        #    (output)
        input_name = 'input'
        output_name = 'output'
        initializers = []

        # make Conv node
        weight_name = 'conv_weight'
        conv_inputs = [input_name, weight_name]
        conv_outputs = ['conv_output']
        conv_name = 'conv_node'
        conv_weight_data = np.random.normal(0, 0.1,
                                            weight_shape).astype(np.float32)
        initializers.append(
            onnx.numpy_helper.from_array(conv_weight_data, name=weight_name))
        conv_node = onnx.helper.make_node('Conv',
                                          conv_inputs,
                                          conv_outputs,
                                          name=conv_name)

        # make Clip node
        clip_min_name = 'clip_min'
        clip_max_name = 'clip_max'
        clip_inputs = [conv_outputs[0], clip_min_name, clip_max_name]
        clip_outputs = [output_name]
        clip_name = 'clip_node'
        initializers.append(
            onnx.numpy_helper.from_array(np.array(-1.0, dtype=np.float32),
                                         name=clip_min_name))
        initializers.append(
            onnx.numpy_helper.from_array(np.array(1.0, dtype=np.float32),
                                         name=clip_max_name))
        clip_node = onnx.helper.make_node('Clip',
                                          clip_inputs,
                                          clip_outputs,
                                          name=clip_name)

        # make graph
        input_tensor = helper.make_tensor_value_info(input_name,
                                                     TensorProto.FLOAT,
                                                     input_shape)
        output_tensor = helper.make_tensor_value_info(output_name,
                                                      TensorProto.FLOAT,
                                                      output_shape)
        graph_name = 'QDQ_Test_Conv_clip'
        graph = helper.make_graph([conv_node, clip_node],
                                  graph_name, [input_tensor], [output_tensor],
                                  initializer=initializers)
        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 13)])
        model.ir_version = onnx.IR_VERSION

        onnx.save(model, output_model_path)
Example #13
0
    def test_custom_op(self):
        """Custom op test."""
        def print_handler(ctx, node, name, args):  # pylint: disable=unused-argument
            # replace tf.Print() with Identity
            #   T output = Print(T input, data, @list(type) U, @string message, @int first_n, @int summarize)
            # becomes:
            #   T output = Identity(T Input)
            self.assertEqual(node.type, "Identity")
            node.domain = _TENSORFLOW_DOMAIN
            self.assertEqual(args[0], "mode")
            del node.input[1:]
            return node

        with tf.Session() as sess:
            x = tf.placeholder(tf.float32, [2, 3], name="input1")
            x_ = tf.Print(x, [x], "hello")
            _ = tf.identity(x_, name="output")
            g = process_tf_graph(sess.graph,
                                 custom_op_handlers={
                                     "Print":
                                     (print_handler, ["Identity", "mode"])
                                 },
                                 extra_opset=helper.make_opsetid(
                                     _TENSORFLOW_DOMAIN, 1))
            self.assertEqual(
                'digraph { input1 [op_type=Placeholder shape="[2, 3]"] Print [op_type=Identity] '
                'output [op_type=Identity] input1:0 -> Print Print:0 -> output }',
                onnx_to_graphviz(g))
def create_model(output_file_name):
    matmul_node = helper.make_node(
        "SparseToDenseMatMul",
        inputs=["sparse_A", "dense_B"],
        outputs=["dense_Y"],
        name="SpMM",
        domain="com.microsoft",
    )

    value_info_A = make_sparse_tensor_value_info("sparse_A", TensorProto.FLOAT,
                                                 [9, 9])
    value_info_B = helper.make_tensor_value_info("dense_B", TensorProto.FLOAT,
                                                 [9, 9])
    value_info_Y = helper.make_tensor_value_info("dense_Y", TensorProto.FLOAT,
                                                 [9, 9])

    graph_def = helper.make_graph(
        nodes=[matmul_node],
        name="SpMM",
        inputs=[value_info_A, value_info_B],
        outputs=[value_info_Y],
    )

    model_def = helper.make_model(
        graph_def,
        producer_name="dmitrism",
        opset_imports=[make_opsetid("com.microsoft", 1)],
    )

    onnx.save(model_def, output_file_name)
Example #15
0
    def _common(cls, node, **kwargs):
        cond = kwargs["tensor_dict"][node.inputs[0]]
        then_branch = node.attrs["then_branch"]
        else_branch = node.attrs["else_branch"]
        current_opset = [make_opsetid(cls.DOMAIN, cls.VERSION)]

        def true_fn():
            subgraph_tensor_dict = onnx_tf.backend.onnx_graph_to_tensorflow_ops(
                subgraph=then_branch,
                input_values={},  # all inputs of then_branch are in tensor_dict
                tensor_dict=kwargs["tensor_dict"],
                opset=current_opset)
            return [subgraph_tensor_dict[o.name] for o in then_branch.output]

        def false_fn():
            subgraph_tensor_dict = onnx_tf.backend.onnx_graph_to_tensorflow_ops(
                subgraph=else_branch,
                input_values={},  # all inputs of else_branch are in tensor_dict
                tensor_dict=kwargs["tensor_dict"],
                opset=current_opset)
            return [subgraph_tensor_dict[o.name] for o in else_branch.output]

        return [
            cls.make_tensor_from_onnx_node(node,
                                           inputs=[cond, true_fn, false_fn])
        ]
Example #16
0
def gen_cdist_test(output_dir, dtype, M, N, K):
    for mode in ['euclidean', 'sqeuclidean']:
        test_folder = os.path.join(
            output_dir,
            "test_cdist_%s_%s_%d_%d_%d" % (dtype.__name__, mode, M, N, K))
        data_dir = os.path.join(test_folder, "test_data_0")
        os.makedirs(data_dir, exist_ok=True)
        a = np.random.randn(M, K).astype(dtype)
        b = np.random.randn(N, K).astype(dtype)
        type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[a.dtype]
        c = distance.cdist(a, b, mode).astype(dtype)
        node_def = helper.make_node('CDist',
                                    inputs=['A', 'B'],
                                    outputs=['C'],
                                    domain="com.microsoft",
                                    metric=mode)
        graph_def = helper.make_graph([node_def], 'test-model', [
            helper.make_tensor_value_info('A', type, a.shape),
            helper.make_tensor_value_info('B', type, b.shape)
        ], [helper.make_tensor_value_info('C', type, c.shape)])
        model_def = helper.make_model(
            graph_def,
            producer_name='onnx-example',
            opset_imports=[helper.make_opsetid("com.microsoft", 1)])
        onnx.save(model_def, os.path.join(test_folder, 'model.onnx'))
        with open(os.path.join(data_dir, "input_0.pb"), "wb") as f:
            write_tensor(f, a, "A")
        with open(os.path.join(data_dir, "input_1.pb"), "wb") as f:
            write_tensor(f, b, "B")
        with open(os.path.join(data_dir, "output_0.pb"), "wb") as f:
            write_tensor(f, c, "C")
        write_config(test_folder)
Example #17
0
    def construct_model_Constant(self, model_path):
        #    (input)    Constant
        #       \         /
        #        \       /
        #         \     /
        #          \   /
        #           Add
        #            |
        #         (output)

        initializers = []
        input = helper.make_tensor_value_info("input", TensorProto.FLOAT,
                                              [4, 8, 12])
        output = helper.make_tensor_value_info("output", TensorProto.FLOAT,
                                               [4, 8, 12])

        # make nodes
        constant_node = onnx.helper.make_node("Constant", [], ["const_output"],
                                              value_float=42.0)
        add_node = onnx.helper.make_node("Add", ["input", "const_output"],
                                         ["output"],
                                         name="Add")
        graph = helper.make_graph(
            [add_node, constant_node],
            "onnx_model_test",
            [input],
            [output],
            initializer=initializers,
        )
        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 13)])
        onnx.save(model, model_path)
Example #18
0
    def construct_model_conv(self, output_model_path, input_shape, weight_shape, output_shape, has_bias):
        #    (input)
        #      |
        #     Conv
        #      |
        #    (output)
        input_name = 'input'
        output_name = 'output'
        initializers = []

        # make Conv node
        weight_name = 'conv_weight'
        bias_name = 'conv_bias'
        conv_inputs = [input_name, weight_name]
        conv_outputs = [output_name]
        conv_name = 'conv_node'
        conv_weight_data = np.random.normal(0, 0.1, weight_shape).astype(np.float32)
        initializers.append(onnx.numpy_helper.from_array(conv_weight_data, name=weight_name))
        if has_bias:
            conv_inputs.append(bias_name)
            bias_data = np.random.normal(0, 0.05, (weight_shape[0])).astype(np.float32)
            initializers.append(onnx.numpy_helper.from_array(bias_data, name=bias_name))
        conv_node = onnx.helper.make_node('Conv', conv_inputs, conv_outputs, name=conv_name)

        # make graph
        input_tensor = helper.make_tensor_value_info(input_name, TensorProto.FLOAT, input_shape)
        output_tensor = helper.make_tensor_value_info(output_name, TensorProto.FLOAT, output_shape)
        graph_name = 'QDQ_Test_Conv'
        graph = helper.make_graph([conv_node], graph_name,
                                  [input_tensor], [output_tensor], initializer=initializers)
        model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)])
        model.ir_version = 7 # use stable onnx ir version

        onnx.save(model, output_model_path)
Example #19
0
    def _common(cls, node, **kwargs):
        cond = kwargs["tensor_dict"][node.inputs[0]]
        then_branch = node.attrs["then_branch"]
        else_branch = node.attrs["else_branch"]
        current_opset = [make_opsetid(cls.DOMAIN, cls.VERSION)]

        def true_fn():
            subgraph_tensor_dict = onnx_tf.backend.onnx_graph_to_tensorflow_ops(
                subgraph=then_branch,
                input_values={},  # all inputs of then_branch are in tensor_dict
                tensor_dict=kwargs["tensor_dict"],
                opset=current_opset)
            return [subgraph_tensor_dict[o.name] for o in then_branch.output]

        def false_fn():
            subgraph_tensor_dict = onnx_tf.backend.onnx_graph_to_tensorflow_ops(
                subgraph=else_branch,
                input_values={},  # all inputs of else_branch are in tensor_dict
                tensor_dict=kwargs["tensor_dict"],
                opset=current_opset)
            return [subgraph_tensor_dict[o.name] for o in else_branch.output]

        # Set strict=True to make sure singleton lists and tuples return from
        # true_fn and false_fn will not be implicitly unpacked to single values
        strict = True
        return [
            cls.make_tensor_from_onnx_node(
                node, inputs=[cond, true_fn, false_fn, strict])
        ]
Example #20
0
    def construct_model_matmul_reshape(self, output_model_path, input_shape,
                                       weight_shape, output_shape):
        #    (input)
        #      |
        #     MatMul
        #      |
        #    Reshape
        #      |
        #    (output)
        input_name = 'input'
        output_name = 'output'
        initializers = []

        # make MatMul node
        weight_name = 'matmul_weight'
        matmul_output_name = 'matmul_output'
        matmul_inputs = [input_name, weight_name]
        matmul_outputs = [matmul_output_name]
        matmul_name = 'matmul_node'
        matmul_weight_data = np.random.normal(0, 0.1,
                                              weight_shape).astype(np.float32)
        initializers.append(
            onnx.numpy_helper.from_array(matmul_weight_data, name=weight_name))

        matmul_node = onnx.helper.make_node('MatMul',
                                            matmul_inputs,
                                            matmul_outputs,
                                            name=matmul_name)

        # make Reshape node
        reshape_shape = 'reshape_shape'
        reshape_inputs = [matmul_output_name, reshape_shape]
        reshape_output = [output_name]
        reshape_name = 'reshape_node'
        initializers.append(
            onnx.numpy_helper.from_array(np.array(output_shape,
                                                  dtype=np.int64),
                                         name=reshape_shape))
        reshape_node = onnx.helper.make_node('Reshape',
                                             reshape_inputs,
                                             reshape_output,
                                             name=reshape_name)

        # make graph
        input_tensor = helper.make_tensor_value_info(input_name,
                                                     TensorProto.FLOAT,
                                                     input_shape)
        output_tensor = helper.make_tensor_value_info(output_name,
                                                      TensorProto.FLOAT,
                                                      output_shape)
        graph_name = 'Reshape_Quant_Test'
        graph = helper.make_graph([matmul_node, reshape_node],
                                  graph_name, [input_tensor], [output_tensor],
                                  initializer=initializers)
        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 11)])
        model.ir_version = onnx.IR_VERSION

        onnx.save(model, output_model_path)
    def test_augment_graph_with_zero_value_dimension(self):
        '''TEST_CONFIG_5'''
        #   Conv
        #    |
        #   Conv
        #    |
        #  Resize

        G = helper.make_tensor_value_info('G', TensorProto.FLOAT, [1, 1, 5, 5])
        H = helper.make_tensor_value_info('H', TensorProto.FLOAT, [1, 1, 3, 3])
        J = helper.make_tensor_value_info('J', TensorProto.FLOAT, [1, 1, 3, 3])
        M = helper.make_tensor_value_info('M', TensorProto.FLOAT, [0])
        N = helper.make_tensor_value_info('N', TensorProto.FLOAT, [0])
        O = helper.make_tensor_value_info('O', TensorProto.FLOAT, [1, 1, 5, 5])
        # O = helper.make_tensor_value_info('O', TensorProto.FLOAT, None)
        conv_node_1 = onnx.helper.make_node('Conv', ['G', 'H'], ['I'],
                                            name='Conv1',
                                            kernel_shape=[3, 3],
                                            pads=[1, 1, 1, 1])
        conv_node_2 = onnx.helper.make_node('Conv', ['I', 'J'], ['K'],
                                            name='Conv2',
                                            kernel_shape=[3, 3],
                                            pads=[1, 1, 1, 1])
        resize_node_1 = onnx.helper.make_node('Resize', ['K', 'M', 'N'], ['O'],
                                              name='Reize1')
        graph = helper.make_graph([conv_node_1, conv_node_2, resize_node_1],
                                  'test_graph_5', [G, H, J, M, N], [O])
        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 13)])
        test_model_path = './test_model_5.onnx'
        onnx.save(model, test_model_path)

        augmented_model_path = './augmented_test_model_5.onnx'
        calibrater = MinMaxCalibrater(test_model_path, [],
                                      augmented_model_path)
        augmented_model = calibrater.get_augment_model()

        augmented_model_node_names = [
            node.name for node in augmented_model.graph.node
        ]
        augmented_model_outputs = [
            output.name for output in augmented_model.graph.output
        ]
        added_node_names = [
            'I_ReduceMin', 'I_ReduceMax', 'K_ReduceMin', 'K_ReduceMax',
            'O_ReduceMin', 'O_ReduceMax'
        ]
        added_outputs = [
            'I_ReduceMin', 'I_ReduceMax', 'K_ReduceMin', 'K_ReduceMax',
            'O_ReduceMin', 'O_ReduceMax'
        ]
        # Original 3 nodes + added ReduceMin/Max nodes * 8
        self.assertEqual(len(augmented_model_node_names), 19)
        # Original 1 graph output + added outputs * 8
        self.assertEqual(len(augmented_model_outputs), 17)
        for name in added_node_names:
            self.assertTrue(name in augmented_model_node_names)
        for output in added_outputs:
            self.assertTrue(output in augmented_model_outputs)
Example #22
0
 def reset_model(self, opset_ver=None):
   if opset_ver is not None:
     opset_imports = [make_opsetid("", opset_ver)]
   else:
     opset_imports = [self.opset_import]
   self.model = make_model_gen_version(onnx.GraphProto(),
                                       opset_imports=opset_imports)
   self.op_counter = collections.Counter()
Example #23
0
 def __init__(self, opset_ver=OPSET_VER):
   self.opset_import = make_opsetid("", opset_ver)
   self.model = make_model_gen_version(onnx.GraphProto(),
                                       opset_imports=[self.opset_import])
   self.op_counter = collections.Counter()
   self.ctx = C.CheckerContext()
   self.ctx.ir_version = self.model.ir_version
   self.ctx.opset_imports = {'': opset_ver}
Example #24
0
    def test_augment_graph_config_2(self):
        """TEST_CONFIG_2"""
        #   Conv
        #    |
        #   Conv

        G = helper.make_tensor_value_info("G", TensorProto.FLOAT, [1, 1, 5, 5])
        H = helper.make_tensor_value_info("H", TensorProto.FLOAT, [1, 1, 3, 3])
        J = helper.make_tensor_value_info("J", TensorProto.FLOAT, [1, 1, 3, 3])
        K = helper.make_tensor_value_info("K", TensorProto.FLOAT, [1, 1, 5, 5])
        conv_node_1 = onnx.helper.make_node(
            "Conv",
            ["G", "H"],
            ["I"],
            name="Conv1",
            kernel_shape=[3, 3],
            pads=[1, 1, 1, 1],
        )
        conv_node_2 = onnx.helper.make_node(
            "Conv",
            ["I", "J"],
            ["K"],
            name="Conv2",
            kernel_shape=[3, 3],
            pads=[1, 1, 1, 1],
        )
        graph = helper.make_graph([conv_node_1, conv_node_2], "test_graph_2",
                                  [G, H, J], [K])
        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 13)])
        test_model_path = "./test_model_2.onnx"
        onnx.save(model, test_model_path)

        augmented_model_path = "./augmented_test_model_2.onnx"
        calibrater = MinMaxCalibrater(test_model_path, ["Conv", "MatMul"],
                                      augmented_model_path)
        augmented_model = calibrater.get_augment_model()

        augmented_model_node_names = [
            node.name for node in augmented_model.graph.node
        ]
        augmented_model_outputs = [
            output.name for output in augmented_model.graph.output
        ]
        added_node_names = [
            "I_ReduceMin", "I_ReduceMax", "K_ReduceMin", "K_ReduceMax"
        ]
        added_outputs = [
            "I_ReduceMin", "I_ReduceMax", "K_ReduceMin", "K_ReduceMax"
        ]
        # Original 2 nodes + added ReduceMin/Max nodes * 4
        self.assertEqual(len(augmented_model_node_names), 12)
        # Original 1 graph output + added outputs * 4
        self.assertEqual(len(augmented_model_outputs), 11)
        for name in added_node_names:
            self.assertTrue(name in augmented_model_node_names)
        for output in added_outputs:
            self.assertTrue(output in augmented_model_outputs)
    def test_augment_graph_config_1(self):
        ''' TEST_CONFIG_1'''

        #     Conv
        #      |
        #     Clip
        #      |
        #     MatMul

        A = helper.make_tensor_value_info('A', TensorProto.FLOAT, [1, 1, 5, 5])
        B = helper.make_tensor_value_info('B', TensorProto.FLOAT, [1, 1, 3, 3])
        E = helper.make_tensor_value_info('E', TensorProto.FLOAT, [1, 1, 5, 1])
        F = helper.make_tensor_value_info('F', TensorProto.FLOAT, [1, 1, 5, 1])
        conv_node = onnx.helper.make_node('Conv', ['A', 'B'], ['C'],
                                          name='Conv',
                                          kernel_shape=[3, 3],
                                          pads=[1, 1, 1, 1])
        clip_node = onnx.helper.make_node('Clip', ['C'], ['D'], name='Clip')
        matmul_node = onnx.helper.make_node('MatMul', ['D', 'E'], ['F'],
                                            name='MatMul')
        graph = helper.make_graph([conv_node, clip_node, matmul_node],
                                  'test_graph_1', [A, B, E], [F])

        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 13)])
        test_model_path = './test_model_1.onnx'
        onnx.save(model, test_model_path)

        # Augmenting graph
        augmented_model_path = './augmented_test_model_1.onnx'
        calibrater = MinMaxCalibrater(test_model_path, ['Conv', 'MatMul'],
                                      augmented_model_path)
        augmented_model = calibrater.get_augment_model()

        # Checking if each added ReduceMin and ReduceMax node and its output exists
        augmented_model_node_names = [
            node.name for node in augmented_model.graph.node
        ]
        augmented_model_outputs = [
            output.name for output in augmented_model.graph.output
        ]
        added_node_names = [
            'C_ReduceMin', 'C_ReduceMax', 'D_ReduceMin', 'D_ReduceMax',
            'F_ReduceMin', 'F_ReduceMax'
        ]
        added_outputs = [
            'C_ReduceMin', 'C_ReduceMax', 'D_ReduceMin', 'D_ReduceMax',
            'F_ReduceMin', 'F_ReduceMax'
        ]
        # Original 3 nodes + added ReduceMin/Max nodes
        self.assertEqual(len(augmented_model_node_names), 15)
        # Original 1 graph output + added outputs * 6
        self.assertEqual(len(augmented_model_outputs), 13)
        for name in added_node_names:
            self.assertTrue(name in augmented_model_node_names)
        for output in added_outputs:
            self.assertTrue(output in augmented_model_outputs)
Example #26
0
    def create_net(self, shape, ratio, ir_version, opset=None):
        """
            ONNX net                                IR net

            Input->Dropout->Sigmoid->Output   =>    Input->sigmoid

        """

        #
        #   Create ONNX model
        #

        from onnx import helper
        from onnx import TensorProto

        input = helper.make_tensor_value_info('input', TensorProto.FLOAT, shape)
        output = helper.make_tensor_value_info('output', TensorProto.FLOAT, shape)

        args = dict()
        if ratio:
            args['ratio'] = ratio
        if opset == 6:
            args['is_test'] = 1
        node_def = helper.make_node(
            'Dropout',
            inputs=['input'],
            outputs=['dropout'],
            **args
        )

        sigmoid_def = helper.make_node(
            'Sigmoid',
            inputs=['dropout'],
            outputs=['output']
        )

        # Create the graph (GraphProto)
        graph_def = helper.make_graph(
            [node_def, sigmoid_def],
            'test_model',
            [input],
            [output],
        )

        # Create the model (ModelProto)
        args = dict(producer_name='test_model')
        if opset:
            args['opset_imports'] = [helper.make_opsetid("", opset)]
        onnx_net = helper.make_model(graph_def, **args)

        #
        #   Create reference IR net
        #

        ref_net = None

        return onnx_net, ref_net
Example #27
0
    def create_flatten_net(self,
                           axis,
                           input_shape,
                           dim,
                           ir_version,
                           opset=None):
        """
            ONNX net                       IR net

            Input->Flatten->Output   =>    Input->Reshape

        """

        #
        #   Create ONNX model
        #

        # TODO: possible move all imports to separate func?
        import onnx
        from onnx import helper
        from onnx import TensorProto

        input = helper.make_tensor_value_info('input', TensorProto.FLOAT,
                                              input_shape)
        output = helper.make_tensor_value_info('output', TensorProto.FLOAT,
                                               dim)

        node_flatten_def = onnx.helper.make_node(
            'Flatten',
            inputs=['input'],
            outputs=['output'],
            axis=axis,
        )

        # Create the graph (GraphProto)
        graph_def = helper.make_graph(
            [node_flatten_def],
            'test_flatten_model',
            [input],
            [output],
        )

        # Create the model (ModelProto)
        args = dict(producer_name='test_model')
        if opset:
            args['opset_imports'] = [helper.make_opsetid("", opset)]
        onnx_net = helper.make_model(graph_def, **args)

        #
        #   Create reference IR net
        #   Please, spesify 'type': 'Input' for inpit node
        #   Moreover, do not forget to validate ALL layer attributes!!!
        #

        ref_net = None

        return onnx_net, ref_net
Example #28
0
  def _get_handlers(cls, opset):
    """ Get all backend handlers with opset.

    :param opset: ONNX OperatorSetIdProto list.
    :return: All backend handlers.
    """
    opset = opset or [make_opsetid(defs.ONNX_DOMAIN, defs.onnx_opset_version())]
    opset_dict = dict([(o.domain, o.version) for o in opset])
    return get_all_backend_handlers(opset_dict)
    def construct_model_conv_squeezes(self, output_model_path,
                                     conv_input_shape, conv_weight_shape, conv_output_shape,
                                     opset = 13):
        #             (input)
        #            /   |     \
        #         Conv1 conv2    conv3
        #           |     |         |
        #       Squeeze1 Squeeze2   |
        #           \      /        |
        #             add1          |
        #               |           |
        #              Unsqueeze    |
        #                      \    |
        #                       add2
        #                         |
        #                      (output)
        input_tensor = helper.make_tensor_value_info('input', TensorProto.FLOAT, conv_input_shape)

        conv1_weight_arr = np.random.randint(-1, 2, conv_weight_shape).astype(np.float32)
        conv1_weight_initializer = onnx.numpy_helper.from_array(conv1_weight_arr, name='conv1_weight')
        conv1_node = onnx.helper.make_node('Conv', ['input', 'conv1_weight'], ['conv1_output'], name='conv1_node')

        conv2_weight_arr = np.random.randint(-1, 2, conv_weight_shape).astype(np.float32)
        conv2_weight_initializer = onnx.numpy_helper.from_array(conv2_weight_arr, name='conv2_weight')
        conv2_node = onnx.helper.make_node('Conv', ['input', 'conv2_weight'], ['conv2_output'], name='conv2_node')

        conv3_weight_arr = np.random.randint(-1, 2, conv_weight_shape).astype(np.float32)
        conv3_weight_initializer = onnx.numpy_helper.from_array(conv3_weight_arr, name='conv3_weight')
        conv3_node = onnx.helper.make_node('Conv', ['input', 'conv3_weight'], ['conv3_output'], name='conv3_node')


        if (opset >= 13):
            squeeze_axes_initializer = onnx.numpy_helper.from_array(np.array([0], dtype=np.int64), name='squeeze_axes')
            squeeze1_node = helper.make_node('Squeeze', ['conv1_output', 'squeeze_axes'], ['squeeze1_output'], name='suqeeze1_node')
            squeeze2_node = helper.make_node('Squeeze', ['conv2_output', 'squeeze_axes'], ['squeeze2_output'], name='suqeeze2_node')
        else:
            squeeze1_node = helper.make_node('Squeeze', ['conv1_output'], ['squeeze1_output'], name='suqeeze1_node', axes=[0])
            squeeze2_node = helper.make_node('Squeeze', ['conv2_output'], ['squeeze2_output'], name='suqeeze2_node', axes=[0])

        add1_node = helper.make_node('Add', ['squeeze1_output', 'squeeze2_output'], ['add1_output'], name='add1_node')
        if (opset >= 13):
            unsqueeze_node = helper.make_node('Unsqueeze', ['add1_output', 'squeeze_axes'], ['unsqueeze_output'], name = 'unsqueeze_node')
        else:
            unsqueeze_node = helper.make_node('Unsqueeze', ['add1_output'], ['unsqueeze_output'], name = 'unsqueeze_node', axes=[0])

        output_tensor = helper.make_tensor_value_info('output', TensorProto.FLOAT, conv_output_shape)
        add2_node = helper.make_node('Add', ['unsqueeze_output', 'conv3_output'], ['output'], name='add2_node')

        initializers = [conv1_weight_initializer, conv2_weight_initializer, conv3_weight_initializer]
        if (opset >= 13):
            initializers.append(squeeze_axes_initializer)
        graph = helper.make_graph([conv1_node, conv2_node, conv3_node, squeeze1_node, squeeze2_node, add1_node, unsqueeze_node, add2_node],
                                  'TestOpSuqeezes_test_model', [input_tensor], [output_tensor], initializer=initializers)
        model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", opset)])
        model.ir_version = onnx.IR_VERSION
        onnx.save(model, output_model_path)
Example #30
0
    def construct_model_matmul_transpose(self, output_model_path, input_shape,
                                         weight_shape, output_shape):
        #    (input)
        #      |
        #     MatMul
        #      |
        #    Transpose
        #      |
        #    (output)
        input_name = "input"
        output_name = "output"
        initializers = []

        # make MatMul node
        weight_name = "matmul_weight"
        matmul_output_name = "matmul_output"
        matmul_inputs = [input_name, weight_name]
        matmul_outputs = [matmul_output_name]
        matmul_name = "matmul_node"
        matmul_weight_data = np.random.normal(0, 0.1,
                                              weight_shape).astype(np.float32)
        initializers.append(
            onnx.numpy_helper.from_array(matmul_weight_data, name=weight_name))
        matmul_node = onnx.helper.make_node("MatMul",
                                            matmul_inputs,
                                            matmul_outputs,
                                            name=matmul_name)

        # make Transpose node
        kwargs = {"perm": (1, 0)}
        transpose_node = onnx.helper.make_node("Transpose",
                                               [matmul_output_name],
                                               [output_name],
                                               name="transpose_node",
                                               **kwargs)

        # make graph
        input_tensor = helper.make_tensor_value_info(input_name,
                                                     TensorProto.FLOAT,
                                                     input_shape)
        output_tensor = helper.make_tensor_value_info(output_name,
                                                      TensorProto.FLOAT,
                                                      output_shape)
        graph_name = "Transpose_Quant_Test"
        graph = helper.make_graph(
            [matmul_node, transpose_node],
            graph_name,
            [input_tensor],
            [output_tensor],
            initializer=initializers,
        )
        model = helper.make_model(graph,
                                  opset_imports=[helper.make_opsetid("", 11)])
        model.ir_version = 7  # use stable onnx ir version

        onnx.save(model, output_model_path)
Example #31
0
    def test_check_old_model(self):  # type: () -> None
        node = helper.make_node(
            "Pad", ["X"], ["Y"], paddings=(0, 0, 0, 0))
        graph = helper.make_graph(
            [node],
            "test",
            [helper.make_tensor_value_info("X", TensorProto.FLOAT, [1, 2])],
            [helper.make_tensor_value_info("Y", TensorProto.FLOAT, [1, 2])])
        onnx_id = helper.make_opsetid("", 1)
        model = helper.make_model(graph, producer_name='test', opset_imports=[onnx_id])

        checker.check_model(model)