-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsDreamer_train.py
1385 lines (1234 loc) · 72.3 KB
/
ConsDreamer_train.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import sys
import yaml
import imageio
from random import randint
from utils.loss_utils import l1_loss, ssim, tv_loss
from gaussian_renderer import render, network_gui
from scene import Scene, GaussianModel
import uuid
from tqdm import tqdm
from argparse import ArgumentParser, Namespace
from arguments import ModelParams, PipelineParams, OptimizationParams, GenerateCamParams, GuidanceParams
import math
try:
from torch.utils.tensorboard import SummaryWriter
TENSORBOARD_FOUND = True
except ImportError:
TENSORBOARD_FOUND = False
from tqdm.notebook import tqdm
from transformers import CLIPTextModel, CLIPTokenizer, logging
from pathlib import Path
import random
logging.set_verbosity_error()
from torchvision.utils import save_image
from torch.cuda.amp import custom_bwd, custom_fwd
from guidance.perpneg_utils import weighted_perpendicular_aggregator
from guidance.sd_step import *
import clip_utils
import os
import torch
import torch.nn.functional as F
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
def rgb2sat(img, T=None):
max_ = torch.max(img, dim=1, keepdim=True).values + 1e-5
min_ = torch.min(img, dim=1, keepdim=True).values
sat = (max_ - min_) / max_
if T is not None:
sat = (1 - T) * sat
return sat
class SpecifyGradient(torch.autograd.Function):
@staticmethod
@custom_fwd
def forward(ctx, input_tensor, gt_grad):
ctx.save_for_backward(gt_grad)
return torch.ones([1], device=input_tensor.device, dtype=input_tensor.dtype)
@staticmethod
@custom_bwd
def backward(ctx, grad_scale):
gt_grad, = ctx.saved_tensors
gt_grad = gt_grad * grad_scale
return gt_grad, None
def seed_everything(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
class StableDiffusion(nn.Module):
flag = 0
def __init__(self, device, fp16, vram_O, t_range=[0.02, 0.98], max_t_range=0.98, num_train_timesteps=None,
ddim_inv=False, textual_inversion_path=None,
LoRA_path=None, guidance_opt=None):
super().__init__()
self.flag = 0
self.device = device
self.precision_t = torch.float16 if fp16 else torch.float32
print(f'[INFO] loading stable diffusion...')
model_key = guidance_opt.model_key
if model_key == None:
model_key = "stabilityai/stable-diffusion-2-1-base"
is_safe_tensor = guidance_opt.is_safe_tensor
base_model_key = model_key
if is_safe_tensor:
pipe = StableDiffusionPipeline.from_single_file(model_key, use_safetensors=True,
torch_dtype=self.precision_t, load_safety_checker=False)
else:
pipe = StableDiffusionPipeline.from_pretrained(model_key, torch_dtype=self.precision_t)
self.pipe = pipe
self.ism = not guidance_opt.sds
self.scheduler = DDIMScheduler.from_pretrained(model_key if not is_safe_tensor else base_model_key,
subfolder="scheduler", torch_dtype=self.precision_t)
self.sche_func = ddim_step
if vram_O:
pipe.enable_sequential_cpu_offload()
pipe.enable_vae_slicing()
pipe.unet.to(memory_format=torch.channels_last)
pipe.enable_attention_slicing(1)
pipe.enable_model_cpu_offload()
pipe.enable_xformers_memory_efficient_attention()
pipe = pipe.to(self.device)
if textual_inversion_path is not None:
pipe.load_textual_inversion(textual_inversion_path)
print("load textual inversion in:.{}".format(textual_inversion_path))
self.pipe = pipe
self.vae = pipe.vae
self.tokenizer = pipe.tokenizer
self.text_encoder = pipe.text_encoder
self.unet = pipe.unet
self.num_train_timesteps = num_train_timesteps if num_train_timesteps is not None else self.scheduler.config.num_train_timesteps
self.scheduler.set_timesteps(self.num_train_timesteps, device=device)
self.timesteps = torch.flip(self.scheduler.timesteps, dims=(0,))
self.min_step = int(self.num_train_timesteps * t_range[0])
self.max_step = int(self.num_train_timesteps * t_range[1])
self.warmup_step = int(self.num_train_timesteps * (max_t_range - t_range[1]))
self.noise_temp = None
self.noise_gen = torch.Generator(self.device)
self.noise_gen.manual_seed(guidance_opt.noise_seed)
self.alphas = self.scheduler.alphas_cumprod.to(self.device)
self.rgb_latent_factors = torch.tensor([
[0.298, 0.207, 0.208],
[0.187, 0.286, 0.173],
[-0.158, 0.189, 0.264],
[-0.184, -0.271, -0.473]
], device=self.device)
print(f'[INFO] loaded stable diffusion!')
def augmentation(self, *tensors):
augs = T.Compose([
T.RandomHorizontalFlip(p=0.5),
])
channels = [ten.shape[1] for ten in tensors]
tensors_concat = torch.concat(tensors, dim=1)
tensors_concat = augs(tensors_concat)
results = []
cur_c = 0
for i in range(len(channels)):
results.append(tensors_concat[:, cur_c:cur_c + channels[i], ...])
cur_c += channels[i]
return (ten for ten in results)
def add_noise_with_cfg(self, latents, noise,
ind_t, ind_prev_t,
text_embeddings=None, cfg=1.0,
delta_t=1, inv_steps=1,
is_noisy_latent=False,
eta=0.0):
text_embeddings = text_embeddings.to(self.precision_t)
if cfg <= 1.0:
uncond_text_embedding = \
text_embeddings.reshape(2, -1, text_embeddings.shape[-2], text_embeddings.shape[-1])[1]
unet = self.unet
if is_noisy_latent:
prev_noisy_lat = latents
else:
prev_noisy_lat = self.scheduler.add_noise(latents, noise, self.timesteps[ind_prev_t])
cur_ind_t = ind_prev_t
cur_noisy_lat = prev_noisy_lat
pred_scores = []
for i in range(inv_steps):
# pred noise
cur_noisy_lat_ = self.scheduler.scale_model_input(cur_noisy_lat, self.timesteps[cur_ind_t]).to(
self.precision_t)
if cfg > 1.0:
latent_model_input = torch.cat([cur_noisy_lat_, cur_noisy_lat_])
timestep_model_input = self.timesteps[cur_ind_t].reshape(1, 1).repeat(latent_model_input.shape[0],
1).reshape(-1)
unet_output = unet(latent_model_input, timestep_model_input,
encoder_hidden_states=text_embeddings).sample
uncond, cond = torch.chunk(unet_output, chunks=2)
unet_output = cond + cfg * (uncond - cond)
else:
timestep_model_input = self.timesteps[cur_ind_t].reshape(1, 1).repeat(cur_noisy_lat_.shape[0],
1).reshape(-1)
StableDiffusion.flag = 0
unet_output = unet(cur_noisy_lat_, timestep_model_input,
encoder_hidden_states=uncond_text_embedding).sample
pred_scores.append((cur_ind_t, unet_output))
next_ind_t = min(cur_ind_t + delta_t, ind_t)
cur_t, next_t = self.timesteps[cur_ind_t], self.timesteps[next_ind_t]
delta_t_ = next_t - cur_t if isinstance(self.scheduler, DDIMScheduler) else next_ind_t - cur_ind_t
cur_noisy_lat = self.sche_func(self.scheduler, unet_output, cur_t, cur_noisy_lat, -delta_t_,
eta).prev_sample
cur_ind_t = next_ind_t
del unet_output
torch.cuda.empty_cache()
if cur_ind_t == ind_t:
break
return prev_noisy_lat, cur_noisy_lat, pred_scores[::-1]
@torch.no_grad()
def get_text_embeds(self, prompt, resolution=(512, 512)):
inputs = self.tokenizer(prompt, padding='max_length', max_length=self.tokenizer.model_max_length,
truncation=True, return_tensors='pt')
embeddings = self.text_encoder(inputs.input_ids.to(self.device))[0]
return embeddings
def train_step_perpneg(self, text_embeddings, pred_rgb, pred_depth=None, pred_alpha=None,
grad_scale=1,
save_folder: Path = None, iteration=0, warm_up_rate=0, weights=0,
resolution=(512, 512), guidance_opt=None, as_latent=False, embedding_inverse=None,
iteration_flag1=2500, iteration_flag2=4000, gaussians=None):
pred_rgb, pred_depth, pred_alpha = self.augmentation(pred_rgb, pred_depth, pred_alpha)
B = pred_rgb.shape[0]
K = text_embeddings.shape[0] - 1
if as_latent:
latents, _ = self.encode_imgs(pred_depth.repeat(1, 3, 1, 1).to(self.precision_t))
else:
latents, _ = self.encode_imgs(pred_rgb.to(self.precision_t))
weights = weights.reshape(-1)
noise = torch.randn((latents.shape[0], 4, resolution[0] // 8, resolution[1] // 8,), dtype=latents.dtype,
device=latents.device, generator=self.noise_gen) + 0.1 * torch.randn((1, 4, 1, 1),
device=latents.device).repeat(
latents.shape[0], 1, 1, 1)
inverse_text_embeddings = embedding_inverse.unsqueeze(1).repeat(1, B, 1, 1).reshape(-1,
embedding_inverse.shape[-2],
embedding_inverse.shape[-1])
text_embeddings = text_embeddings.reshape(-1, text_embeddings.shape[-2], text_embeddings.shape[-1])
if guidance_opt.annealing_intervals:
current_delta_t = int(
guidance_opt.delta_t + np.ceil((warm_up_rate) * (guidance_opt.delta_t_start - guidance_opt.delta_t)))
else:
current_delta_t = guidance_opt.delta_t
ind_t = \
torch.randint(self.min_step, self.max_step + int(self.warmup_step * warm_up_rate), (1,), dtype=torch.long,
generator=self.noise_gen, device=self.device)[0]
ind_prev_t = max(ind_t - current_delta_t, torch.ones_like(ind_t) * 0)
t = self.timesteps[ind_t]
prev_t = self.timesteps[ind_prev_t]
if (1200 > iteration > 1500 and random.random() < 0.3 and prev_t <= 300 and not as_latent) or (
iteration < 3000 and random.random() < 0.2 and as_latent and prev_t <= 400):
with torch.no_grad():
# Step 1: sample x_s with larger steps
xs_delta_t = guidance_opt.xs_delta_t if guidance_opt.xs_delta_t is not None else current_delta_t
xs_inv_steps = guidance_opt.xs_inv_steps if guidance_opt.xs_inv_steps is not None else int(
np.ceil(ind_prev_t / xs_delta_t))
starting_ind = max(ind_prev_t - xs_delta_t * xs_inv_steps, torch.ones_like(ind_t) * 0)
_, prev_latents_noisy, pred_scores_xs = self.add_noise_with_cfg(latents, noise, ind_prev_t,
starting_ind, inverse_text_embeddings,
guidance_opt.denoise_guidance_scale,
xs_delta_t, xs_inv_steps,
eta=guidance_opt.xs_eta)
# Step 2: sample x_t
_, latents_noisy, pred_scores_xt = self.add_noise_with_cfg(prev_latents_noisy, noise, ind_t, ind_prev_t,
inverse_text_embeddings,
guidance_opt.denoise_guidance_scale,
current_delta_t, 1, is_noisy_latent=True)
pred_scores = pred_scores_xt + pred_scores_xs
target = pred_scores[0][1]
with torch.no_grad():
latent_model_input = latents_noisy[None, :, ...].repeat(1 + K, 1, 1, 1, 1).reshape(-1, 4,
resolution[0] // 8,
resolution[1] // 8, )
tt = t.reshape(1, 1).repeat(latent_model_input.shape[0], 1).reshape(-1)
latent_model_input = self.scheduler.scale_model_input(latent_model_input, tt[0])
StableDiffusion.flag = 1
unet_output = self.unet(latent_model_input.to(self.precision_t), tt.to(self.precision_t),
encoder_hidden_states=text_embeddings.to(self.precision_t)).sample
StableDiffusion.flag = 0
unet_output = unet_output.reshape(1 + K, -1, 4, resolution[0] // 8, resolution[1] // 8, )
noise_pred_uncond, noise_pred_text = unet_output[:1].reshape(-1, 4, resolution[0] // 8,
resolution[1] // 8, ), unet_output[
1:].reshape(-1, 4,
resolution[
0] // 8,
resolution[
1] // 8, )
pred_x0_latent_sp = pred_original(self.scheduler, noise_pred_uncond, prev_t, prev_latents_noisy)
pred_x0_sp = self.decode_latents(pred_x0_latent_sp.type(self.precision_t))
w = lambda alphas: (((1 - alphas) / alphas) ** 0.5)
grad = w(self.alphas[t]) * (pred_x0_latent_sp - latents)
grad = torch.nan_to_num(grad_scale * grad)
loss = SpecifyGradient.apply(latents, grad)
pred_x0_latent_sp = pred_original(self.scheduler, noise_pred_uncond, prev_t, prev_latents_noisy)
pred_x0_sp = self.decode_latents(pred_x0_latent_sp.type(self.precision_t))
flaggg = 1
else:
with torch.no_grad():
# Step 1: sample x_s with larger steps
xs_delta_t = guidance_opt.xs_delta_t if guidance_opt.xs_delta_t is not None else current_delta_t
xs_inv_steps = guidance_opt.xs_inv_steps if guidance_opt.xs_inv_steps is not None else int(
np.ceil(ind_prev_t / xs_delta_t))
starting_ind = max(ind_prev_t - xs_delta_t * xs_inv_steps, torch.ones_like(ind_t) * 0)
_, prev_latents_noisy, pred_scores_xs = self.add_noise_with_cfg(latents, noise, ind_prev_t,
starting_ind, inverse_text_embeddings,
guidance_opt.denoise_guidance_scale,
xs_delta_t, xs_inv_steps,
eta=guidance_opt.xs_eta)
# Step 2: sample x_t
_, latents_noisy, pred_scores_xt = self.add_noise_with_cfg(prev_latents_noisy, noise, ind_t, ind_prev_t,
inverse_text_embeddings,
guidance_opt.denoise_guidance_scale,
current_delta_t, 1, is_noisy_latent=True)
pred_scores = pred_scores_xt + pred_scores_xs
target = pred_scores[0][1]
with torch.no_grad():
latent_model_input = latents_noisy[None, :, ...].repeat(1 + K, 1, 1, 1, 1).reshape(-1, 4,
resolution[0] // 8,
resolution[1] // 8, )
tt = t.reshape(1, 1).repeat(latent_model_input.shape[0], 1).reshape(-1)
latent_model_input = self.scheduler.scale_model_input(latent_model_input, tt[0])
unet_output = self.unet(latent_model_input.to(self.precision_t), tt.to(self.precision_t),
encoder_hidden_states=text_embeddings.to(self.precision_t)).sample
unet_output = unet_output.reshape(1 + K, -1, 4, resolution[0] // 8, resolution[1] // 8, )
noise_pred_uncond, noise_pred_text = unet_output[:1].reshape(-1, 4, resolution[0] // 8,
resolution[1] // 8, ), unet_output[
1:].reshape(
-1, 4, resolution[0] // 8, resolution[1] // 8, )
delta_noise_preds = noise_pred_text - noise_pred_uncond.repeat(K, 1, 1, 1)
delta_DSD = weighted_perpendicular_aggregator(delta_noise_preds, \
weights, \
B)
pred_noise = noise_pred_uncond + guidance_opt.guidance_scale * delta_DSD
w = lambda alphas: (((1 - alphas) / alphas) ** 0.5)
grad = w(self.alphas[t]) * (pred_noise - target)
grad = torch.nan_to_num(grad_scale * grad)
loss = SpecifyGradient.apply(latents, grad)
flaggg = 0
pred_x0_latent_sp = pred_original(self.scheduler, noise_pred_uncond, prev_t, prev_latents_noisy)
pred_x0_sp = self.decode_latents(pred_x0_latent_sp.type(self.precision_t))
def ordered_similarity_loss(sim_A, sim_B, sim_C):
diff_AB = sim_B - sim_A
diff_BC = sim_C - sim_B
loss_AB = torch.relu(diff_AB)
loss_BC = torch.relu(diff_BC)
lossABC = loss_AB + loss_BC
return lossABC
def compute_alex_loss(pred_x0_sp):
total_score = 0
for i in range(3):
alex_x2next = lpips_alex(pred_x0_sp[i + 1][0], pred_x0_sp[i][0])
total_score += alex_x2next
return total_score / 3
def compute_P_loss(embed_rgb):
sim = []
for i in range(3):
consistency_x_to_1 = torch.cosine_similarity(embed_rgb[0][0], embed_rgb[i + 1][0], dim=-1)
sim.append(consistency_x_to_1)
lossABC = ordered_similarity_loss(sim[0], sim[1], sim[2])
return lossABC
if not as_latent:
lambda_sum_cos_loss = 1
lambda_ABC_cos_loss = 1
if iteration >= iteration_flag2:
clip_loss1 = compute_alex_loss(pred_rgb)
clip_loss = clip_loss1 * lambda_sum_cos_loss
loss = loss + clip_loss
elif iteration <= iteration_flag1:
pred_rgb_resize = F.interpolate(pred_x0_sp, size=(224, 224), mode="bicubic")
embed_rgb = embed(pred_rgb_resize)
clip_loss1 = compute_P_loss(embed_rgb)
clip_loss = clip_loss1 * lambda_ABC_cos_loss
loss = loss + clip_loss
if iteration % 10 == 0:
if not flaggg:
noise_pred_post = noise_pred_uncond + guidance_opt.guidance_scale * delta_DSD
lat2rgb = lambda x: torch.clip(
(x.permute(0, 2, 3, 1) @ self.rgb_latent_factors.to(x.dtype)).permute(0, 3, 1, 2), 0., 1.)
save_path_iter = os.path.join(save_folder, "iter_{}_step_{}.jpg".format(iteration, prev_t.item()))
with torch.no_grad():
pred_x0_latent_pos = pred_original(self.scheduler, noise_pred_post, prev_t, prev_latents_noisy)
pred_x0_pos = self.decode_latents(pred_x0_latent_pos.type(self.precision_t))
grad_abs = torch.abs(grad.detach())
norm_grad = F.interpolate((grad_abs / grad_abs.max()).mean(dim=1, keepdim=True),
(resolution[0], resolution[1]), mode='bilinear',
align_corners=False).repeat(
1, 3, 1, 1)
latents_rgb = F.interpolate(lat2rgb(latents), (resolution[0], resolution[1]), mode='bilinear',
align_corners=False)
latents_sp_rgb = F.interpolate(lat2rgb(pred_x0_latent_sp), (resolution[0], resolution[1]),
mode='bilinear', align_corners=False)
viz_images = torch.cat([pred_rgb,
pred_depth.repeat(1, 3, 1, 1),
pred_alpha.repeat(1, 3, 1, 1),
rgb2sat(pred_rgb, pred_alpha).repeat(1, 3, 1, 1),
latents_rgb, latents_sp_rgb,
pred_x0_pos, pred_x0_sp,
norm_grad], dim=0)
save_image(viz_images, save_path_iter)
else:
lat2rgb = lambda x: torch.clip(
(x.permute(0, 2, 3, 1) @ self.rgb_latent_factors.to(x.dtype)).permute(0, 3, 1, 2), 0., 1.)
save_path_iter = os.path.join(save_folder, "iter_{}_step_{}.jpg".format(iteration, prev_t.item()))
with torch.no_grad():
grad_abs = torch.abs(grad.detach())
norm_grad = F.interpolate((grad_abs / grad_abs.max()).mean(dim=1, keepdim=True),
(resolution[0], resolution[1]), mode='bilinear',
align_corners=False).repeat(
1, 3, 1, 1)
latents_rgb = F.interpolate(lat2rgb(latents), (resolution[0], resolution[1]), mode='bilinear',
align_corners=False)
latents_sp_rgb = F.interpolate(lat2rgb(pred_x0_latent_sp), (resolution[0], resolution[1]),
mode='bilinear', align_corners=False)
viz_images = torch.cat([pred_rgb,
pred_depth.repeat(1, 3, 1, 1),
pred_alpha.repeat(1, 3, 1, 1),
rgb2sat(pred_rgb, pred_alpha).repeat(1, 3, 1, 1),
latents_rgb, latents_sp_rgb,
pred_x0_sp, norm_grad], dim=0)
save_image(viz_images, save_path_iter)
return loss
def train_step(self, text_embeddings, pred_rgb, pred_depth=None, pred_alpha=None,
grad_scale=1,
save_folder: Path = None, iteration=0, warm_up_rate=0,
resolution=(512, 512), guidance_opt=None, as_latent=False, embedding_inverse=None):
pred_rgb, pred_depth, pred_alpha = self.augmentation(pred_rgb, pred_depth, pred_alpha)
B = pred_rgb.shape[0]
K = text_embeddings.shape[0] - 1
if as_latent:
latents, _ = self.encode_imgs(pred_depth.repeat(1, 3, 1, 1).to(self.precision_t))
else:
latents, _ = self.encode_imgs(pred_rgb.to(self.precision_t))
if self.noise_temp is None:
self.noise_temp = torch.randn((latents.shape[0], 4, resolution[0] // 8, resolution[1] // 8,),
dtype=latents.dtype, device=latents.device,
generator=self.noise_gen) + 0.1 * torch.randn((1, 4, 1, 1),
device=latents.device).repeat(
latents.shape[0], 1, 1, 1)
if guidance_opt.fix_noise:
noise = self.noise_temp
else:
noise = torch.randn((latents.shape[0], 4, resolution[0] // 8, resolution[1] // 8,), dtype=latents.dtype,
device=latents.device, generator=self.noise_gen) + 0.1 * torch.randn((1, 4, 1, 1),
device=latents.device).repeat(
latents.shape[0], 1, 1, 1)
text_embeddings = text_embeddings[:, :, ...]
text_embeddings = text_embeddings.reshape(-1, text_embeddings.shape[-2],
text_embeddings.shape[-1]) # make it k+1, c * t, ...
inverse_text_embeddings = embedding_inverse.unsqueeze(1).repeat(1, B, 1, 1).reshape(-1,
embedding_inverse.shape[-2],
embedding_inverse.shape[-1])
if guidance_opt.annealing_intervals:
current_delta_t = int(
guidance_opt.delta_t + (warm_up_rate) * (guidance_opt.delta_t_start - guidance_opt.delta_t))
else:
current_delta_t = guidance_opt.delta_t
ind_t = \
torch.randint(self.min_step, self.max_step + int(self.warmup_step * warm_up_rate), (1,), dtype=torch.long,
generator=self.noise_gen, device=self.device)[0]
ind_prev_t = max(ind_t - current_delta_t, torch.ones_like(ind_t) * 0)
t = self.timesteps[ind_t]
prev_t = self.timesteps[ind_prev_t]
with torch.no_grad():
# step unroll via ddim inversion
if self.ism:
prev_latents_noisy = self.scheduler.add_noise(latents, noise, prev_t)
latents_noisy = self.scheduler.add_noise(latents, noise, t)
target = noise
else:
# Step 1: sample x_s with larger steps
xs_delta_t = guidance_opt.xs_delta_t if guidance_opt.xs_delta_t is not None else current_delta_t
xs_inv_steps = guidance_opt.xs_inv_steps if guidance_opt.xs_inv_steps is not None else int(
np.ceil(ind_prev_t / xs_delta_t))
starting_ind = max(ind_prev_t - xs_delta_t * xs_inv_steps, torch.ones_like(ind_t) * 0)
_, prev_latents_noisy, pred_scores_xs = self.add_noise_with_cfg(latents, noise, ind_prev_t,
starting_ind, inverse_text_embeddings,
guidance_opt.denoise_guidance_scale,
xs_delta_t, xs_inv_steps,
eta=guidance_opt.xs_eta)
# Step 2: sample x_t
_, latents_noisy, pred_scores_xt = self.add_noise_with_cfg(prev_latents_noisy, noise, ind_t, ind_prev_t,
inverse_text_embeddings,
guidance_opt.denoise_guidance_scale,
current_delta_t, 1, is_noisy_latent=True)
pred_scores = pred_scores_xt + pred_scores_xs
target = pred_scores[0][1]
with torch.no_grad():
latent_model_input = latents_noisy[None, :, ...].repeat(2, 1, 1, 1, 1).reshape(-1, 4, resolution[0] // 8,
resolution[1] // 8, )
tt = t.reshape(1, 1).repeat(latent_model_input.shape[0], 1).reshape(-1)
latent_model_input = self.scheduler.scale_model_input(latent_model_input, tt[0])
unet_output = self.unet(latent_model_input.to(self.precision_t), tt.to(self.precision_t),
encoder_hidden_states=text_embeddings.to(self.precision_t)).sample
unet_output = unet_output.reshape(2, -1, 4, resolution[0] // 8, resolution[1] // 8, )
noise_pred_uncond, noise_pred_text = unet_output[:1].reshape(-1, 4, resolution[0] // 8,
resolution[1] // 8, ), unet_output[1:].reshape(
-1, 4, resolution[0] // 8, resolution[1] // 8, )
delta_DSD = noise_pred_text - noise_pred_uncond
pred_noise = noise_pred_uncond + guidance_opt.guidance_scale * delta_DSD
w = lambda alphas: (((1 - alphas) / alphas) ** 0.5)
grad = w(self.alphas[t]) * (pred_noise - target)
grad = torch.nan_to_num(grad_scale * grad)
loss = SpecifyGradient.apply(latents, grad)
if iteration % 10 == 0:
noise_pred_post = noise_pred_uncond + guidance_opt.guidance_scale * delta_DSD
lat2rgb = lambda x: torch.clip(
(x.permute(0, 2, 3, 1) @ self.rgb_latent_factors.to(x.dtype)).permute(0, 3, 1, 2), 0., 1.)
save_path_iter = os.path.join(save_folder, "iter_{}_step_{}.jpg".format(iteration, prev_t.item()))
with torch.no_grad():
pred_x0_latent_sp = pred_original(self.scheduler, noise_pred_uncond, prev_t, prev_latents_noisy)
pred_x0_latent_pos = pred_original(self.scheduler, noise_pred_post, prev_t, prev_latents_noisy)
pred_x0_pos = self.decode_latents(pred_x0_latent_pos.type(self.precision_t))
pred_x0_sp = self.decode_latents(pred_x0_latent_sp.type(self.precision_t))
grad_abs = torch.abs(grad.detach())
norm_grad = F.interpolate((grad_abs / grad_abs.max()).mean(dim=1, keepdim=True),
(resolution[0], resolution[1]), mode='bilinear', align_corners=False).repeat(
1, 3, 1, 1)
latents_rgb = F.interpolate(lat2rgb(latents), (resolution[0], resolution[1]), mode='bilinear',
align_corners=False)
latents_sp_rgb = F.interpolate(lat2rgb(pred_x0_latent_sp), (resolution[0], resolution[1]),
mode='bilinear', align_corners=False)
viz_images = torch.cat([pred_rgb,
pred_depth.repeat(1, 3, 1, 1),
pred_alpha.repeat(1, 3, 1, 1),
rgb2sat(pred_rgb, pred_alpha).repeat(1, 3, 1, 1),
latents_rgb, latents_sp_rgb,
pred_x0_pos, pred_x0_sp,
norm_grad], dim=0)
save_image(viz_images, save_path_iter)
return loss
def decode_latents(self, latents):
target_dtype = latents.dtype
latents = latents / self.vae.config.scaling_factor
imgs = self.vae.decode(latents.to(self.vae.dtype)).sample
imgs = (imgs / 2 + 0.5).clamp(0, 1)
return imgs.to(target_dtype)
def encode_imgs(self, imgs):
target_dtype = imgs.dtype
# imgs: [B, 3, H, W]
imgs = 2 * imgs - 1
posterior = self.vae.encode(imgs.to(self.vae.dtype)).latent_dist
kl_divergence = posterior.kl()
latents = posterior.sample() * self.vae.config.scaling_factor
return latents.to(target_dtype), kl_divergence
from dataclasses import dataclass, field
from typing import Tuple, Dict, List
import torch
from torch import nn
from jaxtyping import Float
@dataclass
class PromptEmbedding:
prompt: str
tokenwise_embeddings: Dict[str, Float[torch.Tensor, 'n d']]
tokenwise_embedding_spans: Dict[str, List[Tuple[int, int]]]
@staticmethod
def merge(*embs: PromptEmbedding) -> PromptEmbedding:
emb_joined = None
for emb in embs:
if emb_joined is None:
emb_joined = PromptEmbedding(
prompt=emb.prompt,
tokenwise_embeddings={k: v for k, v in emb.tokenwise_embeddings.items()},
tokenwise_embedding_spans={k: v for k, v in emb.tokenwise_embedding_spans.items()},
)
else:
assert emb.prompt == emb_joined.prompt
emb_joined.tokenwise_embeddings = emb_joined.tokenwise_embeddings | emb.tokenwise_embeddings
emb_joined.tokenwise_embedding_spans = emb_joined.tokenwise_embedding_spans | emb.tokenwise_embedding_spans
return emb_joined
def get_tokenwise_mask(self, characterwise_mask: List[bool]) -> Dict[str, List[bool]]:
tokenwise_masks = {}
for k, t_embs, t_spans in ((k, self.tokenwise_embeddings[k], self.tokenwise_embedding_spans[k]) for k in
self.tokenwise_embeddings):
token_mask = [False] * len(t_embs)
for i_t, (t_span_start, t_span_end) in enumerate(t_spans):
if t_span_start != t_span_end:
m = characterwise_mask[t_span_start:t_span_end]
assert all(m) or not any(m), 'Inconsistent mask'
if all(m):
token_mask[i_t] = True
tokenwise_masks[k] = token_mask
return tokenwise_masks
class EmbeddingDelta(nn.Module):
def __init__(self, dims: Dict[str, int]) -> None:
super().__init__()
self.tokenwise_delta_front = nn.ParameterDict(
{k: nn.Parameter(torch.zeros(d), requires_grad=True) for k, d in dims.items()})
self.tokenwise_delta_side = nn.ParameterDict(
{k: nn.Parameter(torch.zeros(d), requires_grad=True) for k, d in dims.items()})
self.tokenwise_delta_back = nn.ParameterDict(
{k: nn.Parameter(torch.zeros(d), requires_grad=True) for k, d in dims.items()})
def apply_back(self, emb: PromptEmbedding, characterwise_mask: List[bool], alpha: float = 1.,
flag="") -> PromptEmbedding:
tokenwise_embeddings = {}
matching_keys = [k for k in self.tokenwise_delta_front if k in emb.tokenwise_embeddings]
directions = ['front', 'side', 'back']
for k in matching_keys:
t_embs = emb.tokenwise_embeddings[k]
t_spans = emb.tokenwise_embedding_spans[k]
projections = {direction: [] for direction in directions}
for direction in directions:
token_mask = [0] * len(t_embs)
for i_t, (t_span_start, t_span_end) in enumerate(t_spans):
if t_span_start != t_span_end:
m = characterwise_mask[t_span_start:t_span_end]
if all(m):
token_mask[i_t] = 1
delta_vector = getattr(self, f'tokenwise_delta_{direction}')[k].unsqueeze(0).to(
t_embs.dtype)
token_emb = t_embs[i_t].unsqueeze(0)
if direction == "front" or direction == "side" or direction == "back":
projection = (torch.sum(token_emb * delta_vector) / torch.sum(
delta_vector * delta_vector)) * delta_vector
t_embs[i_t] -= projection.squeeze(0)
tokenwise_embeddings[k] = (t_embs + alpha * torch.tensor(token_mask, dtype=t_embs.dtype,
device=t_embs.device).unsqueeze(-1) * \
self.tokenwise_delta_back[k].unsqueeze(0).to(t_embs.dtype))
return PromptEmbedding(
prompt=emb.prompt,
tokenwise_embeddings=tokenwise_embeddings,
tokenwise_embedding_spans=emb.tokenwise_embedding_spans,
)
def apply_side(self, emb: PromptEmbedding, characterwise_mask: List[bool], alpha: float = 1.,
flag="") -> PromptEmbedding:
tokenwise_embeddings = {}
matching_keys = [k for k in self.tokenwise_delta_front if k in emb.tokenwise_embeddings]
directions = ['front', 'side', 'back']
for k in matching_keys:
t_embs = emb.tokenwise_embeddings[k]
t_spans = emb.tokenwise_embedding_spans[k]
projections = {direction: [] for direction in directions}
for direction in directions:
token_mask = [0] * len(t_embs)
for i_t, (t_span_start, t_span_end) in enumerate(t_spans):
if t_span_start != t_span_end:
m = characterwise_mask[t_span_start:t_span_end]
if all(m):
token_mask[i_t] = 1
delta_vector = getattr(self, f'tokenwise_delta_{direction}')[k].unsqueeze(0).to(
t_embs.dtype)
token_emb = t_embs[i_t].unsqueeze(0)
if direction == "front" or direction == "side" or direction == "back":
projection = (torch.sum(token_emb * delta_vector) / torch.sum(
delta_vector * delta_vector)) * delta_vector
t_embs[i_t] -= projection.squeeze(0)
tokenwise_embeddings[k] = (t_embs + alpha * torch.tensor(token_mask, dtype=t_embs.dtype,
device=t_embs.device).unsqueeze(-1) * \
self.tokenwise_delta_side[k].unsqueeze(0).to(t_embs.dtype))
return PromptEmbedding(
prompt=emb.prompt,
tokenwise_embeddings=tokenwise_embeddings,
tokenwise_embedding_spans=emb.tokenwise_embedding_spans,
)
import re
from typing import List
def get_mask_regex(prompt: str, keyword: str, verbose: bool = True) -> List[bool]:
characterwise_mask = [False] * len(prompt)
num_matches = 0
for m in re.finditer(keyword, prompt):
num_matches += 1
characterwise_mask[m.span()[0]:m.span()[1]] = [True] * len(m.group(0))
return characterwise_mask
def get_mask(prompt: str, target: str, verbose: bool = True) -> List[bool]:
return get_mask_regex(prompt=prompt, keyword=re.escape(target), verbose=verbose)
def Token_spans(prompt: str) -> Dict[str, List[Tuple[int, int]]]:
SPECIAL_TOKENS = ['<|startoftext|>', '<|endoftext|>']
all_token_spans: Dict[str, List[Tuple[int, int]]] = {}
for name in {'clip_encoder'}:
d = guidance.tokenizer(prompt)
token_ids = d['input_ids']
vocab = guidance.tokenizer.get_vocab()
vocab_reverse = {v: k for k, v in vocab.items()}
tokens = [vocab_reverse[i] for i in token_ids]
token_spans = []
i = 0
skipped_chars = []
for token in tokens:
if token in SPECIAL_TOKENS:
token_spans.append((i, i))
continue
if token.endswith('</w>'):
token = token[:-len('</w>')]
while prompt[i] != token[0]:
skipped_chars.append(prompt[i])
i += 1
assert i + len(token) <= len(prompt)
assert prompt[i:(i + len(token))] == token, 'Misaligned'
token_spans.append((i, i + len(token)))
i += len(token)
skipped_chars = ''.join(skipped_chars) + prompt[i:]
assert not any(l.isalpha() for l in
skipped_chars), f'Failed to assign some alphabetical characters to tokens for tokenizer {name}. Prompt: "{prompt}", unassigned characters: "{skipped_chars}".'
all_token_spans[name] = token_spans
return all_token_spans
def adjust_text_embeddings(embeddings, azimuth, guidance_opt, iteration=0):
text_z_list = []
weights_list = []
K = 0
text_z_, weights_ = get_pos_neg_text_embeddings(embeddings, azimuth, guidance_opt, iteration=iteration)
text_z_depth, weights_depth = get_pos_neg_text_embeddings(embeddings, azimuth, guidance_opt)
K = max(K, weights_.shape[0])
text_z_list.append(text_z_)
weights_list.append(weights_)
text_embeddings = []
for i in range(K):
for text_z in text_z_list:
text_embeddings.append(text_z[i] if i < len(text_z) else text_z[0])
text_embeddings = torch.stack(text_embeddings, dim=0) # [B * K, 77, 768]
weights = []
for i in range(K):
for weights_ in weights_list:
weights.append(weights_[i] if i < len(weights_) else torch.zeros_like(weights_[0]))
weights = torch.stack(weights, dim=0)
return text_embeddings, weights
def get_pos_neg_text_embeddings(embeddings, azimuth_val, opt, iteration=0):
if azimuth_val >= -90 and azimuth_val < 90:
if azimuth_val >= 0:
r = 1 - azimuth_val / 90
else:
r = 1 + azimuth_val / 90
if random.random() < 0.7 and iteration < 5000:
embs = delta.apply_side(emb_side, characterwise_mask_side, alpha=(1 - r), flag="side")
embeddings['side'] = embs.tokenwise_embeddings["clip_encoder"].unsqueeze(0)
start_z = embeddings['front']
end_z = embeddings['side']
pos_z = r * start_z + (1 - r) * end_z
text_z = torch.cat([pos_z, embeddings['front'], embeddings['side']], dim=0)
if r > 0.8:
front_neg_w = 0.0
else:
front_neg_w = math.exp(-r * opt.front_decay_factor) * opt.negative_w
if r < 0.2:
side_neg_w = 0.0
else:
side_neg_w = math.exp(-(1 - r) * opt.side_decay_factor) * opt.negative_w
weights = torch.tensor([1.0, front_neg_w, side_neg_w])
else:
if azimuth_val >= 0:
r = 1 - (azimuth_val - 90) / 90
else:
r = 1 + (azimuth_val + 90) / 90
if random.random() < 0.7 and iteration < 5000:
embs1 = delta.apply_back(emb_back, characterwise_mask_back, alpha=2 * (1 - r), flag="back")
embs2 = delta.apply_side(emb_side, characterwise_mask_side, alpha=r, flag="side")
embeddings['back'] = embs1.tokenwise_embeddings["clip_encoder"].unsqueeze(0)
embeddings['side'] = embs2.tokenwise_embeddings["clip_encoder"].unsqueeze(0)
start_z = embeddings['side']
end_z = embeddings['back']
pos_z = r * start_z + (1 - r) * end_z
text_z = torch.cat([pos_z, embeddings['side'], embeddings['front']], dim=0)
front_neg_w = opt.negative_w
if r > 0.8:
side_neg_w = 0.0
else:
side_neg_w = math.exp(-r * opt.side_decay_factor) * opt.negative_w / 2
weights = torch.tensor([1.0, side_neg_w, front_neg_w])
return text_z, weights.to(text_z.device)
def prepare_embeddings(guidance_opt, guidance, vdm_prompts):
embeddings = {}
embeddings['default'] = guidance.get_text_embeds([guidance_opt.text])
embeddings['uncond'] = guidance.get_text_embeds([guidance_opt.negative])
embeddings['none'] = guidance.get_text_embeds("")
for d in ['front', 'side', 'back']:
embeddings[d] = guidance.get_text_embeds([f"{guidance_opt.text}, {d} view"])
embeddings[f'prompt_{d}'] = guidance.get_text_embeds([f"{d} view of {vdm_prompts['prompt_target']}"])
embeddings['prompt_target'] = guidance.get_text_embeds(vdm_prompts['prompt_target'])
embeddings['inverse_text'] = guidance.get_text_embeds(guidance_opt.inverse_text)
return embeddings
def guidance_setup(guidance_opt, vdm_prompts):
if guidance_opt.guidance == "SD":
guidance = StableDiffusion(guidance_opt.g_device, guidance_opt.fp16, guidance_opt.vram_O,
guidance_opt.t_range, guidance_opt.max_t_range,
num_train_timesteps=guidance_opt.num_train_timesteps,
ddim_inv=guidance_opt.ddim_inv,
textual_inversion_path=guidance_opt.textual_inversion_path,
LoRA_path=guidance_opt.LoRA_path,
guidance_opt=guidance_opt)
else:
raise ValueError(f'{guidance_opt.guidance} not supported.')
if guidance is not None:
for p in guidance.parameters():
p.requires_grad = False
embeddings = prepare_embeddings(guidance_opt, guidance, vdm_prompts)
return guidance, embeddings
def prepare_output_and_logger(args):
if not args._model_path:
if os.getenv('OAR_JOB_ID'):
unique_str = os.getenv('OAR_JOB_ID')
else:
unique_str = str(uuid.uuid4())
args._model_path = os.path.join("./output/", args.workspace)
# Set up output folder
print("Output folder: {}".format(args._model_path))
os.makedirs(args._model_path, exist_ok=True)
# copy configs
if args.opt_path is not None:
os.system(' '.join(['cp', args.opt_path, os.path.join(args._model_path, 'config.yaml')]))
with open(os.path.join(args._model_path, "cfg_args"), 'w') as cfg_log_f:
cfg_log_f.write(str(Namespace(**vars(args))))
# Create Tensorboard writer
tb_writer = None
if TENSORBOARD_FOUND:
tb_writer = SummaryWriter(args._model_path)
else:
print("Tensorboard not available: not logging progress")
return tb_writer
def training_report(tb_writer, iteration, elapsed, testing_iterations, scene: Scene, renderFunc, renderArgs):
if tb_writer:
tb_writer.add_scalar('iter_time', elapsed, iteration)
# Report test and samples of training set
if iteration in testing_iterations:
save_folder = os.path.join(scene.args._model_path, "test_six_views/{}_iteration".format(iteration))
if not os.path.exists(save_folder):
os.makedirs(save_folder)
print('test views is in :', save_folder)
torch.cuda.empty_cache()
config = ({'name': 'test', 'cameras': scene.getTestCameras()})
if config['cameras'] and len(config['cameras']) > 0:
for idx, viewpoint in enumerate(config['cameras']):
render_out = renderFunc(viewpoint, scene.gaussians, *renderArgs, test=True)
rgb, depth = render_out["render"], render_out["depth"]
if depth is not None:
depth_norm = depth / depth.max()
save_image(depth_norm, os.path.join(save_folder, "render_depth_{}.png".format(viewpoint.uid)))
image = torch.clamp(rgb, 0.0, 1.0)
save_image(image, os.path.join(save_folder, "render_view_{}.png".format(viewpoint.uid)))
if tb_writer:
tb_writer.add_images(config['name'] + "_view_{}/render".format(viewpoint.uid), image[None],
global_step=iteration)
print("\n[ITER {}] Eval Done!".format(iteration))
if tb_writer:
tb_writer.add_histogram("scene/opacity_histogram", scene.gaussians.get_opacity, iteration)
tb_writer.add_scalar('total_points', scene.gaussians.get_xyz.shape[0], iteration)
torch.cuda.empty_cache()
def video_inference(iteration, scene: Scene, renderFunc, renderArgs):
sharp = T.RandomAdjustSharpness(3, p=1.0)
save_folder = os.path.join(scene.args._model_path, "videos/{}_iteration".format(iteration))
if not os.path.exists(save_folder):
os.makedirs(save_folder) # makedirs
print('videos is in :', save_folder)
torch.cuda.empty_cache()
config = ({'name': 'test', 'cameras': scene.getCircleVideoCameras()})
if config['cameras'] and len(config['cameras']) > 0:
img_frames = []
depth_frames = []
print("Generating Video using", len(config['cameras']), "different view points")
for idx, viewpoint in enumerate(config['cameras']):
render_out = renderFunc(viewpoint, scene.gaussians, *renderArgs, test=True)
rgb, depth = render_out["render"], render_out["depth"]
if depth is not None:
depth_norm = depth / depth.max()
depths = torch.clamp(depth_norm, 0.0, 1.0)
depths = depths.detach().cpu().permute(1, 2, 0).numpy()
depths = (depths * 255).round().astype('uint8')
depth_frames.append(depths)
image = torch.clamp(rgb, 0.0, 1.0)
image = image.detach().cpu().permute(1, 2, 0).numpy()
image = (image * 255).round().astype('uint8')
img_frames.append(image)
# Img to Numpy
imageio.mimwrite(os.path.join(save_folder, "video_rgb_{}.mp4".format(iteration)), img_frames, fps=30, quality=8)
if len(depth_frames) > 0:
imageio.mimwrite(os.path.join(save_folder, "video_depth_{}.mp4".format(iteration)), depth_frames, fps=30,
quality=8)
print("\n[ITER {}] Video Save Done!".format(iteration))
torch.cuda.empty_cache()
def latent2image(vae, latents):
latents = 1 / 0.18215 * latents
image = vae.decode(latents)['sample']
image = (image / 2 + 0.5).clamp(0, 1)
image = image.cpu().permute(0, 2, 3, 1).numpy()
image = (image * 255).astype(np.uint8)
return image
def training(guidance, embeddings, tokenizer, dataset, opt, pipe, gcams, guidance_opt, testing_iterations,
saving_iterations, checkpoint_iterations, checkpoint, debug_from, save_video, iteration_flag1,
iteration_flag2):
first_iter = 0
tb_writer = prepare_output_and_logger(dataset)
gaussians = GaussianModel(dataset.sh_degree)
scene = Scene(dataset, gcams, gaussians)
gaussians.training_setup(opt)
if checkpoint:
(model_params, first_iter) = torch.load(checkpoint)
gaussians.restore(model_params, opt)
print(f"use checkpoint, first_iter = {first_iter}")
bg_color = [1, 1, 1] if dataset._white_background else [0, 0, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device=dataset.data_device)
iter_start = torch.cuda.Event(enable_timing=True)
iter_end = torch.cuda.Event(enable_timing=True)
save_folder = os.path.join(dataset._model_path, "train_process/")
if not os.path.exists(save_folder):
os.makedirs(save_folder) # makedirs
print('train_process is in :', save_folder)
ema_loss_for_log = 0.0
progress_bar = tqdm(range(first_iter, opt.iterations), desc="Training progress")
first_iter += 1
if opt.save_process:
save_folder_proc = os.path.join(scene.args._model_path, "process_videos/")
if not os.path.exists(save_folder_proc):
os.makedirs(save_folder_proc) # makedirs
process_view_points = scene.getCircleVideoCameras(batch_size=opt.pro_frames_num,
render45=opt.pro_render_45).copy()
save_process_iter = opt.iterations // len(process_view_points)
pro_img_frames = []
for iteration in range(first_iter, opt.iterations + 1):
if network_gui.conn == None:
network_gui.try_connect()
while network_gui.conn != None:
try:
net_image_bytes = None
custom_cam, do_training, pipe.convert_SHs_python, pipe.compute_cov3D_python, keep_alive, scaling_modifer = network_gui.receive()
if custom_cam != None:
net_image = render(custom_cam, gaussians, pipe, background, scaling_modifer)["render"]
net_image_bytes = memoryview((torch.clamp(net_image, min=0, max=1.0) * 255).byte().permute(1, 2,
0).contiguous().cpu().numpy())
network_gui.send(net_image_bytes, guidance_opt.text)
if do_training and ((iteration < int(opt.iterations)) or not keep_alive):
break