|
| 1 | +import os |
| 2 | +from typing import Any, Callable, List, Optional, Tuple |
| 3 | +from PIL import Image |
| 4 | +from .vision import VisionDataset |
| 5 | +from .utils import check_integrity, download_and_extract_archive, download_url, verify_str_arg |
| 6 | + |
| 7 | + |
| 8 | +class _LFW(VisionDataset): |
| 9 | + |
| 10 | + base_folder = 'lfw-py' |
| 11 | + download_url_prefix = "http://vis-www.cs.umass.edu/lfw/" |
| 12 | + |
| 13 | + file_dict = { |
| 14 | + 'original': ("lfw", "lfw.tgz", "a17d05bd522c52d84eca14327a23d494"), |
| 15 | + 'funneled': ("lfw_funneled", "lfw-funneled.tgz", "1b42dfed7d15c9b2dd63d5e5840c86ad"), |
| 16 | + 'deepfunneled': ("lfw-deepfunneled", "lfw-deepfunneled.tgz", "68331da3eb755a505a502b5aacb3c201") |
| 17 | + } |
| 18 | + checksums = { |
| 19 | + 'pairs.txt': '9f1ba174e4e1c508ff7cdf10ac338a7d', |
| 20 | + 'pairsDevTest.txt': '5132f7440eb68cf58910c8a45a2ac10b', |
| 21 | + 'pairsDevTrain.txt': '4f27cbf15b2da4a85c1907eb4181ad21', |
| 22 | + 'people.txt': '450f0863dd89e85e73936a6d71a3474b', |
| 23 | + 'peopleDevTest.txt': 'e4bf5be0a43b5dcd9dc5ccfcb8fb19c5', |
| 24 | + 'peopleDevTrain.txt': '54eaac34beb6d042ed3a7d883e247a21', |
| 25 | + 'lfw-names.txt': 'a6d0a479bd074669f656265a6e693f6d' |
| 26 | + } |
| 27 | + annot_file = {'10fold': '', 'train': 'DevTrain', 'test': 'DevTest'} |
| 28 | + names = "lfw-names.txt" |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + root: str, |
| 33 | + split: str, |
| 34 | + image_set: str, |
| 35 | + view: str, |
| 36 | + transform: Optional[Callable] = None, |
| 37 | + target_transform: Optional[Callable] = None, |
| 38 | + download: bool = False, |
| 39 | + ): |
| 40 | + super(_LFW, self).__init__(os.path.join(root, self.base_folder), |
| 41 | + transform=transform, target_transform=target_transform) |
| 42 | + |
| 43 | + self.image_set = verify_str_arg(image_set.lower(), 'image_set', self.file_dict.keys()) |
| 44 | + images_dir, self.filename, self.md5 = self.file_dict[self.image_set] |
| 45 | + |
| 46 | + self.view = verify_str_arg(view.lower(), 'view', ['people', 'pairs']) |
| 47 | + self.split = verify_str_arg(split.lower(), 'split', ['10fold', 'train', 'test']) |
| 48 | + self.labels_file = f"{self.view}{self.annot_file[self.split]}.txt" |
| 49 | + self.data: List[Any] = [] |
| 50 | + |
| 51 | + if download: |
| 52 | + self.download() |
| 53 | + |
| 54 | + if not self._check_integrity(): |
| 55 | + raise RuntimeError('Dataset not found or corrupted.' + |
| 56 | + ' You can use download=True to download it') |
| 57 | + |
| 58 | + self.images_dir = os.path.join(self.root, images_dir) |
| 59 | + |
| 60 | + def _loader(self, path: str) -> Image.Image: |
| 61 | + with open(path, 'rb') as f: |
| 62 | + img = Image.open(f) |
| 63 | + return img.convert('RGB') |
| 64 | + |
| 65 | + def _check_integrity(self): |
| 66 | + st1 = check_integrity(os.path.join(self.root, self.filename), self.md5) |
| 67 | + st2 = check_integrity(os.path.join(self.root, self.labels_file), self.checksums[self.labels_file]) |
| 68 | + if not st1 or not st2: |
| 69 | + return False |
| 70 | + if self.view == "people": |
| 71 | + return check_integrity(os.path.join(self.root, self.names), self.checksums[self.names]) |
| 72 | + return True |
| 73 | + |
| 74 | + def download(self): |
| 75 | + if self._check_integrity(): |
| 76 | + print('Files already downloaded and verified') |
| 77 | + return |
| 78 | + url = f"{self.download_url_prefix}{self.filename}" |
| 79 | + download_and_extract_archive(url, self.root, filename=self.filename, md5=self.md5) |
| 80 | + download_url(f"{self.download_url_prefix}{self.labels_file}", self.root) |
| 81 | + if self.view == "people": |
| 82 | + download_url(f"{self.download_url_prefix}{self.names}", self.root) |
| 83 | + |
| 84 | + def _get_path(self, identity, no): |
| 85 | + return os.path.join(self.images_dir, identity, f"{identity}_{int(no):04d}.jpg") |
| 86 | + |
| 87 | + def extra_repr(self) -> str: |
| 88 | + return f"Alignment: {self.image_set}\nSplit: {self.split}" |
| 89 | + |
| 90 | + def __len__(self): |
| 91 | + return len(self.data) |
| 92 | + |
| 93 | + |
| 94 | +class LFWPeople(_LFW): |
| 95 | + """`LFW <http://vis-www.cs.umass.edu/lfw/>`_ Dataset. |
| 96 | +
|
| 97 | + Args: |
| 98 | + root (string): Root directory of dataset where directory |
| 99 | + ``lfw-py`` exists or will be saved to if download is set to True. |
| 100 | + split (string, optional): The image split to use. Can be one of ``train``, ``test``, |
| 101 | + ``10fold`` (default). |
| 102 | + image_set (str, optional): Type of image funneling to use, ``original``, ``funneled`` or |
| 103 | + ``deepfunneled``. Defaults to ``funneled``. |
| 104 | + transform (callable, optional): A function/transform that takes in an PIL image |
| 105 | + and returns a transformed version. E.g, ``transforms.RandomRotation`` |
| 106 | + target_transform (callable, optional): A function/transform that takes in the |
| 107 | + target and transforms it. |
| 108 | + download (bool, optional): If true, downloads the dataset from the internet and |
| 109 | + puts it in root directory. If dataset is already downloaded, it is not |
| 110 | + downloaded again. |
| 111 | +
|
| 112 | + """ |
| 113 | + |
| 114 | + def __init__( |
| 115 | + self, |
| 116 | + root: str, |
| 117 | + split: str = "10fold", |
| 118 | + image_set: str = "funneled", |
| 119 | + transform: Optional[Callable] = None, |
| 120 | + target_transform: Optional[Callable] = None, |
| 121 | + download: bool = False, |
| 122 | + ): |
| 123 | + super(LFWPeople, self).__init__(root, split, image_set, "people", |
| 124 | + transform, target_transform, download) |
| 125 | + |
| 126 | + self.class_to_idx = self._get_classes() |
| 127 | + self.data, self.targets = self._get_people() |
| 128 | + |
| 129 | + def _get_people(self): |
| 130 | + data, targets = [], [] |
| 131 | + with open(os.path.join(self.root, self.labels_file), 'r') as f: |
| 132 | + lines = f.readlines() |
| 133 | + n_folds, s = (int(lines[0]), 1) if self.split == "10fold" else (1, 0) |
| 134 | + |
| 135 | + for fold in range(n_folds): |
| 136 | + n_lines = int(lines[s]) |
| 137 | + people = [line.strip().split("\t") for line in lines[s + 1: s + n_lines + 1]] |
| 138 | + s += n_lines + 1 |
| 139 | + for i, (identity, num_imgs) in enumerate(people): |
| 140 | + for num in range(1, int(num_imgs) + 1): |
| 141 | + img = self._get_path(identity, num) |
| 142 | + data.append(img) |
| 143 | + targets.append(self.class_to_idx[identity]) |
| 144 | + |
| 145 | + return data, targets |
| 146 | + |
| 147 | + def _get_classes(self): |
| 148 | + with open(os.path.join(self.root, self.names), 'r') as f: |
| 149 | + lines = f.readlines() |
| 150 | + names = [line.strip().split()[0] for line in lines] |
| 151 | + class_to_idx = {name: i for i, name in enumerate(names)} |
| 152 | + return class_to_idx |
| 153 | + |
| 154 | + def __getitem__(self, index: int) -> Tuple[Any, Any]: |
| 155 | + """ |
| 156 | + Args: |
| 157 | + index (int): Index |
| 158 | +
|
| 159 | + Returns: |
| 160 | + tuple: Tuple (image, target) where target is the identity of the person. |
| 161 | + """ |
| 162 | + img = self._loader(self.data[index]) |
| 163 | + target = self.targets[index] |
| 164 | + |
| 165 | + if self.transform is not None: |
| 166 | + img = self.transform(img) |
| 167 | + |
| 168 | + if self.target_transform is not None: |
| 169 | + target = self.target_transform(target) |
| 170 | + |
| 171 | + return img, target |
| 172 | + |
| 173 | + def extra_repr(self) -> str: |
| 174 | + return super().extra_repr() + "\nClasses (identities): {}".format(len(self.class_to_idx)) |
| 175 | + |
| 176 | + |
| 177 | +class LFWPairs(_LFW): |
| 178 | + """`LFW <http://vis-www.cs.umass.edu/lfw/>`_ Dataset. |
| 179 | +
|
| 180 | + Args: |
| 181 | + root (string): Root directory of dataset where directory |
| 182 | + ``lfw-py`` exists or will be saved to if download is set to True. |
| 183 | + split (string, optional): The image split to use. Can be one of ``train``, ``test``, |
| 184 | + ``10fold``. Defaults to ``10fold``. |
| 185 | + image_set (str, optional): Type of image funneling to use, ``original``, ``funneled`` or |
| 186 | + ``deepfunneled``. Defaults to ``funneled``. |
| 187 | + transform (callable, optional): A function/transform that takes in an PIL image |
| 188 | + and returns a transformed version. E.g, ``transforms.RandomRotation`` |
| 189 | + target_transform (callable, optional): A function/transform that takes in the |
| 190 | + target and transforms it. |
| 191 | + download (bool, optional): If true, downloads the dataset from the internet and |
| 192 | + puts it in root directory. If dataset is already downloaded, it is not |
| 193 | + downloaded again. |
| 194 | +
|
| 195 | + """ |
| 196 | + |
| 197 | + def __init__( |
| 198 | + self, |
| 199 | + root: str, |
| 200 | + split: str = "10fold", |
| 201 | + image_set: str = "funneled", |
| 202 | + transform: Optional[Callable] = None, |
| 203 | + target_transform: Optional[Callable] = None, |
| 204 | + download: bool = False, |
| 205 | + ): |
| 206 | + super(LFWPairs, self).__init__(root, split, image_set, "pairs", |
| 207 | + transform, target_transform, download) |
| 208 | + |
| 209 | + self.pair_names, self.data, self.targets = self._get_pairs(self.images_dir) |
| 210 | + |
| 211 | + def _get_pairs(self, images_dir): |
| 212 | + pair_names, data, targets = [], [], [] |
| 213 | + with open(os.path.join(self.root, self.labels_file), 'r') as f: |
| 214 | + lines = f.readlines() |
| 215 | + if self.split == "10fold": |
| 216 | + n_folds, n_pairs = lines[0].split("\t") |
| 217 | + n_folds, n_pairs = int(n_folds), int(n_pairs) |
| 218 | + else: |
| 219 | + n_folds, n_pairs = 1, int(lines[0]) |
| 220 | + s = 1 |
| 221 | + |
| 222 | + for fold in range(n_folds): |
| 223 | + matched_pairs = [line.strip().split("\t") for line in lines[s: s + n_pairs]] |
| 224 | + unmatched_pairs = [line.strip().split("\t") for line in lines[s + n_pairs: s + (2 * n_pairs)]] |
| 225 | + s += (2 * n_pairs) |
| 226 | + for pair in matched_pairs: |
| 227 | + img1, img2, same = self._get_path(pair[0], pair[1]), self._get_path(pair[0], pair[2]), 1 |
| 228 | + pair_names.append((pair[0], pair[0])) |
| 229 | + data.append((img1, img2)) |
| 230 | + targets.append(same) |
| 231 | + for pair in unmatched_pairs: |
| 232 | + img1, img2, same = self._get_path(pair[0], pair[1]), self._get_path(pair[2], pair[3]), 0 |
| 233 | + pair_names.append((pair[0], pair[2])) |
| 234 | + data.append((img1, img2)) |
| 235 | + targets.append(same) |
| 236 | + |
| 237 | + return pair_names, data, targets |
| 238 | + |
| 239 | + def __getitem__(self, index: int) -> Tuple[Any, Any, int]: |
| 240 | + """ |
| 241 | + Args: |
| 242 | + index (int): Index |
| 243 | +
|
| 244 | + Returns: |
| 245 | + tuple: (image1, image2, target) where target is `0` for different indentities and `1` for same identities. |
| 246 | + """ |
| 247 | + img1, img2 = self.data[index] |
| 248 | + img1, img2 = self._loader(img1), self._loader(img2) |
| 249 | + target = self.targets[index] |
| 250 | + |
| 251 | + if self.transform is not None: |
| 252 | + img1, img2 = self.transform(img1), self.transform(img2) |
| 253 | + |
| 254 | + if self.target_transform is not None: |
| 255 | + target = self.target_transform(target) |
| 256 | + |
| 257 | + return img1, img2, target |
0 commit comments