def extract(cls, node):
        attrs = get_mxnet_layer_attrs(node.symbol_dict)
        top_k = attrs.int("nms_topk", -1)
        nms_threshold = attrs.float("nms_threshold", 0.5)
        confidence_threshold = attrs.float("threshold", 0.01)
        clip = 0 if not attrs.bool("clip", True) else 1

        node_attrs = {
            'type': 'DetectionOutput',
            'op': __class__.op,
            'keep_top_k': top_k,
            'variance_encoded_in_target': 0,
            'code_type': "caffe.PriorBoxParameter.CENTER_SIZE",
            'share_location': 1,
            'confidence_threshold': confidence_threshold,
            'background_label_id': 0,
            'nms_threshold': nms_threshold,
            'top_k': top_k,
            'decrease_label_id': 1,
            'clip_before_nms': clip,
            'normalized': 1,
        }

        DetectionOutput.update_node_stat(node, node_attrs)

        return cls.enabled
    def extract(cls, node):
        attr_names = [
            'variance_encoded_in_target',
            'nms_threshold',
            'confidence_threshold',
            'top_k',
            'keep_top_k',
            'code_type',
            'share_location',
            'background_label_id',
            'clip_before_nms',
        ]

        attrs = {}
        for attr in attr_names:
            if hasattr(node.module, attr):
                attrs[attr] = getattr(node.module, attr)

        DetectionOutput.update_node_stat(node, attrs)
        return cls.enabled
Example #3
0
    def extract(cls, node):
        pl = node.pb
        assert pl, 'Protobuf layer can not be empty'

        param = pl.detection_output_param

        # TODO rewrite params as complex structures
        if hasattr(param, 'nms_param'):
            nms_threshold = param.nms_param.nms_threshold
            eta = param.nms_param.eta
            if param.nms_param.top_k == 0:
                top_k = -1
            else:
                top_k = param.nms_param.top_k

        code_type_values = [
            "", "caffe.PriorBoxParameter.CORNER",
            "caffe.PriorBoxParameter.CENTER_SIZE",
            "caffe.PriorBoxParameter.CORNER_SIZE"
        ]

        code_type = code_type_values[1]
        if hasattr(param, 'code_type'):
            if param.code_type < 1 or param.code_type > 3:
                log.error("Incorrect value of code_type parameter")
                return
            code_type = code_type_values[param.code_type]

        visualize_threshold = param.visualize_threshold if param.visualize_threshold else 0.6

        resize_mode_values = [
            "", "caffe.ResizeParameter.WARP",
            "caffe.ResizeParameter.FIT_SMALL_SIZE",
            "caffe.ResizeParameter.FIT_LARGE_SIZE_AND_PAD"
        ]

        if param.save_output_param.resize_param.resize_mode < 1 or param.save_output_param.resize_param.resize_mode > 3:
            log.error("Incorrect value of resize_mode parameter")
            return
        resize_mode = resize_mode_values[
            param.save_output_param.resize_param.resize_mode]

        pad_mode_values = [
            "", "caffe.ResizeParameter.CONSTANT",
            "caffe.ResizeParameter.MIRRORED",
            "caffe.ResizeParameter.REPEAT_NEAREST"
        ]

        if param.save_output_param.resize_param.pad_mode < 1 or param.save_output_param.resize_param.pad_mode > 3:
            log.error("Incorrect value of pad_mode parameter")
        else:
            pad_mode = pad_mode_values[
                param.save_output_param.resize_param.pad_mode]

        interp_mode_values = [
            "", "caffe.ResizeParameter.LINEAR", "caffe.ResizeParameter.AREA",
            "caffe.ResizeParameter.NEAREST", "caffe.ResizeParameter.CUBIC",
            "caffe.ResizeParameter.LANCZOS4"
        ]
        interp_mode = ""
        for x in param.save_output_param.resize_param.interp_mode:
            if x < 1 or x > 5:
                log.error("Incorrect value of interp_mode parameter")
                return
            interp_mode += interp_mode_values[x]

        attrs = {
            'share_location':
            int(param.share_location),
            'background_label_id':
            param.background_label_id,
            'code_type':
            code_type,
            'variance_encoded_in_target':
            int(param.variance_encoded_in_target),
            'keep_top_k':
            param.keep_top_k,
            'confidence_threshold':
            param.confidence_threshold,
            'visualize':
            param.visualize,
            'visualize_threshold':
            visualize_threshold,
            'save_file':
            param.save_file,
            # nms_param
            'nms_threshold':
            nms_threshold,
            'top_k':
            top_k,
            'eta':
            eta,
            # save_output_param
            'output_directory':
            param.save_output_param.output_directory,
            'output_name_prefix':
            param.save_output_param.output_name_prefix,
            'output_format':
            param.save_output_param.output_format,
            'label_map_file':
            param.save_output_param.label_map_file,
            'name_size_file':
            param.save_output_param.name_size_file,
            'num_test_image':
            param.save_output_param.num_test_image,
            # save_output_param.resize_param
            'prob':
            param.save_output_param.resize_param.prob,
            'resize_mode':
            resize_mode,
            'height':
            param.save_output_param.resize_param.height,
            'width':
            param.save_output_param.resize_param.width,
            'height_scale':
            param.save_output_param.resize_param.height_scale,
            'width_scale':
            param.save_output_param.resize_param.width_scale,
            'pad_mode':
            pad_mode,
            'pad_value':
            ','.join(
                str(x)
                for x in param.save_output_param.resize_param.pad_value),
            'interp_mode':
            interp_mode,
        }

        # these params can be omitted in caffe.proto and in param as consequence,
        # so check if it is set or set to default
        fields = [field[0].name for field in param.ListFields()]
        if 'input_width' in fields:
            attrs['input_width'] = param.input_width
        if 'input_height' in fields:
            attrs['input_height'] = param.input_height
        if 'normalized' in fields:
            attrs['normalized'] = int(param.normalized)
        if 'objectness_score' in fields:
            attrs['objectness_score'] = param.objectness_score

        mapping_rule = merge_attrs(param, attrs)

        # update the attributes of the node
        DetectionOutput.update_node_stat(node, mapping_rule)
        return cls.enabled
Example #4
0
    def extract(cls, node):
        nms_threshold = onnx_attr(node, 'nms_threshold', 'f', default=0.0)
        eta = onnx_attr(node, 'eta', 'f', default=0.0)
        top_k = onnx_attr(node, 'top_k', 'i', default=-1)

        code_type_values = {
            b"CORNER": "caffe.PriorBoxParameter.CORNER",
            b"CENTER_SIZE": "caffe.PriorBoxParameter.CENTER_SIZE",
        }

        code_type = onnx_attr(node, 'code_type', 's', default=code_type_values[b"CORNER"])
        try:
            code_type = code_type_values[code_type]
        except KeyError:
            raise Error("Incorrect value of code_type parameter {}".format(code_type))

        resize_mode_values = {
            b"": "",
            b"WARP": "caffe.ResizeParameter.WARP",
            b"FIT_SMALL_SIZE": "caffe.ResizeParameter.FIT_SMALL_SIZE",
            b"FIT_LARGE_SIZE_AND_PAD": "caffe.ResizeParameter.FIT_LARGE_SIZE_AND_PAD",
        }
        resize_mode = onnx_attr(node, 'resize_mode', 's', default=b"")
        try:
            resize_mode = resize_mode_values[resize_mode]
        except KeyError:
            raise Error("Incorrect value of resize_mode parameter {}".format(resize_mode))

        pad_mode_values = {
            b"": "",
            b"CONSTANT": "caffe.ResizeParameter.CONSTANT",
            b"MIRRORED": "caffe.ResizeParameter.MIRRORED",
            b"REPEAT_NEAREST": "caffe.ResizeParameter.REPEAT_NEAREST"
        }
        pad_mode = onnx_attr(node, 'pad_mode', 's', default=b"")
        try:
            pad_mode = pad_mode_values[pad_mode]
        except KeyError:
            raise Error("Incorrect value of pad_mode parameter {}".format(pad_mode))

        interp_mode_values = {
            b"": "",
            b"LINEAR": "caffe.ResizeParameter.LINEAR",
            b"AREA": "caffe.ResizeParameter.AREA",
            b"NEAREST": "caffe.ResizeParameter.NEAREST",
            b"CUBIC": "caffe.ResizeParameter.CUBIC",
            b"LANCZOS4": "caffe.ResizeParameter.LANCZOS4"
        }
        interp_mode = onnx_attr(node, 'interp_mode', 's', default=b"")
        try:
            interp_mode = interp_mode_values[interp_mode]
        except KeyError:
            raise Error("Incorrect value of interp_mode parameter {}".format(interp_mode))

        attrs = {
            'num_classes': onnx_attr(node, 'num_classes', 'i', default=0),
            'share_location': onnx_attr(node, 'share_location', 'i', default=0),
            'background_label_id': onnx_attr(node, 'background_label_id', 'i', default=0),
            'code_type': code_type,
            'variance_encoded_in_target': onnx_attr(node, 'variance_encoded_in_target', 'i', default=0),
            'keep_top_k': onnx_attr(node, 'keep_top_k', 'i', default=0),
            'confidence_threshold':  onnx_attr(node, 'confidence_threshold', 'f', default=0),
            'visualize_threshold': onnx_attr(node, 'visualize_threshold', 'f', default=0.6),
            # nms_param
            'nms_threshold': nms_threshold,
            'top_k': top_k,
            'eta': eta,
            # save_output_param.resize_param
            'prob': onnx_attr(node, 'prob', 'f', default=0),
            'resize_mode': resize_mode,
            'height': onnx_attr(node, 'height', 'i', default=0),
            'width': onnx_attr(node, 'width', 'i', default=0),
            'height_scale': onnx_attr(node, 'height_scale', 'i', default=0),
            'width_scale': onnx_attr(node, 'width_scale', 'i', default=0),
            'pad_mode': pad_mode,
            'pad_value': onnx_attr(node, 'pad_value', 's', default=""),
            'interp_mode': interp_mode,
            'input_width': onnx_attr(node, 'input_width', 'i', default=1),
            'input_height': onnx_attr(node, 'input_height', 'i', default=1),
            'normalized': onnx_attr(node, 'normalized', 'i', default=1),
        }

        # update the attributes of the node
        DetectionOutput.update_node_stat(node, attrs)
        return cls.enabled