-
Notifications
You must be signed in to change notification settings - Fork 5
/
engine.py
148 lines (119 loc) · 6.07 KB
/
engine.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
import math
import os
import sys
from typing import Iterable
import torch
import functools
print = functools.partial(print, flush=True)
import util.misc as utils
from einops import rearrange
import numpy as np
import matplotlib.pyplot as plt
import time
def denorm_img(tensor, opts):
tensor = rearrange(tensor[0:4], 'b c w h -> b w h c').detach().cpu()
tensor = tensor * torch.tensor((opts.patch_std,opts.patch_std,opts.patch_std)) + torch.tensor((opts.patch_mean,opts.patch_mean,opts.patch_mean))
tensor = np.clip(tensor.flatten(0, 1).numpy(), 0, 1)
return tensor
def train_one_epoch(opts, GEN: torch.nn.Module, DIS: torch.nn.Module, criterion: torch.nn.Module,
data_loader: Iterable, gen_opt: torch.optim.Optimizer, dis_opt: torch.optim.Optimizer,
device: torch.device, epoch: int, g_grad_scale=None):
GEN.train()
DIS.train()
criterion.train()
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = 'Epoch: [{}]'.format(epoch)
print_freq = 10
display_freq=75
for i, samples in enumerate(metric_logger.log_every(data_loader, print_freq, header)):
for k,v in samples.items():
if isinstance(samples[k], torch.Tensor):
samples[k]=v.to(device)
if g_grad_scale is None:
gt=samples['ground_truth']
fake = GEN(samples)
if i%display_freq==0:
fig_name = f"{epoch}_{time.time():04f}"
fig = np.concatenate([denorm_img(gt, opts), denorm_img(fake, opts)], axis=1)
plt.imsave(os.path.join(opts.visdir, fig_name + '.png'), fig, vmin=0, vmax=1)
D_loss_dict = criterion.get_dis_loss(fake, gt, DIS)
D_losses = sum(D_loss_dict[k] * criterion.dis_weight_dict[k] for k in D_loss_dict.keys())
dis_opt.zero_grad()
D_losses.backward()
dis_opt.step()
G_loss_dict = criterion.get_gen_loss(fake, gt, DIS)
G_losses = sum(G_loss_dict[k] * criterion.gen_weight_dict[k] for k in G_loss_dict.keys())
gen_opt.zero_grad()
G_losses.backward()
gen_opt.step()
else:
with torch.cuda.amp.autocast():
gt = samples['ground_truth']
fake = GEN(samples)
if i % display_freq == 0:
fig_name = f"{epoch}_{time.time():04f}"
fig = np.concatenate([denorm_img(gt, opts), denorm_img(fake, opts)], axis=1)
plt.imsave(os.path.join(opts.visdir, fig_name + '.png'), fig, vmin=0, vmax=1)
D_loss_dict = criterion.get_dis_loss(fake, gt)
D_losses = sum(D_loss_dict[k] * criterion.dis_weight_dict[k] for k in D_loss_dict.keys())
dis_opt.zero_grad()
D_losses.backward()
dis_opt.step()
with torch.cuda.amp.autocast():
G_loss_dict = criterion.get_gen_loss(fake, gt)
G_losses = sum(G_loss_dict[k] * criterion.gen_weight_dict[k] for k in G_loss_dict.keys())
gen_opt.zero_grad()
g_grad_scale.scale(G_losses).backward()
g_grad_scale.step(gen_opt)
g_grad_scale.update()
metric_logger.update(**G_loss_dict,**D_loss_dict)
metric_logger.update(lr=dis_opt.param_groups[0]["lr"])
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}
def train_one_epoch_warmup(opts, GEN: torch.nn.Module,criterion: torch.nn.Module,
data_loader: Iterable, gen_opt: torch.optim.Optimizer,
device: torch.device, epoch: int, g_grad_scale=None):
GEN.train()
criterion.train()
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = '(Warning Up!!)Epoch: [{}]'.format(epoch)
print_freq = 10
display_freq = 75
for i,samples in enumerate(metric_logger.log_every(data_loader, print_freq, header)):
for k, v in samples.items():
if isinstance(samples[k], torch.Tensor):
samples[k] = v.to(device)
if g_grad_scale is None:
gt = samples['ground_truth']
outputs = GEN(samples)
if i%display_freq==0:
fig_name = f"{epoch}_{time.time():04f}"
fig = np.concatenate([denorm_img(gt, opts), denorm_img(outputs, opts)], axis=1)
plt.imsave(os.path.join(opts.visdir, fig_name + '.png'), fig, vmin=0, vmax=1)
G_loss_dict = criterion.get_gen_loss(outputs, gt, warmup=True)
G_losses = sum(G_loss_dict[k] * criterion.gen_weight_dict[k] for k in G_loss_dict.keys())
gen_opt.zero_grad()
G_losses.backward()
gen_opt.step()
else:
gen_opt.zero_grad()
with torch.cuda.amp.autocast():
gt = samples['ground_truth']
outputs = GEN(samples)
if i % display_freq == 0:
fig_name = f"{epoch}_{time.time():04f}"
fig = np.concatenate([denorm_img(gt, opts), denorm_img(outputs, opts)], axis=1)
plt.imsave(os.path.join(opts.visdir, fig_name + '.png'), fig, vmin=0, vmax=1)
G_loss_dict = criterion.get_gen_loss(outputs, gt, warmup=True)
G_losses = sum(G_loss_dict[k] * criterion.gen_weight_dict[k] for k in G_loss_dict.keys())
g_grad_scale.scale(G_losses).backward()
g_grad_scale.step(gen_opt)
g_grad_scale.update()
metric_logger.update(**G_loss_dict)
metric_logger.update(lr=gen_opt.param_groups[0]["lr"])
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}