-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraining_loop_dataloader.py
251 lines (198 loc) · 7.79 KB
/
training_loop_dataloader.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import tyro
import sys
import torch
import numpy as np
import os
from pathlib import Path
from tqdm import tqdm
from typing import List
from mvdatasets.mvdataset import MVDataset
from mvdatasets.utils.printing import print_error, print_warning, print_success
from mvdatasets import Profiler
from mvdatasets import DataSplit
from mvdatasets.configs.example_config import ExampleConfig
from examples import get_dataset_test_preset, custom_exception_handler
def main(cfg: ExampleConfig, pc_paths: List[Path]):
device = cfg.machine.device
datasets_path = cfg.datasets_path
output_path = cfg.output_path
scene_name = cfg.scene_name
dataset_name = cfg.data.dataset_name
# dataset loading
mv_data = MVDataset(
dataset_name,
scene_name,
datasets_path,
config=cfg.data.asdict(),
point_clouds_paths=pc_paths,
verbose=True,
)
nr_epochs = 10
num_workers = os.cpu_count() // 2
print(f"num_workers: {num_workers}")
shuffle = True
persistent_workers = True # Reduce worker initialization overhead
pin_memory = True # For faster transfer to GPU
batch_size = 4 # nr full frames per batch
print(f"batch_size: {batch_size}")
benchmark = True
# -------------------------------------------------------------------------
nr_sequence_frames = 0
cameras_temporal_dim = mv_data.get_split("train")[0].get_temporal_dim()
if cameras_temporal_dim > 1:
use_incremental_sequence_lenght = True
else:
use_incremental_sequence_lenght = False
increase_nr_sequence_frames_each = 1
# index frames -------------------------------------------------------------
run_index_frames = True
if run_index_frames:
if benchmark:
# Set profiler
profiler = Profiler() # nb: might slow down the code
else:
profiler = None
# Loop over epochs
step = 0
epochs_pbar = tqdm(range(nr_epochs), desc="epochs", ncols=100)
for epoch_nr in epochs_pbar:
if profiler is not None:
profiler.start("epoch")
# if first iteration or need to update time dimension of data split
if (
epoch_nr == 0 # first iteration
# need to update time dimension of data split
or (
use_incremental_sequence_lenght
and nr_sequence_frames < cameras_temporal_dim
and epoch_nr % increase_nr_sequence_frames_each == 0
)
):
nr_sequence_frames += 1
# initialize data loader
data_split = DataSplit(
cameras=mv_data.get_split("train"),
nr_sequence_frames=nr_sequence_frames,
modalities=mv_data.get_split_modalities("train"),
index_pixels=False, # index frames (!!!)
)
#
data_loader = torch.utils.data.DataLoader(
data_split,
batch_size=batch_size,
num_workers=num_workers,
shuffle=shuffle,
persistent_workers=persistent_workers,
pin_memory=pin_memory,
)
# Iterate over batches
iter_pbar = tqdm(data_loader, desc="iter", ncols=100, disable=False)
for iter_nr, batch in enumerate(iter_pbar):
if profiler is not None:
profiler.start("iter")
# Move batch to device
batch = {k: v.to(device) for k, v in batch.items()}
if not benchmark:
# Print batch shape
for k, v in batch.items():
print(
f"{epoch_nr}, {iter_nr}, {k}: {v.shape}, {v.dtype}, {v.device}"
)
# Increment step
step += 1
if profiler is not None:
profiler.end("iter")
if profiler is not None:
profiler.end("epoch")
print_success(f"Done after {step} steps")
if profiler is not None:
profiler.print_avg_times()
# index pixels (too slow) -------------------------------------------------
run_index_pixels = False
if run_index_pixels:
if benchmark:
# Set profiler
profiler = Profiler() # nb: might slow down the code
else:
profiler = None
# initialize data loader
data_split = DataSplit(
cameras=mv_data.get_split("train"),
modalities=mv_data.get_split_modalities("train"),
index_pixels=True,
)
batch_size = 512 # nr of rays sampled per batch
print(f"batch_size: {batch_size}")
# test loop
# Loop over epochs
step = 0
epochs_pbar = tqdm(range(nr_epochs), desc="epochs", ncols=100)
for epoch_nr in epochs_pbar:
if profiler is not None:
profiler.start("epoch")
# if first iteration or need to update time dimension of data split
if (
epoch_nr == 0 # first iteration
# need to update time dimension of data split
or (
use_incremental_sequence_lenght
and nr_sequence_frames < cameras_temporal_dim
and epoch_nr % increase_nr_sequence_frames_each == 0
)
):
nr_sequence_frames += 1
# initialize data loader
data_split = DataSplit(
cameras=mv_data.get_split("train"),
nr_sequence_frames=nr_sequence_frames,
modalities=mv_data.get_split_modalities("train"),
index_pixels=True, # index pixels (!!!)
)
#
data_loader = torch.utils.data.DataLoader(
data_split,
batch_size=batch_size,
num_workers=num_workers,
shuffle=shuffle,
persistent_workers=persistent_workers,
pin_memory=pin_memory,
)
# Iterate over batches
iter_pbar = tqdm(data_loader, desc="iter", ncols=100, disable=False)
for iter_nr, batch in enumerate(iter_pbar):
if profiler is not None:
profiler.start("iter")
# Move batch to device
batch = {k: v.to(device) for k, v in batch.items()}
if not benchmark:
# Print batch shape
for k, v in batch.items():
print(
f"{epoch_nr}, {iter_nr}, {k}: {v.shape}, {v.dtype}, {v.device}"
)
# Increment step
step += 1
if profiler is not None:
profiler.end("iter")
if profiler is not None:
profiler.end("epoch")
print_success(f"Done after {step} steps")
if profiler is not None:
profiler.print_avg_times()
if __name__ == "__main__":
# custom exception handler
sys.excepthook = custom_exception_handler
# parse arguments
args = tyro.cli(ExampleConfig)
# get test preset
test_preset = get_dataset_test_preset(args.data.dataset_name)
# scene name
if args.scene_name is None:
args.scene_name = test_preset["scene_name"]
print_warning(
f"scene_name is None, using preset test scene {args.scene_name} for dataset"
)
# additional point clouds paths (if any)
pc_paths = test_preset["pc_paths"]
# start the example program
main(args, pc_paths)