dtd.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import pathlib
  3. from typing import Any, Callable, Optional, Tuple
  4. import PIL.Image
  5. from .utils import download_and_extract_archive, verify_str_arg
  6. from .vision import VisionDataset
  7. class DTD(VisionDataset):
  8. """`Describable Textures Dataset (DTD) <https://www.robots.ox.ac.uk/~vgg/data/dtd/>`_.
  9. Args:
  10. root (string): Root directory of the dataset.
  11. split (string, optional): The dataset split, supports ``"train"`` (default), ``"val"``, or ``"test"``.
  12. partition (int, optional): The dataset partition. Should be ``1 <= partition <= 10``. Defaults to ``1``.
  13. .. note::
  14. The partition only changes which split each image belongs to. Thus, regardless of the selected
  15. partition, combining all splits will result in all images.
  16. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed
  17. version. E.g, ``transforms.RandomCrop``.
  18. target_transform (callable, optional): A function/transform that takes in the target and transforms it.
  19. download (bool, optional): If True, downloads the dataset from the internet and
  20. puts it in root directory. If dataset is already downloaded, it is not
  21. downloaded again. Default is False.
  22. """
  23. _URL = "https://www.robots.ox.ac.uk/~vgg/data/dtd/download/dtd-r1.0.1.tar.gz"
  24. _MD5 = "fff73e5086ae6bdbea199a49dfb8a4c1"
  25. def __init__(
  26. self,
  27. root: str,
  28. split: str = "train",
  29. partition: int = 1,
  30. transform: Optional[Callable] = None,
  31. target_transform: Optional[Callable] = None,
  32. download: bool = False,
  33. ) -> None:
  34. self._split = verify_str_arg(split, "split", ("train", "val", "test"))
  35. if not isinstance(partition, int) and not (1 <= partition <= 10):
  36. raise ValueError(
  37. f"Parameter 'partition' should be an integer with `1 <= partition <= 10`, "
  38. f"but got {partition} instead"
  39. )
  40. self._partition = partition
  41. super().__init__(root, transform=transform, target_transform=target_transform)
  42. self._base_folder = pathlib.Path(self.root) / type(self).__name__.lower()
  43. self._data_folder = self._base_folder / "dtd"
  44. self._meta_folder = self._data_folder / "labels"
  45. self._images_folder = self._data_folder / "images"
  46. if download:
  47. self._download()
  48. if not self._check_exists():
  49. raise RuntimeError("Dataset not found. You can use download=True to download it")
  50. self._image_files = []
  51. classes = []
  52. with open(self._meta_folder / f"{self._split}{self._partition}.txt") as file:
  53. for line in file:
  54. cls, name = line.strip().split("/")
  55. self._image_files.append(self._images_folder.joinpath(cls, name))
  56. classes.append(cls)
  57. self.classes = sorted(set(classes))
  58. self.class_to_idx = dict(zip(self.classes, range(len(self.classes))))
  59. self._labels = [self.class_to_idx[cls] for cls in classes]
  60. def __len__(self) -> int:
  61. return len(self._image_files)
  62. def __getitem__(self, idx: int) -> Tuple[Any, Any]:
  63. image_file, label = self._image_files[idx], self._labels[idx]
  64. image = PIL.Image.open(image_file).convert("RGB")
  65. if self.transform:
  66. image = self.transform(image)
  67. if self.target_transform:
  68. label = self.target_transform(label)
  69. return image, label
  70. def extra_repr(self) -> str:
  71. return f"split={self._split}, partition={self._partition}"
  72. def _check_exists(self) -> bool:
  73. return os.path.exists(self._data_folder) and os.path.isdir(self._data_folder)
  74. def _download(self) -> None:
  75. if self._check_exists():
  76. return
  77. download_and_extract_archive(self._URL, download_root=str(self._base_folder), md5=self._MD5)