def infer(cls, node: Node):
        input_shape = node.in_node(0).shape
        input_h = input_shape[2]
        input_w = input_shape[3]
        output_h = node.output_size[0]
        output_w = node.output_size[1]

        stride_h = input_h // output_h
        stride_w = input_w // output_w
        kernel_h = input_h - (output_h - 1) * stride_h
        kernel_w = input_w - (output_w - 1) * stride_w

        data = {
            'window': int64_array([1, 1, kernel_h, kernel_w]),
            'stride': int64_array([1, 1, stride_h, stride_w]),
            'pad': int64_array([[0, 0], [0, 0], [0, 0], [0, 0]]),
            'pad_spatial_shape': int64_array([[0, 0], [0, 0]]),
            'pool_method': 'avg',
            'exclude_pad': False,
            'output_spatial_shape': None,
            'spatial_dims': None,
            'channel_dims': int64_array([1]),
            'batch_dims': int64_array([0]),
            'layout': 'NCHW',
            'rounding_type': 'floor',
            'pooling_convention': 'valid'
        }

        # update the attributes of the node
        Pooling.update_node_stat(node, data)
        Pooling.infer(node)
Beispiel #2
0
    def test_pooling_infer_no_shape(self):
        graph = build_graph(
            nodes_attributes, [('node_1', 'pool'), ('pool', 'node_2'),
                               ('node_2', 'op_output')],
            {
                'node_2': {
                    'shape': None
                },
                'node_1': {
                    'shape': None
                },
                'pool': {
                    'window': np.array([1, 1, 1, 1]),
                    'stride': np.array([1, 1, 2, 2]),
                    'pad': np.array([[0, 0], [0, 0], [3, 3], [3, 3]]),
                    'pad_spatial_shape': np.array([[3, 3], [3, 3]]),
                    'pool_method': 'avg',
                    'exclude_pad': False,
                    'output_spatial_shape': None,
                    'output_shape': None,
                    'kernel_spatial': np.array([3, 3]),
                    'spatial_dims': np.array([2, 3]),
                    'channel_dims': np.array([1]),
                    'batch_dims': np.array([0]),
                    'pooling_convention': 'full'
                }
            })

        pool_node = Node(graph, 'pool')
        Pooling.infer(pool_node)
        res_shape = graph.node['node_2']['shape']
        self.assertIsNone(res_shape)
Beispiel #3
0
    def test_pooling_infer_wrong_input_shape(self):
        graph = build_graph(
            nodes_attributes, [('node_1', 'pool'), ('pool', 'node_2'),
                               ('node_2', 'op_output')],
            {
                'node_2': {
                    'shape': None
                },
                'node_1': {
                    'shape': np.array([1, 3, 1, 1])
                },
                'pool': {
                    'window': np.array([1, 1, 5, 5]),
                    'stride': np.array([1, 1, 2, 2]),
                    'pad': np.array([[0, 0], [0, 0], [1, 1], [1, 1]]),
                    'pad_spatial_shape': np.array([[1, 1], [1, 1]]),
                    'pool_method': 'avg',
                    'exclude_pad': False,
                    'global_pool': False,
                    'output_spatial_shape': None,
                    'output_shape': None,
                    'kernel_spatial': np.array([3, 3]),
                    'spatial_dims': np.array([2, 3]),
                    'channel_dims': np.array([1]),
                    'batch_dims': np.array([0]),
                    'pooling_convention': 'full'
                }
            })

        pool_node = Node(graph, 'pool')

        with self.assertRaises(Error):
            Pooling.infer(pool_node)
Beispiel #4
0
    def test_pooling_infer_no_convention(self):
        graph = build_graph(
            nodes_attributes, [('node_1', 'pool'), ('pool', 'node_2'),
                               ('node_2', 'op_output')],
            {
                'node_2': {
                    'shape': None
                },
                'node_1': {
                    'shape': np.array([1, 3, 256, 256])
                },
                'pool': {
                    'window': np.array([1, 1, 1, 1]),
                    'stride': np.array([1, 1, 2, 2]),
                    'pad': np.array([[0, 0], [0, 0], [3, 3], [3, 3]]),
                    'pad_spatial_shape': np.array([[3, 3], [3, 3]]),
                    'pool_method': 'avg',
                    'exclude_pad': False,
                    'global_pool': False,
                    'output_spatial_shape': None,
                    'output_shape': None,
                    'kernel_spatial': np.array([3, 3]),
                    'spatial_dims': np.array([2, 3]),
                    'channel_dims': np.array([1]),
                    'batch_dims': np.array([0])
                }
            })

        pool_node = Node(graph, 'pool')

        Pooling.infer(pool_node)
        exp_shape = np.array([1, 3, 130, 130])
        res_shape = graph.node['node_2']['shape']
        for i in range(0, len(exp_shape)):
            self.assertEqual(exp_shape[i], res_shape[i])
Beispiel #5
0
    def test_pooling_dynamic_infer(self):
        graph = build_graph(
            nodes_attributes, [('node_1', 'pool'), ('pool', 'node_2'),
                               ('node_2', 'op_output')],
            {
                'node_2': {
                    'shape': None
                },
                'node_1': {
                    'shape':
                    shape_array([
                        1, dynamic_dimension_value, dynamic_dimension_value,
                        256
                    ])
                },
                'pool': {
                    'window': np.array([1, 1, 1, 1]),
                    'stride': np.array([1, 1, 2, 2]),
                    'pad': np.array([[0, 0], [0, 0], [3, 3], [3, 3]]),
                    'pad_spatial_shape': np.array([[3, 3], [3, 3]]),
                    'pool_method': 'avg',
                    'exclude_pad': False,
                    'global_pool': False,
                    'output_spatial_shape': None,
                    'output_shape': None,
                    'kernel_spatial': np.array([3, 3]),
                    'spatial_dims': np.array([2, 3]),
                    'channel_dims': np.array([1]),
                    'batch_dims': np.array([0]),
                    'pooling_convention': 'full'
                }
            })

        pool_node = Node(graph, 'pool')

        Pooling.infer(pool_node)
        exp_shape = shape_array(
            [1, dynamic_dimension_value, dynamic_dimension_value, 131])
        res_shape = graph.node['node_2']['shape']
        self.assertTrue(strict_compare_tensors(exp_shape, res_shape))