Provide methods to access metadata for :class:`ILSVRC12` dataset.
| 19 | |
| 20 | |
| 21 | class ILSVRCMeta(object): |
| 22 | """ |
| 23 | Provide methods to access metadata for :class:`ILSVRC12` dataset. |
| 24 | """ |
| 25 | |
| 26 | def __init__(self, dir=None): |
| 27 | if dir is None: |
| 28 | dir = get_dataset_path('ilsvrc_metadata') |
| 29 | self.dir = os.path.expanduser(dir) |
| 30 | mkdir_p(self.dir) |
| 31 | f = os.path.join(self.dir, 'synsets.txt') |
| 32 | if not os.path.isfile(f): |
| 33 | self._download_caffe_meta() |
| 34 | self.caffepb = None |
| 35 | |
| 36 | def get_synset_words_1000(self): |
| 37 | """ |
| 38 | Returns: |
| 39 | dict: {cls_number: cls_name} |
| 40 | """ |
| 41 | fname = os.path.join(self.dir, 'synset_words.txt') |
| 42 | assert os.path.isfile(fname), fname |
| 43 | lines = [x.strip() for x in open(fname).readlines()] |
| 44 | return dict(enumerate(lines)) |
| 45 | |
| 46 | def get_synset_1000(self): |
| 47 | """ |
| 48 | Returns: |
| 49 | dict: {cls_number: synset_id} |
| 50 | """ |
| 51 | fname = os.path.join(self.dir, 'synsets.txt') |
| 52 | assert os.path.isfile(fname) |
| 53 | lines = [x.strip() for x in open(fname).readlines()] |
| 54 | return dict(enumerate(lines)) |
| 55 | |
| 56 | def _download_caffe_meta(self): |
| 57 | fpath = download(CAFFE_ILSVRC12_URL[0], self.dir, expect_size=CAFFE_ILSVRC12_URL[1]) |
| 58 | tarfile.open(fpath, 'r:gz').extractall(self.dir) |
| 59 | |
| 60 | def get_image_list(self, name, dir_structure='original'): |
| 61 | """ |
| 62 | Args: |
| 63 | name (str): 'train' or 'val' or 'test' |
| 64 | dir_structure (str): same as in :meth:`ILSVRC12.__init__()`. |
| 65 | Returns: |
| 66 | list: list of (image filename, label) |
| 67 | """ |
| 68 | assert name in ['train', 'val', 'test'] |
| 69 | assert dir_structure in ['original', 'train'] |
| 70 | add_label_to_fname = (name != 'train' and dir_structure != 'original') |
| 71 | if add_label_to_fname: |
| 72 | synset = self.get_synset_1000() |
| 73 | |
| 74 | fname = os.path.join(self.dir, name + '.txt') |
| 75 | assert os.path.isfile(fname), fname |
| 76 | with open(fname) as f: |
| 77 | ret = [] |
| 78 | for line in f.readlines(): |