Skip to content
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

Implementation of Stanford Cars prototype dataset #5442

Merged
merged 71 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
72ccc5c
basic stanford cars structure added.
abhi-glitchhg Feb 18, 2022
1ce692c
added class in the init.
abhi-glitchhg Feb 18, 2022
20794e2
how to decode the images
abhi-glitchhg Feb 18, 2022
64dca37
Merge branch 'main' into stanfordcars
abhi-glitchhg Feb 18, 2022
e4c3f9d
fix typo in init
abhi-glitchhg Feb 18, 2022
9ae5f23
works partially
abhi-glitchhg Feb 18, 2022
d965622
formatting
abhi-glitchhg Feb 18, 2022
eb2bc93
formatting
abhi-glitchhg Feb 18, 2022
a4041a8
formatting
abhi-glitchhg Feb 18, 2022
c681d39
formatting
abhi-glitchhg Feb 19, 2022
a3953fe
formatting
abhi-glitchhg Feb 19, 2022
6774c24
Merge branch 'main' into stanfordcars
abhi-glitchhg Feb 21, 2022
b9b38e8
Merge branch 'main' into stanfordcars
abhi-glitchhg Feb 25, 2022
ae5a47b
Merge branch 'main' into stanfordcars
abhi-glitchhg Feb 26, 2022
3e47f8e
Merge branch 'main' into stanfordcars
abhi-glitchhg Feb 27, 2022
8bd7348
minor changes
abhi-glitchhg Feb 27, 2022
7ad896e
minor changes
abhi-glitchhg Feb 27, 2022
5922e1b
issue resolved
abhi-glitchhg Mar 3, 2022
bca2a6b
both datapipes are in sync
abhi-glitchhg Mar 3, 2022
5fe8e1c
Merge branch 'main' into stanfordcars
abhi-glitchhg Mar 3, 2022
7d0b908
checksum dict keys changed
abhi-glitchhg Mar 3, 2022
862fc1e
formatting
abhi-glitchhg Mar 3, 2022
c1e9034
Merge branch 'stanfordcars' of https://github.com/abhi-glitchhg/visio…
abhi-glitchhg Mar 3, 2022
8f91280
"ufmt formatting"
abhi-glitchhg Mar 3, 2022
873a6f8
"ufmt formatting"
abhi-glitchhg Mar 3, 2022
4c40f86
formatting
abhi-glitchhg Mar 3, 2022
752bc4b
minor change
abhi-glitchhg Mar 3, 2022
2583c7c
minor change
abhi-glitchhg Mar 3, 2022
197d2e1
test added
abhi-glitchhg Mar 5, 2022
852805a
Merge branch 'main' into stanfordcars
abhi-glitchhg Mar 5, 2022
b0b217d
lint (end of line issue)
abhi-glitchhg Mar 5, 2022
da99ab1
Merge branch 'stanfordcars' of https://github.com/abhi-glitchhg/visio…
abhi-glitchhg Mar 5, 2022
a067e4b
lint (end of line issue)
abhi-glitchhg Mar 5, 2022
e792ed7
minor changes
abhi-glitchhg Mar 5, 2022
8d0eaa5
mock dataset formatting
abhi-glitchhg Mar 5, 2022
4a6f1b1
minor changes
abhi-glitchhg Mar 5, 2022
c4cfad4
hopefully final
abhi-glitchhg Mar 5, 2022
89fef01
formatting
abhi-glitchhg Mar 5, 2022
f3bfc99
prototype test
abhi-glitchhg Mar 5, 2022
a9ecaf9
prototype test
abhi-glitchhg Mar 5, 2022
3b98146
prototype test
abhi-glitchhg Mar 5, 2022
e39a840
prototype test
abhi-glitchhg Mar 5, 2022
e318c50
prototype test
abhi-glitchhg Mar 5, 2022
729ec17
prototype test
abhi-glitchhg Mar 5, 2022
7c09090
Merge branch 'main' into stanfordcars
abhi-glitchhg Mar 5, 2022
562973f
prototype test
abhi-glitchhg Mar 5, 2022
b41d075
Merge branch 'stanfordcars' of https://github.com/abhi-glitchhg/visio…
abhi-glitchhg Mar 5, 2022
bbe9fb1
prototype test
abhi-glitchhg Mar 5, 2022
47d8403
Update builtin_dataset_mocks.py
abhi-glitchhg Mar 5, 2022
2136ec5
Update stanford_cars.py
abhi-glitchhg Mar 6, 2022
be01e62
Update torchvision/prototype/datasets/_builtin/stanford_cars.py
abhi-glitchhg Mar 7, 2022
038c574
Update torchvision/prototype/datasets/_builtin/stanford_cars.py
abhi-glitchhg Mar 7, 2022
54246bb
Update torchvision/prototype/datasets/_builtin/stanford_cars.py
abhi-glitchhg Mar 7, 2022
e5ffb52
Update torchvision/prototype/datasets/_builtin/stanford_cars.py
abhi-glitchhg Mar 7, 2022
9ea352e
formatting and cleaning
abhi-glitchhg Mar 7, 2022
369c021
formatting and cleaning
abhi-glitchhg Mar 7, 2022
3a57e8b
Update torchvision/prototype/datasets/_builtin/stanford_cars.py
abhi-glitchhg Mar 7, 2022
d04e9d4
Merge branch 'main' into stanfordcars
abhi-glitchhg Mar 8, 2022
ce73276
generate catefories script
abhi-glitchhg Mar 8, 2022
44d91b1
Merge branch 'stanfordcars' of https://github.com/abhi-glitchhg/visio…
abhi-glitchhg Mar 8, 2022
6248e03
generate categories script added
abhi-glitchhg Mar 8, 2022
05f81a6
sigh
abhi-glitchhg Mar 8, 2022
f4938bb
sigh
abhi-glitchhg Mar 8, 2022
a8d88d1
sigh
abhi-glitchhg Mar 8, 2022
7e83969
minor changes
abhi-glitchhg Mar 9, 2022
0302eb6
linting
abhi-glitchhg Mar 9, 2022
0c409dc
Merge branch 'main' into stanfordcars
abhi-glitchhg Mar 9, 2022
d9f3af7
Merge branch 'main' into stanfordcars
abhi-glitchhg Mar 9, 2022
7dc2c71
Merge branch 'main' into stanfordcars
abhi-glitchhg Mar 9, 2022
70035d1
Merge branch 'main' into stanfordcars
NicolasHug Mar 10, 2022
801e9b0
Merge branch 'main' into stanfordcars
pmeier Mar 10, 2022
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
41 changes: 41 additions & 0 deletions test/builtin_dataset_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,3 +1390,44 @@ def pcam(info, root, config):
compressed_file.write(compressed_data)

return num_images


@register_mock
def stanford_cars(info, root, config):
import scipy.io as io
from numpy.core.records import fromarrays

num_samples = {"train": 5, "test": 7}[config["split"]]
num_categories = 3

devkit = root / "devkit"
devkit.mkdir(parents=True)

if config["split"] == "train":
images_folder_name = "cars_train"
annotations_mat_path = devkit / "cars_train_annos.mat"
else:
images_folder_name = "cars_test"
annotations_mat_path = root / "cars_test_annos_withlabels.mat"

create_image_folder(
root=root,
name=images_folder_name,
file_name_fn=lambda image_index: f"{image_index:5d}.jpg",
num_examples=num_samples,
)

make_tar(root, f"cars_{config.split}.tgz", images_folder_name)
bbox = np.random.randint(1, 200, num_samples, dtype=np.uint8)
classes = np.random.randint(1, num_categories + 1, num_samples, dtype=np.uint8)
fnames = [f"{i:5d}.jpg" for i in range(num_samples)]
rec_array = fromarrays(
[bbox, bbox, bbox, bbox, classes, fnames],
names=["bbox_x1", "bbox_y1", "bbox_x2", "bbox_y2", "class", "fname"],
)

io.savemat(annotations_mat_path, {"annotations": rec_array})
if config.split == "train":
make_tar(root, "car_devkit.tgz", devkit, compression="gz")

return num_samples
1 change: 1 addition & 0 deletions torchvision/prototype/datasets/_builtin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
from .pcam import PCAM
from .sbd import SBD
from .semeion import SEMEION
from .stanford_cars import StanfordCars
from .svhn import SVHN
from .voc import VOC
196 changes: 196 additions & 0 deletions torchvision/prototype/datasets/_builtin/stanford-cars.categories
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
AM General Hummer SUV 2000
abhi-glitchhg marked this conversation as resolved.
Show resolved Hide resolved
Acura RL Sedan 2012
Acura TL Sedan 2012
Acura TL Type-S 2008
Acura TSX Sedan 2012
Acura Integra Type R 2001
Acura ZDX Hatchback 2012
Aston Martin V8 Vantage Convertible 2012
Aston Martin V8 Vantage Coupe 2012
Aston Martin Virage Convertible 2012
Aston Martin Virage Coupe 2012
Audi RS 4 Convertible 2008
Audi A5 Coupe 2012
Audi TTS Coupe 2012
Audi R8 Coupe 2012
Audi V8 Sedan 1994
Audi 100 Sedan 1994
Audi 100 Wagon 1994
Audi TT Hatchback 2011
Audi S6 Sedan 2011
Audi S5 Convertible 2012
Audi S5 Coupe 2012
Audi S4 Sedan 2012
Audi S4 Sedan 2007
Audi TT RS Coupe 2012
BMW ActiveHybrid 5 Sedan 2012
BMW 1 Series Convertible 2012
BMW 1 Series Coupe 2012
BMW 3 Series Sedan 2012
BMW 3 Series Wagon 2012
BMW 6 Series Convertible 2007
BMW X5 SUV 2007
BMW X6 SUV 2012
BMW M3 Coupe 2012
BMW M5 Sedan 2010
BMW M6 Convertible 2010
BMW X3 SUV 2012
BMW Z4 Convertible 2012
Bentley Continental Supersports Conv. Convertible 2012
Bentley Arnage Sedan 2009
Bentley Mulsanne Sedan 2011
Bentley Continental GT Coupe 2012
Bentley Continental GT Coupe 2007
Bentley Continental Flying Spur Sedan 2007
Bugatti Veyron 16.4 Convertible 2009
Bugatti Veyron 16.4 Coupe 2009
Buick Regal GS 2012
Buick Rainier SUV 2007
Buick Verano Sedan 2012
Buick Enclave SUV 2012
Cadillac CTS-V Sedan 2012
Cadillac SRX SUV 2012
Cadillac Escalade EXT Crew Cab 2007
Chevrolet Silverado 1500 Hybrid Crew Cab 2012
Chevrolet Corvette Convertible 2012
Chevrolet Corvette ZR1 2012
Chevrolet Corvette Ron Fellows Edition Z06 2007
Chevrolet Traverse SUV 2012
Chevrolet Camaro Convertible 2012
Chevrolet HHR SS 2010
Chevrolet Impala Sedan 2007
Chevrolet Tahoe Hybrid SUV 2012
Chevrolet Sonic Sedan 2012
Chevrolet Express Cargo Van 2007
Chevrolet Avalanche Crew Cab 2012
Chevrolet Cobalt SS 2010
Chevrolet Malibu Hybrid Sedan 2010
Chevrolet TrailBlazer SS 2009
Chevrolet Silverado 2500HD Regular Cab 2012
Chevrolet Silverado 1500 Classic Extended Cab 2007
Chevrolet Express Van 2007
Chevrolet Monte Carlo Coupe 2007
Chevrolet Malibu Sedan 2007
Chevrolet Silverado 1500 Extended Cab 2012
Chevrolet Silverado 1500 Regular Cab 2012
Chrysler Aspen SUV 2009
Chrysler Sebring Convertible 2010
Chrysler Town and Country Minivan 2012
Chrysler 300 SRT-8 2010
Chrysler Crossfire Convertible 2008
Chrysler PT Cruiser Convertible 2008
Daewoo Nubira Wagon 2002
Dodge Caliber Wagon 2012
Dodge Caliber Wagon 2007
Dodge Caravan Minivan 1997
Dodge Ram Pickup 3500 Crew Cab 2010
Dodge Ram Pickup 3500 Quad Cab 2009
Dodge Sprinter Cargo Van 2009
Dodge Journey SUV 2012
Dodge Dakota Crew Cab 2010
Dodge Dakota Club Cab 2007
Dodge Magnum Wagon 2008
Dodge Challenger SRT8 2011
Dodge Durango SUV 2012
Dodge Durango SUV 2007
Dodge Charger Sedan 2012
Dodge Charger SRT-8 2009
Eagle Talon Hatchback 1998
FIAT 500 Abarth 2012
FIAT 500 Convertible 2012
Ferrari FF Coupe 2012
Ferrari California Convertible 2012
Ferrari 458 Italia Convertible 2012
Ferrari 458 Italia Coupe 2012
Fisker Karma Sedan 2012
Ford F-450 Super Duty Crew Cab 2012
Ford Mustang Convertible 2007
Ford Freestar Minivan 2007
Ford Expedition EL SUV 2009
Ford Edge SUV 2012
Ford Ranger SuperCab 2011
Ford GT Coupe 2006
Ford F-150 Regular Cab 2012
Ford F-150 Regular Cab 2007
Ford Focus Sedan 2007
Ford E-Series Wagon Van 2012
Ford Fiesta Sedan 2012
GMC Terrain SUV 2012
GMC Savana Van 2012
GMC Yukon Hybrid SUV 2012
GMC Acadia SUV 2012
GMC Canyon Extended Cab 2012
Geo Metro Convertible 1993
HUMMER H3T Crew Cab 2010
HUMMER H2 SUT Crew Cab 2009
Honda Odyssey Minivan 2012
Honda Odyssey Minivan 2007
Honda Accord Coupe 2012
Honda Accord Sedan 2012
Hyundai Veloster Hatchback 2012
Hyundai Santa Fe SUV 2012
Hyundai Tucson SUV 2012
Hyundai Veracruz SUV 2012
Hyundai Sonata Hybrid Sedan 2012
Hyundai Elantra Sedan 2007
Hyundai Accent Sedan 2012
Hyundai Genesis Sedan 2012
Hyundai Sonata Sedan 2012
Hyundai Elantra Touring Hatchback 2012
Hyundai Azera Sedan 2012
Infiniti G Coupe IPL 2012
Infiniti QX56 SUV 2011
Isuzu Ascender SUV 2008
Jaguar XK XKR 2012
Jeep Patriot SUV 2012
Jeep Wrangler SUV 2012
Jeep Liberty SUV 2012
Jeep Grand Cherokee SUV 2012
Jeep Compass SUV 2012
Lamborghini Reventon Coupe 2008
Lamborghini Aventador Coupe 2012
Lamborghini Gallardo LP 570-4 Superleggera 2012
Lamborghini Diablo Coupe 2001
Land Rover Range Rover SUV 2012
Land Rover LR2 SUV 2012
Lincoln Town Car Sedan 2011
MINI Cooper Roadster Convertible 2012
Maybach Landaulet Convertible 2012
Mazda Tribute SUV 2011
McLaren MP4-12C Coupe 2012
Mercedes-Benz 300-Class Convertible 1993
Mercedes-Benz C-Class Sedan 2012
Mercedes-Benz SL-Class Coupe 2009
Mercedes-Benz E-Class Sedan 2012
Mercedes-Benz S-Class Sedan 2012
Mercedes-Benz Sprinter Van 2012
Mitsubishi Lancer Sedan 2012
Nissan Leaf Hatchback 2012
Nissan NV Passenger Van 2012
Nissan Juke Hatchback 2012
Nissan 240SX Coupe 1998
Plymouth Neon Coupe 1999
Porsche Panamera Sedan 2012
Ram C/V Cargo Van Minivan 2012
Rolls-Royce Phantom Drophead Coupe Convertible 2012
Rolls-Royce Ghost Sedan 2012
Rolls-Royce Phantom Sedan 2012
Scion xD Hatchback 2012
Spyker C8 Convertible 2009
Spyker C8 Coupe 2009
Suzuki Aerio Sedan 2007
Suzuki Kizashi Sedan 2012
Suzuki SX4 Hatchback 2012
Suzuki SX4 Sedan 2012
Tesla Model S Sedan 2012
Toyota Sequoia SUV 2012
Toyota Camry Sedan 2012
Toyota Corolla Sedan 2012
Toyota 4Runner SUV 2012
Volkswagen Golf Hatchback 2012
Volkswagen Golf Hatchback 1991
Volkswagen Beetle Hatchback 2012
Volvo C30 Hatchback 2012
Volvo 240 Sedan 1993
Volvo XC90 SUV 2007
smart fortwo Convertible 2012
96 changes: 96 additions & 0 deletions torchvision/prototype/datasets/_builtin/stanford_cars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import pathlib
from typing import Any, Dict, List, Tuple, Iterator, BinaryIO

from torchdata.datapipes.iter import Filter, IterDataPipe, Mapper, Zipper
from torchvision.prototype.datasets.utils import Dataset, DatasetConfig, DatasetInfo, HttpResource, OnlineResource
from torchvision.prototype.datasets.utils._internal import hint_sharding, hint_shuffling, path_comparator, read_mat
from torchvision.prototype.features import BoundingBox, EncodedImage, Label


class StanfordCarsLabelReader(IterDataPipe[Tuple[int, int, int, int, int, str]]):
def __init__(self, datapipe: IterDataPipe[Dict[str, Any]]) -> None:
self.datapipe = datapipe

def __iter__(self) -> Iterator[Tuple[int, int, int, int, int, str]]:
for _, file in self.datapipe:
data = read_mat(file, squeeze_me=True)
for ann in data["annotations"]:
yield tuple(ann) # type: ignore[misc]


class StanfordCars(Dataset):
def _make_info(self) -> DatasetInfo:
return DatasetInfo(
name="stanford-cars",
homepage="https://ai.stanford.edu/~jkrause/cars/car_dataset.html",
dependencies=("scipy",),
valid_options=dict(
split=("test", "train"),
),
)

_URL_ROOT = "https://ai.stanford.edu/~jkrause/"
_URLS = {
"train": f"{_URL_ROOT}car196/cars_train.tgz",
"test": f"{_URL_ROOT}car196/cars_test.tgz",
"cars_test_annos_withlabels": f"{_URL_ROOT}car196/cars_test_annos_withlabels.mat",
"car_devkit": f"{_URL_ROOT}cars/car_devkit.tgz",
}

_CHECKSUM = {
"train": "b97deb463af7d58b6bfaa18b2a4de9829f0f79e8ce663dfa9261bf7810e9accd",
"test": "bffea656d6f425cba3c91c6d83336e4c5f86c6cffd8975b0f375d3a10da8e243",
"cars_test_annos_withlabels": "790f75be8ea34eeded134cc559332baf23e30e91367e9ddca97d26ed9b895f05",
"car_devkit": "512b227b30e2f0a8aab9e09485786ab4479582073a144998da74d64b801fd288",
}

def resources(self, config: DatasetConfig) -> List[OnlineResource]:
resources: List[OnlineResource] = [HttpResource(self._URLS[config.split], sha256=self._CHECKSUM[config.split])]
if config.split == "train":
resources.append(HttpResource(url=self._URLS["car_devkit"], sha256=self._CHECKSUM["car_devkit"]))

else:
resources.append(
HttpResource(
self._URLS["cars_test_annos_withlabels"], sha256=self._CHECKSUM["cars_test_annos_withlabels"]
)
)
return resources

def _prepare_sample(self, data: Tuple[Tuple[str, BinaryIO], Tuple[int, int, int, int, int, str]]) -> Dict[str, Any]:
image, target = data
path, buffer = image
image = EncodedImage.from_file(buffer)

return dict(
path=path,
image=image,
label=Label(target[4] - 1, categories=self.categories),
bounding_box=BoundingBox(target[:4], format="xyxy", image_size=image.image_size),
)

def _make_datapipe(
self,
resource_dps: List[IterDataPipe],
*,
config: DatasetConfig,
) -> IterDataPipe[Dict[str, Any]]:

images_dp, targets_dp = resource_dps
if config.split == "train":
abhi-glitchhg marked this conversation as resolved.
Show resolved Hide resolved
targets_dp = Filter(targets_dp, path_comparator("name", "cars_train_annos.mat"))
targets_dp = StanfordCarsLabelReader(targets_dp)
dp = Zipper(images_dp, targets_dp)
abhi-glitchhg marked this conversation as resolved.
Show resolved Hide resolved
dp = hint_sharding(dp)
dp = hint_shuffling(dp)
return Mapper(dp, self._prepare_sample)

def _generate_categories(self, root: pathlib.Path) -> List[str]:
config = self.info.make_config(split="train")
resources = self.resources(config)

devkit_dp = resources[1].load(root)
meta_dp = Filter(devkit_dp, path_comparator("name", "cars_meta.mat"))
_, meta_file = next(iter(meta_dp))

return list(read_mat(meta_file, squeeze_me=True)["class_names"])