celeba.py 8.3 KB

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