| 39 | |
| 40 | |
| 41 | class REFER: |
| 42 | def __init__(self, data_root, dataset='refcoco', splitBy='unc'): |
| 43 | # provide data_root folder which contains refclef, refcoco, refcoco+ and refcocog |
| 44 | # also provide dataset name and splitBy information |
| 45 | # e.g., dataset = 'refcoco', splitBy = 'unc' |
| 46 | print('loading dataset %s into memory...' % dataset) |
| 47 | self.ROOT_DIR = osp.abspath(osp.dirname(__file__)) |
| 48 | self.DATA_DIR = osp.join(data_root, dataset) |
| 49 | if dataset in ['refcoco', 'refcoco+', 'refcocog']: |
| 50 | self.IMAGE_DIR = osp.join(data_root, 'images/train2014') |
| 51 | elif dataset == 'refclef': |
| 52 | self.IMAGE_DIR = osp.join(data_root, 'images/saiapr_tc-12') |
| 53 | else: |
| 54 | print('No refer dataset is called [%s]' % dataset) |
| 55 | sys.exit() |
| 56 | |
| 57 | # load refs from data/dataset/refs(dataset).json |
| 58 | tic = time.time() |
| 59 | ref_file = osp.join(self.DATA_DIR, 'refs(' + splitBy + ').p') |
| 60 | self.data = {} |
| 61 | self.data['dataset'] = dataset |
| 62 | |
| 63 | self.data['refs'] = pickle.load(open(ref_file, 'rb'), fix_imports=True) |
| 64 | |
| 65 | # load annotations from data/dataset/instances.json |
| 66 | instances_file = osp.join(self.DATA_DIR, 'instances.json') |
| 67 | instances = json.load(open(instances_file, 'r')) |
| 68 | self.data['images'] = instances['images'] |
| 69 | self.data['annotations'] = instances['annotations'] |
| 70 | self.data['categories'] = instances['categories'] |
| 71 | |
| 72 | # create index |
| 73 | self.createIndex() |
| 74 | print('DONE (t=%.2fs)' % (time.time() - tic)) |
| 75 | |
| 76 | def createIndex(self): |
| 77 | # create sets of mapping |
| 78 | # 1) Refs: {ref_id: ref} |
| 79 | # 2) Anns: {ann_id: ann} |
| 80 | # 3) Imgs: {image_id: image} |
| 81 | # 4) Cats: {category_id: category_name} |
| 82 | # 5) Sents: {sent_id: sent} |
| 83 | # 6) imgToRefs: {image_id: refs} |
| 84 | # 7) imgToAnns: {image_id: anns} |
| 85 | # 8) refToAnn: {ref_id: ann} |
| 86 | # 9) annToRef: {ann_id: ref} |
| 87 | # 10) catToRefs: {category_id: refs} |
| 88 | # 11) sentToRef: {sent_id: ref} |
| 89 | # 12) sentToTokens: {sent_id: tokens} |
| 90 | print('creating index...') |
| 91 | # fetch info from instances |
| 92 | Anns, Imgs, Cats, imgToAnns = {}, {}, {}, {} |
| 93 | for ann in self.data['annotations']: |
| 94 | Anns[ann['id']] = ann |
| 95 | imgToAnns[ann['image_id']] = imgToAnns.get(ann['image_id'], |
| 96 | []) + [ann] |
| 97 | for img in self.data['images']: |
| 98 | Imgs[img['id']] = img |
no outgoing calls
no test coverage detected