我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pycocotools.coco.COCO。
def __init__(self, root, annFile, transform=None, target_transform=None): from pycocotools.coco import COCO self.root = root self.coco = COCO(annFile) self.ids = list(self.coco.imgs.keys()) self.transform = transform self.target_transform = target_transform
def __init__(self, split, memoization=False): assert not memoization self.dataset = 'coco' self.coco_ids_to_internal = {k: self.cats_to_ids[v] for k, v in coco_ids_to_cats.items()} self.ids_to_coco_ids = dict(map(reversed, self.coco_ids_to_internal.items())) self.split = split assert self.split in ['train2014', 'val2014', 'test2014', 'test2015', 'minival2014', 'valminusminival2014', 'test-dev2015'] self.root = DATASETS_ROOT + 'coco/' self.included_coco_ids = list(coco_ids.values()) if 'test' in self.split: json = '%s/annotations/image_info_%s.json' else: json = '%s/annotations/instances_%s.json' self.coco = COCO(json % (self.root, self.split)) self.filenames = self.coco.getImgIds() self.num_classes = COCOLoader.num_classes self.real_split = self.split if self.real_split in ['minival2014', 'valminusminival2014']: self.real_split = 'val2014' self.coco = COCO('%s/annotations/instances_%s.json' % (self.root, self.real_split)) if 'test' in self.real_split: self.real_split = 'test2015' log.info("Created a COCO loader %s with %i images" % (split, len(self.coco.getImgIds())))
def get_loader(root, json, vocab, transform, batch_size, shuffle, num_workers): """Returns torch.utils.data.DataLoader for custom coco dataset.""" # COCO caption dataset coco = CocoDataset(root=root, json=json, vocab=vocab, transform=transform) # Data loader for COCO dataset # This will return (images, captions, lengths) for every iteration. # images: tensor of shape (batch_size, 3, 224, 224). # captions: tensor of shape (batch_size, padded_length). # lengths: list indicating valid length for each caption. length is (batch_size). data_loader = torch.utils.data.DataLoader(dataset=coco, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers, collate_fn=collate_fn) return data_loader
def __init__(self, annotations_path, class_names='all'): self.coco = COCO(annotations_path) self.class_names = class_names if self.class_names == 'all': class_data = self.coco.loadCats(self.coco.getCatIds()) self.class_names = [class_['name'] for class_ in class_data] coco_ids = [class_['id'] for class_ in class_data] one_hot_ids = list(range(1, len(coco_ids) + 1)) self.coco_id_to_class_arg = dict(zip(coco_ids, one_hot_ids)) self.class_names = ['background'] + self.class_names self.num_classes = len(self.class_names) else: """ https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoDemo.ipynb catIds = self.coco.getCatIds(catNms=self.class_names) imgIds = self.coco.getImgIds(catIds=catIds) imgIds = self.coco.getImgIds(imgIds=[324158]) """
def get_loader_single(data_name, split, root, json, vocab, transform, batch_size=100, shuffle=True, num_workers=2, ids=None, collate_fn=collate_fn): """Returns torch.utils.data.DataLoader for custom coco dataset.""" if 'coco' in data_name: # COCO custom dataset dataset = CocoDataset(root=root, json=json, vocab=vocab, transform=transform, ids=ids) elif 'f8k' in data_name or 'f30k' in data_name: dataset = FlickrDataset(root=root, split=split, json=json, vocab=vocab, transform=transform) # Data loader data_loader = torch.utils.data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=shuffle, pin_memory=True, num_workers=num_workers, collate_fn=collate_fn) return data_loader
def __init__(self, which_set='train', warn_grayscale=False, *args, **kwargs): sys.path.append(os.path.join(os.path.dirname( os.path.abspath(__file__)), 'coco', 'PythonAPI')) self.which_set = 'val' if which_set == 'valid' else which_set self.warn_grayscale = warn_grayscale # constructing the ThreadedDataset # it also creates/copies the dataset in self.path if not already there super(MSCocoDataset, self).__init__(*args, **kwargs) self.set_has_GT = self.which_set != 'test' if self.seq_length != 1 or self.seq_per_subset != 0: raise NotImplementedError('Images in COCO are not sequential. ' 'It does not make sense to request a ' 'sequence. seq_length {} ' 'seq_per_subset {}'.format( self.seq_length, self.seq_per_subset))
def setUp(self): self.annotations_file = "data/coco/annotations/instances_minival2014.json" if getattr(self, 'coco', None) is None: self.coco = COCO(self.annotations_file) image_name = config.IMAGE_NAME_FORMAT % ('val2014', self.coco.getImgIds()[0]) image_path = config.IMAGE_PATH_FORMAT % ('val2014', image_name) self.image = load_image(image_path)
def __init__(self, annotation_file, ign_null_img=False): BaseDataset.__init__(self) self.annotation_file = annotation_file coco.COCO.__init__(self, annotation_file) for i in self.getImgIds(): image_file_name = config.IMAGE_NAME_FORMAT % (config.IMAGE_SET, i) image_file_path = config.IMAGE_PATH_FORMAT % (config.IMAGE_SET, image_file_name) try: if len(self.imgToAnns[i]) == 0: raise KeyError self.append(COCOItem(image_file_path, self.imgToAnns[i])) except KeyError: if ign_null_img is False: self.append(COCOItem(image_file_path, []))
def __init__(self, image_set, year): imdb.__init__(self, 'coco_' + year + '_' + image_set) # COCO specific config options self.config = {'use_salt': True, 'cleanup': True} # name, paths self._year = year self._image_set = image_set self._data_path = osp.join(cfg.DATA_DIR, 'coco') # load COCO API, classes, class <-> id mappings self._COCO = COCO(self._get_ann_file()) cats = self._COCO.loadCats(self._COCO.getCatIds()) self._classes = tuple(['__background__'] + [c['name'] for c in cats]) self._class_to_ind = dict(list(zip(self.classes, list(range(self.num_classes))))) self._class_to_coco_cat_id = dict(list(zip([c['name'] for c in cats], self._COCO.getCatIds()))) self._image_index = self._load_image_set_index() # Default to roidb handler self.set_proposal_method('gt') self.competition_mode(False) # Some image sets are "views" (i.e. subsets) into others. # For example, minival2014 is a random 5000 image subset of val2014. # This mapping tells us where the view's images and proposals come from. self._view_map = { 'minival2014': 'val2014', # 5k val2014 subset 'valminusminival2014': 'val2014', # val2014 \setminus minival2014 'test-dev2015': 'test2015', } coco_name = image_set + year # e.g., "val2014" self._data_name = (self._view_map[coco_name] if coco_name in self._view_map else coco_name) # Dataset splits that have ground-truth annotations (test splits # do not have gt annotations) self._gt_splits = ('train', 'val', 'minival')
def _do_detection_eval(self, res_file, output_dir): ann_type = 'bbox' coco_dt = self._COCO.loadRes(res_file) coco_eval = COCOeval(self._COCO, coco_dt) coco_eval.params.useSegm = (ann_type == 'segm') coco_eval.evaluate() coco_eval.accumulate() self._print_detection_eval_metrics(coco_eval) eval_file = osp.join(output_dir, 'detection_results.pkl') with open(eval_file, 'wb') as fid: pickle.dump(coco_eval, fid, pickle.HIGHEST_PROTOCOL) print('Wrote COCO eval results to: {}'.format(eval_file))
def __init__(self, image_set, year): imdb.__init__(self, 'coco_' + year + '_' + image_set) # COCO specific config options self.config = {'top_k' : 2000, 'use_salt' : True, 'cleanup' : True, 'crowd_thresh' : 0.7, 'min_size' : 2} # name, paths self._year = year self._image_set = image_set self._data_path = osp.join(cfg.DATA_DIR, 'coco') # load COCO API, classes, class <-> id mappings self._COCO = COCO(self._get_ann_file()) cats = self._COCO.loadCats(self._COCO.getCatIds()) self._classes = tuple(['__background__'] + [c['name'] for c in cats]) self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes))) self._class_to_coco_cat_id = dict(zip([c['name'] for c in cats], self._COCO.getCatIds())) self._image_index = self._load_image_set_index() # Default to roidb handler self.set_proposal_method('selective_search') self.competition_mode(False) # Some image sets are "views" (i.e. subsets) into others. # For example, minival2014 is a random 5000 image subset of val2014. # This mapping tells us where the view's images and proposals come from. self._view_map = { 'minival2014' : 'val2014', # 5k val2014 subset 'valminusminival2014' : 'val2014', # val2014 \setminus minival2014 } coco_name = image_set + year # e.g., "val2014" self._data_name = (self._view_map[coco_name] if self._view_map.has_key(coco_name) else coco_name) # Dataset splits that have ground-truth annotations (test splits # do not have gt annotations) self._gt_splits = ('train', 'val', 'minival')
def _do_detection_eval(self, res_file, output_dir): ann_type = 'bbox' coco_dt = self._COCO.loadRes(res_file) coco_eval = COCOeval(self._COCO, coco_dt) coco_eval.params.useSegm = (ann_type == 'segm') coco_eval.evaluate() coco_eval.accumulate() self._print_detection_eval_metrics(coco_eval) eval_file = osp.join(output_dir, 'detection_results.pkl') with open(eval_file, 'wb') as fid: cPickle.dump(coco_eval, fid, cPickle.HIGHEST_PROTOCOL) print 'Wrote COCO eval results to: {}'.format(eval_file)
def __init__(self, root, json, vocab, transform=None): """Set the path for images, captions and vocabulary wrapper. Args: root: image directory. json: coco annotation file path. vocab: vocabulary wrapper. transform: image transformer. """ self.root = root self.coco = COCO(json) self.ids = list(self.coco.anns.keys()) self.vocab = vocab self.transform = transform
def build_vocab(json, threshold): """Build a simple vocabulary wrapper.""" coco = COCO(json) counter = Counter() ids = coco.anns.keys() for i, id in enumerate(ids): caption = str(coco.anns[id]['caption']) tokens = nltk.tokenize.word_tokenize(caption.lower()) counter.update(tokens) if i % 1000 == 0: print("[%d/%d] Tokenized the captions." %(i, len(ids))) # If the word frequency is less than 'threshold', then the word is discarded. words = [word for word, cnt in counter.items() if cnt >= threshold] # Creates a vocab wrapper and add some special tokens. vocab = Vocabulary() vocab.add_word('<pad>') vocab.add_word('<start>') vocab.add_word('<end>') vocab.add_word('<unk>') # Adds the words to the vocabulary. for i, word in enumerate(words): vocab.add_word(word) return vocab
def __init__(self, image_set, year, devkit_path=None): datasets.imdb.__init__(self, 'coco_' + year + '_' + image_set) # COCO specific config options self.config = {'top_k' : 2000, 'use_salt' : True, 'cleanup' : True, 'crowd_thresh' : 0.7, 'min_size' : 2} # name, paths self._year = year self._image_set = image_set self._data_path = self._get_default_path() if devkit_path is None \ else devkit_path # load COCO API, classes, class <-> id mappings self._COCO = COCO(self._get_ann_file()) cats = self._COCO.loadCats(self._COCO.getCatIds()) self._classes = tuple(['__background__'] + [c['name'] for c in cats]) self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes))) self._class_to_coco_cat_id = dict(zip([c['name'] for c in cats], self._COCO.getCatIds())) self._image_index = self._load_image_set_index() # Default to roidb handler self._roidb_handler = self.gt_roidb self.competition_mode(False) self._data_name = image_set + year # e.g., "val2014" if self._data_name == 'test-dev2015': self._data_name_path = 'test2015' else: self._data_name_path = self._data_name
def __init__(self, data_dir, set_name, image_data_generator, *args, **kwargs): self.data_dir = data_dir self.set_name = set_name self.coco = COCO(os.path.join(data_dir, 'annotations', 'instances_' + set_name + '.json')) self.image_ids = self.coco.getImgIds() self.load_classes() super(CocoGenerator, self).__init__(image_data_generator, **kwargs)
def __init__(self, data_dir, data_type): ann_file = '%s/annotations/instances_%s.json' % (data_dir, data_type) # initialize COCO api for instance annotations self.coco = COCO(ann_file)
def __init__(self, data_dir, image_set, year): self.name = 'coco_' + year + '_' + image_set # name, paths self.year = year self.image_set = image_set self.data_dir = data_dir # COCO API self.COCO = COCO(self._get_ann_file()) cats = self.COCO.loadCats(self.COCO.getCatIds()) self.classes = tuple([c['name'] for c in cats]) self.num_classes = len(self.classes) # e.g: 'person' -> 0, 'toothbrush' -> 79 self.class_to_ind = dict(zip(self.classes, xrange(self.num_classes))) # e.g: 'person' -> 1, 'toothbrush' -> 90 self.class_to_coco_cat_id = dict(zip([c['name'] for c in cats], self.COCO.getCatIds())) # coco' cat_id (1,,90) -> label_id (0,,79) self.coco_cat_id_to_class_ind = dict([(self.class_to_coco_cat_id[cls], self.class_to_ind[cls]) for cls in self.classes]) self.image_index = self._load_image_set_index() self.num_images = len(self.image_index) coco_name = image_set + year # e.g., "val2014" self.data_name = coco_name
def _load_image_set_index(self): """ Load image ids. """ image_ids = self.COCO.getImgIds() return image_ids
def image_path_from_index(self, index): """ Construct an image path from the image's "index" identifier. """ # Example image path for index=119993: # train2014/COCO_train2014_000000119993.jpg image = self.COCO.loadImgs(index)[0] image_path = osp.join(self.data_dir, self.data_name, image['file_name']) assert osp.exists(image_path), 'Path does not exist: {}'.format(image_path) return image_path
def _load_coco_annotation(self, index): annIds = self.COCO.getAnnIds(imgIds=index, iscrowd=None) objs = self.COCO.loadAnns(annIds) bag_labels = np.zeros(self.num_classes) for obj in objs: cls = self.coco_cat_id_to_class_ind[obj['category_id']] bag_labels[cls] = 1 return bag_labels
def language_eval(dataset, preds, model_id, split): import sys sys.path.append("coco-caption") annFile = 'coco-caption/annotations/captions_val2014.json' from pycocotools.coco import COCO from pycocoevalcap.eval import COCOEvalCap encoder.FLOAT_REPR = lambda o: format(o, '.3f') if not os.path.isdir('eval_results'): os.mkdir('eval_results') cache_path = os.path.join('eval_results/', model_id + '_' + split + '.json') coco = COCO(annFile) valids = coco.getImgIds() # filter results to only those in MSCOCO validation set (will be about a third) preds_filt = [p for p in preds if p['image_id'] in valids] print('using %d/%d predictions' % (len(preds_filt), len(preds))) json.dump(preds_filt, open(cache_path, 'w')) # serialize to temporary json file. Sigh, COCO API... cocoRes = coco.loadRes(cache_path) cocoEval = COCOEvalCap(coco, cocoRes) cocoEval.params['image_id'] = cocoRes.getImgIds() cocoEval.evaluate() # create output dictionary out = {} for metric, score in cocoEval.eval.items(): out[metric] = score imgToEval = cocoEval.imgToEval for p in preds_filt: image_id, caption = p['image_id'], p['caption'] imgToEval[image_id]['caption'] = caption with open(cache_path, 'w') as outfile: json.dump({'overall': out, 'imgToEval': imgToEval}, outfile) return out
def eval_coco(annFile, resFile): coco = COCO(annFile) cocoRes = coco.loadRes(resFile) cocoEval = COCOEvalCap(coco, cocoRes) cocoEval.evaluate() Bleu_4 = cocoEval.eval['Bleu_4'] METEOR = cocoEval.eval['METEOR'] ROUGE_L = cocoEval.eval['ROUGE_L'] CIDEr = cocoEval.eval['CIDEr'] total = Bleu_4 + METEOR + ROUGE_L + CIDEr score = {'Bleu_4': Bleu_4, 'METEOR': METEOR, 'ROUGE_L': ROUGE_L, 'CIDEr': CIDEr, 'total': total} return score