-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_nerf_federated.py
386 lines (366 loc) · 14.2 KB
/
train_nerf_federated.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import argparse
import os
import threading
from distutils.dir_util import copy_tree
import IPython
import numpy as np
import torch
import yaml
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm, trange
from nerf import CfgNode
from train_nerf import create_models, load_dataset, set_seed, train_nerf
from update_compression import create_models_ML, get_parameter_count
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--config", type=str, required=True, help="Path to (.yml) config file."
)
configargs = parser.parse_args()
gpu_count = torch.cuda.device_count()
# Read config file.
cfg = None
with open(configargs.config, "r") as f:
cfg_dict = yaml.load(f, Loader=yaml.FullLoader)
cfg = CfgNode(cfg_dict)
(
USE_CACHED_DATASET,
train_paths,
validation_paths,
images,
poses,
render_poses,
hwf,
i_split,
H,
W,
focal,
i_train,
i_val,
i_test,
) = load_dataset(cfg)
assert not USE_CACHED_DATASET # doesn't play nicely with partitioning
# Partition training dataset
set_seed(cfg)
np.random.shuffle(i_train)
assert cfg.federated.initialise_image_count < len(i_train)
initialise_ids = i_train[: cfg.federated.initialise_image_count]
i_train = i_train[cfg.federated.initialise_image_count :]
partitioned_ids = None # list of cfg.federated.nodes number of i_train lists
if cfg.federated.partition == "interleave": # interleave by angle around z axis
image_angles = []
for i, pose in zip(i_train, poses[i_train]):
coordinate = pose[0:3, 3] / (pose[3, 3] if pose.shape[0] > 3 else 1)
angle = torch.arctan2(coordinate[1], coordinate[0])
image_angles.append((i, angle))
sorted_image_ids_by_angle = [
i for i, _ in sorted(image_angles, key=lambda ia: ia[1])
]
partitioned_ids = [
sorted_image_ids_by_angle[x :: cfg.federated.nodes]
for x in range(cfg.federated.nodes)
]
elif cfg.federated.partition == "separate":
image_angles = []
for i, pose in zip(i_train, poses[i_train]):
coordinate = pose[0:3, 3] / (pose[3, 3] if pose.shape[0] > 3 else 1)
angle = torch.arctan2(coordinate[1], coordinate[0])
image_angles.append((i, angle))
sorted_image_ids_by_angle = [
i for i, _ in sorted(image_angles, key=lambda ia: ia[1])
]
partitioned_ids = [
list(l)
for l in np.array_split(sorted_image_ids_by_angle, cfg.federated.nodes)
]
else:
raise ValueError()
print("Initialise IDs: ", initialise_ids)
print("Training partition IDs: ", partitioned_ids)
# Setup global logging and copy config
logdir = os.path.join(cfg.experiment.logdir, cfg.experiment.id)
os.makedirs(logdir, exist_ok=False)
with open(os.path.join(logdir, "config.yml"), "w") as f:
f.write(cfg.dump()) # cfg, f, default_flow_style=False)
# Create and train the initialisation NeRF
device = torch.device("cuda:0")
set_seed(
cfg
) # by resetting seeds before creating each model, they have the same initial weights
(
init_encode_position_fn,
init_encode_direction_fn,
init_model_coarse,
init_model_fine,
init_trainable_parameters,
init_optimizer,
) = create_models(cfg, device)
if isinstance(cfg.federated.train_initial, bool) and cfg.federated.train_initial:
individual_logdir = os.path.join(logdir, f"node_initial")
writer = SummaryWriter(individual_logdir)
os.makedirs(individual_logdir, exist_ok=True)
print("############# TRAINING INITIAL NERF")
train_nerf(
device,
cfg,
0,
cfg.experiment.train_iters,
USE_CACHED_DATASET,
init_model_coarse,
init_model_fine,
init_optimizer,
writer,
individual_logdir,
train_paths,
validation_paths,
init_encode_position_fn,
init_encode_direction_fn,
initialise_ids,
i_val,
images,
poses,
H,
W,
focal,
)
print("################ INITIAL NERF TRAINED")
torch.cuda.empty_cache()
elif isinstance(
cfg.federated.train_initial, str
) and cfg.federated.train_initial.startswith("fromexp:"):
load_from_exp_name = cfg.federated.train_initial[8:]
copy_tree(
os.path.join(cfg.experiment.logdir, load_from_exp_name, "node_initial"),
os.path.join(cfg.experiment.logdir, cfg.experiment.id, "node_initial"),
)
ckpt = torch.load(
os.path.join(
cfg.experiment.logdir,
cfg.experiment.id,
"node_initial",
f"checkpoint{str((cfg.experiment.train_iters-1)).zfill(5)}.ckpt",
),
map_location=device,
)
init_model_coarse.load_state_dict(ckpt["model_coarse_state_dict"])
if init_model_fine:
init_model_fine.load_state_dict(ckpt["model_fine_state_dict"])
torch.cuda.empty_cache()
else:
raise ValueError()
# Create and train the control NeRF
if isinstance(cfg.federated.train_control, bool) and cfg.federated.train_control:
set_seed(
cfg
) # by resetting seeds before creating each model, they have the same initial weights
(
control_encode_position_fn,
control_encode_direction_fn,
control_model_coarse,
control_model_fine,
control_trainable_parameters,
control_optimizer,
) = create_models(cfg, device)
control_model_coarse.load_state_dict(init_model_coarse.state_dict())
if control_model_fine is not None:
control_model_fine.load_state_dict(init_model_fine.state_dict())
individual_logdir = os.path.join(logdir, f"node_control")
writer = SummaryWriter(individual_logdir)
os.makedirs(individual_logdir, exist_ok=True)
print("############# TRAINING CONTROL NERF")
train_nerf(
device,
cfg,
0,
cfg.experiment.train_iters,
USE_CACHED_DATASET,
control_model_coarse,
control_model_fine,
control_optimizer,
writer,
individual_logdir,
train_paths,
validation_paths,
control_encode_position_fn,
control_encode_direction_fn,
i_train, # all the remaning non-initialisation data together :)
i_val,
images,
poses,
H,
W,
focal,
)
print("################ CONTROL NERF TRAINED")
torch.cuda.empty_cache()
elif isinstance(
cfg.federated.train_control, str
) and cfg.federated.train_control.startswith("fromexp:"):
load_from_exp_name = cfg.federated.train_control[8:]
copy_tree(
os.path.join(cfg.experiment.logdir, load_from_exp_name, "node_control"),
os.path.join(cfg.experiment.logdir, cfg.experiment.id, "node_control"),
)
torch.cuda.empty_cache()
else:
raise ValueError()
# Create the separate NeRFs
nerfs = []
for i in range(cfg.federated.nodes):
device = torch.device("cuda", i % gpu_count)
set_seed(
cfg
) # by resetting seeds before creating each model, they have the same initial weights
(
encode_position_fn,
encode_direction_fn,
model_coarse,
model_fine,
trainable_parameters,
optimizer,
) = create_models(cfg, device)
# Setup nerf from the initially trained model
if cfg.federated.compress_method == "none":
model_coarse.load_state_dict(init_model_coarse.state_dict().to(device))
if model_fine is not None:
model_fine.load_state_dict(init_model_fine.state_dict().to(device))
elif cfg.federated.compress_method == "ML":
(
model_coarse,
model_fine,
trainable_parameters,
optimizer,
) = create_models_ML(init_model_coarse, init_model_fine, cfg)
model_coarse = model_coarse.to(device)
model_fine = model_fine.to(device)
if i == 0:
print(
f"###### COMPRESSION FROM INITIAL {get_parameter_count(init_model_coarse)} TO {get_parameter_count(model_coarse)} PARAMETERS"
)
else:
raise ValueError
individual_logdir = os.path.join(logdir, f"node{i}")
writer = SummaryWriter(individual_logdir)
os.makedirs(individual_logdir, exist_ok=True)
nerfs.append(
dict(
i_train=partitioned_ids[i],
device=device,
encode_position_fn=encode_position_fn,
encode_direction_fn=encode_direction_fn,
model_coarse=model_coarse,
model_fine=model_fine,
trainable_parameters=trainable_parameters,
optimizer=optimizer,
logdir=individual_logdir,
writer=writer,
)
)
# Federated training loop
torch.cuda.empty_cache()
start_iter = 0
for iter_upto in trange(
cfg.federated.merge_every,
cfg.experiment.train_iters + 1,
cfg.federated.merge_every,
desc="Overall progress",
):
# Train up to next step
training_threads = []
print(f"########### TRAINING FROM {start_iter} TO {iter_upto}")
coarse_combined_state_dict = None # initialise here to accumulate over all "batches" of NeRFs that can run in the GPUs at once
fine_combined_state_dict = None
combine_device = torch.device("cpu")
# Train by batch that can run on the provided GPU count
for current_nerfs in tqdm(
[nerfs[x : x + gpu_count] for x in range(0, len(nerfs), gpu_count)],
desc="GPU set batches",
):
for nerf in current_nerfs:
training_threads.append(
threading.Thread(
target=train_nerf,
args=(
nerf["device"],
cfg,
start_iter,
iter_upto,
USE_CACHED_DATASET,
nerf["model_coarse"],
nerf["model_fine"],
nerf["optimizer"],
nerf["writer"],
nerf["logdir"],
train_paths,
validation_paths,
nerf["encode_position_fn"],
nerf["encode_direction_fn"],
nerf["i_train"],
i_val,
images,
poses,
H,
W,
focal,
),
)
)
training_threads[-1].start()
for thread in training_threads:
thread.join()
torch.cuda.empty_cache()
# Combine the current weights (leaving individual optimizer states as they are)
# start with the weighted weights of the first network if necessary
reinitialise_state_dict = False
if coarse_combined_state_dict is None:
reinitialise_state_dict = True
coarse_combined_state_dict = {
k: len(nerfs[0]["i_train"]) * t.to(combine_device)
for k, t in nerfs[0]["model_coarse"].state_dict().items()
}
fine_combined_state_dict = {
k: len(nerfs[0]["i_train"]) * t.to(combine_device)
for k, t in nerfs[0]["model_fine"].state_dict().items()
}
# now add the weights of the remaining networks
for nerf in current_nerfs[1 if reinitialise_state_dict else 0 :]:
coarse_combined_state_dict = {
k: coarse_combined_state_dict[k]
+ len(nerf["i_train"]) * t.to(combine_device)
for k, t in nerf["model_coarse"].state_dict().items()
}
fine_combined_state_dict = {
k: fine_combined_state_dict[k]
+ len(nerf["i_train"]) * t.to(combine_device)
for k, t in nerf["model_fine"].state_dict().items()
}
# Finally, divide by total weights now that all GPU set batches are done
total_weight = len(i_train)
coarse_combined_state_dict = {
k: t / total_weight for k, t in coarse_combined_state_dict.items()
}
fine_combined_state_dict = {
k: t / total_weight for k, t in fine_combined_state_dict.items()
}
# Now update all NeRFs with the combined weights and also save a checkpoint
for nerf in nerfs:
nerf["model_coarse"].load_state_dict(coarse_combined_state_dict)
nerf["model_fine"].load_state_dict(fine_combined_state_dict)
os.makedirs(os.path.join(logdir, "nodes_combined"), exist_ok=True)
torch.save(
{
"model_coarse_state_dict": coarse_combined_state_dict,
"model_fine_state_dict": fine_combined_state_dict,
"iter": iter_upto - 1,
"optimizer_state_dicts": [
nerf["optimizer"].state_dict() for nerf in nerfs
],
"partitioned_ids": partitioned_ids,
},
os.path.join(
logdir, "nodes_combined", f"checkpoint{str(iter_upto-1).zfill(5)}.ckpt"
),
)
print("############ SAVED COMBINED CHECKPOINT")
torch.cuda.empty_cache()
start_iter = iter_upto