[docs]classStanfordCars(VisionDataset):"""Stanford Cars Dataset The Cars dataset contains 16,185 images of 196 classes of cars. The data is split into 8,144 training images and 8,041 testing images, where each class has been split roughly in a 50-50 split The original URL is https://ai.stanford.edu/~jkrause/cars/car_dataset.html, but it is broken. .. note:: This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format. Args: root (str or ``pathlib.Path``): Root directory of dataset split (string, optional): The dataset split, supports ``"train"`` (default) or ``"test"``. 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): This parameter exists for backward compatibility but it does not download the dataset, since the original URL is not available anymore. The dataset seems to be available on Kaggle so you can try to manually download it using `these instructions <https://github.com/pytorch/vision/issues/7545#issuecomment-1631441616>`_. """def__init__(self,root:Union[str,pathlib.Path],split:str="train",transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,download:bool=False,)->None:try:importscipy.ioassioexceptImportError:raiseRuntimeError("Scipy is not found. This dataset needs to have scipy installed: pip install scipy")super().__init__(root,transform=transform,target_transform=target_transform)self._split=verify_str_arg(split,"split",("train","test"))self._base_folder=pathlib.Path(root)/"stanford_cars"devkit=self._base_folder/"devkit"ifself._split=="train":self._annotations_mat_path=devkit/"cars_train_annos.mat"self._images_base_path=self._base_folder/"cars_train"else:self._annotations_mat_path=self._base_folder/"cars_test_annos_withlabels.mat"self._images_base_path=self._base_folder/"cars_test"ifdownload:self.download()ifnotself._check_exists():raiseRuntimeError("Dataset not found. Try to manually download following the instructions in ""https://github.com/pytorch/vision/issues/7545#issuecomment-1631441616.")self._samples=[(str(self._images_base_path/annotation["fname"]),annotation["class"]-1,# Original target mapping starts from 1, hence -1)forannotationinsio.loadmat(self._annotations_mat_path,squeeze_me=True)["annotations"]]self.classes=sio.loadmat(str(devkit/"cars_meta.mat"),squeeze_me=True)["class_names"].tolist()self.class_to_idx={cls:ifori,clsinenumerate(self.classes)}def__len__(self)->int:returnlen(self._samples)
[docs]def__getitem__(self,idx:int)->Tuple[Any,Any]:"""Returns pil_image and class_id for given index"""image_path,target=self._samples[idx]pil_image=Image.open(image_path).convert("RGB")ifself.transformisnotNone:pil_image=self.transform(pil_image)ifself.target_transformisnotNone:target=self.target_transform(target)returnpil_image,target
def_check_exists(self)->bool:ifnot(self._base_folder/"devkit").is_dir():returnFalsereturnself._annotations_mat_path.exists()andself._images_base_path.is_dir()defdownload(self):raiseValueError("The original URL is broken so the StanfordCars dataset is not available for automatic ""download anymore. You can try to download it manually following ""https://github.com/pytorch/vision/issues/7545#issuecomment-1631441616, ""and set download=False to avoid this error.")
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.