<commit_before>import copy
import numpy as np

from chainer import reporter
import chainer.training.extensions

from chainercv.evaluations import eval_detection_coco
from chainercv.utils import apply_to_iterator


class DetectionCOCOEvaluator(chainer.training.extensions.Evaluator):

    """An extension that evaluates a detection model by MS COCO metric.

    This extension iterates over an iterator and evaluates the prediction
    results.
    The results consist of average precisions (APs) and average
    recalls (ARs) as well as the mean of each (mean average precision and mean
    average recall).
    This extension reports the following values with keys.
    Please note that if
    :obj:`label_names` is not specified, only the mAPs and  mARs are reported.

    .. csv-table::
        :header: key, description

        ap/iou=0.50:0.95/area=all/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.50/area=all/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.75/area=all/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.50:0.95/area=small/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.50:0.95/area=medium/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.50:0.95/area=large/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ar/iou=0.50:0.95/area=all/maxDets=1/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.50/area=all/maxDets=10/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.75/area=all/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.50:0.95/area=small/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.50:0.95/area=medium/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.50:0.95/area=large/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_2]_
        map/iou=0.50:0.95/area=all/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.50/area=all/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.75/area=all/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.50:0.95/area=small/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.50:0.95/area=medium/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.50:0.95/area=large/maxDets=100, \
            [#coco_det_ext_3]_
        ar/iou=0.50:0.95/area=all/maxDets=1, \
            [#coco_det_ext_4]_
        ar/iou=0.50/area=all/maxDets=10, \
            [#coco_det_ext_4]_
        ar/iou=0.75/area=all/maxDets=100, \
            [#coco_det_ext_4]_
        ar/iou=0.50:0.95/area=small/maxDets=100, \
            [#coco_det_ext_4]_
        ar/iou=0.50:0.95/area=medium/maxDets=100, \
            [#coco_det_ext_4]_
        ar/iou=0.50:0.95/area=large/maxDets=100, \
            [#coco_det_ext_4]_

    .. [#coco_det_ext_1] Average precision for class \
        :obj:`label_names[l]`, where :math:`l` is the index of the class. \
        If class :math:`l` does not exist in either :obj:`pred_labels` or \
        :obj:`gt_labels`, the corresponding value is set to :obj:`numpy.nan`.
    .. [#coco_det_ext_2] Average recall for class \
        :obj:`label_names[l]`, where :math:`l` is the index of the class. \
        If class :math:`l` does not exist in either :obj:`pred_labels` or \
        :obj:`gt_labels`, the corresponding value is set to :obj:`numpy.nan`.
    .. [#coco_det_ext_3] The average of average precisions over classes.
    .. [#coco_det_ext_4] The average of average recalls over classes.

    Args:
        iterator (chainer.Iterator): An iterator. Each sample should be
            following tuple :obj:`img, bbox, label, area, crowded`.
        target (chainer.Link): A detection link. This link must have
            :meth:`predict` method that takes a list of images and returns
            :obj:`bboxes`, :obj:`labels` and :obj:`scores`.
        label_names (iterable of strings): An iterable of names of classes.
            If this value is specified, average precision and average
            recalls for each class are reported.

    """

    trigger = 1, 'epoch'
    default_name = 'validation'
    priority = chainer.training.PRIORITY_WRITER

    def __init__(
            self, iterator, target,
            label_names=None):
        super(DetectionCOCOEvaluator, self).__init__(
            iterator, target)
        self.label_names = label_names

    def evaluate(self):
        iterator = self._iterators['main']
        target = self._targets['main']
<commit_msg>raise Error at initialization when pycocotools is not installed<commit_after>import copy
import numpy as np

from chainer import reporter
import chainer.training.extensions

from chainercv.evaluations import eval_detection_coco
from chainercv.utils import apply_to_iterator

try:
    import pycocotools.coco  # NOQA
    _available = True
except ImportError:
    _available = False


class DetectionCOCOEvaluator(chainer.training.extensions.Evaluator):

    """An extension that evaluates a detection model by MS COCO metric.

    This extension iterates over an iterator and evaluates the prediction
    results.
    The results consist of average precisions (APs) and average
    recalls (ARs) as well as the mean of each (mean average precision and mean
    average recall).
    This extension reports the following values with keys.
    Please note that if
    :obj:`label_names` is not specified, only the mAPs and  mARs are reported.

    .. csv-table::
        :header: key, description

        ap/iou=0.50:0.95/area=all/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.50/area=all/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.75/area=all/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.50:0.95/area=small/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.50:0.95/area=medium/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ap/iou=0.50:0.95/area=large/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_1]_
        ar/iou=0.50:0.95/area=all/maxDets=1/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.50/area=all/maxDets=10/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.75/area=all/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.50:0.95/area=small/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.50:0.95/area=medium/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_2]_
        ar/iou=0.50:0.95/area=large/maxDets=100/<label_names[l]>, \
            [#coco_det_ext_2]_
        map/iou=0.50:0.95/area=all/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.50/area=all/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.75/area=all/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.50:0.95/area=small/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.50:0.95/area=medium/maxDets=100, \
            [#coco_det_ext_3]_
        map/iou=0.50:0.95/area=large/maxDets=100, \
            [#coco_det_ext_3]_
        ar/iou=0.50:0.95/area=all/maxDets=1, \
            [#coco_det_ext_4]_
        ar/iou=0.50/area=all/maxDets=10, \
            [#coco_det_ext_4]_
        ar/iou=0.75/area=all/maxDets=100, \
            [#coco_det_ext_4]_
        ar/iou=0.50:0.95/area=small/maxDets=100, \
            [#coco_det_ext_4]_
        ar/iou=0.50:0.95/area=medium/maxDets=100, \
            [#coco_det_ext_4]_
        ar/iou=0.50:0.95/area=large/maxDets=100, \
            [#coco_det_ext_4]_

    .. [#coco_det_ext_1] Average precision for class \
        :obj:`label_names[l]`, where :math:`l` is the index of the class. \
        If class :math:`l` does not exist in either :obj:`pred_labels` or \
        :obj:`gt_labels`, the corresponding value is set to :obj:`numpy.nan`.
    .. [#coco_det_ext_2] Average recall for class \
        :obj:`label_names[l]`, where :math:`l` is the index of the class. \
        If class :math:`l` does not exist in either :obj:`pred_labels` or \
        :obj:`gt_labels`, the corresponding value is set to :obj:`numpy.nan`.
    .. [#coco_det_ext_3] The average of average precisions over classes.
    .. [#coco_det_ext_4] The average of average recalls over classes.

    Args:
        iterator (chainer.Iterator): An iterator. Each sample should be
            following tuple :obj:`img, bbox, label, area, crowded`.
        target (chainer.Link): A detection link. This link must have
            :meth:`predict` method that takes a list of images and returns
            :obj:`bboxes`, :obj:`labels` and :obj:`scores`.
        label_names (iterable of strings): An iterable of names of classes.
            If this value is specified, average precision and average
            recalls for each class are reported.

    """

    trigger = 1, 'epoch'
    default_name = 'validation'
    priority = chainer.training.PRIORITY_WRITER

    def __init__(
            self, iterator, target,
            label_names=None):
        if not _available:
            raise ValueError(
                'Please install pycocotools \n'
                'pip install -e \'git+https://github.com/pdollar/coco.git'
                '#egg=pycocotools&subdirectory=PythonAPI\'')
        super(DetectionCOCOEvaluator, self).__init__(
            iterator, target)
        self.label_names = label_names

    def evaluate(self):
        iterator = self._iterators['main']
        target = self._targets['main']

{'fraction_non_alphanumeric': 0.12696280991735537, 'fraction_numerical': 0.04442148760330578, 'mean_word_length': 2.4439701173959447, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, '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': '6437328', 'n_tokens_mistral': 3762, 'n_tokens_neox': 3495, 'n_words': 658}