def __init__(self, root=None, split='train', mode=None, transform=None, **kwargs): root = root if root is not None else os.path.join( dataset_dir(), 'COCO') super(MSCOCO, self).__init__(root, split, mode, transform, **kwargs) try_import_pycocotools() from pycocotools.coco import COCO from pycocotools import mask if split == 'train': print('train set') ann_file = os.path.join(root, 'annotations/instances_train2017.json') ids_file = os.path.join(root, 'annotations/train_ids.mx') self.root = os.path.join(root, 'train2017') else: print('val set') ann_file = os.path.join(root, 'annotations/instances_val2017.json') ids_file = os.path.join(root, 'annotations/val_ids.mx') self.root = os.path.join(root, 'val2017') self.coco = COCO(ann_file) self.coco_mask = mask if os.path.exists(ids_file): with open(ids_file, 'rb') as f: self.ids = pickle.load(f) else: ids = list(self.coco.imgs.keys()) self.ids = self._preprocess(ids, ids_file) self.transform = transform
def _load_jsons(self): """Load all image paths and labels from JSON annotation files into buffer.""" samples = dict() labels = dict() # lazy import pycocotools try_import_pycocotools() from pycocotools.coco import COCO for split in self._splits: anno = os.path.join(self.root, self.annotation_dir, split) + '.json' _coco = COCO(anno) self._coco.append(_coco) classes = [c['name'] for c in _coco.loadCats(_coco.getCatIds())] if not classes == self.classes: raise ValueError("Incompatible category names with COCO: ") assert classes == self.classes json_id_to_contiguous = { v: k for k, v in enumerate(_coco.getCatIds()) } if self.json_id_to_contiguous is None: self.json_id_to_contiguous = json_id_to_contiguous self.contiguous_id_to_json = { v: k for k, v in self.json_id_to_contiguous.items() } else: assert self.json_id_to_contiguous == json_id_to_contiguous # iterate through the annotations image_ids = sorted(_coco.getImgIds()) for entry in _coco.loadImgs(image_ids): abs_path = self._parse_image_path(entry) if not os.path.exists(abs_path): raise IOError('Image: {} not exists.'.format(abs_path)) label = self._check_load_bbox(_coco, entry) if not label: continue sample_id = len(samples) samples[sample_id] = abs_path labels[sample_id] = label if sample_id not in self._im_shapes: self._im_shapes[sample_id] = (entry['width'], entry['height']) return samples, labels
def _load_jsons(self): """Load all image paths and labels from JSON annotation files into buffer.""" images = dict() boxes = dict() captions = dict() # lazy import pycocotools try_import_pycocotools() from pycocotools.coco import COCO for split in self._splits: anno = os.path.join(self.root, self.annotation_dir, split) + '.json' _coco = COCO(anno) self._coco.append(_coco) anno = os.path.join(self.root, self.annotation_dir, split.replace('instances', 'captions')) + '.json' _coco_cap = COCO(anno) self._coco.append(_coco_cap) categories = [c['name'] for c in _coco.loadCats(_coco.getCatIds())] if not categories == self.categories: raise ValueError("Incompatible category names with COCO: ") assert categories == self.categories json_id_to_contiguous = { v: k for k, v in enumerate(_coco.getCatIds())} if self.json_id_to_contiguous is None: self.json_id_to_contiguous = json_id_to_contiguous self.contiguous_id_to_json = { v: k for k, v in self.json_id_to_contiguous.items()} else: assert self.json_id_to_contiguous == json_id_to_contiguous # iterate through the annotations for img_id, entry in zip(sorted(_coco.getImgIds()), _coco.loadImgs(sorted(_coco.getImgIds()))): abs_path = self._parse_image_path(entry) if not os.path.exists(abs_path): raise IOError('Image: {} not exists.'.format(abs_path)) box = self._check_load_bbox(_coco, entry) caption = self._load_captions(_coco_cap, entry) if not box and not caption: print("%s doesn't have any boxes or captions" % img_id) continue images[img_id] = abs_path boxes[img_id] = box captions[img_id] = caption return images, boxes, captions
def _load_data(self): """Load all image paths and labels from JSON annotation files into buffer.""" items = [] labels = [] im_aspect_ratios = [] # lazy import pycocotools try_import_pycocotools() from pycocotools.coco import COCO convert_gt_to_coco(self.data_path, self.channel, self.image_path, self.field_name, self.annotation_path) anno = os.path.join(self.data_path, self.annotation_path) _coco = COCO(anno) self._coco.append(_coco) classes = [c['name'] for c in _coco.loadCats(_coco.getCatIds())] if not classes == self.classes: raise ValueError("Incompatible category names with COCO: ") assert classes == self.classes json_id_to_contiguous = {v: k for k, v in enumerate(_coco.getCatIds())} if self.json_id_to_contiguous is None: self.json_id_to_contiguous = json_id_to_contiguous self.contiguous_id_to_json = { v: k for k, v in self.json_id_to_contiguous.items() } else: assert self.json_id_to_contiguous == json_id_to_contiguous # iterate through the annotations image_ids = sorted(_coco.getImgIds()) for entry in _coco.loadImgs(image_ids): abs_path = self._parse_image_path(entry) if not os.path.exists(abs_path): raise IOError('Image: {} not exists.'.format(abs_path)) label = self._check_load_bbox(_coco, entry) if not label: continue im_aspect_ratios.append(float(entry['width']) / entry['height']) items.append(abs_path) labels.append(label) return items, labels, im_aspect_ratios
def _update(self): """Use coco to get real scores. """ if not self._current_id == len(self._img_ids): warnings.warn( 'Recorded {} out of {} validation images, incomplete results'. format(self._current_id, len(self._img_ids))) if not self._results: # in case of empty results, push a dummy result self._results.append({ 'image_id': self._img_ids[0], 'category_id': 0, 'bbox': [0, 0, 0, 0], 'score': 0 }) import json try: with open(self._filename, 'w') as f: json.dump(self._results, f) except IOError as e: raise RuntimeError( "Unable to dump json file, ignored. What(): {}".format(str(e))) # lazy import pycocotools try_import_pycocotools() if not hasattr( self.dataset, 'coco' ): # it doesn't have a coco, probably isn't a coco set, so let us make a coco json_path = self.dataset.build_coco_json() from pycocotools.coco import COCO self.dataset.coco = COCO(json_path) pred = self.dataset.coco.loadRes(self._filename) gt = self.dataset.coco from pycocotools.cocoeval import COCOeval coco_eval = COCOeval(gt, pred, 'bbox') # coco_eval.params.recThrs = np.linspace(.0, 1.00, np.round((1.00 - .0) / .1) + 1, endpoint=True) ## tried to get the same as voc but still not same coco_eval.evaluate() coco_eval.accumulate() self._coco_eval = coco_eval return coco_eval
for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with zipfile.ZipFile(filename) as zf: zf.extractall(path=path) if __name__ == '__main__': args = parse_args() path = os.path.expanduser(args.download_dir) if not os.path.isdir(path) or not os.path.isdir(os.path.join(path, 'train2017')) \ or not os.path.isdir(os.path.join(path, 'val2017')) \ or not os.path.isdir(os.path.join(path, 'annotations')): if args.no_download: raise ValueError( ('{} is not a valid directory, make sure it is present.' ' Or you should not disable "--no-download" to grab it'. format(path))) else: download_coco(path, overwrite=args.overwrite) # make symlink makedirs(os.path.expanduser('~/.mxnet/datasets')) if os.path.isdir(_TARGET_DIR): os.remove(_TARGET_DIR) os.symlink(path, _TARGET_DIR) try_import_pycocotools()
# '46cdcf715b6b4f67e980b529534e79c2edffe084'), # test2017.zip, for those who want to attend the competition. # ('http://images.cocodataset.org/zips/test2017.zip', # '4e443f8a2eca6b1dac8a6c57641b67dd40621a49'), ] makedirs(path) for url, checksum in _DOWNLOAD_URLS: filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with zipfile.ZipFile(filename) as zf: zf.extractall(path=path) if __name__ == '__main__': args = parse_args() path = os.path.expanduser(args.download_dir) if not os.path.isdir(path) or not os.path.isdir(os.path.join(path, 'train2017')) \ or not os.path.isdir(os.path.join(path, 'val2017')) \ or not os.path.isdir(os.path.join(path, 'annotations')): if args.no_download: raise ValueError(('{} is not a valid directory, make sure it is present.' ' Or you should not disable "--no-download" to grab it'.format(path))) else: download_coco(path, overwrite=args.overwrite) # make symlink makedirs(os.path.expanduser('~/.mxnet/datasets')) if os.path.isdir(_TARGET_DIR): os.remove(_TARGET_DIR) os.symlink(path, _TARGET_DIR) try_import_pycocotools()