Exemplo n.º 1
0
        def loop_test(data):
            ox = data.ox
            shape = ox.shape(data)
            dim_0 = ox.gather([shape, ox.constant(value=0)], axis=0)
            dim_1 = ox.gather(
                [shape,
                 ox.constant(value=np.array([1], dtype=np.int64))],
                axis=0)
            zeros = ox.constant_of_shape(dim_1, value=0.0)
            is_true = ox.constant(value=True)

            @onnx_function(outputs=['c_o', 'total_o', 'scan_o'],
                           output_types=[_Ty.b,
                                         _Ty.F([None]),
                                         _Ty.F([None])],
                           input_types=[_Ty.I([1]), _Ty.b,
                                        _Ty.F([None])])
            def range_body(iter_n, cond, total):
                ox = iter_n.ox
                iter_scalar = ox.squeeze(iter_n, axes=[0])
                col = ox.gather([data, iter_scalar], axis=0)
                total = ox.add([total, col])
                return (is_true, total, total)

            final_total, scan_res = ox.loop(
                dim_0,
                is_true,
                range_body,
                inputs=[zeros],
                outputs=['final_total', 'scan_res'])
            return final_total, scan_res
Exemplo n.º 2
0
    def test_float16_with_loop(self):
        @onnx_function(outputs=['y1', 'y2'],
                       input_types=[_Ty.F([None, None])],
                       output_types=[_Ty.F([None]),
                                     _Ty.F([None, None])])
        def loop_test(data):
            ox = data.ox
            shape = ox.shape(data)
            dim_0 = ox.gather([shape, ox.constant(value=0)], axis=0)
            dim_1 = ox.gather(
                [shape,
                 ox.constant(value=np.array([1], dtype=np.int64))],
                axis=0)
            zeros = ox.constant_of_shape(dim_1, value=0.0)
            is_true = ox.constant(value=True)

            @onnx_function(outputs=['c_o', 'total_o', 'scan_o'],
                           output_types=[_Ty.b,
                                         _Ty.F([None]),
                                         _Ty.F([None])],
                           input_types=[_Ty.I([1]), _Ty.b,
                                        _Ty.F([None])])
            def range_body(iter_n, cond, total):
                ox = iter_n.ox
                iter_scalar = ox.squeeze(iter_n, axes=[0])
                col = ox.gather([data, iter_scalar], axis=0)
                total = ox.add([total, col])
                return (is_true, total, total)

            final_total, scan_res = ox.loop(
                dim_0,
                is_true,
                range_body,
                inputs=[zeros],
                outputs=['final_total', 'scan_res'])
            return final_total, scan_res

        m1 = np.array([[2, 3], [4, 5], [6, 7]], dtype=np.float32)
        expected_res = loop_test(m1)

        model = loop_test.to_model()
        f16model = convert_float_to_float16(copy.deepcopy(model))
        actual_res = _ort_inference(f16model, {'data': m1.astype(np.float16)})
        for expected, actual in zip(expected_res, actual_res):
            self.assertTrue(np.allclose(expected, actual))
            self.assertTrue(actual.dtype == np.float16)

        f16model2 = convert_float_to_float16(copy.deepcopy(model),
                                             keep_io_types=True)
        actual_res2 = _ort_inference(f16model2, {'data': m1})
        for expected, actual2 in zip(expected_res, actual_res2):
            self.assertTrue(np.allclose(expected, actual2))
            self.assertTrue(actual2.dtype == np.float32)
Exemplo n.º 3
0
    def test_float16(self):
        @onnx_function(outputs=['z'],
                       input_types=(_Ty.F([1, 1, 6, 1])),
                       output_types=[_Ty.f])
        def transpose_n_matmul(x):
            ox = x.ox  # type: OnnxOperatorBuilderX
            wm = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
                           12]).astype(np.float32).reshape([2, 6])
            b = ox.constant(value=wm)
            a = ox.transpose(x, perm=[0, 1, 3, 2])
            c = ox.transpose(b, perm=[1, 0])
            return ox.matmul([a, c])

        m1 = np.array([[2, 3], [4, 5],
                       [6, 7]]).astype(np.float32).reshape([1, 1, 6, 1])
        expected = transpose_n_matmul(m1)
        model = transpose_n_matmul.to_model()
        f16model = convert_float_to_float16(copy.deepcopy(model))
        actual = _ort_inference(f16model, {'x': m1.astype(np.float16)})
        self.assertTrue(np.allclose(expected, actual))

        f16model2 = convert_float_to_float16(copy.deepcopy(model),
                                             keep_io_types=True)
        actual2 = _ort_inference(f16model2, {'x': m1})
        self.assertTrue(np.allclose(expected, actual2))
Exemplo n.º 4
0
        def loop_test(len):
            ox = len.ox
            s_len = ox.squeeze(len, axes=[0])
            is_true = ox.constant(value=True)

            @onnx_function(outputs=['c_o', 'i_o', 'j_o', 'all_i', 'all_j'],
                           output_types=[_Ty.b, _Ty.f, _Ty.f, _Ty.f, _Ty.f],
                           input_types=[_Ty.I([1]), _Ty.b, _Ty.F([1]), _Ty.F([1])])
            def range_body(iter_n, cond, i, j):
                return (is_true,
                        i + i.ox.constant(value=1.0), j + 2.0, i, j)

            one_c = ox.constant(value=-1.0)
            y1, y2, y3, y4 = ox.loop(s_len, is_true, range_body, inputs=[one_c, one_c],
                                     outputs=['y1_o', 'y2_o', 'y3_o', 'y4_o'])
            return y1, y2, y3, y4
Exemplo n.º 5
0
    def test_loop(self):
        @onnx_function(outputs=['y1', 'y2', 'y3', 'y4'],
                       input_types=[_Ty.I([1])],
                       output_types=[_Ty.F([None]), _Ty.F([None]), _Ty.F([None, 1]), _Ty.F([None, 1])])
        def loop_test(len):
            ox = len.ox
            s_len = ox.squeeze(len, axes=[0])
            is_true = ox.constant(value=True)

            @onnx_function(outputs=['c_o', 'i_o', 'j_o', 'all_i', 'all_j'],
                           output_types=[_Ty.b, _Ty.f, _Ty.f, _Ty.f, _Ty.f],
                           input_types=[_Ty.I([1]), _Ty.b, _Ty.F([1]), _Ty.F([1])])
            def range_body(iter_n, cond, i, j):
                return (is_true,
                        i + i.ox.constant(value=1.0), j + 2.0, i, j)

            one_c = ox.constant(value=-1.0)
            y1, y2, y3, y4 = ox.loop(s_len, is_true, range_body, inputs=[one_c, one_c],
                                     outputs=['y1_o', 'y2_o', 'y3_o', 'y4_o'])
            return y1, y2, y3, y4

        self.assertEqual(
            loop_test(np.array([16], dtype=np.int64))[2][4], 3.0)
    def test_matmul_opt(self):
        @onnx_function(outputs=['z'],
                       input_types=(_Ty.F([1, 1, 6, 1])),
                       output_types=[_Ty.f])
        def transpose_n_matmul(x):
            ox = x.ox  # type: OnnxOperatorBuilderX
            wm = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]).astype(np.float32).reshape([2, 6])
            b = ox.constant(value=wm)
            a = ox.transpose(x, perm=[0, 1, 3, 2])
            c = ox.transpose(b, perm=[1, 0])
            return ox.matmul([a, c])

        m1 = np.array([[2, 3], [4, 5], [6, 7]]).astype(np.float32).reshape([1, 1, 6, 1])
        expected = transpose_n_matmul(m1)
        opted = optimize_onnx_model(transpose_n_matmul.to_model())
        actual = _ort_inference(opted, {'x': m1})
        self.assertTrue(np.allclose(expected, actual), "The result mismatch")
                       cast_batch, op_version=operator.target_opset, axes=[0])
    apply_cast(scope, cast_batch, operator.output_full_names[2], container, to=onnx_proto.TensorProto.INT32)

    apply_identity(scope, box_batch, operator.output_full_names[0], container)
    apply_identity(scope, score_batch, operator.output_full_names[1], container)


set_converter(YOLONMSLayer, convert_NMSLayer)

yolo_model_graph_tiny = None
evaluation_model_graph_tiny = None
nms_model_graph_tiny = None
num_classes = 20

@Graph.trace(
    input_types=[_Ty.F(shape=['N', 3, 'M1', 'M2']), _Ty.F(shape=['N', 2])],
    output_types=[_Ty.F(shape=[1, 'M1', 4]), _Ty.F(shape=[1, num_classes, 'M2']), _Ty.I32(shape=[1, 'M3', 3])],
    outputs=["yolonms_layer_1", "yolonms_layer_1_1", "yolonms_layer_1_2"])
def combine_model_tiny(input_1, image_shape):
    global yolo_model_graph_tiny
    global evaluation_model_graph_tiny
    global nms_model_graph_tiny
    output_1 = yolo_model_graph_tiny(input_1)
    input_2 = output_1 + (image_shape,)
    yolo_evaluation_layer_1, yolo_evaluation_layer_2 = evaluation_model_graph_tiny(*input_2)
    nms_layer_1_1, nms_layer_1_2, nms_layer_1_3 = nms_model_graph_tiny(yolo_evaluation_layer_1, yolo_evaluation_layer_2)
    return nms_layer_1_1, nms_layer_1_2, nms_layer_1_3


yolo_model_graph = None
evaluation_model_graph = None
Exemplo n.º 8
0
               to=onnx_proto.TensorProto.INT32)

    apply_identity(scope, box_batch, operator.output_full_names[0], container)
    apply_identity(scope, score_batch, operator.output_full_names[1],
                   container)


set_converter(YOLONMSLayer, convert_NMSLayer)

yolo_model_graph_tiny = None
evaluation_model_graph_tiny = None
nms_model_graph_tiny = None


@Graph.trace(
    input_types=[_Ty.F(shape=['N', 3, 'M1', 'M2']),
                 _Ty.F(shape=['N', 2])],
    output_types=[
        _Ty.F(shape=[1, 'M1', 4]),
        _Ty.F(shape=[1, 80, 'M2']),
        _Ty.I32(shape=[1, 'M3', 3])
    ],
    outputs=["yolonms_layer_1", "yolonms_layer_1_1", "yolonms_layer_1_2"])
def combine_model_tiny(input_1, image_shape):
    global yolo_model_graph_tiny
    global evaluation_model_graph_tiny
    global nms_model_graph_tiny
    output_1 = yolo_model_graph_tiny(input_1)
    input_2 = output_1 + (image_shape, )
    yolo_evaluation_layer_1, yolo_evaluation_layer_2 = evaluation_model_graph_tiny(
        *input_2)