semeion.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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
  6. from .vision import VisionDataset
  7. class SEMEION(VisionDataset):
  8. r"""`SEMEION <http://archive.ics.uci.edu/ml/datasets/semeion+handwritten+digit>`_ Dataset.
  9. Args:
  10. root (string): Root directory of dataset where directory
  11. ``semeion.py`` exists.
  12. transform (callable, optional): A function/transform that takes in an PIL image
  13. and returns a transformed version. E.g, ``transforms.RandomCrop``
  14. target_transform (callable, optional): A function/transform that takes in the
  15. target and transforms it.
  16. download (bool, optional): If true, downloads the dataset from the internet and
  17. puts it in root directory. If dataset is already downloaded, it is not
  18. downloaded again.
  19. """
  20. url = "http://archive.ics.uci.edu/ml/machine-learning-databases/semeion/semeion.data"
  21. filename = "semeion.data"
  22. md5_checksum = "cb545d371d2ce14ec121470795a77432"
  23. def __init__(
  24. self,
  25. root: str,
  26. transform: Optional[Callable] = None,
  27. target_transform: Optional[Callable] = None,
  28. download: bool = True,
  29. ) -> None:
  30. super().__init__(root, transform=transform, target_transform=target_transform)
  31. if download:
  32. self.download()
  33. if not self._check_integrity():
  34. raise RuntimeError("Dataset not found or corrupted. You can use download=True to download it")
  35. fp = os.path.join(self.root, self.filename)
  36. data = np.loadtxt(fp)
  37. # convert value to 8 bit unsigned integer
  38. # color (white #255) the pixels
  39. self.data = (data[:, :256] * 255).astype("uint8")
  40. self.data = np.reshape(self.data, (-1, 16, 16))
  41. self.labels = np.nonzero(data[:, 256:])[1]
  42. def __getitem__(self, index: int) -> Tuple[Any, Any]:
  43. """
  44. Args:
  45. index (int): Index
  46. Returns:
  47. tuple: (image, target) where target is index of the target class.
  48. """
  49. img, target = self.data[index], int(self.labels[index])
  50. # doing this so that it is consistent with all other datasets
  51. # to return a PIL Image
  52. img = Image.fromarray(img, mode="L")
  53. if self.transform is not None:
  54. img = self.transform(img)
  55. if self.target_transform is not None:
  56. target = self.target_transform(target)
  57. return img, target
  58. def __len__(self) -> int:
  59. return len(self.data)
  60. def _check_integrity(self) -> bool:
  61. root = self.root
  62. fpath = os.path.join(root, self.filename)
  63. if not check_integrity(fpath, self.md5_checksum):
  64. return False
  65. return True
  66. def download(self) -> None:
  67. if self._check_integrity():
  68. print("Files already downloaded and verified")
  69. return
  70. root = self.root
  71. download_url(self.url, root, self.filename, self.md5_checksum)