-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add SUN397 Dataset #5132
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 SUN397 Dataset #5132
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c742860
dataset class added
saswatpp 66ddf2f
fix code format
saswatpp 636de77
fixed requested changes
saswatpp 8704eab
Merge branch 'main' of https://github.com/pytorch/vision into sun397-…
saswatpp 61b4f05
fixed issues in sun397
saswatpp e4bc4e0
resolve merge conflicts
saswatpp 90cee2e
Update torchvision/datasets/sun397.py
NicolasHug 69ee35e
Merge branch 'main' into sun397-dataset
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,98 @@ | ||
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 SUN397(VisionDataset): | ||
"""`The SUN397 Data Set <https://vision.princeton.edu/projects/2010/SUN/>`_. | ||
|
||
The SUN397 or Scene UNderstanding (SUN) is a dataset for scene recognition consisting of | ||
397 categories with 108'754 images. The dataset also provides 10 partitions for training | ||
and testing, with each partition consisting of 50 images per class. | ||
|
||
Args: | ||
root (string): Root directory of the dataset. | ||
split (string, optional): The dataset split, supports ``"train"`` (default) and ``"test"``. | ||
partition (int, optional): A valid partition can be an integer from 1 to 10 or None, | ||
for the entire dataset. | ||
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. | ||
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. | ||
""" | ||
|
||
_DATASET_URL = "http://vision.princeton.edu/projects/2010/SUN/SUN397.tar.gz" | ||
_DATASET_MD5 = "8ca2778205c41d23104230ba66911c7a" | ||
_PARTITIONS_URL = "https://vision.princeton.edu/projects/2010/SUN/download/Partitions.zip" | ||
_PARTITIONS_MD5 = "29a205c0a0129d21f36cbecfefe81881" | ||
|
||
def __init__( | ||
self, | ||
root: str, | ||
split: str = "train", | ||
partition: Optional[int] = 1, | ||
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.partition = partition | ||
self._data_dir = Path(self.root) / "SUN397" | ||
|
||
if self.partition is not None: | ||
if self.partition < 0 or self.partition > 10: | ||
raise RuntimeError(f"The partition parameter should be an int in [1, 10] or None, got {partition}.") | ||
|
||
if download: | ||
self._download() | ||
|
||
if not self._check_exists(): | ||
raise RuntimeError("Dataset not found. You can use download=True to download it") | ||
|
||
with open(self._data_dir / "ClassName.txt") as f: | ||
self.classes = [c[3:].strip() for c in f] | ||
|
||
self.class_to_idx = dict(zip(self.classes, range(len(self.classes)))) | ||
if self.partition is not None: | ||
with open(self._data_dir / f"{self.split.title()}ing_{self.partition:02d}.txt", "r") as f: | ||
self._image_files = [self._data_dir.joinpath(*line.strip()[1:].split("/")) for line in f] | ||
else: | ||
self._image_files = list(self._data_dir.rglob("sun_*.jpg")) | ||
|
||
self._labels = [ | ||
self.class_to_idx["/".join(path.relative_to(self._data_dir).parts[1:-1])] for path in self._image_files | ||
] | ||
|
||
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 _check_exists(self) -> bool: | ||
return self._data_dir.exists() and self._data_dir.is_dir() | ||
|
||
def extra_repr(self) -> str: | ||
return "Split: {split}".format(**self.__dict__) | ||
|
||
def _download(self) -> None: | ||
saswatpp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if self._check_exists(): | ||
return | ||
download_and_extract_archive(self._DATASET_URL, download_root=self.root, md5=self._DATASET_MD5) | ||
download_and_extract_archive(self._PARTITIONS_URL, download_root=str(self._data_dir), md5=self._PARTITIONS_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.