Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
31fadbe
Adding multiweight support for shufflenetv2 prototype models
jdsgomes Oct 29, 2021
1e578b7
Revert "Adding multiweight support for shufflenetv2 prototype models"
jdsgomes Oct 29, 2021
85e4429
Merge branch 'pytorch:main' into main
jdsgomes Oct 29, 2021
4e3d900
Adding multiweight support for shufflenetv2 prototype models
jdsgomes Oct 29, 2021
615b612
Revert "Adding multiweight support for shufflenetv2 prototype models"
jdsgomes Oct 29, 2021
a0bbece
Merge branch 'pytorch:main' into main
jdsgomes Oct 31, 2021
ba966f4
Merge branch 'pytorch:main' into main
jdsgomes Nov 1, 2021
6cdd49b
Merge branch 'pytorch:main' into main
jdsgomes Dec 10, 2021
d4f1638
Merge branch 'pytorch:main' into main
jdsgomes Dec 17, 2021
7d66c8b
Add Food101 Dataset
jdsgomes Dec 21, 2021
b2ad8fb
Merge branch 'main' into food-101-dataset
jdsgomes Dec 21, 2021
6b08514
Remove unecessary Path contructor calls
jdsgomes Dec 21, 2021
93b78a5
Remove unecessary Path contructor callsi and fix types
jdsgomes Dec 21, 2021
88ddbb2
Merge branch 'food-101-dataset' of github.com:jdsgomes/vision into fo…
jdsgomes Dec 21, 2021
72a8eaa
Fix tests
jdsgomes Dec 21, 2021
1407dbd
Address PR comments from @pmeier
jdsgomes Dec 22, 2021
23f685a
Fix bug in tests and in food101 dataset
jdsgomes Dec 22, 2021
6285b31
Fix bug in tests and in food101 dataset
jdsgomes Dec 22, 2021
fa73c17
Update torchvision/datasets/food101.py
pmeier Dec 22, 2021
ed4e0b7
Merge branch 'main' into food-101-dataset
pmeier Dec 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ You can also create your own datasets using the provided :ref:`base classes <bas
Flickr30k
FlyingChairs
FlyingThings3D
Food101
HD1K
HMDB51
ImageNet
Expand Down
37 changes: 37 additions & 0 deletions test/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2168,5 +2168,42 @@ def inject_fake_data(self, tmpdir, config):
return num_sequences * (num_examples_per_sequence - 1)


class Food101TestCase(datasets_utils.ImageDatasetTestCase):
DATASET_CLASS = datasets.Food101
FEATURE_TYPES = (PIL.Image.Image, int)

ADDITIONAL_CONFIGS = datasets_utils.combinations_grid(split=("train", "test"))

def inject_fake_data(self, tmpdir: str, config):
root_folder = pathlib.Path(tmpdir) / "food-101"
image_folder = root_folder / "images"
meta_folder = root_folder / "meta"

image_folder.mkdir(parents=True)
meta_folder.mkdir()

num_images_per_class = 5

metadata = {}
n_samples_per_class = 3 if config["split"] == "train" else 2
sampled_classes = ("apple_pie", "crab_cakes", "gyoza")
for cls in sampled_classes:
im_fnames = datasets_utils.create_image_folder(
image_folder,
cls,
file_name_fn=lambda idx: f"{idx}.jpg",
num_examples=num_images_per_class,
)
metadata[cls] = [
"/".join(fname.relative_to(image_folder).with_suffix("").parts)
for fname in random.choices(im_fnames, k=n_samples_per_class)
]

with open(meta_folder / f"{config['split']}.json", "w") as file:
file.write(json.dumps(metadata))

return len(sampled_classes * n_samples_per_class)


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions torchvision/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .fakedata import FakeData
from .flickr import Flickr8k, Flickr30k
from .folder import ImageFolder, DatasetFolder
from .food101 import Food101
from .hmdb51 import HMDB51
from .imagenet import ImageNet
from .inaturalist import INaturalist
Expand Down Expand Up @@ -77,4 +78,5 @@
"FlyingChairs",
"FlyingThings3D",
"HD1K",
"Food101",
)
90 changes: 90 additions & 0 deletions torchvision/datasets/food101.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import json
from pathlib import Path
from typing import Any, Tuple, Callable, Optional

import PIL.Image

from .utils import verify_str_arg, download_and_extract_archive
from .vision import VisionDataset


class Food101(VisionDataset):
"""`The Food-101 Data Set <https://data.vision.ee.ethz.ch/cvl/datasets_extra/food-101/>`_.

The Food-101 is a challenging data set of 101 food categories, with 101'000 images.
For each class, 250 manually reviewed test images are provided as well as 750 training images.
On purpose, the training images were not cleaned, and thus still contain some amount of noise.
This comes mostly in the form of intense colors and sometimes wrong labels. All images were
rescaled to have a maximum side length of 512 pixels.


Args:
root (string): Root directory of the dataset.
split (string, optional): The dataset split, supports ``"train"`` (default) and ``"test"``.
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.
"""

_URL = "http://data.vision.ee.ethz.ch/cvl/food-101.tar.gz"
_MD5 = "85eeb15f3717b99a5da872d97d918f87"

def __init__(
self,
root: str,
split: str = "train",
download: bool = True,
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", "test"))
self._base_folder = Path(self._root_path) / "food-101"
self._meta_folder = self._base_folder / "meta"
self._images_folder = self._base_folder / "images"

if download:
self._download()

if not self._check_exists():
raise RuntimeError("Dataset not found. You can use download=True to download it")

self._labels = []
self._image_files = []
with open(self._meta_folder / f"{split}.json", "r") as f:
metadata = json.loads(f.read())

self.classes = sorted(metadata.keys())
self.class_to_idx = dict(zip(self.classes, range(len(self.classes))))

for class_label, im_rel_paths in metadata.items():
self._labels += [self.class_to_idx[class_label]] * len(im_rel_paths)
self._image_files += [
self._images_folder.joinpath(*f"{im_rel_path}.jpg".split("/")) for im_rel_path in im_rel_paths
]

def __len__(self) -> int:
return len(self._image_files)

def __getitem__(self, idx) -> Tuple[Any, Any]:
image_file, label = self._image_files[idx], self._labels[idx]
image = PIL.Image.open(image_file).convert("RGB")

if self.transform:
image = self.transform(image)

if self.target_transform:
label = self.target_transform(label)

return image, label

def extra_repr(self) -> str:
return f"split={self._split}"

def _check_exists(self) -> bool:
return all(folder.exists() and folder.is_dir() for folder in (self._meta_folder, self._images_folder))

def _download(self) -> None:
if self._check_exists():
return
download_and_extract_archive(self._URL, download_root=self.root, md5=self._MD5)