-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add GTSRB dataset to the list of supporting datasets #5117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dc113b8
Added GTSRB dataset
sumukhaithal6 bc1f00c
Added unittest for GTSRB dataset
sumukhaithal6 e2fa625
Merge branch 'main' into gtsrb
pmeier b5f512c
Apply suggestions from code review
sumukhaithal6 7fc44bb
More changes from code review
sumukhaithal6 6db7f86
Merge branch 'main' into gtsrb
pmeier 297dfd2
readd accidental removed line
pmeier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import csv | ||
import os | ||
from typing import Any, Callable, Optional, Tuple | ||
|
||
import PIL | ||
|
||
from .folder import make_dataset | ||
from .utils import download_and_extract_archive | ||
from .vision import VisionDataset | ||
|
||
|
||
class GTSRB(VisionDataset): | ||
"""`German Traffic Sign Recognition Benchmark (GTSRB) <https://benchmark.ini.rub.de/>`_ Dataset. | ||
|
||
Args: | ||
root (string): Root directory of the dataset. | ||
train (bool, optional): If True, creates dataset from training set, otherwise | ||
creates from test set. | ||
transform (callable, optional): A function/transform that takes in an PIL image and returns a transformed | ||
version. E.g, ``transforms.RandomCrop``. | ||
target_transform (callable, optional): A function/transform that takes in the target and transforms it. | ||
download (bool, optional): If True, downloads the dataset from the internet and | ||
puts it in root directory. If dataset is already downloaded, it is not | ||
downloaded again. | ||
""" | ||
|
||
# Ground Truth for the test set | ||
_gt_url = "https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/GTSRB_Final_Test_GT.zip" | ||
_gt_csv = "GT-final_test.csv" | ||
_gt_md5 = "fe31e9c9270bbcd7b84b7f21a9d9d9e5" | ||
|
||
# URLs for the test and train set | ||
_urls = ( | ||
"https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/GTSRB_Final_Test_Images.zip", | ||
"https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/GTSRB-Training_fixed.zip", | ||
) | ||
|
||
_md5s = ("c7e4e6327067d32654124b0fe9e82185", "513f3c79a4c5141765e10e952eaa2478") | ||
|
||
def __init__( | ||
self, | ||
root: str, | ||
train: bool = True, | ||
transform: Optional[Callable] = None, | ||
target_transform: Optional[Callable] = None, | ||
download: bool = False, | ||
) -> None: | ||
|
||
super().__init__(root, transform=transform, target_transform=target_transform) | ||
|
||
self.root = os.path.expanduser(root) | ||
|
||
self.train = train | ||
|
||
self._base_folder = os.path.join(self.root, type(self).__name__) | ||
self._target_folder = os.path.join(self._base_folder, "Training" if self.train else "Final_Test/Images") | ||
|
||
if download: | ||
self.download() | ||
|
||
if not self._check_exists(): | ||
raise RuntimeError("Dataset not found. You can use download=True to download it") | ||
|
||
if train: | ||
samples = make_dataset(self._target_folder, extensions=(".ppm",)) | ||
else: | ||
with open(os.path.join(self._base_folder, self._gt_csv)) as csv_file: | ||
samples = [ | ||
(os.path.join(self._target_folder, row["Filename"]), int(row["ClassId"])) | ||
for row in csv.DictReader(csv_file, delimiter=";", skipinitialspace=True) | ||
] | ||
|
||
self._samples = samples | ||
self.transform = transform | ||
self.target_transform = target_transform | ||
|
||
def __len__(self) -> int: | ||
return len(self._samples) | ||
|
||
def __getitem__(self, index: int) -> Tuple[Any, Any]: | ||
|
||
path, target = self._samples[index] | ||
sample = PIL.Image.open(path).convert("RGB") | ||
|
||
if self.transform is not None: | ||
sample = self.transform(sample) | ||
|
||
if self.target_transform is not None: | ||
target = self.target_transform(target) | ||
|
||
return sample, target | ||
|
||
def _check_exists(self) -> bool: | ||
return os.path.exists(self._target_folder) and os.path.isdir(self._target_folder) | ||
|
||
def download(self) -> None: | ||
if self._check_exists(): | ||
return | ||
|
||
download_and_extract_archive(self._urls[self.train], download_root=self.root, md5=self._md5s[self.train]) | ||
|
||
if not self.train: | ||
# Download Ground Truth for the test set | ||
download_and_extract_archive( | ||
self._gt_url, download_root=self.root, extract_root=self._base_folder, md5=self._gt_md5 | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.