def test_region_infer_do_softmax(self):
        graph = build_graph(
            nodes_attributes, [('node_1', 'region'), ('region', 'node_3'),
                               ('node_3', 'op_output')], {
                                   'node_3': {
                                       'shape': None
                                   },
                                   'node_1': {
                                       'shape': np.array([1, 227, 227, 3])
                                   },
                                   'region': {
                                       'do_softmax': 0,
                                       'end_axis': -1,
                                       'axis': 1,
                                       'classes': 80,
                                       'coords': 4,
                                       'mask': np.array([6, 7, 8]),
                                       **layout_attrs()
                                   }
                               })

        graph.graph['layout'] = 'NHWC'
        reorg_node = Node(graph, 'region')
        RegionYoloOp.regionyolo_infer(reorg_node)
        exp_shape = np.array([1, 227, 227, (80 + 4 + 1) * 3])
        res_shape = graph.node['node_3']['shape']
        for i in range(0, len(exp_shape)):
            self.assertEqual(exp_shape[i], res_shape[i])
    def extract(cls, node):
        proto_layer = node.pb
        param = proto_layer.region_yolo_param
        flatten_param = proto_layer.flatten_param
        axis = flatten_param.axis
        end_axis = flatten_param.end_axis
        coords = param.coords
        classes = param.classes
        num = param.num
        update_attrs = {
            'coords': coords,
            'classes': classes,
            'num': num,
            'do_softmax': int(param.do_softmax),
            'anchors': np.array(param.anchors),
            'mask': np.array(param.mask)
        }

        flatten_attrs = {'axis': axis, 'end_axis': end_axis}

        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(flatten_attrs)
        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        RegionYoloOp.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemple #3
0
 def test_region_infer_dynamic_flatten(self):
     graph = build_graph(
         nodes_attributes, [('node_1', 'region'), ('region', 'node_3'),
                            ('node_3', 'op_output')],
         {
             'node_3': {
                 'shape': None,
                 'value': None
             },
             'node_1': {
                 'shape': shape_array(
                     [1, dynamic_dimension_value, 227, 227])
             },
             'region': {
                 'end_axis': 1,
                 'axis': 0,
                 'do_softmax': 1,
                 **layout_attrs()
             }
         })
     graph.graph['layout'] = 'NCHW'
     reorg_node = Node(graph, 'region')
     RegionYoloOp.regionyolo_infer(reorg_node)
     exp_shape = shape_array([dynamic_dimension_value, 227, 227])
     res_shape = graph.node['node_3']['shape']
     self.assertTrue(strict_compare_tensors(exp_shape, res_shape))
Exemple #4
0
 def test_region_infer_flatten(self):
     graph = build_graph(
         nodes_attributes, [('node_1', 'region'), ('region', 'node_3'),
                            ('node_3', 'op_output')], {
                                'node_3': {
                                    'shape': None,
                                    'value': None
                                },
                                'node_1': {
                                    'shape': np.array([1, 3, 227, 227])
                                },
                                'region': {
                                    'end_axis': 1,
                                    'axis': 0,
                                    'do_softmax': 1,
                                    **layout_attrs()
                                }
                            })
     graph.graph['layout'] = 'NCHW'
     reorg_node = Node(graph, 'region')
     RegionYoloOp.regionyolo_infer(reorg_node)
     exp_shape = np.array([1 * 3, 227, 227])
     res_shape = graph.node['node_3']['shape']
     for i in range(0, len(exp_shape)):
         self.assertEqual(exp_shape[i], res_shape[i])
Exemple #5
0
 def transform_graph(self, graph: Graph, replacement_descriptions):
     op_outputs = [n for n, d in graph.nodes(data=True) if 'op' in d and d['op'] == 'Result']
     for op_output in op_outputs:
         last_node = Node(graph, op_output).in_node(0)
         op_params = dict(name=last_node.id + '/YoloRegion', axis=1, end_axis=-1)
         op_params.update(replacement_descriptions)
         region_layer = RegionYoloOp(graph, op_params)
         region_layer_node = region_layer.create_node([last_node])
         # here we remove 'axis' from 'dim_attrs' to avoid permutation from axis = 1 to axis = 2
         region_layer_node.dim_attrs.remove('axis')
         Result(graph).create_node([region_layer_node])
         graph.remove_node(op_output)
 def transform_graph(self, graph: Graph, replacement_descriptions):
     graph.remove_nodes_from(graph.get_nodes_with_attributes(op='Result'))
     for i, input_node_name in enumerate(
             replacement_descriptions['entry_points']):
         if input_node_name not in graph.nodes():
             raise Error(
                 'TensorFlow YOLO V3 conversion mechanism was enabled. '
                 'Entry points "{}" were provided in the configuration file. '
                 'Entry points are nodes that feed YOLO Region layers. '
                 'Node with name {} doesn\'t exist in the graph. '
                 'Refer to documentation about converting YOLO models for more information.'
                 .format(
                     ', '.join(replacement_descriptions['entry_points']),
                     input_node_name))
         last_node = Node(graph, input_node_name).in_node(0)
         op_params = dict(name=last_node.id + '/YoloRegion',
                          axis=1,
                          end_axis=-1,
                          do_softmax=0)
         op_params.update(replacement_descriptions)
         if 'masks' in op_params:
             op_params['mask'] = op_params['masks'][i]
             del op_params['masks']
         region_layer_node = RegionYoloOp(graph, op_params).create_node(
             [last_node])
         # TODO: do we need change axis for further permutation
         region_layer_node.dim_attrs.remove('axis')
         Result(graph, {
             'name': region_layer_node.id + '/Result'
         }).create_node([region_layer_node])