Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

gtsrb.py 3.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  1. import csv
  2. import pathlib
  3. from typing import Any, Callable, Optional, Tuple, Union
  4. import PIL
  5. from .folder import make_dataset
  6. from .utils import download_and_extract_archive, verify_str_arg
  7. from .vision import VisionDataset
  8. class GTSRB(VisionDataset):
  9. """`German Traffic Sign Recognition Benchmark (GTSRB) <https://benchmark.ini.rub.de/>`_ Dataset.
  10. Args:
  11. root (str or ``pathlib.Path``): Root directory of the dataset.
  12. split (string, optional): The dataset split, supports ``"train"`` (default), or ``"test"``.
  13. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed
  14. version. E.g, ``transforms.RandomCrop``.
  15. target_transform (callable, optional): A function/transform that takes in the 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. def __init__(
  21. self,
  22. root: Union[str, pathlib.Path],
  23. split: str = "train",
  24. transform: Optional[Callable] = None,
  25. target_transform: Optional[Callable] = None,
  26. download: bool = False,
  27. ) -> None:
  28. super().__init__(root, transform=transform, target_transform=target_transform)
  29. self._split = verify_str_arg(split, "split", ("train", "test"))
  30. self._base_folder = pathlib.Path(root) / "gtsrb"
  31. self._target_folder = (
  32. self._base_folder / "GTSRB" / ("Training" if self._split == "train" else "Final_Test/Images")
  33. )
  34. if download:
  35. self.download()
  36. if not self._check_exists():
  37. raise RuntimeError("Dataset not found. You can use download=True to download it")
  38. if self._split == "train":
  39. samples = make_dataset(str(self._target_folder), extensions=(".ppm",))
  40. else:
  41. with open(self._base_folder / "GT-final_test.csv") as csv_file:
  42. samples = [
  43. (str(self._target_folder / row["Filename"]), int(row["ClassId"]))
  44. for row in csv.DictReader(csv_file, delimiter=";", skipinitialspace=True)
  45. ]
  46. self._samples = samples
  47. self.transform = transform
  48. self.target_transform = target_transform
  49. def __len__(self) -> int:
  50. return len(self._samples)
  51. def __getitem__(self, index: int) -> Tuple[Any, Any]:
  52. path, target = self._samples[index]
  53. sample = PIL.Image.open(path).convert("RGB")
  54. if self.transform is not None:
  55. sample = self.transform(sample)
  56. if self.target_transform is not None:
  57. target = self.target_transform(target)
  58. return sample, target
  59. def _check_exists(self) -> bool:
  60. return self._target_folder.is_dir()
  61. def download(self) -> None:
  62. if self._check_exists():
  63. return
  64. base_url = "https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/"
  65. if self._split == "train":
  66. download_and_extract_archive(
  67. f"{base_url}GTSRB-Training_fixed.zip",
  68. download_root=str(self._base_folder),
  69. md5="513f3c79a4c5141765e10e952eaa2478",
  70. )
  71. else:
  72. download_and_extract_archive(
  73. f"{base_url}GTSRB_Final_Test_Images.zip",
  74. download_root=str(self._base_folder),
  75. md5="c7e4e6327067d32654124b0fe9e82185",
  76. )
  77. download_and_extract_archive(
  78. f"{base_url}GTSRB_Final_Test_GT.zip",
  79. download_root=str(self._base_folder),
  80. md5="fe31e9c9270bbcd7b84b7f21a9d9d9e5",
  81. )
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...