fer2013.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import csv
  2. import pathlib
  3. from typing import Any, Callable, Optional, Tuple
  4. import torch
  5. from PIL import Image
  6. from .utils import check_integrity, verify_str_arg
  7. from .vision import VisionDataset
  8. class FER2013(VisionDataset):
  9. """`FER2013
  10. <https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge>`_ Dataset.
  11. Args:
  12. root (string): Root directory of dataset where directory
  13. ``root/fer2013`` exists.
  14. split (string, optional): The dataset split, supports ``"train"`` (default), or ``"test"``.
  15. transform (callable, optional): A function/transform that takes in an PIL image and returns a transformed
  16. version. E.g, ``transforms.RandomCrop``
  17. target_transform (callable, optional): A function/transform that takes in the target and transforms it.
  18. """
  19. _RESOURCES = {
  20. "train": ("train.csv", "3f0dfb3d3fd99c811a1299cb947e3131"),
  21. "test": ("test.csv", "b02c2298636a634e8c2faabbf3ea9a23"),
  22. }
  23. def __init__(
  24. self,
  25. root: str,
  26. split: str = "train",
  27. transform: Optional[Callable] = None,
  28. target_transform: Optional[Callable] = None,
  29. ) -> None:
  30. self._split = verify_str_arg(split, "split", self._RESOURCES.keys())
  31. super().__init__(root, transform=transform, target_transform=target_transform)
  32. base_folder = pathlib.Path(self.root) / "fer2013"
  33. file_name, md5 = self._RESOURCES[self._split]
  34. data_file = base_folder / file_name
  35. if not check_integrity(str(data_file), md5=md5):
  36. raise RuntimeError(
  37. f"{file_name} not found in {base_folder} or corrupted. "
  38. f"You can download it from "
  39. f"https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge"
  40. )
  41. with open(data_file, "r", newline="") as file:
  42. self._samples = [
  43. (
  44. torch.tensor([int(idx) for idx in row["pixels"].split()], dtype=torch.uint8).reshape(48, 48),
  45. int(row["emotion"]) if "emotion" in row else None,
  46. )
  47. for row in csv.DictReader(file)
  48. ]
  49. def __len__(self) -> int:
  50. return len(self._samples)
  51. def __getitem__(self, idx: int) -> Tuple[Any, Any]:
  52. image_tensor, target = self._samples[idx]
  53. image = Image.fromarray(image_tensor.numpy())
  54. if self.transform is not None:
  55. image = self.transform(image)
  56. if self.target_transform is not None:
  57. target = self.target_transform(target)
  58. return image, target
  59. def extra_repr(self) -> str:
  60. return f"split={self._split}"