|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any, Callable, Optional, Tuple |
| 3 | + |
| 4 | +from PIL import Image |
| 5 | + |
| 6 | +from .folder import find_classes, make_dataset |
| 7 | +from .utils import download_and_extract_archive, verify_str_arg |
| 8 | +from .vision import VisionDataset |
| 9 | + |
| 10 | + |
| 11 | +class Imagenette(VisionDataset): |
| 12 | + """`Imagenette <https://github.com/fastai/imagenette#imagenette-1>`_ image classification dataset. |
| 13 | +
|
| 14 | + Args: |
| 15 | + root (string): Root directory of the Imagenette dataset. |
| 16 | + split (string, optional): The dataset split. Supports ``"train"`` (default), and ``"val"``. |
| 17 | + size (string, optional): The image size. Supports ``"full"`` (default), ``"320px"``, and ``"160px"``. |
| 18 | + download (bool, optional): If ``True``, downloads the dataset components and places them in ``root``. Already |
| 19 | + downloaded archives are not downloaded again. |
| 20 | + transform (callable, optional): A function/transform that takes in an PIL image and returns a transformed |
| 21 | + version, e.g. ``transforms.RandomCrop``. |
| 22 | + target_transform (callable, optional): A function/transform that takes in the target and transforms it. |
| 23 | +
|
| 24 | + Attributes: |
| 25 | + classes (list): List of the class name tuples. |
| 26 | + class_to_idx (dict): Dict with items (class name, class index). |
| 27 | + wnids (list): List of the WordNet IDs. |
| 28 | + wnid_to_idx (dict): Dict with items (WordNet ID, class index). |
| 29 | + """ |
| 30 | + |
| 31 | + _ARCHIVES = { |
| 32 | + "full": ("https://s3.amazonaws.com/fast-ai-imageclas/imagenette2.tgz", "fe2fc210e6bb7c5664d602c3cd71e612"), |
| 33 | + "320px": ("https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-320.tgz", "3df6f0d01a2c9592104656642f5e78a3"), |
| 34 | + "160px": ("https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-160.tgz", "e793b78cc4c9e9a4ccc0c1155377a412"), |
| 35 | + } |
| 36 | + _WNID_TO_CLASS = { |
| 37 | + "n01440764": ("tench", "Tinca tinca"), |
| 38 | + "n02102040": ("English springer", "English springer spaniel"), |
| 39 | + "n02979186": ("cassette player",), |
| 40 | + "n03000684": ("chain saw", "chainsaw"), |
| 41 | + "n03028079": ("church", "church building"), |
| 42 | + "n03394916": ("French horn", "horn"), |
| 43 | + "n03417042": ("garbage truck", "dustcart"), |
| 44 | + "n03425413": ("gas pump", "gasoline pump", "petrol pump", "island dispenser"), |
| 45 | + "n03445777": ("golf ball",), |
| 46 | + "n03888257": ("parachute", "chute"), |
| 47 | + } |
| 48 | + |
| 49 | + def __init__( |
| 50 | + self, |
| 51 | + root: str, |
| 52 | + split: str = "train", |
| 53 | + size: str = "full", |
| 54 | + download=False, |
| 55 | + transform: Optional[Callable] = None, |
| 56 | + target_transform: Optional[Callable] = None, |
| 57 | + ) -> None: |
| 58 | + super().__init__(root, transform=transform, target_transform=target_transform) |
| 59 | + |
| 60 | + self._split = verify_str_arg(split, "split", ["train", "val"]) |
| 61 | + self._size = verify_str_arg(size, "size", ["full", "320px", "160px"]) |
| 62 | + |
| 63 | + self._url, self._md5 = self._ARCHIVES[self._size] |
| 64 | + self._size_root = Path(self.root) / Path(self._url).stem |
| 65 | + self._image_root = str(self._size_root / self._split) |
| 66 | + |
| 67 | + if download: |
| 68 | + self._download() |
| 69 | + elif not self._check_exists(): |
| 70 | + raise RuntimeError("Dataset not found. You can use download=True to download it.") |
| 71 | + |
| 72 | + self.wnids, self.wnid_to_idx = find_classes(self._image_root) |
| 73 | + self.classes = [self._WNID_TO_CLASS[wnid] for wnid in self.wnids] |
| 74 | + self.class_to_idx = { |
| 75 | + class_name: idx for wnid, idx in self.wnid_to_idx.items() for class_name in self._WNID_TO_CLASS[wnid] |
| 76 | + } |
| 77 | + self._samples = make_dataset(self._image_root, self.wnid_to_idx, extensions=".jpeg") |
| 78 | + |
| 79 | + def _check_exists(self) -> bool: |
| 80 | + return self._size_root.exists() |
| 81 | + |
| 82 | + def _download(self): |
| 83 | + if self._check_exists(): |
| 84 | + raise RuntimeError( |
| 85 | + f"The directory {self._size_root} already exists. " |
| 86 | + f"If you want to re-download or re-extract the images, delete the directory." |
| 87 | + ) |
| 88 | + |
| 89 | + download_and_extract_archive(self._url, self.root, md5=self._md5) |
| 90 | + |
| 91 | + def __getitem__(self, idx: int) -> Tuple[Any, Any]: |
| 92 | + path, label = self._samples[idx] |
| 93 | + image = Image.open(path).convert("RGB") |
| 94 | + |
| 95 | + if self.transform is not None: |
| 96 | + image = self.transform(image) |
| 97 | + |
| 98 | + if self.target_transform is not None: |
| 99 | + label = self.target_transform(label) |
| 100 | + |
| 101 | + return image, label |
| 102 | + |
| 103 | + def __len__(self) -> int: |
| 104 | + return len(self._samples) |
0 commit comments