-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathengine_jit.py
More file actions
160 lines (128 loc) · 5.98 KB
/
engine_jit.py
File metadata and controls
160 lines (128 loc) · 5.98 KB
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
import math
import sys
import os
import shutil
import torch
import numpy as np
import cv2
import util.misc as misc
import util.lr_sched as lr_sched
import torch_fidelity
import copy
def train_one_epoch(model, model_without_ddp, data_loader, optimizer, device, epoch, log_writer=None, args=None):
model.train(True)
metric_logger = misc.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', misc.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = 'Epoch: [{}]'.format(epoch)
print_freq = 20
optimizer.zero_grad()
if log_writer is not None:
print('log_dir: {}'.format(log_writer.log_dir))
for data_iter_step, (x, labels) in enumerate(metric_logger.log_every(data_loader, print_freq, header)):
# per iteration (instead of per epoch) lr scheduler
lr_sched.adjust_learning_rate(optimizer, data_iter_step / len(data_loader) + epoch, args)
# normalize image to [-1, 1]
x = x.to(device, non_blocking=True).to(torch.float32).div_(255)
x = x * 2.0 - 1.0
labels = labels.to(device, non_blocking=True)
with torch.amp.autocast('cuda', dtype=torch.bfloat16):
loss = model(x, labels)
loss_value = loss.item()
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
sys.exit(1)
optimizer.zero_grad()
loss.backward()
optimizer.step()
torch.cuda.synchronize()
model_without_ddp.update_ema()
metric_logger.update(loss=loss_value)
lr = optimizer.param_groups[0]["lr"]
metric_logger.update(lr=lr)
loss_value_reduce = misc.all_reduce_mean(loss_value)
if log_writer is not None:
# Use epoch_1000x as the x-axis in TensorBoard to calibrate curves.
epoch_1000x = int((data_iter_step / len(data_loader) + epoch) * 1000)
if data_iter_step % args.log_freq == 0:
log_writer.add_scalar('train_loss', loss_value_reduce, epoch_1000x)
log_writer.add_scalar('lr', lr, epoch_1000x)
def evaluate(model_without_ddp, args, epoch, batch_size=64, log_writer=None):
model_without_ddp.eval()
world_size = misc.get_world_size()
local_rank = misc.get_rank()
num_steps = args.num_images // (batch_size * world_size) + 1
# Construct the folder name for saving generated images.
save_folder = os.path.join(
args.output_dir,
"{}-steps{}-cfg{}-interval{}-{}-image{}-res{}".format(
model_without_ddp.method, model_without_ddp.steps, model_without_ddp.cfg_scale,
model_without_ddp.cfg_interval[0], model_without_ddp.cfg_interval[1], args.num_images, args.img_size
)
)
print("Save to:", save_folder)
if misc.get_rank() == 0 and not os.path.exists(save_folder):
os.makedirs(save_folder)
# switch to ema params, hard-coded to be the first one
model_state_dict = copy.deepcopy(model_without_ddp.state_dict())
ema_state_dict = copy.deepcopy(model_without_ddp.state_dict())
for i, (name, _value) in enumerate(model_without_ddp.named_parameters()):
assert name in ema_state_dict
ema_state_dict[name] = model_without_ddp.ema_params1[i]
print("Switch to ema")
model_without_ddp.load_state_dict(ema_state_dict)
# ensure that the number of images per class is equal.
class_num = args.class_num
assert args.num_images % class_num == 0, "Number of images per class must be the same"
class_label_gen_world = np.arange(0, class_num).repeat(args.num_images // class_num)
class_label_gen_world = np.hstack([class_label_gen_world, np.zeros(50000)])
for i in range(num_steps):
print("Generation step {}/{}".format(i, num_steps))
start_idx = world_size * batch_size * i + local_rank * batch_size
end_idx = start_idx + batch_size
labels_gen = class_label_gen_world[start_idx:end_idx]
labels_gen = torch.Tensor(labels_gen).long().cuda()
with torch.amp.autocast('cuda', dtype=torch.bfloat16):
sampled_images = model_without_ddp.generate(labels_gen)
torch.distributed.barrier()
# denormalize images
sampled_images = (sampled_images + 1) / 2
sampled_images = sampled_images.detach().cpu()
# distributed save images
for b_id in range(sampled_images.size(0)):
img_id = i * sampled_images.size(0) * world_size + local_rank * sampled_images.size(0) + b_id
if img_id >= args.num_images:
break
gen_img = np.round(np.clip(sampled_images[b_id].numpy().transpose([1, 2, 0]) * 255, 0, 255))
gen_img = gen_img.astype(np.uint8)[:, :, ::-1]
cv2.imwrite(os.path.join(save_folder, '{}.png'.format(str(img_id).zfill(5))), gen_img)
torch.distributed.barrier()
# back to no ema
print("Switch back from ema")
model_without_ddp.load_state_dict(model_state_dict)
# compute FID and IS
if log_writer is not None:
if args.img_size == 256:
fid_statistics_file = 'fid_stats/jit_in256_stats.npz'
elif args.img_size == 512:
fid_statistics_file = 'fid_stats/jit_in512_stats.npz'
else:
raise NotImplementedError
metrics_dict = torch_fidelity.calculate_metrics(
input1=save_folder,
input2=None,
fid_statistics_file=fid_statistics_file,
cuda=True,
isc=True,
fid=True,
kid=False,
prc=False,
verbose=False,
)
fid = metrics_dict['frechet_inception_distance']
inception_score = metrics_dict['inception_score_mean']
postfix = "_cfg{}_res{}".format(model_without_ddp.cfg_scale, args.img_size)
log_writer.add_scalar('fid{}'.format(postfix), fid, epoch)
log_writer.add_scalar('is{}'.format(postfix), inception_score, epoch)
print("FID: {:.4f}, Inception Score: {:.4f}".format(fid, inception_score))
shutil.rmtree(save_folder)
torch.distributed.barrier()