-
Notifications
You must be signed in to change notification settings - Fork 7.1k
add Imagenette dataset #8139
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
add Imagenette dataset #8139
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
227a363
add Imagenette dataset
pmeier 07be817
add tests
pmeier a029071
add Imagenette to transforms v2 wrapper
pmeier e092477
add documentation
pmeier a5fc971
Merge branch 'main' into imagenette
pmeier fb8e284
add Imagenette to documentation
pmeier 6a89129
fix link
pmeier db83c6e
Merge branch 'main' into imagenette
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ Image classification | |
GTSRB | ||
INaturalist | ||
ImageNet | ||
Imagenette | ||
KMNIST | ||
LFWPeople | ||
LSUN | ||
|
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,104 @@ | ||
from pathlib import Path | ||
from typing import Any, Callable, Optional, Tuple | ||
|
||
from PIL import Image | ||
|
||
from .folder import find_classes, make_dataset | ||
from .utils import download_and_extract_archive, verify_str_arg | ||
from .vision import VisionDataset | ||
|
||
|
||
class Imagenette(VisionDataset): | ||
"""`Imagenette <https://github.com/fastai/imagenette#imagenette-1>`_ image classification dataset. | ||
|
||
Args: | ||
root (string): Root directory of the Imagenette dataset. | ||
split (string, optional): The dataset split. Supports ``"train"`` (default), and ``"val"``. | ||
size (string, optional): The image size. Supports ``"full"`` (default), ``"320px"``, and ``"160px"``. | ||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
download (bool, optional): If ``True``, downloads the dataset components and places them in ``root``. Already | ||
downloaded archives are not downloaded again. | ||
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. | ||
|
||
Attributes: | ||
classes (list): List of the class name tuples. | ||
class_to_idx (dict): Dict with items (class name, class index). | ||
wnids (list): List of the WordNet IDs. | ||
wnid_to_idx (dict): Dict with items (WordNet ID, class index). | ||
""" | ||
|
||
_ARCHIVES = { | ||
"full": ("https://s3.amazonaws.com/fast-ai-imageclas/imagenette2.tgz", "fe2fc210e6bb7c5664d602c3cd71e612"), | ||
"320px": ("https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-320.tgz", "3df6f0d01a2c9592104656642f5e78a3"), | ||
"160px": ("https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-160.tgz", "e793b78cc4c9e9a4ccc0c1155377a412"), | ||
} | ||
_WNID_TO_CLASS = { | ||
"n01440764": ("tench", "Tinca tinca"), | ||
"n02102040": ("English springer", "English springer spaniel"), | ||
"n02979186": ("cassette player",), | ||
"n03000684": ("chain saw", "chainsaw"), | ||
"n03028079": ("church", "church building"), | ||
"n03394916": ("French horn", "horn"), | ||
"n03417042": ("garbage truck", "dustcart"), | ||
"n03425413": ("gas pump", "gasoline pump", "petrol pump", "island dispenser"), | ||
"n03445777": ("golf ball",), | ||
"n03888257": ("parachute", "chute"), | ||
} | ||
|
||
def __init__( | ||
self, | ||
root: str, | ||
split: str = "train", | ||
size: str = "full", | ||
download=False, | ||
transform: Optional[Callable] = None, | ||
target_transform: Optional[Callable] = None, | ||
) -> None: | ||
super().__init__(root, transform=transform, target_transform=target_transform) | ||
|
||
self._split = verify_str_arg(split, "split", ["train", "val"]) | ||
self._size = verify_str_arg(size, "size", ["full", "320px", "160px"]) | ||
|
||
self._url, self._md5 = self._ARCHIVES[self._size] | ||
self._size_root = Path(self.root) / Path(self._url).stem | ||
self._image_root = str(self._size_root / self._split) | ||
|
||
if download: | ||
self._download() | ||
elif not self._check_exists(): | ||
raise RuntimeError("Dataset not found. You can use download=True to download it.") | ||
|
||
self.wnids, self.wnid_to_idx = find_classes(self._image_root) | ||
self.classes = [self._WNID_TO_CLASS[wnid] for wnid in self.wnids] | ||
self.class_to_idx = { | ||
class_name: idx for wnid, idx in self.wnid_to_idx.items() for class_name in self._WNID_TO_CLASS[wnid] | ||
} | ||
self._samples = make_dataset(self._image_root, self.wnid_to_idx, extensions=".jpeg") | ||
|
||
def _check_exists(self) -> bool: | ||
return self._size_root.exists() | ||
|
||
def _download(self): | ||
if self._check_exists(): | ||
raise RuntimeError( | ||
f"The directory {self._size_root} already exists. " | ||
f"If you want to re-download or re-extract the images, delete the directory." | ||
) | ||
|
||
download_and_extract_archive(self._url, self.root, md5=self._md5) | ||
|
||
def __getitem__(self, idx: int) -> Tuple[Any, Any]: | ||
path, label = self._samples[idx] | ||
image = Image.open(path).convert("RGB") | ||
|
||
if self.transform is not None: | ||
image = self.transform(image) | ||
|
||
if self.target_transform is not None: | ||
label = self.target_transform(label) | ||
|
||
return image, label | ||
|
||
def __len__(self) -> int: | ||
return len(self._samples) |
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
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.