celeba.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import csv
  2. import os
  3. from collections import namedtuple
  4. from pathlib import Path
  5. from typing import Any, Callable, Optional, Union
  6. import PIL
  7. import torch
  8. from .utils import check_integrity, download_file_from_google_drive, extract_archive, verify_str_arg
  9. from .vision import VisionDataset
  10. CSV = namedtuple("CSV", ["header", "index", "data"])
  11. class CelebA(VisionDataset):
  12. """`Large-scale CelebFaces Attributes (CelebA) Dataset <http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html>`_ Dataset.
  13. Args:
  14. root (str or ``pathlib.Path``): Root directory where images are downloaded to.
  15. split (string): One of {'train', 'valid', 'test', 'all'}.
  16. Accordingly dataset is selected.
  17. target_type (string or list, optional): Type of target to use, ``attr``, ``identity``, ``bbox``,
  18. or ``landmarks``. Can also be a list to output a tuple with all specified target types.
  19. The targets represent:
  20. - ``attr`` (Tensor shape=(40,) dtype=int): binary (0, 1) labels for attributes
  21. - ``identity`` (int): label for each person (data points with the same identity are the same person)
  22. - ``bbox`` (Tensor shape=(4,) dtype=int): bounding box (x, y, width, height).
  23. .. warning::
  24. These bounding box coordinates correspond to the original uncropped
  25. CelebA images, not the cropped and aligned images returned by this
  26. dataset. As a result, the coordinates will not match and may fall
  27. outside the image boundaries.
  28. See `Issue #9008 <https://github.com/pytorch/vision/issues/9008>`_ for
  29. details and potential workarounds.
  30. - ``landmarks`` (Tensor shape=(10,) dtype=int): landmark points (lefteye_x, lefteye_y, righteye_x,
  31. righteye_y, nose_x, nose_y, leftmouth_x, leftmouth_y, rightmouth_x, rightmouth_y)
  32. Defaults to ``attr``. If empty, ``None`` will be returned as target.
  33. transform (callable, optional): A function/transform that takes in a PIL image
  34. and returns a transformed version. E.g, ``transforms.PILToTensor``
  35. target_transform (callable, optional): A function/transform that takes in the
  36. target and transforms it.
  37. download (bool, optional): If true, downloads the dataset from the internet and
  38. puts it in root directory. If dataset is already downloaded, it is not
  39. downloaded again.
  40. .. warning::
  41. To download the dataset `gdown <https://github.com/wkentaro/gdown>`_ is required.
  42. """
  43. base_folder = "celeba"
  44. # There currently does not appear to be an easy way to extract 7z in python (without introducing additional
  45. # dependencies). The "in-the-wild" (not aligned+cropped) images are only in 7z, so they are not available
  46. # right now.
  47. file_list = [
  48. # File ID MD5 Hash Filename
  49. ("0B7EVK8r0v71pZjFTYXZWM3FlRnM", "00d2c5bc6d35e252742224ab0c1e8fcb", "img_align_celeba.zip"),
  50. # ("0B7EVK8r0v71pbWNEUjJKdDQ3dGc","b6cd7e93bc7a96c2dc33f819aa3ac651", "img_align_celeba_png.7z"),
  51. # ("0B7EVK8r0v71peklHb0pGdDl6R28", "b6cd7e93bc7a96c2dc33f819aa3ac651", "img_celeba.7z"),
  52. ("0B7EVK8r0v71pblRyaVFSWGxPY0U", "75e246fa4810816ffd6ee81facbd244c", "list_attr_celeba.txt"),
  53. ("1_ee_0u7vcNLOfNLegJRHmolfH5ICW-XS", "32bd1bd63d3c78cd57e08160ec5ed1e2", "identity_CelebA.txt"),
  54. ("0B7EVK8r0v71pbThiMVRxWXZ4dU0", "00566efa6fedff7a56946cd1c10f1c16", "list_bbox_celeba.txt"),
  55. ("0B7EVK8r0v71pd0FJY3Blby1HUTQ", "cc24ecafdb5b50baae59b03474781f8c", "list_landmarks_align_celeba.txt"),
  56. # ("0B7EVK8r0v71pTzJIdlJWdHczRlU", "063ee6ddb681f96bc9ca28c6febb9d1a", "list_landmarks_celeba.txt"),
  57. ("0B7EVK8r0v71pY0NSMzRuSXJEVkk", "d32c9cbf5e040fd4025c592c306e6668", "list_eval_partition.txt"),
  58. ]
  59. def __init__(
  60. self,
  61. root: Union[str, Path],
  62. split: str = "train",
  63. target_type: Union[list[str], str] = "attr",
  64. transform: Optional[Callable] = None,
  65. target_transform: Optional[Callable] = None,
  66. download: bool = False,
  67. ) -> None:
  68. super().__init__(root, transform=transform, target_transform=target_transform)
  69. self.split = split
  70. if isinstance(target_type, list):
  71. self.target_type = target_type
  72. else:
  73. self.target_type = [target_type]
  74. if not self.target_type and self.target_transform is not None:
  75. raise RuntimeError("target_transform is specified but target_type is empty")
  76. if download:
  77. self.download()
  78. if not self._check_integrity():
  79. raise RuntimeError("Dataset not found or corrupted. You can use download=True to download it")
  80. split_map = {
  81. "train": 0,
  82. "valid": 1,
  83. "test": 2,
  84. "all": None,
  85. }
  86. split_ = split_map[
  87. verify_str_arg(
  88. split.lower() if isinstance(split, str) else split,
  89. "split",
  90. ("train", "valid", "test", "all"),
  91. )
  92. ]
  93. splits = self._load_csv("list_eval_partition.txt")
  94. identity = self._load_csv("identity_CelebA.txt")
  95. bbox = self._load_csv("list_bbox_celeba.txt", header=1)
  96. landmarks_align = self._load_csv("list_landmarks_align_celeba.txt", header=1)
  97. attr = self._load_csv("list_attr_celeba.txt", header=1)
  98. mask = slice(None) if split_ is None else (splits.data == split_).squeeze()
  99. if mask == slice(None): # if split == "all"
  100. self.filename = splits.index
  101. else:
  102. self.filename = [splits.index[i] for i in torch.squeeze(torch.nonzero(mask))] # type: ignore[arg-type]
  103. self.identity = identity.data[mask]
  104. self.bbox = bbox.data[mask]
  105. self.landmarks_align = landmarks_align.data[mask]
  106. self.attr = attr.data[mask]
  107. # map from {-1, 1} to {0, 1}
  108. self.attr = torch.div(self.attr + 1, 2, rounding_mode="floor")
  109. self.attr_names = attr.header
  110. def _load_csv(
  111. self,
  112. filename: str,
  113. header: Optional[int] = None,
  114. ) -> CSV:
  115. with open(os.path.join(self.root, self.base_folder, filename)) as csv_file:
  116. data = list(csv.reader(csv_file, delimiter=" ", skipinitialspace=True))
  117. if header is not None:
  118. headers = data[header]
  119. data = data[header + 1 :]
  120. else:
  121. headers = []
  122. indices = [row[0] for row in data]
  123. data = [row[1:] for row in data]
  124. data_int = [list(map(int, i)) for i in data]
  125. return CSV(headers, indices, torch.tensor(data_int))
  126. def _check_integrity(self) -> bool:
  127. for _, md5, filename in self.file_list:
  128. fpath = os.path.join(self.root, self.base_folder, filename)
  129. _, ext = os.path.splitext(filename)
  130. # Allow original archive to be deleted (zip and 7z)
  131. # Only need the extracted images
  132. if ext not in [".zip", ".7z"] and not check_integrity(fpath, md5):
  133. return False
  134. # Should check a hash of the images
  135. return os.path.isdir(os.path.join(self.root, self.base_folder, "img_align_celeba"))
  136. def download(self) -> None:
  137. if self._check_integrity():
  138. return
  139. for file_id, md5, filename in self.file_list:
  140. download_file_from_google_drive(file_id, os.path.join(self.root, self.base_folder), filename, md5)
  141. extract_archive(os.path.join(self.root, self.base_folder, "img_align_celeba.zip"))
  142. def __getitem__(self, index: int) -> tuple[Any, Any]:
  143. X = PIL.Image.open(os.path.join(self.root, self.base_folder, "img_align_celeba", self.filename[index]))
  144. target: Any = []
  145. for t in self.target_type:
  146. if t == "attr":
  147. target.append(self.attr[index, :])
  148. elif t == "identity":
  149. target.append(self.identity[index, 0])
  150. elif t == "bbox":
  151. target.append(self.bbox[index, :])
  152. elif t == "landmarks":
  153. target.append(self.landmarks_align[index, :])
  154. else:
  155. # TODO: refactor with utils.verify_str_arg
  156. raise ValueError(f'Target type "{t}" is not recognized.')
  157. if self.transform is not None:
  158. X = self.transform(X)
  159. if target:
  160. target = tuple(target) if len(target) > 1 else target[0]
  161. if self.target_transform is not None:
  162. target = self.target_transform(target)
  163. else:
  164. target = None
  165. return X, target
  166. def __len__(self) -> int:
  167. return len(self.attr)
  168. def extra_repr(self) -> str:
  169. lines = ["Target type: {target_type}", "Split: {split}"]
  170. return "\n".join(lines).format(**self.__dict__)