File size: 6,570 Bytes
3e56913
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
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