MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / COCODetection

Class COCODetection

examples/FasterRCNN/dataset/coco.py:17–218  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15
16
17class COCODetection(DatasetSplit):
18 # handle a few special splits whose names do not match the directory names
19 _INSTANCE_TO_BASEDIR = {
20 'valminusminival2014': 'val2014',
21 'minival2014': 'val2014',
22 'val2017_100': 'val2017',
23 }
24
25 """
26 Mapping from the incontinuous COCO category id to an id in [1, #category]
27 For your own coco-format, dataset, change this to an **empty dict**.
28 """
29 COCO_id_to_category_id = {13: 12, 14: 13, 15: 14, 16: 15, 17: 16, 18: 17, 19: 18, 20: 19, 21: 20, 22: 21, 23: 22, 24: 23, 25: 24, 27: 25, 28: 26, 31: 27, 32: 28, 33: 29, 34: 30, 35: 31, 36: 32, 37: 33, 38: 34, 39: 35, 40: 36, 41: 37, 42: 38, 43: 39, 44: 40, 46: 41, 47: 42, 48: 43, 49: 44, 50: 45, 51: 46, 52: 47, 53: 48, 54: 49, 55: 50, 56: 51, 57: 52, 58: 53, 59: 54, 60: 55, 61: 56, 62: 57, 63: 58, 64: 59, 65: 60, 67: 61, 70: 62, 72: 63, 73: 64, 74: 65, 75: 66, 76: 67, 77: 68, 78: 69, 79: 70, 80: 71, 81: 72, 82: 73, 84: 74, 85: 75, 86: 76, 87: 77, 88: 78, 89: 79, 90: 80} # noqa
30
31 def __init__(self, basedir, split):
32 """
33 Args:
34 basedir (str): root of the dataset which contains the subdirectories for each split and annotations
35 split (str): the name of the split, e.g. "train2017".
36 The split has to match an annotation file in "annotations/" and a directory of images.
37
38 Examples:
39 For a directory of this structure:
40
41 DIR/
42 annotations/
43 instances_XX.json
44 instances_YY.json
45 XX/
46 YY/
47
48 use `COCODetection(DIR, 'XX')` and `COCODetection(DIR, 'YY')`
49 """
50 basedir = os.path.expanduser(basedir)
51 self._imgdir = os.path.realpath(os.path.join(
52 basedir, self._INSTANCE_TO_BASEDIR.get(split, split)))
53 assert os.path.isdir(self._imgdir), "{} is not a directory!".format(self._imgdir)
54 annotation_file = os.path.join(
55 basedir, 'annotations/instances_{}.json'.format(split))
56 assert os.path.isfile(annotation_file), annotation_file
57
58 from pycocotools.coco import COCO
59 self.coco = COCO(annotation_file)
60 self.annotation_file = annotation_file
61 logger.info("Instances loaded from {}.".format(annotation_file))
62
63 # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
64 def print_coco_metrics(self, results):
65 """
66 Args:
67 results(list[dict]): results in coco format
68 Returns:
69 dict: the evaluation metrics
70 """
71 from pycocotools.cocoeval import COCOeval
72 ret = {}
73 has_mask = "segmentation" in results[0] # results will be modified by loadRes
74

Callers 2

register_cocoFunction · 0.85
coco.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…