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