usps.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import os
  2. from typing import Any, Callable, Optional, Tuple
  3. import numpy as np
  4. from PIL import Image
  5. from .utils import download_url
  6. from .vision import VisionDataset
  7. class USPS(VisionDataset):
  8. """`USPS <https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass.html#usps>`_ Dataset.
  9. The data-format is : [label [index:value ]*256 \\n] * num_lines, where ``label`` lies in ``[1, 10]``.
  10. The value for each pixel lies in ``[-1, 1]``. Here we transform the ``label`` into ``[0, 9]``
  11. and make pixel values in ``[0, 255]``.
  12. Args:
  13. root (string): Root directory of dataset to store``USPS`` data files.
  14. train (bool, optional): If True, creates dataset from ``usps.bz2``,
  15. otherwise from ``usps.t.bz2``.
  16. transform (callable, optional): A function/transform that takes in an PIL image
  17. and returns a transformed version. E.g, ``transforms.RandomCrop``
  18. target_transform (callable, optional): A function/transform that takes in the
  19. target and transforms it.
  20. download (bool, optional): If true, downloads the dataset from the internet and
  21. puts it in root directory. If dataset is already downloaded, it is not
  22. downloaded again.
  23. """
  24. split_list = {
  25. "train": [
  26. "https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/usps.bz2",
  27. "usps.bz2",
  28. "ec16c51db3855ca6c91edd34d0e9b197",
  29. ],
  30. "test": [
  31. "https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/usps.t.bz2",
  32. "usps.t.bz2",
  33. "8ea070ee2aca1ac39742fdd1ef5ed118",
  34. ],
  35. }
  36. def __init__(
  37. self,
  38. root: str,
  39. train: bool = True,
  40. transform: Optional[Callable] = None,
  41. target_transform: Optional[Callable] = None,
  42. download: bool = False,
  43. ) -> None:
  44. super().__init__(root, transform=transform, target_transform=target_transform)
  45. split = "train" if train else "test"
  46. url, filename, checksum = self.split_list[split]
  47. full_path = os.path.join(self.root, filename)
  48. if download and not os.path.exists(full_path):
  49. download_url(url, self.root, filename, md5=checksum)
  50. import bz2
  51. with bz2.open(full_path) as fp:
  52. raw_data = [line.decode().split() for line in fp.readlines()]
  53. tmp_list = [[x.split(":")[-1] for x in data[1:]] for data in raw_data]
  54. imgs = np.asarray(tmp_list, dtype=np.float32).reshape((-1, 16, 16))
  55. imgs = ((imgs + 1) / 2 * 255).astype(dtype=np.uint8)
  56. targets = [int(d[0]) - 1 for d in raw_data]
  57. self.data = imgs
  58. self.targets = targets
  59. def __getitem__(self, index: int) -> Tuple[Any, Any]:
  60. """
  61. Args:
  62. index (int): Index
  63. Returns:
  64. tuple: (image, target) where target is index of the target class.
  65. """
  66. img, target = self.data[index], int(self.targets[index])
  67. # doing this so that it is consistent with all other datasets
  68. # to return a PIL Image
  69. img = Image.fromarray(img, mode="L")
  70. if self.transform is not None:
  71. img = self.transform(img)
  72. if self.target_transform is not None:
  73. target = self.target_transform(target)
  74. return img, target
  75. def __len__(self) -> int:
  76. return len(self.data)