-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Stanford cars #5166
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
Stanford cars #5166
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
efcf0cb
[WIP]
abhi-glitchhg 62ca7bf
[WIP]
abhi-glitchhg 4b123e4
[WIP]
abhi-glitchhg 52cd5b9
[WIP]
abhi-glitchhg 346036e
edited StanfordCars class
abhi-glitchhg c0c372a
Merge branch 'main' into stanford_cars
abhi-glitchhg acb389b
Merge remote-tracking branch 'origin/stanford_cars' into stanford_cars
abhi-glitchhg db410b9
Adding Testcase for stanford cars
abhi-glitchhg 0e91bf0
Added Testcase for stanford cars
abhi-glitchhg fbd3122
Added Testcase for stanford cars
abhi-glitchhg 817e9f2
minor edit
abhi-glitchhg 52c98f3
made changes as per the suggestions
abhi-glitchhg 2eceade
fixed typo in naming stanford_cars.py
abhi-glitchhg af25d72
cars_meta.mat file will be created in test
abhi-glitchhg cbd3d9b
Merge branch 'main' of github.com:pytorch/vision into stanford_cars
NicolasHug fe75e81
Merge branch 'main' into stanford_cars
abhi-glitchhg 8fceb0b
Some cleanups
NicolasHug aa13db9
Merge branch 'stanford_cars' of github.com:abhi-glitchhg/vision into …
NicolasHug dc463d7
Sigh
NicolasHug b593760
don't convert to strings
NicolasHug 121bb55
Merge branch 'main' of github.com:pytorch/vision into stanford_cars
NicolasHug 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,145 @@ | ||||||
import os | ||||||
import os.path | ||||||
from typing import Callable, Optional | ||||||
|
||||||
from PIL import Image | ||||||
|
||||||
from .utils import download_and_extract_archive, download_url | ||||||
from .vision import VisionDataset | ||||||
|
||||||
|
||||||
class StanfordCars(VisionDataset): | ||||||
"""`Stanford Cars <https://ai.stanford.edu/~jkrause/cars/car_dataset.html>`_ Dataset | ||||||
|
||||||
.. warning:: | ||||||
|
||||||
This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format. | ||||||
|
||||||
Args: | ||||||
root (string): Root directory of dataset | ||||||
train (bool, optional):If True, creates dataset from training set, otherwise creates from test set | ||||||
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. | ||||||
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.""" | ||||||
|
||||||
urls = ( | ||||||
"https://ai.stanford.edu/~jkrause/car196/cars_test.tgz", | ||||||
"https://ai.stanford.edu/~jkrause/car196/cars_train.tgz", | ||||||
) # test and train image urls | ||||||
|
||||||
md5s = ( | ||||||
"4ce7ebf6a94d07f1952d94dd34c4d501", | ||||||
"065e5b463ae28d29e77c1b4b166cfe61", | ||||||
) # md5checksum for test and train data | ||||||
|
||||||
annot_urls = ( | ||||||
"https://ai.stanford.edu/~jkrause/car196/cars_test_annos_withlabels.mat", | ||||||
"https://ai.stanford.edu/~jkrause/cars/car_devkit.tgz", | ||||||
) # annotations and labels for test and train | ||||||
|
||||||
annot_md5s = ( | ||||||
"b0a2b23655a3edd16d84508592a98d10", | ||||||
"c3b158d763b6e2245038c8ad08e45376", | ||||||
) # md5 checksum for annotations | ||||||
|
||||||
def __init__( | ||||||
self, | ||||||
root: str, | ||||||
train: bool = True, | ||||||
transform: Optional[Callable] = None, | ||||||
target_transform: Optional[Callable] = None, | ||||||
download: bool = False, | ||||||
) -> None: | ||||||
|
||||||
try: | ||||||
from scipy.io import loadmat | ||||||
|
||||||
self._loadmat = loadmat | ||||||
except ImportError: | ||||||
raise RuntimeError("Scipy is not found. This dataset needs to have scipy installed: pip install scipy") | ||||||
|
||||||
super().__init__(root, transform=transform, target_transform=target_transform) | ||||||
|
||||||
self.train = train | ||||||
|
||||||
if download: | ||||||
self.download() | ||||||
|
||||||
if not self._check_exists(): | ||||||
raise RuntimeError("Dataset not found. You can use download=True to download it") | ||||||
|
||||||
self._samples = self._make_dataset() | ||||||
self.classes = self._get_classes_name() # class_id to class_name mapping | ||||||
|
||||||
def _get_class_names(self) -> dict: | ||||||
abhi-glitchhg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
Returns Mapping of class ids to class names in form of Dictionary | ||||||
""" | ||||||
meta_data = self._loadmat(os.path.join(self.root, "devkit/cars_meta.mat")) | ||||||
class_names = meta_data["class_names"][0] | ||||||
return {class_name[0].replace(" ", "_").replace("/", "_"): i for i, class_name in enumerate(class_names)} | ||||||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
def _make_dataset(self): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
Returns Annotations for training data and testing data | ||||||
""" | ||||||
annotations = None | ||||||
if self.train: | ||||||
annotations = self._loadmat(os.path.join(self.root, "devkit/cars_train_annos.mat")) | ||||||
else: | ||||||
annotations = self._loadmat(os.path.join(self.root, "cars_test_annos_withlabels.mat")) | ||||||
samples = [] | ||||||
annotations = annotations["annotations"][0] | ||||||
for index in range(len(annotations)): | ||||||
target = annotations[index][4][0, 0] | ||||||
image_file = annotations[index][5][0] | ||||||
# Beware: Stanford cars targets starts at 1 | ||||||
target = target - 1 | ||||||
samples.append((image_file, target)) | ||||||
return samples | ||||||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
def __len__(self) -> int: | ||||||
return len(self._samples) | ||||||
|
||||||
def __getitem__(self, idx: int) -> (Image, int): | ||||||
abhi-glitchhg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
"""Returns pil_image and class_id for given index""" | ||||||
image_file, target = self._samples[idx] | ||||||
image_path = os.path.join(self.root, f"cars_{'train' if self.train else 'test'}", image_file) | ||||||
pil_image = Image.open(image_path).convert("RGB") | ||||||
|
||||||
if self.transform is not None: | ||||||
pil_image = self.transform(pil_image) | ||||||
if self.target_transform is not None: | ||||||
target = self.target_transform(target) | ||||||
return pil_image, target | ||||||
|
||||||
def download(self) -> None: | ||||||
if self._check_exists(): | ||||||
return | ||||||
else: | ||||||
download_and_extract_archive( | ||||||
url=self.urls[self.train], download_root=self.root, extract_root=self.root, md5=self.md5s[self.train] | ||||||
abhi-glitchhg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
) | ||||||
download_and_extract_archive( | ||||||
url=self.annot_urls[1], download_root=self.root, extract_root=self.root, md5=self.annot_md5s[1] | ||||||
) | ||||||
if not self.train: | ||||||
download_url( | ||||||
url=self.annot_urls[0], | ||||||
filename="cars_test_annos_withlabels.mat", | ||||||
abhi-glitchhg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
root=self.root, | ||||||
md5=self.annot_md5s[0], | ||||||
) | ||||||
abhi-glitchhg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
def _check_exists(self) -> bool: | ||||||
return ( | ||||||
os.path.exists(os.path.join(self.root, f"cars_{'train' if self.train else 'test'}")) | ||||||
and os.path.isdir(os.path.join(self.root, f"cars_{'train' if self.train else 'test'}")) | ||||||
and os.path.exists(os.path.join(self.root, "devkit/cars_meta.mat")) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is not created in the test and thus, all tests are failing. |
||||||
if self.train | ||||||
else os.path.exists(os.path.join(self.root, "cars_test_annos_withlabels.mat")) | ||||||
) |
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.