diff --git a/test/builtin_dataset_mocks.py b/test/builtin_dataset_mocks.py index 94305952568..a78ea098718 100644 --- a/test/builtin_dataset_mocks.py +++ b/test/builtin_dataset_mocks.py @@ -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 diff --git a/torchvision/prototype/datasets/_builtin/__init__.py b/torchvision/prototype/datasets/_builtin/__init__.py index 1567ef29811..feb558aa03f 100644 --- a/torchvision/prototype/datasets/_builtin/__init__.py +++ b/torchvision/prototype/datasets/_builtin/__init__.py @@ -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 diff --git a/torchvision/prototype/datasets/_builtin/stanford-cars.categories b/torchvision/prototype/datasets/_builtin/stanford-cars.categories new file mode 100644 index 00000000000..e54040f7779 --- /dev/null +++ b/torchvision/prototype/datasets/_builtin/stanford-cars.categories @@ -0,0 +1,196 @@ +AM General Hummer SUV 2000 +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 diff --git a/torchvision/prototype/datasets/_builtin/stanford_cars.py b/torchvision/prototype/datasets/_builtin/stanford_cars.py new file mode 100644 index 00000000000..d4c4dcfe02b --- /dev/null +++ b/torchvision/prototype/datasets/_builtin/stanford_cars.py @@ -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": + targets_dp = Filter(targets_dp, path_comparator("name", "cars_train_annos.mat")) + targets_dp = StanfordCarsLabelReader(targets_dp) + dp = Zipper(images_dp, targets_dp) + 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"])