<commit_before>from __future__ import division
import numpy as np

from chainer import cuda

from chainercv.utils.bbox._nms_gpu_post import _nms_gpu_post


if cuda.available:
    import cupy

    @cupy.util.memoize(for_each_device=True)
    def _load_kernel(kernel_name, code, options=()):
        assert isinstance(options, tuple)
        kernel_code = cupy.cuda.compile_with_cache(code, options=options)
        return kernel_code.get_function(kernel_name)


def non_maximum_suppression(bbox, thresh, score=None,
                            limit=None):
    """Suppress bounding boxes according to their Jaccard overlap.

    This method checks each bounding box sequentially and selects the bounding
    box if the Jaccard overlap between the bounding box and the previously
    selected bounding boxes is less than :obj:`thresh`. This method is
    mainly used as postprocessing of object detection.
    The bounding boxes are selected from ones with higher scores.
    If :obj:`score` is not provided as an argument, the bounding box
    is ordered by its index in ascending order.

    The bounding boxes are expected to be packed into a two dimensional
    tensor of shape :math:`(R, 4)`, where :math:`R` is the number of
    bounding boxes in the image. The second axis represents attributes of
    the bounding box. They are :obj:`(x_min, y_min, x_max, y_max)`,
    where the four attributes are coordinates of the top left and the
    bottom right vertices.

    :obj:`score` is a float array of shape :math:`(R,)`. Each score indicates
    confidence of prediction.

    This function accepts both :obj:`numpy.ndarray` and :obj:`cupy.ndarray` as
    inputs. Please note that both :obj:`bbox` and :obj:`score` need to be
    same type.
    The output is same type as the type of the inputs.

    Args:
        bbox (array): Bounding boxes to be transformed. The shape is
            :math:`(R, 4)`. :math:`R` is the number of bounding boxes.
        thresh (float): Thresold of Jaccard overlap.
        score (array): An array of confidences whose shape is :math:`(R,)`.
        limit (int): The upper bound of the number of the output bounding
            boxes. If it is not specified, this method selects as many
            bounding boxes as possible.

    Returns:
        array:
        An array with indices of bounding boxes that are selected. \
        The shape of this array is :math:`(K,)` and its dtype is\
        :obj:`numpy.int32`. Note that :math:`K \\leq R`.

    """

    xp = cuda.get_array_module(bbox)
    if xp == np:
        return _non_maximum_suppression_cpu(bbox, thresh, score, limit)
    else:
        return _non_maximum_suppression_gpu(bbox, thresh, score, limit)<commit_msg>add to doc on sorting outputs in descending order<commit_after>from __future__ import division
import numpy as np

from chainer import cuda

from chainercv.utils.bbox._nms_gpu_post import _nms_gpu_post


if cuda.available:
    import cupy

    @cupy.util.memoize(for_each_device=True)
    def _load_kernel(kernel_name, code, options=()):
        assert isinstance(options, tuple)
        kernel_code = cupy.cuda.compile_with_cache(code, options=options)
        return kernel_code.get_function(kernel_name)


def non_maximum_suppression(bbox, thresh, score=None,
                            limit=None):
    """Suppress bounding boxes according to their Jaccard overlap.

    This method checks each bounding box sequentially and selects the bounding
    box if the Jaccard overlap between the bounding box and the previously
    selected bounding boxes is less than :obj:`thresh`. This method is
    mainly used as postprocessing of object detection.
    The bounding boxes are selected from ones with higher scores.
    If :obj:`score` is not provided as an argument, the bounding box
    is ordered by its index in ascending order.

    The bounding boxes are expected to be packed into a two dimensional
    tensor of shape :math:`(R, 4)`, where :math:`R` is the number of
    bounding boxes in the image. The second axis represents attributes of
    the bounding box. They are :obj:`(x_min, y_min, x_max, y_max)`,
    where the four attributes are coordinates of the top left and the
    bottom right vertices.

    :obj:`score` is a float array of shape :math:`(R,)`. Each score indicates
    confidence of prediction.

    This function accepts both :obj:`numpy.ndarray` and :obj:`cupy.ndarray` as
    inputs. Please note that both :obj:`bbox` and :obj:`score` need to be
    same type.
    The output is same type as the type of the inputs.

    Args:
        bbox (array): Bounding boxes to be transformed. The shape is
            :math:`(R, 4)`. :math:`R` is the number of bounding boxes.
        thresh (float): Thresold of Jaccard overlap.
        score (array): An array of confidences whose shape is :math:`(R,)`.
        limit (int): The upper bound of the number of the output bounding
            boxes. If it is not specified, this method selects as many
            bounding boxes as possible.

    Returns:
        array:
        An array with indices of bounding boxes that are selected. \
        They are sorted by the scores of bounding boxes in descending \
        order. \
        The shape of this array is :math:`(K,)` and its dtype is\
        :obj:`numpy.int32`. Note that :math:`K \\leq R`.

    """

    xp = cuda.get_array_module(bbox)
    if xp == np:
        return _non_maximum_suppression_cpu(bbox, thresh, score, limit)
    else:
        return _non_maximum_suppression_gpu(bbox, thresh, score, limit)
{'fraction_non_alphanumeric': 0.07396077019974806, 'fraction_numerical': 0.001439625697318697, 'mean_word_length': 3.0217076700434156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4433173', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1532, 'n_words': 673}