def extract(cls, node):
        proto_layer = node.pb
        pb_model = node.model_pb
        param = proto_layer.prelu_param

        update_attrs = {
            'channel_shared': int(param.channel_shared)
        }

        variance_norm_caffe_map = {
            0: 'caffe.FillerParameter.FAN_IN',
            1: 'caffe.FillerParameter.FAN_OUT',
            2: 'caffe.FillerParameter.AVERAGE'
        }

        if hasattr(param, 'filler'):
            update_attrs.update({
                'filler_type': param.filler.type,
                'filler_value': int(param.filler.value),
                'min': int(param.filler.min),
                'max': int(param.filler.max),
                'mean': int(param.filler.mean),
                'std': int(param.filler.std),
                'sparse': param.filler.sparse,
                'variance_norm': variance_norm_caffe_map[param.filler.variance_norm]
            })

        mapping_rule = merge_attrs(param, update_attrs)
        mapping_rule.update(weights_biases(False, pb_model))
        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        PReLU.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemplo n.º 2
0
    def extract(cls, node):
        proto_layer, model_layer = node.pb, node.model_pb

        if not proto_layer:
            raise Error('Protobuf layer can not be empty')

        conv_param = proto_layer.convolution_param
        conv_type = 'ConvND' if len(proto_layer.bottom) > 1 else 'Conv2D'

        params = conv_set_params(conv_param, conv_type)
        attrs = conv_create_attrs(params)
        attrs.update({
            'op': conv_type,
            'get_group': lambda node: node.group,
            'get_output_feature_dim': lambda node: node.output,
            'weights_index': 1 if conv_type == 'Conv2D' else 2
        })

        # Embed weights and biases as attributes
        # It will be moved to a separate nodes in special pass
        attrs.update(
            weights_biases(conv_param.bias_term,
                           model_layer,
                           start_index=len(proto_layer.bottom),
                           proto=conv_param))
        attrs.update(layout_attrs())

        # update the attributes of the node
        Convolution.update_node_stat(node, attrs)
        return cls.enabled
Exemplo n.º 3
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])
Exemplo n.º 4
0
    def test_reverse_infer(self):
        graph = build_graph(
            nodes_attributes,
            [('node_1', 'node_1_data'), ('node_1_data', 'reverse'),
             ('node_2', 'node_2_data'), ('node_2_data', 'reverse'),
             ('reverse', 'node_3'), ('node_3', 'op_output')], {
                 'node_3': {
                     'shape': None,
                     'value': None
                 },
                 'node_1_data': {
                     'shape': np.array([1, 4])
                 },
                 'reverse': {
                     'stride': 2,
                     **layout_attrs()
                 }
             })

        reverse_node = Node(graph, 'reverse')
        Reverse.infer(reverse_node)
        exp_shape = np.array([1, 4])
        exp_value = np.array([[227, 227, 3, 1]])
        res_shape = graph.node['node_3']['shape']
        res_value = graph.node['node_3']['value']
        for i in range(0, len(exp_shape)):
            self.assertEqual(exp_shape[i], res_shape[i])
        for i in range(0, len(exp_value[0])):
            self.assertEqual(exp_value[0][i], res_value[0][i])
Exemplo n.º 5
0
    def extract(cls, node):
        pb = node.parameters
        collect_until_token(pb, b'<PoolSize>')
        kernel = read_binary_integer32_token(pb)
        tag = find_next_tag(pb)
        if tag == '<PoolStep>':
            read_placeholder(pb, 1)
            stride = read_binary_integer32_token(pb)
            pool_step = stride
            pool_stride = read_token_value(pb, b'<PoolStride>')
        elif tag == '<PoolStride>':
            stride = 1
            pool_step = None
            read_placeholder(pb, 1)
            pool_stride = read_binary_integer32_token(pb)
        else:
            raise Error('Can not extract parameters for {}'.format(node))

        mapping_rule = {
            'window': np.array([1, 1, 1, kernel], dtype=np.int64),
            'stride': np.array([1, 1, stride, stride], dtype=np.int64),
            'pool_stride': pool_stride,
            'pool_step': pool_step,
            'pad': np.array([[0, 0], [0, 0], [0, 0], [0, 0]], dtype=np.int64),
            'pad_spatial_shape': np.array([[0, 0], [0, 0]], dtype=np.int64),
            'pool_method': 'max',
        }
        mapping_rule.update(layout_attrs())
        Pooling.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemplo n.º 6
0
    def extract(cls, node):
        mapping_rule = collect_attributes(node.pb.shuffle_channel_param)
        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        ShuffleChannels.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemplo n.º 7
0
    def extract(cls, node):
        proto_layer = node.pb
        param = proto_layer.eltwise_param

        input_len = len(node.in_edges())

        eltwise_caffe_map = {
            0: EltwiseNMul if input_len > 2 else Mul,
            1: EltwiseNAdd if input_len > 2 else Add,
            2: EltwiseNMax if input_len > 2 else Maximum,
        }

        operation = int(param.operation)
        if operation not in eltwise_caffe_map:
            raise Exception(
                'Unsupported type of operation in Eltwise layer: ' + node.name)

        lin_op_class = eltwise_caffe_map[operation]

        mapping_rule = merge_attrs(param, {'coeff': np.array(param.coeff)})
        mapping_rule.update(layout_attrs())

        assert len(param.coeff) <= input_len

        lin_op_class.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemplo n.º 8
0
    def extract(cls, node):
        proto_layer = node.pb
        param = proto_layer.prior_box_param

        variance = param.variance
        if len(variance) == 0:
            variance = [0.1]

        update_attrs = {
            'width': list(param.width),
            'height': list(param.height),
            'flip': int(param.flip),
            'clip': int(param.clip),
            'variance': list(variance),
            'img_size': param.img_size,
            'img_h': param.img_h,
            'img_w': param.img_w,
            'step': param.step,
            'step_h': param.step_h,
            'step_w': param.step_w,
            'offset': param.offset,
        }

        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        PriorBoxClusteredOp.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemplo n.º 9
0
 def test_pooling_ext(self):
     params = {
         'kernel_size': 1,
         'stride': 2,
         'pad': 3,
         'pool': 1,
         'global_pooling': 0,
         'ceil_mode': 0
     }
     node = PB({'pb': FakeProtoLayer(FakeMultiParam(params))})
     PoolingFrontExtractor.extract(node)
     res = node
     exp_res = {
         'window': np.array([1, 1, 1, 1], dtype=np.int64),
         'stride': np.array([1, 1, 2, 2], dtype=np.int64),
         'pad': np.array([[0, 0], [0, 0], [3, 3], [3, 3]], dtype=np.int64),
         'pad_spatial_shape': np.array([[3, 3], [3, 3]], dtype=np.int64),
         'pool_method': 'avg',
         'exclude_pad': 'false',
         'infer': Pooling.infer,
         'global_pool': 0,
         'output_spatial_shape': None,
         'pooling_convention': 'valid'
     }
     exp_res.update(layout_attrs())
     for i in exp_res.keys():
         if i in ('window', 'stride',
                  'pad', 'pad_spatial_shape',
                  'spatial_dims', 'batch_dims',
                  'channel_dims'):
             np.testing.assert_array_equal(res[i], exp_res[i])
         else:
             self.assertEqual(res[i], exp_res[i])
Exemplo n.º 10
0
    def extract(node):
        proto_layer = node.pb
        param = proto_layer.prior_box_param

        variance = param.variance
        if len(variance) == 0:
            variance = [0.1]

        update_attrs = {
            'aspect_ratio': np.array(param.aspect_ratio),
            'min_size': np.array(param.min_size),
            'max_size': np.array(param.max_size),
            'flip': int(param.flip),
            'clip': int(param.clip),
            'variance': list(variance),
            'img_size': param.img_size,
            'img_h': param.img_h,
            'img_w': param.img_w,
            'step': param.step,
            'step_h': param.step_h,
            'step_w': param.step_w,
            'offset': param.offset,
        }

        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        Op.get_op_class_by_name(__class__.op).update_node_stat(
            node, mapping_rule)
        return __class__.enabled
Exemplo n.º 11
0
    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])
Exemplo n.º 12
0
    def extract(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
        Op.get_op_class_by_name(__class__.op).update_node_stat(
            node, mapping_rule)
        return __class__.enabled
Exemplo n.º 13
0
    def extract(node):
        proto_layer = node.pb
        param = proto_layer.correlation_param

        corr_type = 'caffe.CorrelationParameter.MULTIPLY'
        if param.correlation_type == 1:
            corr_type = 'caffe.CorrelationParameter.SUBTRACT'

        update_attrs = {
            'pad': param.pad,
            'kernel_size': param.kernel_size,
            'max_displacement': param.max_displacement,
            'stride_1': param.stride_1,
            'stride_2': param.stride_2,
            'single_direction': param.single_direction,
            'do_abs': int(param.do_abs),
            'correlation_type': corr_type,
        }

        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        Op.get_op_class_by_name(__class__.op).update_node_stat(
            node, mapping_rule)
        return __class__.enabled
Exemplo n.º 14
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))
Exemplo n.º 15
0
    def extract(cls, node):
        proto_layer, model_layer = node.pb, node.model_pb

        if not proto_layer:
            raise Error('Protobuf layer can not be empty')

        deconv_param = proto_layer.convolution_param

        params = conv_set_params(deconv_param, 'Deconv2D')
        attrs = conv_create_attrs(params)
        attrs.update({
            'type': 'Deconvolution',
            'op': 'Deconv2D',
            'get_group': lambda node: node.group,
            'get_output_feature_dim': lambda node: node.output,
            'input_feature_channel': 0,
            'output_feature_channel': 1,
        })

        # Embed weights and biases as attributes
        # It will be moved to a separate nodes in special pass
        attrs.update(weights_biases(deconv_param.bias_term, model_layer))
        attrs.update(layout_attrs())

        # update the attributes of the node
        Convolution.update_node_stat(node, attrs)
        return cls.enabled
Exemplo n.º 16
0
def scale_shift_ext(attrs):
    node_attrs = {
        'type': 'ScaleShift',
        'fix_gamma': attrs.bool("fix_gamma", True),
        'infer': batch_norm_4_infer
    }
    node_attrs.update(layout_attrs())
    return node_attrs
Exemplo n.º 17
0
    def extract(node):
        mapping_rule = collect_attributes(node.pb.shuffle_channel_param)
        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        Op.get_op_class_by_name(__class__.op).update_node_stat(
            node, mapping_rule)
        return __class__.enabled
Exemplo n.º 18
0
def batch_norm_ext(attrs):
    node_attrs = {
        'type': 'BatchNormalization',
        'eps': attrs.float('eps', 0.001),
        'infer': batch_norm_4_infer,
        'fix_gamma': attrs.bool('fix_gamma', False)
    }
    node_attrs.update(layout_attrs())
    return node_attrs
    def extract(cls, node: Node) -> bool:
        """
        Extract conv parameters from node.parameters.
        node.parameters like file descriptor object.
        :param node: Convolution node
        :return:
        """
        pb = node.parameters
        kernel = read_token_value(pb, b'<PatchDim>')
        stride = read_token_value(pb, b'<PatchStep>')
        patch_stride = read_token_value(pb, b'<PatchStride>')

        read_learning_info(pb)

        collect_until_whitespace(pb)
        weights, weights_shape = read_binary_matrix(pb)

        collect_until_whitespace(pb)
        biases = read_binary_vector(pb)

        if (patch_stride - kernel) % stride != 0:
            raise Error(
                'Kernel size and stride does not correspond to `patch_stride` attribute of Convolution layer. '
                + refer_to_faq_msg(93))

        output = biases.shape[0]
        if weights_shape[0] != output:
            raise Error(
                'Weights shape does not correspond to the `output` attribute of Convolution layer. '
                + refer_to_faq_msg(93))

        mapping_rule = {
            'output': output,
            'patch_stride': patch_stride,
            'bias_term': None,
            'pad': np.array([[0, 0], [0, 0], [0, 0], [0, 0]], dtype=np.int64),
            'pad_spatial_shape': np.array([[0, 0], [0, 0]], dtype=np.int64),
            'dilation': np.array([1, 1, 1, 1], dtype=np.int64),
            'kernel': np.array([1, 1, 1, kernel], dtype=np.int64),
            'stride': np.array([1, 1, 1, stride], dtype=np.int64),
            'kernel_spatial': np.array([1, kernel], dtype=np.int64),
            'input_feature_channel': 1,
            'output_feature_channel': 0,
            'kernel_spatial_idx': [2, 3],
            'group': 1,
            'reshape_kernel': True,
        }

        mapping_rule.update(layout_attrs())
        embed_input(mapping_rule, 1, 'weights', weights)
        embed_input(mapping_rule, 2, 'biases', biases)

        mapping_rule['bias_addable'] = len(biases) > 0

        Convolution.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemplo n.º 20
0
def relu_ext(proto_layer, model_layer):
    assert proto_layer, 'Protobuf layer can not be empty'
    param = proto_layer.relu_param
    negative_slope = param.negative_slope

    attrs = {
        'op': 'ReLU',
        'type': 'ReLU',
        'negative_slope': negative_slope,
        'infer': copy_shape_infer
    }
    attrs.update(layout_attrs())
    return attrs
Exemplo n.º 21
0
    def extract(cls, node):
        proto_layer = node.pb
        param = proto_layer.ctc_decoder_param

        update_attrs = {'ctc_merge_repeated': (int)(param.ctc_merge_repeated)}

        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        CTCGreedyDecoderOp.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemplo n.º 22
0
Arquivo: crop.py Projeto: pc2/CustoNN2
def crop_ext(attr):
    offset = attr.tuple("offset", int, ())
    axis = attr.int("num_args", 0)

    node_attrs = {
        'op': 'Crop',
        'type': 'Crop',
        'axis': axis,
        'offset': list(offset),
        'dim': None,
        'infer': crop_infer
    }
    node_attrs.update(layout_attrs())
    return node_attrs
Exemplo n.º 23
0
    def test_proposal_infer_one_output(self):
        graph = build_graph(nodes_attributes,
                            [('proposal_input', 'proposal'),
                             ('proposal', 'proposal_out_data_1'),
                             ('proposal_out_data_1', 'op_output')
                             ],
                            {'proposal_input': {'shape': int64_array([1, 3, 227, 227])},
                             'proposal': {'post_nms_topn': 2, **layout_attrs()}
                             })

        proposal_node = Node(graph, 'proposal')
        ProposalOp.proposal_infer(proposal_node)

        self.assertListEqual([1 * 2, 5], list(graph.node['proposal_out_data_1']['shape']))
    def extract(node):
        proto_layer = node.pb
        param = proto_layer.ctc_decoder_param

        update_attrs = {'ctc_merge_repeated': (int)(param.ctc_merge_repeated)}

        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        Op.get_op_class_by_name(__class__.op).update_node_stat(
            node, mapping_rule)
        return __class__.enabled
Exemplo n.º 25
0
    def test_proposal_infer(self):
        graph = build_graph(nodes_attributes,
                            [('node_1', 'proposal'),
                             ('proposal', 'node_3')],
                            {'node_3': {'is_output': True, 'shape': None},
                             'node_1': {'shape': np.array([1, 3, 227, 227])},
                             'proposal': {'post_nms_topn': 2, **layout_attrs()}
                             })

        proposal_node = Node(graph, 'proposal')
        ProposalOp.proposal_infer(proposal_node)
        exp_shape = np.array([1 * 2, 5])
        res_shape = graph.node['node_3']['shape']
        for i in range(0, len(exp_shape)):
            self.assertEqual(exp_shape[i], res_shape[i])
Exemplo n.º 26
0
    def extract(cls, node):
        proto_layer = node.pb
        param = proto_layer.reorg_yolo_param

        stride = param.stride
        update_attrs = {
            'stride': stride,
        }
        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        ReorgYoloOp.update_node_stat(node, mapping_rule)
        return cls.enabled
Exemplo n.º 27
0
    def test_relu_ext(self):
        params = {
            'negative_slope': 0.1,
        }

        res = relu_ext(FakeParam('relu_param', FakeMultiParam(params)), None)
        exp_res = {
            'negative_slope': 0.1,
            'infer': copy_shape_infer,
        }
        exp_res.update(layout_attrs())
        for i in exp_res.keys():
            if i == 'negative_slope':
                self.assertEqual(res[i], exp_res[i])
            else:
                np.testing.assert_array_equal(res[i], exp_res[i])
Exemplo n.º 28
0
    def extract(node):
        proto_layer = node.pb
        param = proto_layer.reorg_yolo_param

        stride = param.stride
        update_attrs = {
            'stride': stride,
        }
        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        Op.get_op_class_by_name(__class__.op).update_node_stat(
            node, mapping_rule)
        return __class__.enabled
Exemplo n.º 29
0
    def extract(node):
        proto_layer = node.pb
        param = proto_layer.psroi_pooling_param

        update_attrs = {
            'spatial_scale': param.spatial_scale,
            'output_dim': param.output_dim,
            'group_size': param.group_size,
        }

        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        Op.get_op_class_by_name(__class__.op).update_node_stat(node, mapping_rule)
        return __class__.enabled
Exemplo n.º 30
0
    def extract(cls, node):
        proto_layer = node.pb
        param = proto_layer.psroi_pooling_param

        update_attrs = {
            'spatial_scale': param.spatial_scale,
            'output_dim': param.output_dim,
            'group_size': param.group_size,
        }

        mapping_rule = merge_attrs(param, update_attrs)

        mapping_rule.update(layout_attrs())

        # update the attributes of the node
        PSROIPoolingOp.update_node_stat(node, mapping_rule)
        return cls.enabled