svhn.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import os.path
  2. from typing import Any, Callable, Optional, Tuple
  3. import numpy as np
  4. from PIL import Image
  5. from .utils import check_integrity, download_url, verify_str_arg
  6. from .vision import VisionDataset
  7. class SVHN(VisionDataset):
  8. """`SVHN <http://ufldl.stanford.edu/housenumbers/>`_ Dataset.
  9. Note: The SVHN dataset assigns the label `10` to the digit `0`. However, in this Dataset,
  10. we assign the label `0` to the digit `0` to be compatible with PyTorch loss functions which
  11. expect the class labels to be in the range `[0, C-1]`
  12. .. warning::
  13. This class needs `scipy <https://docs.scipy.org/doc/>`_ to load data from `.mat` format.
  14. Args:
  15. root (string): Root directory of the dataset where the data is stored.
  16. split (string): One of {'train', 'test', 'extra'}.
  17. Accordingly dataset is selected. 'extra' is Extra training set.
  18. transform (callable, optional): A function/transform that takes in an PIL image
  19. and returns a transformed version. E.g, ``transforms.RandomCrop``
  20. target_transform (callable, optional): A function/transform that takes in the
  21. target and transforms it.
  22. download (bool, optional): If true, downloads the dataset from the internet and
  23. puts it in root directory. If dataset is already downloaded, it is not
  24. downloaded again.
  25. """
  26. split_list = {
  27. "train": [
  28. "http://ufldl.stanford.edu/housenumbers/train_32x32.mat",
  29. "train_32x32.mat",
  30. "e26dedcc434d2e4c54c9b2d4a06d8373",
  31. ],
  32. "test": [
  33. "http://ufldl.stanford.edu/housenumbers/test_32x32.mat",
  34. "test_32x32.mat",
  35. "eb5a983be6a315427106f1b164d9cef3",
  36. ],
  37. "extra": [
  38. "http://ufldl.stanford.edu/housenumbers/extra_32x32.mat",
  39. "extra_32x32.mat",
  40. "a93ce644f1a588dc4d68dda5feec44a7",
  41. ],
  42. }
  43. def __init__(
  44. self,
  45. root: str,
  46. split: str = "train",
  47. transform: Optional[Callable] = None,
  48. target_transform: Optional[Callable] = None,
  49. download: bool = False,
  50. ) -> None:
  51. super().__init__(root, transform=transform, target_transform=target_transform)
  52. self.split = verify_str_arg(split, "split", tuple(self.split_list.keys()))
  53. self.url = self.split_list[split][0]
  54. self.filename = self.split_list[split][1]
  55. self.file_md5 = self.split_list[split][2]
  56. if download:
  57. self.download()
  58. if not self._check_integrity():
  59. raise RuntimeError("Dataset not found or corrupted. You can use download=True to download it")
  60. # import here rather than at top of file because this is
  61. # an optional dependency for torchvision
  62. import scipy.io as sio
  63. # reading(loading) mat file as array
  64. loaded_mat = sio.loadmat(os.path.join(self.root, self.filename))
  65. self.data = loaded_mat["X"]
  66. # loading from the .mat file gives an np.ndarray of type np.uint8
  67. # converting to np.int64, so that we have a LongTensor after
  68. # the conversion from the numpy array
  69. # the squeeze is needed to obtain a 1D tensor
  70. self.labels = loaded_mat["y"].astype(np.int64).squeeze()
  71. # the svhn dataset assigns the class label "10" to the digit 0
  72. # this makes it inconsistent with several loss functions
  73. # which expect the class labels to be in the range [0, C-1]
  74. np.place(self.labels, self.labels == 10, 0)
  75. self.data = np.transpose(self.data, (3, 2, 0, 1))
  76. def __getitem__(self, index: int) -> Tuple[Any, Any]:
  77. """
  78. Args:
  79. index (int): Index
  80. Returns:
  81. tuple: (image, target) where target is index of the target class.
  82. """
  83. img, target = self.data[index], int(self.labels[index])
  84. # doing this so that it is consistent with all other datasets
  85. # to return a PIL Image
  86. img = Image.fromarray(np.transpose(img, (1, 2, 0)))
  87. if self.transform is not None:
  88. img = self.transform(img)
  89. if self.target_transform is not None:
  90. target = self.target_transform(target)
  91. return img, target
  92. def __len__(self) -> int:
  93. return len(self.data)
  94. def _check_integrity(self) -> bool:
  95. root = self.root
  96. md5 = self.split_list[self.split][2]
  97. fpath = os.path.join(root, self.filename)
  98. return check_integrity(fpath, md5)
  99. def download(self) -> None:
  100. md5 = self.split_list[self.split][2]
  101. download_url(self.url, self.root, self.filename, md5)
  102. def extra_repr(self) -> str:
  103. return "Split: {split}".format(**self.__dict__)