Team Ai
Apppublic

Arulkumar03/Wheat_HEAD_Detection_Counting_ComputerVision_Model

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
fast_eval_api.py122 linesDownload Raw Back to evaluation
1# Copyright (c) Facebook, Inc. and its affiliates.2import copy3import logging4import numpy as np5import time6from pycocotools.cocoeval import COCOeval7 8from detectron2 import _C9 10logger = logging.getLogger(__name__)11 12 13class COCOeval_opt(COCOeval):14    """15    This is a slightly modified version of the original COCO API, where the functions evaluateImg()16    and accumulate() are implemented in C++ to speedup evaluation17    """18 19    def evaluate(self):20        """21        Run per image evaluation on given images and store results in self.evalImgs_cpp, a22        datastructure that isn't readable from Python but is used by a c++ implementation of23        accumulate().  Unlike the original COCO PythonAPI, we don't populate the datastructure24        self.evalImgs because this datastructure is a computational bottleneck.25        :return: None26        """27        tic = time.time()28 29        p = self.params30        # add backward compatibility if useSegm is specified in params31        if p.useSegm is not None:32            p.iouType = "segm" if p.useSegm == 1 else "bbox"33        logger.info("Evaluate annotation type *{}*".format(p.iouType))34        p.imgIds = list(np.unique(p.imgIds))35        if p.useCats:36            p.catIds = list(np.unique(p.catIds))37        p.maxDets = sorted(p.maxDets)38        self.params = p39 40        self._prepare()  # bottleneck41 42        # loop through images, area range, max detection number43        catIds = p.catIds if p.useCats else [-1]44 45        if p.iouType == "segm" or p.iouType == "bbox":46            computeIoU = self.computeIoU47        elif p.iouType == "keypoints":48            computeIoU = self.computeOks49        self.ious = {50            (imgId, catId): computeIoU(imgId, catId) for imgId in p.imgIds for catId in catIds51        }  # bottleneck52 53        maxDet = p.maxDets[-1]54 55        # <<<< Beginning of code differences with original COCO API56        def convert_instances_to_cpp(instances, is_det=False):57            # Convert annotations for a list of instances in an image to a format that's fast58            # to access in C++59            instances_cpp = []60            for instance in instances:61                instance_cpp = _C.InstanceAnnotation(62                    int(instance["id"]),63                    instance["score"] if is_det else instance.get("score", 0.0),64                    instance["area"],65                    bool(instance.get("iscrowd", 0)),66                    bool(instance.get("ignore", 0)),67                )68                instances_cpp.append(instance_cpp)69            return instances_cpp70 71        # Convert GT annotations, detections, and IOUs to a format that's fast to access in C++72        ground_truth_instances = [73            [convert_instances_to_cpp(self._gts[imgId, catId]) for catId in p.catIds]74            for imgId in p.imgIds75        ]76        detected_instances = [77            [convert_instances_to_cpp(self._dts[imgId, catId], is_det=True) for catId in p.catIds]78            for imgId in p.imgIds79        ]80        ious = [[self.ious[imgId, catId] for catId in catIds] for imgId in p.imgIds]81 82        if not p.useCats:83            # For each image, flatten per-category lists into a single list84            ground_truth_instances = [[[o for c in i for o in c]] for i in ground_truth_instances]85            detected_instances = [[[o for c in i for o in c]] for i in detected_instances]86 87        # Call C++ implementation of self.evaluateImgs()88        self._evalImgs_cpp = _C.COCOevalEvaluateImages(89            p.areaRng, maxDet, p.iouThrs, ious, ground_truth_instances, detected_instances90        )91        self._evalImgs = None92 93        self._paramsEval = copy.deepcopy(self.params)94        toc = time.time()95        logger.info("COCOeval_opt.evaluate() finished in {:0.2f} seconds.".format(toc - tic))96        # >>>> End of code differences with original COCO API97 98    def accumulate(self):99        """100        Accumulate per image evaluation results and store the result in self.eval.  Does not101        support changing parameter settings from those used by self.evaluate()102        """103        logger.info("Accumulating evaluation results...")104        tic = time.time()105        assert hasattr(106            self, "_evalImgs_cpp"107        ), "evaluate() must be called before accmulate() is called."108 109        self.eval = _C.COCOevalAccumulate(self._paramsEval, self._evalImgs_cpp)110 111        # recall is num_iou_thresholds X num_categories X num_area_ranges X num_max_detections112        self.eval["recall"] = np.array(self.eval["recall"]).reshape(113            self.eval["counts"][:1] + self.eval["counts"][2:]114        )115 116        # precision and scores are num_iou_thresholds X num_recall_thresholds X num_categories X117        # num_area_ranges X num_max_detections118        self.eval["precision"] = np.array(self.eval["precision"]).reshape(self.eval["counts"])119        self.eval["scores"] = np.array(self.eval["scores"]).reshape(self.eval["counts"])120        toc = time.time()121        logger.info("COCOeval_opt.accumulate() finished in {:0.2f} seconds.".format(toc - tic))122