-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Added KITTI dataset #3640
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
fmassa
merged 14 commits into
pytorch:master
from
prabhat00155:prabhat00155/add_kitti_dataset
Apr 9, 2021
Merged
Added KITTI dataset #3640
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
684e73a
Added KITTI dataset
prabhat00155 57b286d
Merge branch 'master' into prabhat00155/add_kitti_dataset
prabhat00155 9223692
Merge branch 'master' into prabhat00155/add_kitti_dataset
prabhat00155 004357d
Addressed review comments
prabhat00155 82d9750
Merge branch 'master' into prabhat00155/add_kitti_dataset
prabhat00155 4786e7b
Changed type of target to List[Dict] and corrected the data types of …
prabhat00155 eecbf87
Updated unit test to rely on ImageDatasetTestCase
prabhat00155 3f6584f
Merge branch 'master' into prabhat00155/add_kitti_dataset
prabhat00155 8d0219b
Added kitti to dataset documentation
prabhat00155 2920913
Merge branch 'master' into prabhat00155/add_kitti_dataset
prabhat00155 9c083f6
Cleaned up test and some minor changes
prabhat00155 544c73d
Made data_url a string instead of a list
prabhat00155 93e5237
Removed unnecessary try and print
prabhat00155 cfe77d1
Merge branch 'master' into prabhat00155/add_kitti_dataset
fmassa 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,161 @@ | ||
import csv | ||
import os | ||
from typing import Any, Callable, List, Optional, Tuple | ||
|
||
from PIL import Image | ||
|
||
from .utils import download_and_extract_archive | ||
from .vision import VisionDataset | ||
|
||
|
||
class Kitti(VisionDataset): | ||
"""`KITTI <http://www.cvlibs.net/datasets/kitti>`_ Dataset. | ||
prabhat00155 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Args: | ||
root (string): 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.ToTensor`` | ||
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: str, | ||
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.root = root | ||
self.train = train | ||
self._location = "training" if self.train else "testing" | ||
|
||
if download: | ||
self.download() | ||
if not self._check_exists(): | ||
raise RuntimeError( | ||
"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) | ||
if self.train: | ||
labels_dir = os.path.join(self._raw_folder, self._location, self.labels_dir_name) | ||
for img_file in os.listdir(image_dir): | ||
self.images.append(os.path.join(image_dir, img_file)) | ||
if self.train: | ||
self.targets.append( | ||
os.path.join(labels_dir, f"{img_file.split('.')[0]}.txt") | ||
) | ||
|
||
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) if self.train else None | ||
if self.transforms: | ||
image, target = self.transforms(image, target) | ||
return image, target | ||
|
||
def _parse_target(self, index: int) -> List: | ||
target = [] | ||
with open(self.targets[index]) as inp: | ||
content = csv.reader(inp, delimiter=" ") | ||
for line in content: | ||
target.append({ | ||
"type": line[0], | ||
"truncated": float(line[1]), | ||
"occluded": int(line[2]), | ||
"alpha": float(line[3]), | ||
"bbox": [float(x) for x in line[4:8]], | ||
"dimensions": [float(x) for x in line[8:11]], | ||
"location": [float(x) for x in line[11:14]], | ||
"rotation_y": float(line[14]), | ||
}) | ||
return target | ||
|
||
def __len__(self) -> int: | ||
return len(self.images) | ||
|
||
@property | ||
def _raw_folder(self) -> str: | ||
return os.path.join(self.root, self.__class__.__name__, "raw") | ||
|
||
def _check_exists(self) -> bool: | ||
"""Check if the data directory exists.""" | ||
folders = [self.image_dir_name] | ||
if self.train: | ||
folders.append(self.labels_dir_name) | ||
return all( | ||
os.path.isdir(os.path.join(self._raw_folder, self._location, fname)) | ||
for fname in folders | ||
) | ||
|
||
def download(self) -> None: | ||
prabhat00155 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Download the KITTI data if it doesn't exist already.""" | ||
|
||
if self._check_exists(): | ||
return | ||
|
||
os.makedirs(self._raw_folder, exist_ok=True) | ||
|
||
# download files | ||
for fname in self.resources: | ||
download_and_extract_archive( | ||
url=f"{self.data_url}{fname}", | ||
download_root=self._raw_folder, | ||
filename=fname, | ||
) |
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.