-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvae_trainer.py
1299 lines (1122 loc) · 52.8 KB
/
vae_trainer.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
import logging
import os
import random
import click
import numpy as np
import torch
import torch.distributed as dist
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision.transforms as transforms
import webdataset as wds
from torch.nn.parallel import DistributedDataParallel as DDP
from torchvision.transforms import GaussianBlur
from transformers import get_cosine_schedule_with_warmup
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
import wandb
from vae import VAE
from utils import LPIPS, PatchDiscriminator, prepare_filter
import time
class GradNormFunction(torch.autograd.Function):
"""
GradNormFunction 类,实现了一个自定义的梯度归一化操作。
该函数在前向传播时返回输入张量的副本,在反向传播时对梯度进行归一化处理。
归一化过程包括计算梯度的范数,并根据给定的权重对其进行缩放。
"""
@staticmethod
def forward(ctx, x, weight):
"""
前向传播方法。
参数:
ctx: 上下文对象,用于在反向传播时传递信息。
x (Tensor): 输入张量。
weight (Tensor): 用于梯度归一化的权重张量。
返回:
Tensor: 输入张量的副本。
"""
# 将权重保存到上下文中,以便在反向传播时使用
ctx.save_for_backward(weight)
# 返回输入张量的副本
return x.clone()
@staticmethod
def backward(ctx, grad_output):
"""
反向传播方法,对梯度进行归一化处理。
参数:
ctx: 上下文对象,包含前向传播时保存的权重。
grad_output (Tensor): 从后续层传递回来的梯度。
返回:
Tuple[Tensor, None]: 归一化后的梯度张量和 None(因为输入张量没有梯度)。
"""
# 从上下文中获取保存的权重
weight = ctx.saved_tensors[0]
# 计算梯度输出的 L2 范数(跨所有维度),然后计算所有样本的平均值
# grad_output_norm = torch.linalg.vector_norm(
# grad_output, dim=list(range(1, len(grad_output.shape))), keepdim=True
# ).mean()
# 计算梯度的 L2 范数并取平均值
grad_output_norm = torch.norm(grad_output).mean().item()
# 在所有节点上对梯度范数进行平均(假设使用分布式训练)
# nccl over all nodes
grad_output_norm = avg_scalar_over_nodes(
grad_output_norm, device=grad_output.device
)
# 对梯度进行归一化处理:grad_output_normalized = weight * grad_output / (grad_output_norm + 1e-8)
grad_output_normalized = weight * grad_output / (grad_output_norm + 1e-8)
# 返回归一化后的梯度张量和 None(因为输入张量没有梯度)
return grad_output_normalized, None
def gradnorm(x, weight=1.0):
"""
对输入张量应用梯度归一化。
参数:
x (Tensor): 输入张量。
weight (float, optional): 用于梯度归一化的权重,默认为1.0。
返回:
Tensor: 应用梯度归一化后的张量。
"""
# 将权重转换为与输入张量相同设备的张量
weight = torch.tensor(weight, device=x.device)
# 应用自定义的梯度归一化函数
return GradNormFunction.apply(x, weight)
# 禁用梯度计算,节省内存
@torch.no_grad()
def avg_scalar_over_nodes(value: float, device):
"""
在所有节点上对标量值进行平均。
该函数用于分布式训练中,将每个节点的标量值进行平均。
参数:
value (float): 要平均的标量值。
device (torch.device): 计算设备。
返回:
float: 平均后的标量值。
"""
# 将标量值转换为张量,并移动到指定设备
value = torch.tensor(value, device=device)
# 在所有节点上执行平均操作
dist.all_reduce(value, op=dist.ReduceOp.AVG)
# 返回标量值
return value.item()
def gan_disc_loss(real_preds, fake_preds, disc_type="bce"):
"""
计算生成对抗网络(GAN)判别器的损失。
该函数根据判别器类型计算真实样本和生成样本的损失,并返回平均损失、真实预测的平均值、生成预测的平均值以及准确率。
参数:
real_preds (Tensor): 判别器对真实样本的预测输出。
fake_preds (Tensor): 判别器对生成样本的预测输出。
disc_type (str, optional): 判别器类型,默认为 'bce'(二元交叉熵)。可选值为 'hinge'。
返回:
Tuple[Tensor, float, float, float]: 平均损失、真实预测的平均值、生成预测的平均值以及准确率。
"""
if disc_type == "bce":
# 计算真实样本的二元交叉熵损失
real_loss = nn.functional.binary_cross_entropy_with_logits(
real_preds, torch.ones_like(real_preds)
)
# 计算生成样本的二元交叉熵损失
fake_loss = nn.functional.binary_cross_entropy_with_logits(
fake_preds, torch.zeros_like(fake_preds)
)
# 计算真实预测的平均值
avg_real_preds = real_preds.mean().item()
# 计算生成预测的平均值
avg_fake_preds = fake_preds.mean().item()
# 计算准确率
with torch.no_grad(): # 禁用梯度计算
acc = (real_preds > 0).sum().item() + (fake_preds < 0).sum().item()
acc = acc / (real_preds.numel() + fake_preds.numel())
if disc_type == "hinge":
# 计算真实样本的 Hinge 损失
real_loss = nn.functional.relu(1 - real_preds).mean()
# 计算生成样本的 Hinge 损失
fake_loss = nn.functional.relu(1 + fake_preds).mean()
# 计算准确率
with torch.no_grad():
acc = (real_preds > 0).sum().item() + (fake_preds < 0).sum().item()
acc = acc / (real_preds.numel() + fake_preds.numel())
# 计算真实预测的平均值
avg_real_preds = real_preds.mean().item()
# 计算生成预测的平均值
avg_fake_preds = fake_preds.mean().item()
# 返回平均损失、真实预测的平均值、生成预测的平均值以及准确率
return (real_loss + fake_loss) * 0.5, avg_real_preds, avg_fake_preds, acc
MAX_WIDTH = 512
# 定义一个标准的图像变换组合
this_transform = transforms.Compose(
[
transforms.ToTensor(), # 将 PIL 图像或 NumPy 数组转换为张量,并归一化到 [0, 1]
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), # 标准化张量,使其均值为0,标准差为1
transforms.CenterCrop(512), # 中心裁剪图像到 512x512
transforms.Resize(MAX_WIDTH), # 调整图像大小到 MAX_WIDTH(512)
]
)
def this_transform_random_crop_resize(x, width=MAX_WIDTH):
"""
对输入图像应用随机裁剪或调整大小。
该函数首先将输入转换为张量并标准化,然后以 50% 的概率随机选择裁剪或调整大小。
如果选择裁剪,则随机裁剪图像到指定宽度;否则,先调整大小到指定宽度,再随机裁剪。
参数:
x: 输入图像,可以是 PIL 图像、NumPy 数组或张量
width (int, optional): 裁剪或调整大小的目标宽度,默认为 MAX_WIDTH(512)
返回:
Tensor: 变换后的图像张量
"""
# 将输入转换为张量,并归一化到 [0, 1]
x = transforms.ToTensor()(x)
# 标准化张量,使其均值为0,标准差为1
x = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])(x)
if random.random() < 0.5:
# 如果随机数小于0.5,则进行随机裁剪
x = transforms.RandomCrop(width)(x)
else:
# 否则,先调整大小到指定宽度,再进行随机裁剪
x = transforms.Resize(width)(x)
x = transforms.RandomCrop(width)(x)
# 返回变换后的图像张量
return x
def create_dataloader(url, batch_size, num_workers, do_shuffle=True, just_resize=False):
"""
创建一个数据加载器,用于从指定的 URL 加载数据。
该函数使用 WebDataset 从指定的 URL 加载数据,并应用相应的图像变换。
如果 `just_resize` 为 True,则仅调整图像大小;否则,应用随机裁剪或调整大小。
参数:
url (str): 数据集的 URL
batch_size (int): 每个批次的样本数量
num_workers (int): 用于数据加载的子进程数量
do_shuffle (bool, optional): 是否打乱数据,默认为 True
just_resize (bool, optional): 是否仅调整图像大小,默认为 False
返回:
WebLoader: 创建的数据加载器
"""
# 创建 WebDataset 对象,指定数据集的 URL,并使用节点分割器和工作者分割器
dataset = wds.WebDataset(
url, nodesplitter=wds.split_by_node, workersplitter=wds.split_by_worker
)
# 如果需要打乱数据,则对数据集进行打乱操作
dataset = dataset.shuffle(1000) if do_shuffle else dataset
# 解码图像为 RGB 格式,并将图像和标签组合成元组
dataset = (
dataset.decode("rgb")
.to_tuple("jpg;png")
.map_tuple(
this_transform_random_crop_resize if not just_resize else this_transform # 根据参数选择变换
)
)
# 创建 WebLoader 对象,加载数据集
loader = wds.WebLoader(
dataset,
batch_size=batch_size, # 设置批次大小
shuffle=False, # 在 WebLoader 中不进行打乱,因为已经在 WebDataset 中打乱
num_workers=num_workers, # 设置子进程数量
pin_memory=True, # 启用内存固定,加速数据传输
)
# 返回创建的数据加载器
return loader
def blurriness_heatmap(input_image):
"""
生成输入图像的模糊度热图。
该函数通过计算图像的边缘响应,并将其转换为模糊度度量,生成模糊度热图。
参数:
input_image (Tensor): 输入图像张量,形状为 (B, C, H, W)
返回:
Tensor: 模糊度热图张量,形状为 (B, 3, H, W)
"""
# 将输入图像转换为灰度图像
# 在通道维度上求均值,得到形状为 (B, 1, H, W)
grayscale_image = input_image.mean(dim=1, keepdim=True)
# 定义拉普拉斯卷积核,用于检测边缘
laplacian_kernel = torch.tensor(
[
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, -20, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
],
dtype=torch.float32,
)
# 重塑为 (1, 1, 5, 5) 的张量
laplacian_kernel = laplacian_kernel.view(1, 1, 5, 5)
# 将卷积核移动到与输入图像相同的设备
laplacian_kernel = laplacian_kernel.to(input_image.device)
# 计算边缘响应
# 应用卷积,填充宽度为2
edge_response = F.conv2d(grayscale_image, laplacian_kernel, padding=2)
# 对边缘响应应用高斯模糊,以平滑边缘
# 应用高斯模糊
edge_magnitude = GaussianBlur(kernel_size=(13, 13), sigma=(2.0, 2.0))(
edge_response.abs()
)
# 将边缘幅度归一化到 [0, 1] 范围
edge_magnitude = (edge_magnitude - edge_magnitude.min()) / (
edge_magnitude.max() - edge_magnitude.min() + 1e-8
)
# 计算模糊度映射,模糊度越高,值越接近1
blurriness_map = 1 - edge_magnitude
# 将模糊度映射中小于0.8的部分设为0,其余部分保持不变
blurriness_map = torch.where(
blurriness_map < 0.8, torch.zeros_like(blurriness_map), blurriness_map
)
# 将模糊度映射在通道维度上重复3次,以匹配RGB图像的通道数
return blurriness_map.repeat(1, 3, 1, 1) # 返回形状为 (B, 3, H, W) 的模糊度热图
def vae_loss_function(x, x_reconstructed, z, do_pool=True, do_recon=False):
"""
计算变分自编码器(VAE)的损失函数。
该函数计算重建损失(reconstruction loss)和潜在空间损失(latent space loss),并返回总损失。
可选的参数允许选择是否进行下采样以及是否计算重建损失。
参数:
x (Tensor): 原始输入图像张量,形状为 (B, C, H, W)
x_reconstructed (Tensor): 重建后的图像张量,形状为 (B, C, H, W)
z (Tensor): 潜在空间表示张量,形状为 (B, Z_dim)
do_pool (bool, optional): 是否对图像进行下采样,默认为 True
do_recon (bool, optional): 是否计算重建损失,默认为 False
返回:
Tuple[Tensor, Dict[str, float]]: 总损失和包含各项损失的字典
"""
# 如果需要计算重建损失
if do_recon:
if do_pool:
# 对重建图像和原始图像进行下采样,下采样因子为1/16(即缩小16倍)
x_reconstructed_down = F.interpolate(
x_reconstructed, scale_factor=1 / 16, mode="area"
)
x_down = F.interpolate(x, scale_factor=1 / 16, mode="area")
# 计算重建损失,使用 L1 损失(绝对误差均值)
recon_loss = ((x_reconstructed_down - x_down)).abs().mean()
else:
# 如果不需要下采样,直接使用原始图像和重建图像计算重建损失
x_reconstructed_down = x_reconstructed
x_down = x
# 使用模糊度热图对重建损失进行加权
recon_loss = (
((x_reconstructed_down - x_down) * blurriness_heatmap(x_down))
.abs()
.mean()
)
# 获取重建损失的标量值
recon_loss_item = recon_loss.item()
else:
# 如果不需要计算重建损失,则将重建损失设为0
recon_loss = 0
recon_loss_item = 0
# 计算潜在空间损失的逐元素均值
elewise_mean_loss = z.pow(2)
zloss = elewise_mean_loss.mean()
# 在不计算梯度的情况下,计算实际的均值损失和 KL 散度损失
with torch.no_grad():
actual_mean_loss = elewise_mean_loss.mean()
actual_ks_loss = actual_mean_loss.mean()
# 计算总损失,重建损失的权重为0,潜在空间损失的权重为0.1
vae_loss = recon_loss * 0.0 + zloss * 0.1
# 返回总损失和包含各项损失的字典
return vae_loss, {
"recon_loss": recon_loss_item, # 重建损失的标量值
"kl_loss": actual_ks_loss.item(), # KL 散度损失的标量值
"average_of_abs_z": z.abs().mean().item(), # 潜在空间表示绝对值的均值
"std_of_abs_z": z.abs().std().item(), # 潜在空间表示绝对值的标准差
"average_of_logvar": 0.0, # 对数方差均值的占位符
"std_of_logvar": 0.0, # 对数方差标准差的占位符
}
def cleanup():
"""
清理分布式训练环境。
该函数销毁进程组,释放分布式训练相关的资源。
"""
# 销毁进程组,清理分布式训练环境
dist.destroy_process_group()
@click.command()
@click.option(
"--dataset_url", type=str, default="", help="URL for the training dataset"
)
@click.option(
"--test_dataset_url", type=str, default="", help="URL for the test dataset"
)
@click.option("--num_epochs", type=int, default=2, help="Number of training epochs")
@click.option("--batch_size", type=int, default=8, help="Batch size for training")
@click.option("--do_ganloss", is_flag=True, help="Whether to use GAN loss")
@click.option(
"--learning_rate_vae", type=float, default=1e-5, help="Learning rate for VAE"
)
@click.option(
"--learning_rate_disc",
type=float,
default=2e-4,
help="Learning rate for discriminator",
)
@click.option("--vae_resolution", type=int, default=256, help="Resolution for VAE")
@click.option("--vae_in_channels", type=int, default=3, help="Input channels for VAE")
@click.option("--vae_ch", type=int, default=256, help="Base channel size for VAE")
@click.option(
"--vae_ch_mult", type=str, default="1,2,4,4", help="Channel multipliers for VAE"
)
@click.option(
"--vae_num_res_blocks",
type=int,
default=2,
help="Number of residual blocks for VAE",
)
@click.option(
"--vae_z_channels", type=int, default=16, help="Number of latent channels for VAE"
)
@click.option("--run_name", type=str, default="run", help="Name of the run for wandb")
@click.option(
"--max_steps", type=int, default=1000, help="Maximum number of steps to train for"
)
@click.option(
"--evaluate_every_n_steps", type=int, default=250, help="Evaluate every n steps"
)
@click.option("--load_path", type=str, default=None, help="Path to load the model from")
@click.option("--do_clamp", is_flag=True, help="Whether to clamp the latent codes")
@click.option(
"--clamp_th", type=float, default=8.0, help="Clamp threshold for the latent codes"
)
@click.option(
"--max_spatial_dim",
type=int,
default=256,
help="Maximum spatial dimension for overall training",
)
@click.option(
"--do_attn", type=bool, default=False, help="Whether to use attention in the VAE"
)
@click.option(
"--decoder_also_perform_hr",
type=bool,
default=False,
help="Whether to perform HR decoding in the decoder",
)
@click.option(
"--project_name",
type=str,
default="vae_sweep_attn_lr_width",
help="Project name for wandb",
)
@click.option(
"--crop_invariance",
type=bool,
default=False,
help="Whether to perform crop invariance",
)
@click.option(
"--flip_invariance",
type=bool,
default=False,
help="Whether to perform flip invariance",
)
@click.option(
"--do_compile",
type=bool,
default=False,
help="Whether to compile the model",
)
@click.option(
"--use_wavelet",
type=bool,
default=False,
help="Whether to use wavelet transform in the encoder",
)
@click.option(
"--augment_before_perceptual_loss",
type=bool,
default=False,
help="Whether to augment the images before the perceptual loss",
)
@click.option(
"--downscale_factor",
type=int,
default=16,
help="Downscale factor for the latent space",
)
@click.option(
"--use_lecam",
type=bool,
default=False,
help="Whether to use Lecam",
)
@click.option(
"--disc_type",
type=str,
default="bce",
help="Discriminator type",
)
def train_ddp(
dataset_url,
test_dataset_url,
num_epochs,
batch_size,
do_ganloss,
learning_rate_vae,
learning_rate_disc,
vae_resolution,
vae_in_channels,
vae_ch,
vae_ch_mult,
vae_num_res_blocks,
vae_z_channels,
run_name,
max_steps,
evaluate_every_n_steps,
load_path,
do_clamp,
clamp_th,
max_spatial_dim,
do_attn,
decoder_also_perform_hr,
project_name,
crop_invariance,
flip_invariance,
do_compile,
use_wavelet,
augment_before_perceptual_loss,
downscale_factor,
use_lecam,
disc_type,
):
"""
训练函数,使用分布式数据并行(DDP)进行模型训练。
参数:
dataset_url (str): 训练数据集的URL。
test_dataset_url (str): 测试数据集的URL。
num_epochs (int): 训练的轮数。
batch_size (int): 每个批次的大小。
do_ganloss (bool): 是否使用生成对抗网络损失。
learning_rate_vae (float): VAE的学习率。
learning_rate_disc (float): 判别器的学习率。
vae_resolution (int): VAE的分辨率。
vae_in_channels (int): VAE输入通道数。
vae_ch (int): VAE基础通道数。
vae_ch_mult (str): VAE通道数倍数(以逗号分隔的字符串)。
vae_num_res_blocks (int): VAE残差块的数量。
vae_z_channels (int): VAE隐空间通道数。
run_name (str): 运行名称,用于记录和标识。
max_steps (int): 最大训练步数。
evaluate_every_n_steps (int): 每隔多少步进行一次评估。
load_path (str): 模型加载路径(如果需要加载预训练模型)。
do_clamp (bool): 是否进行梯度裁剪。
clamp_th (float): 梯度裁剪的阈值。
max_spatial_dim (int): 最大空间维度。
do_attn (bool): 是否使用注意力机制。
decoder_also_perform_hr (bool): 解码器是否也执行高分辨率操作。
project_name (str): Weights & Biases项目名称。
crop_invariance (bool): 裁剪不变性(是否启用)。
flip_invariance (bool): 翻转不变性(是否启用)。
do_compile (bool): 是否进行模型编译以加速。
use_wavelet (bool): 是否使用小波变换。
augment_before_perceptual_loss (bool): 在感知损失前是否进行数据增强。
downscale_factor (float): 下采样因子。
use_lecam (bool): 是否使用LECAM(局部能量约束对抗性方法)。
disc_type (str): 判别器类型。
"""
# 设置随机种子以确保结果的可重复性
torch.manual_seed(42)
torch.cuda.manual_seed(42)
torch.cuda.manual_seed_all(42)
np.random.seed(42)
random.seed(42)
# 定义训练和测试数据集的索引范围
start_train = 0
# 假设每个tar文件包含128个样本,16个tar文件用于训练
end_train = 128 * 16
start_test = end_train + 1
end_test = start_test + 8 # 8个tar文件用于测试
# 根据索引范围构建训练和测试数据集的URL
dataset_url = f"/flux_ipadapter_trainer/dataset/art_webdataset/{{{start_train:05d}..{end_train:05d}}}.tar"
test_dataset_url = f"/flux_ipadapter_trainer/dataset/art_webdataset/{{{start_test:05d}..{end_test:05d}}}.tar"
# 检查CUDA是否可用,因为DDP需要GPU支持
assert torch.cuda.is_available(), "CUDA is required for DDP"
# 初始化分布式进程组
dist.init_process_group(backend="nccl")
# 获取当前进程的排名和本地排名,以及总进程数
ddp_rank = int(os.environ["RANK"])
ddp_local_rank = int(os.environ["LOCAL_RANK"])
world_size = int(os.environ["WORLD_SIZE"])
# 设置当前设备
device = f"cuda:{ddp_local_rank}"
torch.cuda.set_device(device)
# 主进程标识
master_process = ddp_rank == 0
print(f"using device: {device}")
# 如果是主进程,则初始化Weights & Biases(wandb)以进行监控和记录
if master_process:
wandb.init(
project=project_name,
entity="simo",
name=run_name,
config={
"learning_rate_vae": learning_rate_vae,
"learning_rate_disc": learning_rate_disc,
"vae_ch": vae_ch,
"vae_resolution": vae_resolution,
"vae_in_channels": vae_in_channels,
"vae_ch_mult": vae_ch_mult,
"vae_num_res_blocks": vae_num_res_blocks,
"vae_z_channels": vae_z_channels,
"batch_size": batch_size,
"num_epochs": num_epochs,
"do_ganloss": do_ganloss,
"do_attn": do_attn,
"use_wavelet": use_wavelet,
},
)
# 初始化VAE模型,并将其移动到GPU
vae = VAE(
resolution=vae_resolution,
in_channels=vae_in_channels,
ch=vae_ch,
out_ch=vae_in_channels,
ch_mult=[int(x) for x in vae_ch_mult.split(",")],
num_res_blocks=vae_num_res_blocks,
z_channels=vae_z_channels,
use_attn=do_attn,
decoder_also_perform_hr=decoder_also_perform_hr,
use_wavelet=use_wavelet,
).cuda()
# 初始化判别器模型,并将其移动到GPU
discriminator = PatchDiscriminator().cuda()
discriminator.requires_grad_(True)
# 使用DDP包装VAE模型以支持分布式训练
vae = DDP(vae, device_ids=[ddp_rank])
# 准备过滤器(具体实现未提供,假设用于数据预处理)
prepare_filter(device)
# 如果需要进行编译优化
if do_compile:
# 使用torch.compile编译VAE的编码器,设置fullgraph=False,模式为"max-autotune"
vae.module.encoder = torch.compile(
vae.module.encoder, fullgraph=False, mode="max-autotune"
)
# 使用torch.compile编译VAE的解码器,设置fullgraph=False,模式为"max-autotune"
vae.module.decoder = torch.compile(
vae.module.decoder, fullgraph=False, mode="max-autotune"
)
# 使用分布式数据并行(DDP)包装判别器,指定设备ID为ddp_rank
discriminator = DDP(discriminator, device_ids=[ddp_rank])
# 设置混合精度上下文,使用cuda设备,精度为bfloat16
ctx = torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16)
# 定义生成器(VAE)的优化器,使用AdamW算法
optimizer_G = optim.AdamW(
[
# 对于VAE中不包含"conv_in"的参数,学习率为learning_rate_vae / vae_ch
{
"params": [p for n, p in vae.named_parameters() if "conv_in" not in n],
"lr": learning_rate_vae / vae_ch,
},
# 对于VAE中包含"conv_in"的参数,学习率为1e-4
{
"params": [p for n, p in vae.named_parameters() if "conv_in" in n],
"lr": 1e-4,
},
],
# 设置权重衰减为1e-3
weight_decay=1e-3,
# 设置AdamW的beta参数为(0.9, 0.95)
betas=(0.9, 0.95),
)
# 定义判别器的优化器,使用AdamW算法
optimizer_D = optim.AdamW(
discriminator.parameters(), # 优化判别器的所有参数
lr=learning_rate_disc, # 学习率为learning_rate_disc
weight_decay=1e-3, # 权重衰减为1e-3
betas=(0.9, 0.95), # AdamW的beta参数为(0.9, 0.95)
)
# 初始化LPIPS(感知损失)模型,并将其移动到CUDA设备
lpips = LPIPS().cuda()
# 创建训练数据加载器
dataloader = create_dataloader(
dataset_url, batch_size, num_workers=4, do_shuffle=True
)
# 创建测试数据加载器
test_dataloader = create_dataloader(
test_dataset_url, batch_size, num_workers=4, do_shuffle=False, just_resize=True
)
# 计算总的训练步数
num_training_steps = max_steps
# 设置预热步数为200
num_warmup_steps = 200
# 使用余弦学习率调度器,并包含预热阶段
lr_scheduler = get_cosine_schedule_with_warmup(
optimizer_G, num_warmup_steps, num_training_steps
)
# 设置日志记录器
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
if master_process:
# 创建控制台处理器
handler = logging.StreamHandler()
# 设置日志格式
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler.setFormatter(formatter)
# 将处理器添加到日志记录器
logger.addHandler(handler)
# 初始化全局步数为0
global_step = 0
# 如果提供了加载路径,则加载模型状态
if load_path is not None:
state_dict = torch.load(load_path, map_location="cpu")
try:
# 尝试严格加载VAE模型的状态
status = vae.load_state_dict(state_dict, strict=True)
except Exception as e:
print(e)
# 如果严格加载失败,则替换键名中的"_orig_mod."为"",然后再次尝试加载
state_dict = {k.replace("_orig_mod.", ""): v for k, v in state_dict.items()}
status = vae.load_state_dict(state_dict, strict=True)
print(status)
# 记录开始时间
t0 = time.time()
# 初始化LeCam损失相关的变量
lecam_loss_weight = 0.1
lecam_anchor_real_logits = 0.0
lecam_anchor_fake_logits = 0.0
lecam_beta = 0.9
# 开始训练循环
for epoch in range(num_epochs):
for i, real_images_hr in enumerate(dataloader):
# 计算数据加载所花费的时间
time_taken_till_load = time.time() - t0
# 重置开始时间
t0 = time.time()
# 将高分辨率真实图像移动到指定设备(GPU)
real_images_hr = real_images_hr[0].to(device)
# 将高分辨率图像调整为256x256,使用面积插值方法
real_images_for_enc = F.interpolate(
real_images_hr, size=(256, 256), mode="area"
)
# 以50%的概率随机翻转图像
if random.random() < 0.5:
real_images_for_enc = torch.flip(real_images_for_enc, [-1])
real_images_hr = torch.flip(real_images_hr, [-1])
# 将编码前的图像输入到VAE的编码器中,获取潜在向量z
z = vae.module.encoder(real_images_for_enc)
# 将z从计算图中分离,并转换为CPU上的张量,然后重塑为1D
with ctx:
z_dist_value: torch.Tensor = z.detach().cpu().reshape(-1)
# 定义计算峰度(Kurtosis)的函数
def kurtosis(x):
return ((x - x.mean()) ** 4).mean() / (x.std() ** 4)
# 定义计算偏度(Skewness)的函数
def skew(x):
return ((x - x.mean()) ** 3).mean() / (x.std() ** 3)
# 计算z的各个分位点、峰度和偏度
z_quantiles = {
"0.0": z_dist_value.quantile(0.0),
"0.2": z_dist_value.quantile(0.2),
"0.4": z_dist_value.quantile(0.4),
"0.6": z_dist_value.quantile(0.6),
"0.8": z_dist_value.quantile(0.8),
"1.0": z_dist_value.quantile(1.0),
"kurtosis": kurtosis(z_dist_value),
"skewness": skew(z_dist_value),
}
# 如果需要进行钳制操作,则对z进行钳制
if do_clamp:
z = z.clamp(-clamp_th, clamp_th)
# 对z进行正则化处理
z_s = vae.module.reg(z)
# 进行数据增强操作
# 以50%的概率随机翻转z_s的最后一个维度
if random.random() < 0.5 and flip_invariance:
z_s = torch.flip(z_s, [-1])
# 对特定范围进行取反
z_s[:, -4:-2] = -z_s[:, -4:-2]
# 同时翻转高分辨率图像的最后一个维度
real_images_hr = torch.flip(real_images_hr, [-1])
# 以50%的概率随机翻转z_s的倒数第二个维度
if random.random() < 0.5 and flip_invariance:
z_s = torch.flip(z_s, [-2])
# 对最后两个通道进行取反
z_s[:, -2:] = -z_s[:, -2:]
# 同时翻转高分辨率图像的倒数第二个维度
real_images_hr = torch.flip(real_images_hr, [-2])
# 检查是否启用裁剪不变性
if random.random() < 0.5 and crop_invariance:
# 对图像和潜在向量进行裁剪操作
# 获取z_s的高度(z_h)和宽度(z_w)
z_h, z_w = z.shape[-2:]
# 随机生成新的高度,范围从12到z_h - 1
new_z_h = random.randint(12, z_h - 1)
# 随机生成新的宽度,范围从12到z_w - 1
new_z_w = random.randint(12, z_w - 1)
# 随机生成高度方向的偏移量,范围从0到z_h - new_z_h - 1
offset_z_h = random.randint(0, z_h - new_z_h - 1)
# 随机生成宽度方向的偏移量,范围从0到z_w - new_z_w - 1
offset_z_w = random.randint(0, z_w - new_z_w - 1)
# 根据解码器是否也执行高分辨率操作,计算新的高度和宽度
new_h = (
new_z_h * downscale_factor * 2
if decoder_also_perform_hr
else new_z_h * downscale_factor
)
new_w = (
new_z_w * downscale_factor * 2
if decoder_also_perform_hr
else new_z_w * downscale_factor
)
# 根据解码器是否也执行高分辨率操作,计算高度方向的偏移量
offset_h = (
offset_z_h * downscale_factor * 2
if decoder_also_perform_hr
else offset_z_h * downscale_factor
)
# 根据解码器是否也执行高分辨率操作,计算宽度方向的偏移量
offset_w = (
offset_z_w * downscale_factor * 2
if decoder_also_perform_hr
else offset_z_w * downscale_factor
)
# 对高分辨率真实图像进行裁剪
real_images_hr = real_images_hr[
:, :, offset_h : offset_h + new_h, offset_w : offset_w + new_w
]
# 对潜在向量z_s进行裁剪
z_s = z_s[
:,
:,
offset_z_h : offset_z_h + new_z_h,
offset_z_w : offset_z_w + new_z_w,
]
# 确保裁剪后的高分辨率图像和潜在向量的尺寸是否正确
assert real_images_hr.shape[-2] == new_h
assert real_images_hr.shape[-1] == new_w
assert z_s.shape[-2] == new_z_h
assert z_s.shape[-1] == new_z_w
# 在混合精度上下文中,对潜在向量z_s进行解码,重建图像
with ctx:
reconstructed = vae.module.decoder(z_s)
# 如果当前全局步数超过最大步数,则跳出循环,结束训练
if global_step >= max_steps:
break
# 如果启用了GAN损失
if do_ganloss:
# 使用判别器对高分辨率真实图像进行判别
real_preds = discriminator(real_images_hr)
# 使用判别器对重建图像进行判别(梯度不反向传播)
fake_preds = discriminator(reconstructed.detach())
# 计算判别器的损失,以及平均真实和虚假逻辑值和判别准确率
d_loss, avg_real_logits, avg_fake_logits, disc_acc = gan_disc_loss(
real_preds, fake_preds, disc_type
)
# 对节点上的平均逻辑值进行平均化处理
avg_real_logits = avg_scalar_over_nodes(avg_real_logits, device)
avg_fake_logits = avg_scalar_over_nodes(avg_fake_logits, device)
# 使用LeCam方法更新真实和虚假锚点逻辑值
lecam_anchor_real_logits = (
lecam_beta * lecam_anchor_real_logits
+ (1 - lecam_beta) * avg_real_logits
)
lecam_anchor_fake_logits = (
lecam_beta * lecam_anchor_fake_logits
+ (1 - lecam_beta) * avg_fake_logits
)
# 计算判别器的总损失
total_d_loss = d_loss.mean()
# 获取判别器损失的数值
d_loss_item = total_d_loss.item()
# 如果使用了LeCam方法,则添加LeCam损失
if use_lecam:
# 对真实预测值与虚假锚点逻辑值之差进行平方惩罚
# 对虚假预测值与真实锚点逻辑值之差进行平方惩罚
lecam_loss = (real_preds - lecam_anchor_fake_logits).pow(
2
).mean() + (fake_preds - lecam_anchor_real_logits).pow(2).mean()
# 获取LeCam损失的数值
lecam_loss_item = lecam_loss.item()
# 将LeCam损失乘以权重后加到总判别器损失中
total_d_loss = total_d_loss + lecam_loss * lecam_loss_weight
# 判别器优化器的梯度归零
optimizer_D.zero_grad()
# 反向传播计算判别器的梯度
total_d_loss.backward(retain_graph=True)
# 更新判别器的参数
optimizer_D.step()
# 对重建图像进行反归一化处理,并计算感知损失
_recon_for_perceptual = gradnorm(reconstructed)
# 如果在计算感知损失之前进行数据增强
if augment_before_perceptual_loss:
# 克隆高分辨率真实图像
real_images_hr_aug = real_images_hr.clone()
# 以50%的概率随机翻转重建图像和真实图像的最后一个维度
if random.random() < 0.5:
_recon_for_perceptual = torch.flip(_recon_for_perceptual, [-1])
real_images_hr_aug = torch.flip(real_images_hr_aug, [-1])
# 以50%的概率随机翻转重建图像和真实图像的倒数第二个维度
if random.random() < 0.5:
_recon_for_perceptual = torch.flip(_recon_for_perceptual, [-2])
real_images_hr_aug = torch.flip(real_images_hr_aug, [-2])
else:
# 如果不进行数据增强,则直接使用高分辨率真实图像
real_images_hr_aug = real_images_hr
# 计算感知重建损失,使用LPIPS模型计算感知相似度
percep_rec_loss = lpips(_recon_for_perceptual, real_images_hr_aug).mean()
# mse, vae loss.
# 计算均方误差(MSE)损失和变分自编码器(VAE)损失。
# 对重建图像进行梯度归一化处理,权重设为0.001
recon_for_mse = gradnorm(reconstructed, weight=0.001)
# 计算VAE的总损失,包括重建损失和KL散度损失
vae_loss, loss_data = vae_loss_function(real_images_hr, recon_for_mse, z)
# gan loss