-
Notifications
You must be signed in to change notification settings - Fork 7.1k
OxfordIIITPet dataset #5116
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
OxfordIIITPet dataset #5116
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2d84076
add prototype dataset for oxford-iiit-pet
pmeier 597d62c
add old-style dataset
pmeier d1b828a
add tests
pmeier 17ece53
fix mypy
pmeier 3c7e975
Merge branch 'main' into datasets/oxford-pets
pmeier 7086f33
fix test
pmeier b5c6539
remove properties and use pathlib
pmeier d28945c
target_type to target_types
pmeier 21a05ed
move target annotation
pmeier 1a0f0d9
add docstring
pmeier 323108a
fix test
pmeier f248e70
Merge branch 'main' into datasets/oxford-pets
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,126 @@ | ||
import os | ||
import os.path | ||
import pathlib | ||
from typing import Any, Callable, Optional, Union, Tuple | ||
from typing import Sequence | ||
|
||
from PIL import Image | ||
|
||
from .utils import download_and_extract_archive, verify_str_arg | ||
from .vision import VisionDataset | ||
|
||
|
||
class OxfordIIITPet(VisionDataset): | ||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""`Oxford-IIIT Pet Dataset <https://www.robots.ox.ac.uk/~vgg/data/pets/>`_. | ||
|
||
Args: | ||
root (string): Root directory of the dataset. | ||
split (string, optional): The dataset split, supports ``"trainval"`` (default) or ``"test"``. | ||
target_types (string, sequence of strings, optional): Types of target to use. Can be ``category`` (default) or | ||
``segmentation``. Can also be a list to output a tuple with all specified target types. The types represent: | ||
|
||
- ``category`` (int): Label for one of the 37 pet categories. | ||
- ``segmentation`` (PIL image): Segmentation trimap of the image. | ||
|
||
If empty, ``None`` will be returned as target. | ||
|
||
transform (callable, optional): A function/transform that takes in a 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 into ``root/dtd``. If | ||
dataset is already downloaded, it is not downloaded again. | ||
""" | ||
|
||
_RESOURCES = ( | ||
("https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz", "5c4f3ee8e5d25df40f4fd59a7f44e54c"), | ||
("https://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz", "95a8c909bbe2e81eed6a22bccdf3f68f"), | ||
) | ||
_VALID_TARGET_TYPES = ("category", "segmentation") | ||
|
||
def __init__( | ||
self, | ||
root: str, | ||
split: str = "trainval", | ||
target_types: Union[Sequence[str], str] = "category", | ||
transforms: Optional[Callable] = None, | ||
transform: Optional[Callable] = None, | ||
target_transform: Optional[Callable] = None, | ||
download: bool = True, | ||
): | ||
self._split = verify_str_arg(split, "split", ("trainval", "test")) | ||
if isinstance(target_types, str): | ||
target_types = [target_types] | ||
self._target_types = [ | ||
verify_str_arg(target_type, "target_types", self._VALID_TARGET_TYPES) for target_type in target_types | ||
] | ||
|
||
super().__init__(root, transforms=transforms, transform=transform, target_transform=target_transform) | ||
self._base_folder = pathlib.Path(self.root) / "oxford-iiit-pet" | ||
self._images_folder = self._base_folder / "images" | ||
self._anns_folder = self._base_folder / "annotations" | ||
self._segs_folder = self._anns_folder / "trimaps" | ||
|
||
if download: | ||
self._download() | ||
|
||
if not self._check_exists(): | ||
raise RuntimeError("Dataset not found. You can use download=True to download it") | ||
|
||
image_ids = [] | ||
self._labels = [] | ||
with open(self._anns_folder / f"{self._split}.txt") as file: | ||
for line in file: | ||
image_id, label, *_ = line.strip().split() | ||
image_ids.append(image_id) | ||
self._labels.append(int(label) - 1) | ||
|
||
self.classes = [ | ||
" ".join(part.title() for part in raw_cls.split("_")) | ||
for raw_cls, _ in sorted( | ||
{(image_id.rsplit("_", 1)[0], label) for image_id, label in zip(image_ids, self._labels)}, | ||
key=lambda image_id_and_label: image_id_and_label[1], | ||
) | ||
] | ||
self.class_to_idx = dict(zip(self.classes, range(len(self.classes)))) | ||
|
||
self._images = [self._images_folder / f"{image_id}.jpg" for image_id in image_ids] | ||
self._segs = [self._segs_folder / f"{image_id}.png" for image_id in image_ids] | ||
|
||
def __len__(self) -> int: | ||
return len(self._images) | ||
|
||
def __getitem__(self, idx: int) -> Tuple[Any, Any]: | ||
image = Image.open(self._images[idx]).convert("RGB") | ||
|
||
target: Any = [] | ||
for target_type in self._target_types: | ||
if target_type == "category": | ||
target.append(self._labels[idx]) | ||
else: # target_type == "segmentation" | ||
target.append(Image.open(self._segs[idx])) | ||
|
||
if not target: | ||
target = None | ||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
elif len(target) == 1: | ||
target = target[0] | ||
else: | ||
target = tuple(target) | ||
|
||
if self.transforms: | ||
image, target = self.transforms(image, target) | ||
|
||
return image, target | ||
|
||
def _check_exists(self) -> bool: | ||
for folder in (self._images_folder, self._anns_folder): | ||
if not (os.path.exists(folder) and os.path.isdir(folder)): | ||
return False | ||
else: | ||
return True | ||
|
||
def _download(self) -> None: | ||
if self._check_exists(): | ||
return | ||
|
||
for url, md5 in self._RESOURCES: | ||
download_and_extract_archive(url, download_root=str(self._base_folder), md5=md5) |
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
37 changes: 37 additions & 0 deletions
37
torchvision/prototype/datasets/_builtin/oxford-iiit-pet.categories
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,37 @@ | ||
Abyssinian | ||
American Bulldog | ||
American Pit Bull Terrier | ||
Basset Hound | ||
Beagle | ||
Bengal | ||
Birman | ||
Bombay | ||
Boxer | ||
British Shorthair | ||
Chihuahua | ||
Egyptian Mau | ||
English Cocker Spaniel | ||
English Setter | ||
German Shorthaired | ||
Great Pyrenees | ||
Havanese | ||
Japanese Chin | ||
Keeshond | ||
Leonberger | ||
Maine Coon | ||
Miniature Pinscher | ||
Newfoundland | ||
Persian | ||
Pomeranian | ||
Pug | ||
Ragdoll | ||
Russian Blue | ||
Saint Bernard | ||
Samoyed | ||
Scottish Terrier | ||
Shiba Inu | ||
Siamese | ||
Sphynx | ||
Staffordshire Bull Terrier | ||
Wheaten Terrier | ||
Yorkshire Terrier |
Oops, something went wrong.
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.