[docs]classKitti(VisionDataset):"""`KITTI <http://www.cvlibs.net/datasets/kitti/eval_object.php?obj_benchmark>`_ Dataset. It corresponds to the "left color images of object" dataset, for object detection. Args: root (str or ``pathlib.Path``): Root directory where images are downloaded to. Expects the following folder structure if download=False: .. code:: <root> └── Kitti └─ raw ├── training | ├── image_2 | └── label_2 └── testing └── image_2 train (bool, optional): Use ``train`` split if true, else ``test`` split. Defaults to ``train``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.PILToTensor`` target_transform (callable, optional): A function/transform that takes in the target and transforms it. transforms (callable, optional): A function/transform that takes input sample and its target as entry and returns a transformed version. 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. """data_url="https://s3.eu-central-1.amazonaws.com/avg-kitti/"resources=["data_object_image_2.zip","data_object_label_2.zip",]image_dir_name="image_2"labels_dir_name="label_2"def__init__(self,root:Union[str,Path],train:bool=True,transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,transforms:Optional[Callable]=None,download:bool=False,):super().__init__(root,transform=transform,target_transform=target_transform,transforms=transforms,)self.images=[]self.targets=[]self.train=trainself._location="training"ifself.trainelse"testing"ifdownload:self.download()ifnotself._check_exists():raiseRuntimeError("Dataset not found. You may use download=True to download it.")image_dir=os.path.join(self._raw_folder,self._location,self.image_dir_name)ifself.train:labels_dir=os.path.join(self._raw_folder,self._location,self.labels_dir_name)forimg_fileinos.listdir(image_dir):self.images.append(os.path.join(image_dir,img_file))ifself.train:self.targets.append(os.path.join(labels_dir,f"{img_file.split('.')[0]}.txt"))
[docs]def__getitem__(self,index:int)->Tuple[Any,Any]:"""Get item at a given index. Args: index (int): Index Returns: tuple: (image, target), where target is a list of dictionaries with the following keys: - type: str - truncated: float - occluded: int - alpha: float - bbox: float[4] - dimensions: float[3] - locations: float[3] - rotation_y: float """image=Image.open(self.images[index])target=self._parse_target(index)ifself.trainelseNoneifself.transforms:image,target=self.transforms(image,target)returnimage,target
def_parse_target(self,index:int)->List:target=[]withopen(self.targets[index])asinp:content=csv.reader(inp,delimiter=" ")forlineincontent:target.append({"type":line[0],"truncated":float(line[1]),"occluded":int(line[2]),"alpha":float(line[3]),"bbox":[float(x)forxinline[4:8]],"dimensions":[float(x)forxinline[8:11]],"location":[float(x)forxinline[11:14]],"rotation_y":float(line[14]),})returntargetdef__len__(self)->int:returnlen(self.images)@propertydef_raw_folder(self)->str:returnos.path.join(self.root,self.__class__.__name__,"raw")def_check_exists(self)->bool:"""Check if the data directory exists."""folders=[self.image_dir_name]ifself.train:folders.append(self.labels_dir_name)returnall(os.path.isdir(os.path.join(self._raw_folder,self._location,fname))forfnameinfolders)defdownload(self)->None:"""Download the KITTI data if it doesn't exist already."""ifself._check_exists():returnos.makedirs(self._raw_folder,exist_ok=True)# download filesforfnameinself.resources:download_and_extract_archive(url=f"{self.data_url}{fname}",download_root=self._raw_folder,filename=fname,)
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.