-
Notifications
You must be signed in to change notification settings - Fork 5
/
datamanager.py
176 lines (154 loc) · 7.07 KB
/
datamanager.py
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
171
172
173
174
175
176
from nerfstudio.data.datamanagers.base_datamanager import *
from NFLStudio.dataparser import NFLDataParser
from nerfstudio.data.utils.dataloaders import CacheDataloader
from torch.utils.data import DataLoader, Dataset
from NFLStudio import raymarching
# from nerfstudio
from NFLStudio.dataparser import (
NFLDataParserConfig
)
import torch
@dataclass
class NFLDataManagerConfig(DataManagerConfig):
"""A basic data manager"""
_target: Type = field(default_factory=lambda: NFLDataManager)
# """Target class to instantiate."""
dataparser_config: NFLDataParserConfig = NFLDataParserConfig()
# """Specifies the dataparser used to unpack the data."""
train_num_rays_per_batch: int = 4096
# """Number of rays per batch to use per training iteration."""
shuffle: bool = True
num_workers: int = 8
eval_num_rays_per_batch: int = 4096
"""Number of rays per batch to use per eval iteration."""
class NFLDataManager(DataManager):
config: NFLDataManagerConfig
train_dataset: Dataset
eval_dataset: Dataset
train_dataparser_outputs: DataparserOutputs
train_pixel_sampler: Optional[PixelSampler] = None
eval_pixel_sampler: Optional[PixelSampler] = None
def __init__(
self,
config: NFLDataManagerConfig,
device: Union[torch.device, str] = "cpu",
):
super().__init__()
torch.manual_seed(49)
self.dataparser = NFLDataParser(config.dataparser_config)
self.train_dataparser_outputs = self.dataparser.get_dataparser_outputs(split='train')
self.eval_dataparser_outputs = self.dataparser.get_dataparser_outputs(split='val')
self.aabb = self.train_dataparser_outputs.scene_box.aabb.flatten()
self.train_dataset = self.train_dataparser_outputs.custom_dataset
self.train_loader = DataLoader(self.train_dataset,
batch_size=config.train_num_rays_per_batch,
shuffle=config.shuffle,
num_workers=config.num_workers)
self.iter_train_loader = iter(self.train_loader)
self.eval_dataset = self.eval_dataparser_outputs.custom_dataset
self.eval_loader = DataLoader(self.eval_dataset,
batch_size=config.eval_num_rays_per_batch,
shuffle=True,
num_workers=config.num_workers)
self.iter_eval_loader = iter(self.eval_loader)
self.config = config
self.device = device
self.eval_count=0
self.train_count=0
self.train_epoch = 1
self.eval_epoch = 1
self.test_dataparser_outputs =self.dataparser.get_dataparser_outputs(split='test')
self.test_dataset = self.test_dataparser_outputs.custom_dataset
self.test_loader = DataLoader(self.test_dataset,
batch_size=2650*8,
shuffle=False,
num_workers=config.num_workers)
self.iter_test_loader = iter(self.test_loader)
def next_train(self, step: int) -> Tuple[list[RayBundle], list[Dict]]:
try:
ray_batch = next(self.iter_train_loader)
except StopIteration:
self.iter_train_loader = iter(self.train_loader)
ray_batch = next(self.iter_train_loader)
self.train_epoch+=1
for key in self.train_dataset.float_args:
ray_batch[key] = ray_batch[key].squeeze().float().to(self.device)
for key in ray_batch.keys():
ray_batch[key] = ray_batch[key].to(self.device)
self.train_count += 1
static_ray_batch = ray_batch
static_ray_bundles = RayBundle(
origins=static_ray_batch['rays_o'],
directions=static_ray_batch['rays_d'],
nears=None,
fars=None,
times=static_ray_batch['time_stamp'][:,None],
pixel_area=None
)
ray_batch_list = [static_ray_batch]
ray_bundles_list = [static_ray_bundles]
for i in range(self.train_dataset.dynamic_object_counter):
mask = torch.logical_and(ray_batch['dynamic_vehicle_idx'] == i, ray_batch['vehicle_mask'].bool())
dynamic_ray_batch = {}
for key in ray_batch.keys():
dynamic_ray_batch.update({key: ray_batch[key][mask]})
ray_batch_list.append(dynamic_ray_batch)
ray_bundles_list.append(
RayBundle(
origins=dynamic_ray_batch['rays_o'],
directions=dynamic_ray_batch['rays_d'],
nears=None,
fars=None,
times=None,
pixel_area=None
)
)
return ray_bundles_list, ray_batch_list
def next_eval(self, step: int) -> Tuple[list[RayBundle], list[Dict]]:
try:
ray_batch = next(self.iter_eval_loader)
except StopIteration:
self.iter_eval_loader = iter(self.eval_loader)
ray_batch = next(self.iter_eval_loader)
self.eval_epoch+=1
for key in self.train_dataset.float_args:
ray_batch[key] = ray_batch[key].squeeze().float().to(self.device)
for key in ray_batch.keys():
ray_batch[key] = ray_batch[key].to(self.device)
self.eval_count += 1
static_ray_batch = ray_batch
ray_batch_list = [static_ray_batch] + [{} for i in range(self.train_dataset.dynamic_object_counter)]
ray_bundles_list = [RayBundle(
origins=static_ray_batch['rays_o'],
directions=static_ray_batch['rays_d'],
nears=None,
fars=None,
times=None,
pixel_area=None
) for i in range(1+self.train_dataset.dynamic_object_counter)]
return ray_bundles_list, ray_batch_list
def next_test(self, step: int) -> Tuple[RayBundle, Dict]:
try:
ray_batch = next(self.iter_test_loader)
except StopIteration:
self.iter_test_loader = iter(self.test_loader)
ray_batch = next(self.iter_test_loader)
for key in self.train_dataset.float_args:
ray_batch[key] = ray_batch[key].squeeze().float().to(self.device)
for key in ray_batch.keys():
ray_batch[key] = ray_batch[key].to(self.device)
static_ray_batch = ray_batch
ray_batch_list = [static_ray_batch] + [{} for i in range(self.train_dataset.dynamic_object_counter)]
ray_bundles_list = [RayBundle(
origins=static_ray_batch['rays_o'],
directions=static_ray_batch['rays_d'],
nears=None,
fars=None,
times=None,
pixel_area=None
) for i in range(1+self.train_dataset.dynamic_object_counter)]
return ray_bundles_list, ray_batch_list
def get_train_rays_per_batch(self) -> int:
return self.config.train_num_rays_per_batch
def get_eval_rays_per_batch(self) -> int:
return self.config.eval_num_rays_per_batch