kitti
KITTI 3D object detection dataset with calibration loading and coordinate-frame conversion helpers.

Classes:
-
KittiCalibβKITTI camera/LiDAR calibration
-
KITTIβKITTI 3D object-detection dataset (LiDAR points + raw LiDAR-frame ground-truth boxes).
Functions:
-
load_kitti_calibβParse a KITTI
calib/{id}.txtinto its projection / rectification / LiDAR-to-camera matrices. -
lidar_to_rectβTransform LiDAR points into the rectified camera frame.
-
rect_to_imgβProject rectified-camera points onto the
P2image plane. -
rect_to_lidarβTransform rectified-camera points back into the LiDAR frame (inverse of
lidar_to_rect). -
fov_flagβBoolean mask of LiDAR points that project into the front-camera image.
-
load_kitti_boxesβParse
label_2/{id}.txtinto raw LiDAR 7-DoF boxes and their per-box attributes.
KITTI [source] ΒΆ
KITTI(
root: PathLike,
*,
train: bool = True,
split_file: Optional[PathLike] = None,
fov: bool = True,
return_calib: bool = False,
transform: Optional[
Callable[[Dict[str, Any]], Dict[str, Any]]
] = None,
download: bool = False,
force_download: bool = False,
force_process: bool = False,
show_progress: bool = True,
num_workers: Optional[int] = None,
)
Bases: PointCloudDataset
KITTI 3D object-detection dataset (LiDAR points + raw LiDAR-frame ground-truth boxes).
The raw object split is read from <root>/KITTI/raw/<split>/ and processed once into a per-frame .npy cache (<root>/KITTI/processed_fov/<split>/ when fov=True, else <root>/KITTI/processed/), so each __getitem__ is a flat array read. The whole split is processed; split_file only selects which cached frames to load. Frames are loaded lazily from the cache, so a full split does not need to fit in host memory.
KITTI requires manual download (a license must be accepted), so download=True raises with the download URL rather than fetching anything.
Parameters:
-
root(PathLike) βDataset root; raw data is read from
<root>/KITTI/raw/<split>/. -
train(bool, default:True) βIf
True, reads thetrainingsplit directory, otherwisetesting. -
split_file(Optional[PathLike], default:None) βOptional text file of frame ids (one per line) selecting which cached frames to load; defaults to every processed frame.
-
fov(bool, default:True) βRestrict points to the front-camera field of view (requires
image_2/). Baked into the cache. -
return_calib(bool, default:False) βAlso emit the
calib(the composed \((3, 4)\) LiDAR-to-image matrix \(P_2 [R_0 T_\text{velo}]\) with the perspective-divide row from \(R_0 T_\text{velo}\), theprojected_ignore_maskcontract) andimage_shape(image(height, width)) keys, read lazily at access time from the rawcalib/txt and theimage_2/PNG header (both must be present in the raw split). -
transform(Optional[Callable[[Dict[str, Any]], Dict[str, Any]]], default:None) βCallable applied to each sample dict (e.g.
RelabelBoxes+ the model's transform). -
download(bool, default:False) βUnsupported; raises a
RuntimeErrorpointing at the manual download page when set. -
force_download(bool, default:False) βUnsupported; raises like
download. -
force_process(bool, default:False) βReprocess the raw split even if a cache already exists.
-
show_progress(bool, default:True) βShow a progress bar while processing.
-
num_workers(Optional[int], default:None) βWorker processes for processing, or
Nonefor sequential processing.
Example
Assuming the raw split is extracted under data/KITTI/raw/training/:
Methods:
-
downloadβKITTI must be downloaded manually after accepting its license.
-
processβConvert every raw frame in the split into its
.npycache directory. -
process_frameβRead one raw frame (optionally FOV-filtered) and write its
.npycache. -
loadβEnumerate the cached frames to load, honoring
split_filewhen given.
Attributes:
-
processed_dir(str) βPath to the processed cache directory, suffixed
_fovwhen the clouds are cropped to the camera view. -
raw_split_dir(Path) βPath to the split's raw directory.
-
processed_split_dir(Path) βPath to the split's processed cache directory.
-
frame_ids(List[str]) βFrame ids of the loaded samples, in dataset order.
-
name(str) βName of the dataset directory.
-
data_dir(str) βPath to the dataset directory
<root>/<name>. -
raw_dir(str) βPath to the raw download directory.
processed_dir property ΒΆ
Path to the processed cache directory, suffixed _fov when the clouds are cropped to the camera view.
processed_split_dir property ΒΆ
Path to the split's processed cache directory.
download [source] ΒΆ
KITTI must be downloaded manually after accepting its license.
Parameters:
-
force(bool, default:False) βUnused; present to mirror the other datasets'
downloadsignature.
Raises: RuntimeError: Always; automatic download is not supported.
process [source] ΒΆ
process(
force: bool = False,
num_workers: Optional[int] = None,
show_progress: bool = True,
) -> None
Convert every raw frame in the split into its .npy cache directory.
Parameters:
-
force(bool, default:False) βReprocess even if a cache already exists.
-
num_workers(Optional[int], default:None) βWorker processes, or
Nonefor sequential processing. -
show_progress(bool, default:True) βShow a progress bar while processing.
load_kitti_calib [source] ΒΆ
load_kitti_calib(calib_file: PathLike) -> KittiCalib
Parse a KITTI calib/{id}.txt into its projection / rectification / LiDAR-to-camera matrices.
Only the three matrices used for box conversion and the FOV filter are kept: the left-color camera projection P2 \((3, 4)\), the rectifying rotation R0_rect \((3, 3)\), and the LiDAR-to-camera transform Tr_velo_to_cam \((3, 4)\).
Parameters:
-
calib_file(PathLike) βPath to a KITTI calibration text file.
Returns:
-
KittiCalibβA
KittiCalibdict holding theP2,R0_rect, andTr_velo_to_camarrays.
lidar_to_rect [source] ΒΆ
lidar_to_rect(
points: ndarray, calib: KittiCalib
) -> ndarray
Transform LiDAR points into the rectified camera frame.
Parameters:
-
points(ndarray) βLiDAR XYZ coordinates.
-
calib(KittiCalib) βCalibration matrices from
load_kitti_calib.
Returns:
-
ndarrayβThe points expressed in the rectified camera frame.
Shape
- Input
points: \((N, 3)\). - Output: \((N, 3)\).
Example
import numpy as np
from torch_pointcloud.datasets.kitti import lidar_to_rect, load_kitti_calib
calib = load_kitti_calib("data/KITTI/raw/training/calib/000000.txt")
points = np.fromfile("data/KITTI/raw/training/velodyne/000000.bin", dtype=np.float32).reshape(-1, 4)[:, :3]
rect = lidar_to_rect(points, calib)
rect_to_img [source] ΒΆ
rect_to_img(
points: ndarray, calib: KittiCalib
) -> Tuple[ndarray, ndarray]
Project rectified-camera points onto the P2 image plane.
Parameters:
-
points(ndarray) βPoints in the rectified camera frame.
-
calib(KittiCalib) βCalibration matrices from
load_kitti_calib.
Returns:
-
Tuple[ndarray, ndarray]βA pair
(pixels, depth)of the projected image coordinates and the per-point camera depth.
Shape
- Input
points: \((N, 3)\). - Output
pixels: \((N, 2)\),depth: \((N,)\).
Example
import numpy as np
from torch_pointcloud.datasets.kitti import lidar_to_rect, load_kitti_calib, rect_to_img
calib = load_kitti_calib("data/KITTI/raw/training/calib/000000.txt")
points = np.fromfile("data/KITTI/raw/training/velodyne/000000.bin", dtype=np.float32).reshape(-1, 4)[:, :3]
pixels, depth = rect_to_img(lidar_to_rect(points, calib), calib)
rect_to_lidar [source] ΒΆ
rect_to_lidar(
points: ndarray, calib: KittiCalib
) -> ndarray
Transform rectified-camera points back into the LiDAR frame (inverse of lidar_to_rect).
Parameters:
-
points(ndarray) βPoints in the rectified camera frame (e.g. label box centers).
-
calib(KittiCalib) βCalibration matrices from
load_kitti_calib.
Returns:
-
ndarrayβThe points expressed in the LiDAR frame.
Shape
- Input
points: \((N, 3)\). - Output: \((N, 3)\).
Example
import numpy as np
from torch_pointcloud.datasets.kitti import lidar_to_rect, load_kitti_calib, rect_to_lidar
calib = load_kitti_calib("data/KITTI/raw/training/calib/000000.txt")
points = np.fromfile("data/KITTI/raw/training/velodyne/000000.bin", dtype=np.float32).reshape(-1, 4)[:, :3]
lidar = rect_to_lidar(lidar_to_rect(points, calib), calib) # back to `points`
fov_flag [source] ΒΆ
fov_flag(
points: ndarray,
image_shape: Tuple[int, int],
calib: KittiCalib,
) -> ndarray
Boolean mask of LiDAR points that project into the front-camera image.
A point is kept when its P2 projection falls inside the image bounds and it lies in front of the camera (positive depth).
Parameters:
-
points(ndarray) βLiDAR points; only the first three columns (XYZ) are used.
-
image_shape(Tuple[int, int]) βFront-camera image
(height, width)in pixels. -
calib(KittiCalib) βCalibration matrices from
load_kitti_calib.
Returns:
-
ndarrayβA boolean mask selecting the in-FOV points.
Shape
- Input
points: \((N, C)\) with \(C \ge 3\). - Output: \((N,)\).
Example
import numpy as np
from torch_pointcloud.datasets.kitti import fov_flag, load_kitti_calib
calib = load_kitti_calib("data/KITTI/raw/training/calib/000000.txt")
points = np.fromfile("data/KITTI/raw/training/velodyne/000000.bin", dtype=np.float32).reshape(-1, 4)[:, :3]
points = points[fov_flag(points, (375, 1242), calib)]
load_kitti_boxes [source] ΒΆ
load_kitti_boxes(
label_file: PathLike, calib: KittiCalib
) -> Dict[str, ndarray]
Parse label_2/{id}.txt into raw LiDAR 7-DoF boxes and their per-box attributes.
Each non-DontCare row is converted from its camera-frame (h, w, l, x, y, z, ry) annotation to a LiDAR box \((cx, cy, cz, dx, dy, dz, \text{heading})\) via rect_to_lidar; classes without a 3D box are skipped. The 2D box height is bbox_bottom - bbox_top in pixels.
Parameters:
-
label_file(PathLike) βPath to a KITTI
label_2file. A missing file yields empty arrays (e.g. test split). -
calib(KittiCalib) βCalibration matrices from
load_kitti_calib, used to convert box centers to the LiDAR frame.
Returns:
-
Dict[str, ndarray]βA dict of arrays keyed by cache file name:
box\((K, 7)\),label\((K,)\),truncation\((K,)\), -
Dict[str, ndarray]βocclusion\((K,)\), andbbox_height\((K,)\).