ConsDrive / dataloader /carla_data.py
ZenoNing's picture
Upload ConsDrive v2 source
3e56913 verified
Raw History Blame Contribute Delete
6.57 kB
"""
Code that loads the dataset for training.
partially taken from https://github.com/autonomousvision/carla_garage/blob/main/team_code/data.py
(MIT licence)
"""
import numpy as np
import random
import cv2
from dataloader.dataset_base import BaseDataset
VIZ_DATA = False
class CARLA_Data(BaseDataset): # pylint: disable=locally-disabled, invalid-name
"""
Custom dataset that dynamically loads a CARLA dataset from disk.
"""
def __init__(self,
**cfg,
):
super().__init__(**cfg, base=True)
# processor is set by DataModule after construction for qwen2_5_vl
self._processor = None
def __getitem__(self, index):
"""Returns the item at index idx. """
# Disable threading because the data loader will already split in threads.
cv2.setNumThreads(0)
data = {}
images = self.images[index]
boxes = self.boxes[index]
measurements = self.measurements[index]
sample_start = self.sample_start[index]
augment_exists = self.augment_exists[index]
######################################################
######## load current and future measurements ########
######################################################
loaded_measurements, current_measurement, measurement_file_current = self.load_current_and_future_measurements(
measurements,
sample_start
)
data['measurement_path'] = measurement_file_current
# Determine whether the augmented camera or the normal camera is used.
if augment_exists and random.random() <= self.img_shift_augmentation_prob and self.img_shift_augmentation:
augment_sample = True
aug_rotation = current_measurement['augmentation_rotation']
aug_translation = current_measurement['augmentation_translation']
else:
augment_sample = False
aug_rotation = 0.0
aug_translation = 0.0
data['augment_sample'] = augment_sample
data['aug_rotation'] = aug_rotation
data['aug_translation'] = aug_translation
######################################################
################## load waypoints ####################
######################################################
data = self.load_waypoints(data, loaded_measurements, aug_translation, aug_rotation)
data['speed'] = current_measurement['speed']
data = self.load_route(data, current_measurement, aug_translation, aug_rotation)
target_point = np.array(current_measurement['target_point'])
target_point = self.augment_target_point(target_point, y_augmentation=aug_translation, yaw_augmentation=aug_rotation)
next_target_point = np.array(current_measurement['target_point_next'])
next_target_point = self.augment_target_point(next_target_point, y_augmentation=aug_translation, yaw_augmentation=aug_rotation)
data['target_point'] = target_point
data['next_target_point'] = next_target_point
# Training-only plan consequence labels. Keep ordinary experiments on
# the original data path, and avoid repeated missing-box stat calls
# when the configured dataset export has no boxes directory.
if getattr(self, "use_plan_consequence", False):
data = self.load_plan_consequence_targets(
data,
loaded_measurements,
boxes,
aug_translation=aug_translation,
aug_rotation=aug_rotation,
load_future_boxes=getattr(
self, "consequence_use_future_boxes", False
),
)
data['is_dreamer'] = False
data['safe_to_execute'] = False
data['safe_to_execute_valid'] = False
if getattr(self, "use_object_bank", False):
data = self.load_objects(
data, boxes, aug_translation=aug_translation, aug_rotation=aug_rotation
)
######################################################
######## load current and past images ########
######################################################
data = self.load_images(data, images, augment_sample=augment_sample)
# Run Qwen image/video processor per-sample in worker (parallel) instead of collate_fn (main process)
data = self.run_image_processor(data)
data = self.set_navigational_outputs(data, current_measurement, target_point, next_target_point)
data['prompt'] = ""
######################################################
###### V-JEPA2 future latents for the world loss #####
######################################################
if getattr(self, "use_jepa_cache", False):
from dataloader.future_cache import cache_identity
key, frame_idx = cache_identity(measurement_file_current)
state, valid = self._get_future_cache().get(key, frame_idx)
data['future_states'] = state # [4, 64, 1024] fp16
# shift-augmented views do not match the cached (non-augmented)
# camera stream, so they are excluded from the world loss
data['wm_valid'] = bool(valid) and not augment_sample
return data
def _get_future_cache(self):
"""Lazy per-process cache handle (dataloader workers fork after init)."""
cache = getattr(self, "_jepa_cache", None)
if cache is None:
from dataloader.future_cache import FutureStateCache
cache = FutureStateCache(
self.jepa_cache_root,
require=True,
allow_missing=True,
)
self._jepa_cache = cache
return cache
if __name__ == "__main__":
from hydra import compose, initialize
initialize(config_path="../config")
cfg = compose(config_name="config")
print('Test Dataset')
dataset = CARLA_Data(
batch_size=cfg.batch_size,
num_workers=cfg.num_workers,
data_path=cfg.dataset.data_path,
bucket_path=cfg.dataset.bucket_path,
hist_len=cfg.dataset.hist_len,
pred_len=cfg.dataset.pred_len,
skip_first_n_frames=cfg.dataset.skip_first_n_frames,
num_route_points=cfg.dataset.num_route_points,
split="train",
bucket_name="all",
)
for i in range(len(dataset)):
data = dataset[i]
print(data)
if i == 10:
break