Example #1
0
    def test_slice_onnx_10_opset_case(self):
        # check for negative end value in the case of ONNX 10 opset
        input = np.array([[4, 5, 6, 7], [2, 3, 5, 6], [5, 6, 8, 9],
                          [5, 6, 8, 9]])
        starts = np.array([0, 1])
        ends = np.array([3, -2])
        expected_values = np.array([[5], [3], [6]])

        graph = build_graph(
            nodes_attributes, [('data_1', 'slice'), ('starts', 'slice'),
                               ('ends', 'slice'), ('slice', 'data_2')], {
                                   'data_1': {
                                       'value': input,
                                       'shape': input.shape
                                   },
                                   'starts': {
                                       'value': starts,
                                       'shape': starts.shape
                                   },
                                   'ends': {
                                       'value': ends,
                                       'shape': ends.shape
                                   },
                                   'slice': {
                                       'format': 'onnx'
                                   }
                               })

        slice_node = Node(graph, 'slice')

        Slice.infer(slice_node)
        self.assertTrue(
            np.array_equal(slice_node.out_node().value, expected_values))
Example #2
0
        def test_slice_infer(self, inp_value, starts, ends, axes, steps, expected, inp_shape=None):
            if inp_value is None:
                input_node = shaped_data('data_1', int64_array(inp_shape))
            else:
                input_node = valued_data('data_1', int64_array(inp_value))

            nodes = {
                **input_node,
                **regular_op_with_empty_data('slice', {'op': 'Slice'}),
                **valued_const_with_data('starts', int64_array(starts)),
                **valued_const_with_data('ends', int64_array(ends)),
                **valued_const_with_data('axes', int64_array(axes)),
                **valued_const_with_data('steps', int64_array(steps)),
            }

            graph = build_graph(nodes,
                                [('data_1', 'slice'),
                                 *connect('starts', '1:slice'),
                                 *connect('ends', '2:slice'),
                                 *connect('axes', '3:slice'),
                                 *connect('steps', '4:slice'),
                                 *connect('slice', 'slice_d')])

            graph.stage = 'middle'
            slice_node = Node(graph, 'slice')

            Slice.infer(slice_node)
            if inp_value is not None:
                self.assertTrue(np.array_equal(slice_node.out_node().value, expected))
            else:
                self.assertTrue(np.array_equal(slice_node.out_node().shape, expected))
Example #3
0
    def test_slice_infer_multiply_params(self):
        # Test case when size[i] == -1 (that means all
        # remaining elements in dimension i are included in the slice)
        graph = build_graph(
            nodes_attributes, [('data_1', 'slice'), ('begin', 'slice'),
                               ('size', 'slice'), ('slice', 'data_2')], {
                                   'data_1': {
                                       'shape': np.array([4, 5, 6])
                                   },
                                   'slice': {
                                       'start': np.array([1, 2]),
                                       'end': np.array([4, 1])
                                   },
                                   'size': {
                                       'value': np.array([3, -1])
                                   },
                                   'begin': {
                                       'value': np.array([1, 2])
                                   }
                               })

        slice_node = Node(graph, 'slice')

        Slice.infer(slice_node)
        self.assertTrue(np.array_equal(slice_node.out_node().value, None))
        self.assertTrue(
            np.array_equal(slice_node.out_node().shape, np.array([3, 3, 6])))
        self.assertTrue(
            np.array_equal(
                slice_node['slices'],
                np.array([slice(1, 4, 1),
                          slice(2, 5, 1),
                          slice(0, 6, 1)])))
Example #4
0
 def extract(node: Node):
     Slice.update_node_stat(node, {
         'axis': None,
         'start': None,
         'end': None,
     })
     return __class__.enabled
Example #5
0
    def test_slice_infer_constant(self):
        # Testing constant path case
        graph = build_graph(
            nodes_attributes, [('data_1', 'slice'), ('begin', 'slice'),
                               ('size', 'slice'), ('slice', 'data_2')], {
                                   'data_1': {
                                       'shape': np.array([4]),
                                       'value': np.array([1, 3, 224, 224])
                                   },
                                   'slice': {
                                       'start': np.array([1]),
                                       'end': np.array([2])
                                   },
                                   'size': {
                                       'value': np.array([1])
                                   },
                                   'begin': {
                                       'value': np.array([1])
                                   }
                               })

        slice_node = Node(graph, 'slice')
        Slice.infer(slice_node)

        self.assertTrue(
            np.array_equal(slice_node.out_node().value, np.array([3])))
        self.assertTrue(
            np.array_equal(slice_node.out_node().shape, np.array([1])))
        self.assertTrue(
            np.array_equal(slice_node['slices'], np.array([slice(1, 2, 1)])))
Example #6
0
    def test_slice_infer_non_constant(self):
        # Testing non-constant path case (when value in input is None)
        # with multiply params
        graph = build_graph(
            nodes_attributes, [('data_1', 'slice'), ('begin', 'slice'),
                               ('size', 'slice'), ('slice', 'data_2')], {
                                   'data_1': {
                                       'shape': np.array([4, 5, 6])
                                   },
                                   'slice': {
                                       'start': np.array([1, 2]),
                                       'end': np.array([4, 3])
                                   },
                                   'size': {
                                       'value': np.array([3, 1])
                                   },
                                   'begin': {
                                       'value': np.array([1, 2])
                                   }
                               })

        slice_node = Node(graph, 'slice')

        Slice.infer(slice_node)
        self.assertTrue(np.array_equal(slice_node.out_node().value, None))
        self.assertTrue(
            np.array_equal(slice_node.out_node().shape, np.array([3, 1, 6])))
        self.assertTrue(
            np.array_equal(
                slice_node['slices'],
                np.array([slice(1, 4, 1),
                          slice(2, 3, 1),
                          slice(0, 6, 1)])))
Example #7
0
 def extract(cls, node: Node):
     Slice.update_node_stat(node, {
         'axis': None,
         'start': None,
         'end': None,
     })
     return cls.enabled
Example #8
0
    def test_no_steps_no_axes(self):
        input_shape = int64_array([5, 10, 20])
        starts_value = int64_array([3, 2, 7])
        ends_value = int64_array([5, 8, 15])
        steps_value = int64_array([1, 1, 1])
        masks_value = np.zeros([len(input_shape)], dtype=np.int64)
        graph = build_graph(self.nodes_attributes,
                            [('placeholder_1', 'placeholder_1_data'),
                             ('placeholder_1_data', 'slice', {'in': 0}),
                             ('starts', 'starts_data'),
                             ('starts_data', 'slice', {'in': 1}),
                             ('ends', 'ends_data'),
                             ('ends_data', 'slice', {'in': 2}),
                             ('slice', 'slice_data'),
                             ('slice_data', 'output_op'),
                             ('output_op', 'output_data'),
                             ('output_data', 'op_output')
                             ],
                            {'placeholder_1_data': {'shape': input_shape},
                             'starts': {'shape': starts_value.shape, 'value': starts_value},
                             'starts_data': {'shape': starts_value.shape, 'value': starts_value},
                             'ends': {'shape': ends_value.shape, 'value': ends_value},
                             'ends_data': {'shape': ends_value.shape, 'value': ends_value},
                             }, nodes_with_edges_only=True
                            )
        slice_node = Node(graph, 'slice')
        Slice.infer(slice_node)

        pattern = ConvertSlice()
        pattern.find_and_replace_pattern(graph)

        ss_node = Node(graph, graph.get_node_id_by_name('slice_node'))
        assert ss_node.type == 'StridedSlice', 'Something wrong with transformed Slice node'

        graph_ref = build_graph(self.nodes_attributes,
                                [('placeholder_1', 'placeholder_1_data'),
                                 ('placeholder_1_data', 'strided_slice', {'in': 0}),
                                 ('starts', 'starts_data'),
                                 ('starts_data', 'strided_slice', {'in': 1}),
                                 ('ends', 'ends_data'),
                                 ('ends_data', 'strided_slice', {'in': 2}),
                                 ('strides', 'strides_data'),
                                 ('strides_data', 'strided_slice', {'in': 3}),
                                 ('strided_slice', 'slice_data'),
                                 ('slice_data', 'output_op'),
                                 ('output_op', 'output_data'),
                                 ('output_data', 'op_output')
                                 ],
                                {'placeholder_1_data': {'shape': input_shape},
                                 'strided_slice': {'new_axis_mask': masks_value, 'shrink_axis_mask': masks_value,
                                                   'ellipsis_mask': masks_value, 'begin_mask': np.ones([3]),
                                                   'end_mask': np.ones([3])},
                                 'slice_data': {'shape': int64_array([2, 6, 8])}
                                 }, nodes_with_edges_only=True
                                )
        (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #9
0
    def test_2(self):
        """
        Testing case with constant path and one
         slicing dimension
        """
        graph = build_graph(
            nodes_attributes, [('placeholder_1', 'placeholder_1_data'),
                               ('placeholder_1_data', 'slice'),
                               ('slice', 'slice_data'),
                               ('slice_data', 'output_op'),
                               ('output_op', 'output_data')], {
                                   'placeholder_1_data': {
                                       'shape': np.array([4, 5, 6])
                                   },
                                   'slice': {
                                       'start': np.array([1]),
                                       'end': np.array([3]),
                                       'axis': None
                                   },
                                   'output_op': {
                                       'is_output': True
                                   }
                               })
        slice_node = Node(graph, 'slice')
        Slice.infer(slice_node)

        pattern = ConvertSlice()
        pattern.find_and_replace_pattern(graph)

        graph_ref = build_graph(
            nodes_attributes, [('placeholder_1', 'placeholder_1_data'),
                               ('placeholder_1_data', 'strided_slice'),
                               ('strided_slice', 'slice_data'),
                               ('slice_data', 'output_op'),
                               ('output_op', 'output_data')],
            {
                'placeholder_1_data': {
                    'shape': np.array([4, 5, 6])
                },
                'strided_slice': {
                    'slices':
                    np.array([slice(1, 3, 1),
                              slice(0, 5, 1),
                              slice(0, 6, 1)]),
                    'shrink_axis_mask':
                    np.array([False, False, False])
                },
                'output_op': {
                    'is_output': True
                }
            })

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output_op',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #10
0
    def test_1(self):
        """
        Testing case with non-constant path and multiple
        slicing dimensions
        :return:
        """
        graph = build_graph(
            nodes_attributes,
            [('placeholder_1', 'placeholder_1_data'),
             ('placeholder_1_data', 'slice'), ('slice', 'slice_data'),
             ('slice_data', 'output_op'), ('output_op', 'output_data'),
             ('output_data', 'op_output')],
            {
                'placeholder_1_data': {
                    'shape': np.array([4, 5, 6])
                },
                'slice': {
                    'start': np.array([1, 2, 3]),
                    'end': np.array([3, 4, 4]),
                    'axis': None
                },
            },
            nodes_with_edges_only=True,
        )
        slice_node = Node(graph, 'slice')
        Slice.infer(slice_node)

        pattern = ConvertSlice()
        pattern.find_and_replace_pattern(graph)
        graph.clean_up()

        ss_node = Node(graph, graph.get_node_id_by_name('slice_node'))
        assert ss_node.type == 'Crop', 'Something wrong with transformed Slice node'

        graph_ref = build_graph(
            nodes_attributes,
            [('placeholder_1', 'placeholder_1_data'),
             ('placeholder_1_data', 'crop'), ('crop', 'slice_data'),
             ('slice_data', 'output_op'), ('output_op', 'output_data'),
             ('output_data', 'op_output')],
            {
                'placeholder_1_data': {
                    'shape': np.array([4, 5, 6])
                },
                'crop': {
                    'axis': np.array([0, 1, 2]),
                    'offset': np.array([1, 2, 3]),
                    'dim': np.array([2, 2, 1])
                },
            },
            nodes_with_edges_only=True,
        )
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output_op',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #11
0
    def test_1(self):
        """
        Testing case with non-constant path and multiple
        slicing dimensions
        :return:
        """
        graph = build_graph(
            nodes_attributes, [('placeholder_1', 'placeholder_1_data'),
                               ('placeholder_1_data', 'slice'),
                               ('slice', 'slice_data'),
                               ('slice_data', 'output_op'),
                               ('output_op', 'output_data')], {
                                   'placeholder_1_data': {
                                       'shape': np.array([4, 5, 6])
                                   },
                                   'slice': {
                                       'start': np.array([1, 2, 3]),
                                       'end': np.array([3, 4, 4]),
                                       'axis': None
                                   },
                                   'output_op': {
                                       'is_output': True
                                   },
                               })
        slice_node = Node(graph, 'slice')
        Slice.infer(slice_node)

        pattern = ConvertSlice()
        pattern.find_and_replace_pattern(graph)

        graph_ref = build_graph(
            nodes_attributes, [('placeholder_1', 'placeholder_1_data'),
                               ('placeholder_1_data', 'crop'),
                               ('crop', 'slice_data'),
                               ('slice_data', 'output_op'),
                               ('output_op', 'output_data')], {
                                   'placeholder_1_data': {
                                       'shape': np.array([4, 5, 6])
                                   },
                                   'crop': {
                                       'axis': np.array([0, 1, 2]),
                                       'offset': np.array([1, 2, 3]),
                                   },
                                   'output_op': {
                                       'is_output': True
                                   },
                                   'dim': {
                                       'dim': np.array([2, 2, 1])
                                   },
                               })
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output_op',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #12
0
 def extract(node):
     pb = node.parameters
     num_slice_points = read_binary_integer32_token(pb)
     mapping_rule = {
         'axis': 1,
         'slice_point': read_blob(pb, num_slice_points, np.int32),
         'batch_dims': 0,
         'spatial_dims': 1,
         'infer': caffe_slice_infer
     }
     node.parameters.close()
     Slice.update_node_stat(node, mapping_rule)
     return __class__.enabled
Example #13
0
    def extract(node):
        axis = np.array(onnx_attr(node, 'axes', 'ints', default=[]), dtype=np.int64)
        start = np.array(onnx_attr(node, 'starts', 'ints', default=[]), dtype=np.int64)
        end = np.array(onnx_attr(node, 'ends', 'ints', default=[]), dtype=np.int64)

        attrs = {
            'axis': axis if len(axis) != 0 else None,
            'start': start if len(start) != 0 else None,
            'end': end if len(end) != 0 else None,
        }

        # update the attributes of the node
        Slice.update_node_stat(node, attrs)
        return __class__.enabled
Example #14
0
    def test_slice_infer(self, inp_value, inp_shape, starts, ends, axes, steps,
                         expected_value, expected_shape):
        if inp_value is None:
            input_node = shaped_data('data_1', int64_array(inp_shape))
        else:
            input_node = valued_data('data_1', int64_array(inp_value))
        if inp_value is not None and inp_shape is not None:
            assert np.array_equal(np.array(inp_value).shape, inp_shape)

        def convert_args(val, name=''):
            if val is not None:
                return valued_const_with_data(name, int64_array(val))
            else:
                return shaped_const_with_data(name, [0])  #fake shape

        starts = convert_args(starts, 'starts')
        ends = convert_args(ends, 'ends')
        axes = convert_args(axes, 'axes')
        steps = convert_args(steps, 'steps')
        if expected_shape is not None:
            expected_shape = shape_array(expected_shape)

        nodes = {
            **input_node,
            **regular_op_with_empty_data('slice', {'op': 'Slice'}),
            **starts,
            **ends,
            **axes,
            **steps,
        }

        graph = build_graph(
            nodes,
            [('data_1', 'slice'), *connect('starts', '1:slice'),
             *connect('ends', '2:slice'), *connect('axes', '3:slice'),
             *connect('steps', '4:slice'), *connect('slice', 'slice_d')])

        graph.stage = 'middle'
        slice_node = Node(graph, 'slice')

        Slice.infer(slice_node)
        if expected_value is not None:
            self.assertTrue(
                strict_compare_tensors(slice_node.out_node().value,
                                       expected_value))
        self.assertTrue(
            strict_compare_tensors(slice_node.out_node().shape,
                                   expected_shape))
Example #15
0
    def extract(cls, node):
        if get_onnx_opset_version(node) < 10:
            starts = int64_array(onnx_attr(node, 'starts', 'ints', default=[]))
            ends = int64_array(onnx_attr(node, 'ends', 'ints', default=[]))
            axes = int64_array(onnx_attr(node, 'axes', 'ints', default=[]))

            if len(starts) == 0 or len(ends) == 0:
                raise Error("starts or/and ends are not specified for the node {}".format(node.name))
            if len(axes) == 0:
                axes = np.arange(len(starts), dtype=np.int)

            attrs = {'axes': axes, 'starts': starts, 'ends': ends}
            AttributedSlice.update_node_stat(node, attrs)
        else:  # onnx_opset_version >= 10
            Slice.update_node_stat(node)
        return cls.enabled
    def replace_sub_graph(self, graph: Graph, match: dict):
        tf_slice_node = match['op']
        slice_name = tf_slice_node.soft_get('name', tf_slice_node.id)
        slice_node = Slice(graph).create_node()
        rename_nodes([(tf_slice_node, slice_name + '/to_be_removed'),
                      (slice_node, slice_name)])
        ends_node = Add(graph, {'name': slice_name + '/ends'}).create_node()

        # reconnect input, begin, and size from TFSlice to the subgraph with Slice
        tf_slice_node.in_port(0).get_connection().set_destination(
            slice_node.in_port(0))
        tf_slice_node.in_port(1).get_connection().set_destination(
            slice_node.in_port(1))
        tf_slice_node.in_port(2).get_connection().set_destination(
            ends_node.in_port(0))
        slice_node.in_port(1).get_connection().add_destination(
            ends_node.in_port(1))

        max_ends = Shape(graph, {
            'name': slice_name + '/ShapeOf'
        }).create_node()
        slice_node.in_port(0).get_connection().add_destination(
            max_ends.in_port(0))

        # check if size[i] == -1, will be applied elementwisely: len(size) = len(begin) = input_rank
        where_max_ends_is_needed = create_op_with_const_inputs(
            graph, Equal, {0: int64_array(-1)},
            {'name': slice_name + '/where_max_ends_is_needed'})
        ends_node.in_port(0).get_connection().add_destination(
            where_max_ends_is_needed.in_port(1))
        # select requires equal dtypes, need to convert ends to I64
        ends_casted_to_i64 = Cast(graph, {
            'name': slice_name + '/CastToI64',
            'dst_type': np.int64
        }).create_node([ends_node])
        # if size[i] == 1 then take max_ends values
        correct_ends = Select(graph, {
            'name': slice_name + '/chosen_ends'
        }).create_node(
            [where_max_ends_is_needed, max_ends, ends_casted_to_i64])
        correct_ends.out_port(0).connect(slice_node.in_port(2))

        tf_slice_node.out_port(0).get_connection().set_source(
            slice_node.out_port(0))
Example #17
0
    def replace_sub_graph(self, graph: Graph, match: dict):
        node = match['op']
        slice_name = node.soft_get('name', node.id)
        slice_node = Slice(graph).create_node()
        rename_nodes([(node, slice_name + '/to_be_removed'), (slice_node, slice_name)])

        eq_node = Equal(graph, {'name': slice_name + '/equal'}).create_node()
        minus_one_node = Const(graph, {'name': slice_name + '/minus_one', 'value': np.array(-1)}).create_node()
        int32_max_node = Const(graph, {'name': slice_name + '/int32_max', 'value': np.iinfo(np.int32).max}).create_node()
        select_node = Select(graph, {'name': slice_name + '/select'}).create_node()

        # node to convert sizes to ends
        sum_node = Add(graph, {'name': slice_name + '/end_const'}).create_node()

        # reconnect input from tfslice to slice
        node.in_port(0).get_source().connect(slice_node.in_port(0))
        node.in_port(0).disconnect()
        # reconnect begin of tfslice to start of slice
        node.in_port(1).get_source().connect(slice_node.in_port(1))
        node.in_port(1).disconnect()

        # (size -> ends) reconnect begins and sizes to sum to evaluate ends for Slice
        # connects begins to slice
        slice_node.in_port(1).get_source().connect(sum_node.in_port(0))
        node.in_port(2).get_source().connect(sum_node.in_port(1))
        node.in_port(2).disconnect()

        # if size[i] == -1 when take int32_max as end[i]
        sum_node.in_port(1).get_source().connect(eq_node.in_port(0))
        minus_one_node.out_port(0).connect(eq_node.in_port(1))
        # from equal to 0 port of select
        eq_node.out_port(0).connect(select_node.in_port(0))
        # from int32_max to 1 of select
        int32_max_node.out_port(0).connect(select_node.in_port(1))
        # from sum to 2nd of select
        sum_node.out_port(0).connect(select_node.in_port(2))
        # out of select to end (2nd of slice)
        select_node.out_port(0).connect(slice_node.in_port(2))

        cast = Cast(graph, dict(name=sum_node.name + '/CastToI64', dst_type=np.int64)).create_node()
        select_node.in_port(2).get_connection().insert_node(cast)

        node.out_port(0).get_connection().set_source(slice_node.out_port(0))
Example #18
0
    def test_3(self):
        """
        Testing case with constant path and one
         slicing dimension
        """
        graph = build_graph(
            nodes_attributes,
            [('placeholder_1', 'placeholder_1_data'),
             ('placeholder_1_data', 'slice'), ('slice', 'slice_data'),
             ('slice_data', 'output_op'), ('output_op', 'output_data'),
             ('output_data', 'op_output')],
            {
                'placeholder_1_data': {
                    'shape': np.array([1, 5, 6])
                },
                'slice': {
                    'start': np.array([1]),
                    'end': np.array([3]),
                    'axis': np.array([1])
                }
            },
            nodes_with_edges_only=True,
        )
        graph.graph['layout'] = 'NHWC'
        slice_node = Node(graph, 'slice')
        Slice.infer(slice_node)

        pattern = ConvertSlice()
        pattern.find_and_replace_pattern(graph)
        graph.clean_up()

        ss_node = Node(graph, graph.get_node_id_by_name('slice_node'))
        assert ss_node.type == 'StridedSlice', 'Something wrong with transformed Slice node'

        graph_ref = build_graph(
            nodes_attributes,
            [('placeholder_1', 'placeholder_1_data'),
             ('placeholder_2', 'placeholder_2_data'),
             ('placeholder_3', 'placeholder_3_data'),
             ('placeholder_1_data', 'strided_slice'),
             ('placeholder_2_data', 'strided_slice'),
             ('placeholder_3_data', 'strided_slice'),
             ('strided_slice', 'slice_data'), ('slice_data', 'output_op'),
             ('output_op', 'output_data'), ('output_data', 'op_output')],
            {
                'placeholder_1_data': {
                    'shape': np.array([1, 5, 6])
                },
                'strided_slice': {
                    'slices':
                    np.array([slice(0, 1, 1),
                              slice(1, 3, 1),
                              slice(0, 6, 1)]),
                    'shrink_axis_mask':
                    np.array([False, False, False])
                },
            },
            nodes_with_edges_only=True,
        )

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output_op',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)