Ejemplo n.º 1
0
def test_check_valid_attributes():
    attr_dict = {
        "mode": "bilinear",
        "coefficients": [1, 2, 3, 4, 5],
    }

    def _is_supported_mode(x):
        return x in ["linear", "area", "cubic", "bilinear"]

    requirements = [
        ("width", False, np.unsignedinteger, None),
        ("mode", True, np.str_, _is_supported_mode),
        ("coefficients", True, np.integer, lambda x: x > 0),
        ("alpha", False, np.float64, None),
    ]

    assert check_valid_attributes("TestOp", attr_dict, requirements)

    requirements[3] = ("alpha", True, np.float64, None)
    try:
        check_valid_attributes("TestOp", attr_dict, requirements)
    except UserInputError:
        pass
    else:
        raise AssertionError(
            "Validation of missing required attribute has unexpectedly passed."
        )
Ejemplo n.º 2
0
def proposal(
    class_probs: Node,
    bbox_deltas: Node,
    image_shape: NodeInput,
    attrs: dict,
    name: Optional[str] = None,
) -> Node:
    """Filter bounding boxes and outputs only those with the highest prediction confidence.

    :param  class_probs:        4D input floating point tensor with class prediction scores.
    :param  bbox_deltas:        4D input floating point tensor with corrected predictions of bounding boxes
    :param  image_shape:        The 1D input tensor with 3 or 4 elements describing image shape.
    :param  attrs:              The dictionary containing key, value pairs for attributes.
    :param  name:               Optional name for the output node.
    * base_size     The size of the anchor to which scale and ratio attributes are applied.
                    Range of values: a positive unsigned integer number
                    Default value: None
                    Required: yes
    * pre_nms_topn  The number of bounding boxes before the NMS operation.
                    Range of values: a positive unsigned integer number
                    Default value: None
                    Required: yes
    * post_nms_topn The number of bounding boxes after the NMS operation.
                    Range of values: a positive unsigned integer number
                    Default value: None
                    Required: yes
    * nms_thresh    The minimum value of the proposal to be taken into consideration.
                    Range of values: a positive floating-point number
                    Default value: None
                    Required: yes
    * feat_stride   The step size to slide over boxes (in pixels).
                    Range of values: a positive unsigned integer
                    Default value: None
                    Required: yes
    * min_size      The minimum size of box to be taken into consideration.
                    Range of values: a positive unsigned integer number
                    Default value: None
                    Required: yes
    * ratio         The ratios for anchor generation.
                    Range of values: a list of floating-point numbers
                    Default value: None
                    Required: yes
    * scale         The scales for anchor generation.
                    Range of values: a list of floating-point numbers
                    Default value: None
                    Required: yes
    * clip_before_nms   The flag that specifies whether to perform clip bounding boxes before
                        non-maximum suppression or not.
                        Range of values: True or False
                        Default value: True
                        Required: no
    * clip_after_nms    The flag that specifies whether to perform clip bounding boxes after
                        non-maximum suppression or not.
                        Range of values: True or False
                        Default value: False
                        Required: no
    * normalize     The flag that specifies whether to perform normalization of output boxes to
                    [0,1] interval or not.
                    Range of values: True or False
                    Default value: False
                    Required: no
    * box_size_scale    Specifies the scale factor applied to logits of box sizes before decoding.
                        Range of values: a positive floating-point number
                        Default value: 1.0
                        Required: no
    * box_coordinate_scale  Specifies the scale factor applied to logits of box coordinates
                            before decoding.
                            Range of values: a positive floating-point number
                            Default value: 1.0
                            Required: no
    * framework     Specifies how the box coordinates are calculated.
                    Range of values: "" (empty string) - calculate box coordinates like in Caffe*
                                     tensorflow - calculate box coordinates like in the TensorFlow*
                                                  Object Detection API models
                    Default value: "" (empty string)
                    Required: no
    Example of attribute dictionary:
    .. code-block:: python
        # just required ones
        attrs = {
            'base_size': 85,
            'pre_nms_topn': 10,
            'post_nms_topn': 20,
            'nms_thresh': 0.34,
            'feat_stride': 16,
            'min_size': 32,
            'ratio': [0.1, 1.5, 2.0, 2.5],
            'scale': [2, 3, 3, 4],
        }
    Optional attributes which are absent from dictionary will be set with corresponding default.
    :return: Node representing Proposal operation.
    """
    requirements = [
        ("base_size", True, np.unsignedinteger, is_positive_value),
        ("pre_nms_topn", True, np.unsignedinteger, is_positive_value),
        ("post_nms_topn", True, np.unsignedinteger, is_positive_value),
        ("nms_thresh", True, np.floating, is_positive_value),
        ("feat_stride", True, np.unsignedinteger, is_positive_value),
        ("min_size", True, np.unsignedinteger, is_positive_value),
        ("ratio", True, np.floating, None),
        ("scale", True, np.floating, None),
        ("clip_before_nms", False, np.bool_, None),
        ("clip_after_nms", False, np.bool_, None),
        ("normalize", False, np.bool_, None),
        ("box_size_scale", False, np.floating, is_positive_value),
        ("box_coordinate_scale", False, np.floating, is_positive_value),
        ("framework", False, np.str_, None),
    ]

    check_valid_attributes("Proposal", attrs, requirements)

    return _get_node_factory_opset4().create(
        "Proposal", [class_probs, bbox_deltas,
                     as_node(image_shape)], attrs)
Ejemplo n.º 3
0
def detection_output(
    box_logits: NodeInput,
    class_preds: NodeInput,
    proposals: NodeInput,
    attrs: dict,
    aux_class_preds: Optional[NodeInput] = None,
    aux_box_preds: Optional[NodeInput] = None,
    name: Optional[str] = None,
) -> Node:
    """Generate the detection output using information on location and confidence predictions.

     Available attributes are:
    * background_label_id   The background label id.
                            Range of values: integer value
                            Default value: 0
                            Required: no
    * top_k                 Maximum number of results to be kept per batch after NMS step.
                            Range of values: integer value
                            Default value: -1
                            Required: no
    * variance_encoded_in_target    The flag that denotes if variance is encoded in target.
                                    Range of values: {False, True}
                                    Default value: False
                                    Required: no
    * keep_top_k            Maximum number of bounding boxes per batch to be kept after NMS step.
                            Range of values: integer values
                            Default value: None
                            Required: yes
    * code_type             The type of coding method for bounding boxes.
                            Range of values: {'caffe.PriorBoxParameter.CENTER_SIZE',
                                             'caffe.PriorBoxParameter.CORNER'}
                            Default value: 'caffe.PriorBoxParameter.CORNER'
                            Required: no
    * share_location        The flag that denotes if bounding boxes are shared among different
                            classes.
                            Range of values: {True, False}
                            Default value: True
                            Required: no
    * nms_threshold         The threshold to be used in the NMS stage.
                            Range of values: floating point value
                            Default value: None
                            Required: yes
    * confidence_threshold  Specifies the minimum confidence threshold for detection boxes to be
                            considered.
                            Range of values: floating point value
                            Default value: 0
                            Required: no
    * clip_after_nms        The flag that denotes whether to perform clip bounding boxes after
                            non-maximum suppression or not.
                            Range of values: {True, False}
                            Default value: False
                            Required: no
    * clip_before_nms       The flag that denotes whether to perform clip bounding boxes before
                            non-maximum suppression or not.
                            Range of values: {True, False}
                            Default value: False
                            Required: no
    * decrease_label_id     The flag that denotes how to perform NMS.
                            Range of values: False - perform NMS like in Caffe*.
                                             True  - perform NMS like in MxNet*.
                            Default value: False
                            Required: no
    * normalized            The flag that denotes whether input tensors with boxes are normalized.
                            Range of values: {True, False}
                            Default value: False
                            Required: no
    * input_height          The input image height.
                            Range of values: positive integer number
                            Default value: 1
                            Required: no
    * input_width           The input image width.
                            Range of values: positive integer number
                            Default value: 1
                            Required: no
    * objectness_score      The threshold to sort out confidence predictions.
                            Range of values: non-negative float number
                            Default value: 0
                            Required: no
    Example of attribute dictionary:

    .. code-block:: python

        # just required ones
        attrs = {
            'keep_top_k': [1, 2, 3],
            'nms_threshold': 0.645,
        }
        attrs = {
            'keep_top_k': [1, 2, 3],
            'nms_threshold': 0.645,
            'normalized': True,
            'clip_before_nms': True,
            'input_height': [32],
            'input_width': [32],
        }

    Optional attributes which are absent from dictionary will be set with corresponding default.

    :param  box_logits:         The 2D input tensor with box logits.
    :param  class_preds:        The 2D input tensor with class predictions.
    :param  proposals:          The 3D input tensor with proposals.
    :param  attrs:              The dictionary containing key, value pairs for attributes.
    :param  aux_class_preds:    The 2D input tensor with additional class predictions information.
    :param  aux_box_preds:      The 2D input tensor with additional box predictions information.
    :param  name:               Optional name for the output node.
    :return: Node representing DetectionOutput operation.
    """
    requirements = [
        ("background_label_id", False, np.integer, None),
        ("top_k", False, np.integer, None),
        ("variance_encoded_in_target", False, np.bool_, None),
        ("keep_top_k", True, np.integer, None),
        ("code_type", False, np.str_, None),
        ("share_location", False, np.bool_, None),
        ("nms_threshold", True, np.floating, None),
        ("confidence_threshold", False, np.floating, None),
        ("clip_after_nms", False, np.bool_, None),
        ("clip_before_nms", False, np.bool_, None),
        ("decrease_label_id", False, np.bool_, None),
        ("normalized", False, np.bool_, None),
        ("input_height", False, np.integer, is_positive_value),
        ("input_width", False, np.integer, is_positive_value),
        ("objectness_score", False, np.floating, is_non_negative_value),
    ]

    check_valid_attributes("DetectionOutput", attrs, requirements)

    inputs = [box_logits, class_preds, proposals]
    if aux_class_preds is not None:
        inputs.append(aux_class_preds)
    if aux_box_preds is not None:
        inputs.append(aux_box_preds)
    inputs = as_nodes(*inputs)

    return _get_node_factory_opset8().create("DetectionOutput", inputs, attrs)
Ejemplo n.º 4
0
def prior_box(layer_shape: Node,
              image_shape: NodeInput,
              attrs: dict,
              name: Optional[str] = None) -> Node:
    """Generate prior boxes of specified sizes and aspect ratios across all dimensions.

     Available attributes are:
    * min_size                      The minimum box size (in pixels).
                                    Range of values: positive floating point numbers
                                    Default value: []
                                    Required: no
    * max_size                      The maximum box size (in pixels).
                                    Range of values: positive floating point numbers
                                    Default value: []
                                    Required: no
    * aspect_ratio                  Aspect ratios of prior boxes.
                                    Range of values: set of positive floating point numbers
                                    Default value: []
                                    Required: no
    * flip                          The flag that denotes that each aspect_ratio is duplicated and flipped.
                                    Range of values: {True, False}
                                    Default value: False
                                    Required: no
    * clip                          The flag that denotes if each value in the output tensor should be clipped
                                    to [0,1] interval.
                                    Range of values: {True, False}
                                    Default value: False
                                    Required: no
    * step                          The distance between box centers.
                                    Range of values: floating point non-negative number
                                    Default value: 0
                                    Required: no
    * offset                        This is a shift of box respectively to top left corner.
                                    Range of values: floating point non-negative number
                                    Default value: None
                                    Required: yes
    * variance                      The variance denotes a variance of adjusting bounding boxes. The attribute
                                    could contain 0, 1 or 4 elements.
                                    Range of values: floating point positive numbers
                                    Default value: []
                                    Required: no
    * scale_all_sizes               The flag that denotes type of inference.
                                    Range of values: False - max_size is ignored
                                                     True  - max_size is used
                                    Default value: True
                                    Required: no
    * fixed_ratio                   This is an aspect ratio of a box.
                                    Range of values: a list of positive floating-point numbers
                                    Default value: None
                                    Required: no
    * fixed_size                    This is an initial box size (in pixels).
                                    Range of values: a list of positive floating-point numbers
                                    Default value: None
                                    Required: no
    * density                       This is the square root of the number of boxes of each type.
                                    Range of values: a list of positive floating-point numbers
                                    Default value: None
                                    Required: no
    * min_max_aspect_ratios_order   The flag that denotes the order of output prior box.
                                    Range of values: False - the output prior box is in order of
                                                             [min, aspect_ratios, max]
                                                     True  - the output prior box is in order of
                                                             [min, max, aspect_ratios]
                                    Default value: True
                                    Required: no
    Example of attribute dictionary:

    .. code-block:: python

        # just required ones
        attrs = {
            'offset': 85,
        }
        attrs = {
            'offset': 85,
            'flip': True,
            'clip': True,
            'fixed_size': [32, 64, 128]
        }

    Optional attributes which are absent from dictionary will be set with corresponding default.

    :param  layer_shape:  Shape of layer for which prior boxes are computed.
    :param  image_shape:  Shape of image to which prior boxes are scaled.
    :param  attrs:        The dictionary containing key, value pairs for attributes.
    :param  name:         Optional name for the output node.
    :return: Node representing prior box operation.
    """
    requirements = [
        ("offset", True, np.floating, is_non_negative_value),
        ("min_size", False, np.floating, is_positive_value),
        ("max_size", False, np.floating, is_positive_value),
        ("aspect_ratio", False, np.floating, is_positive_value),
        ("flip", False, np.bool_, None),
        ("clip", False, np.bool_, None),
        ("step", False, np.floating, is_non_negative_value),
        ("variance", False, np.floating, is_positive_value),
        ("scale_all_sizes", False, np.bool_, None),
        ("fixed_ratio", False, np.floating, is_positive_value),
        ("fixed_size", False, np.floating, is_positive_value),
        ("density", False, np.floating, is_positive_value),
        ("min_max_aspect_ratios_order", False, np.bool_, None),
    ]

    check_valid_attributes("PriorBox", attrs, requirements)

    return _get_node_factory_opset8().create(
        "PriorBox", [layer_shape, as_node(image_shape)], attrs)