-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
170 lines (162 loc) · 9.15 KB
/
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
from model_def import Discriminator, ECNet
from loss import RecLoss, PyrLoss, AdvLoss
from dataLoader import make_loader
import json
import torch as t
import os
from torch import nn, optim
from tools import calc_pnsr
from prun import prune_model
def train_epoch(model, discriminator, recLoss, pyrLoss, advLoss, d_criterion, train_loader, current_epoch, begin_use_adv_loss_epoch, g_optimizer, d_optimizer, device_ids, epochs):
steps = len(train_loader)
current_step = 1
for d_laplacian, l_gaussian in train_loader:
model.train()
discriminator.eval()
d_laplacian = [i.cuda(device_ids[0]) for i in d_laplacian]
l_gaussian = [i.cuda(device_ids[0]) for i in l_gaussian]
model_outputs = model(d_laplacian)
rec_loss = recLoss(model_outputs, l_gaussian)
pyr_loss = pyrLoss(model_outputs, l_gaussian)
if begin_use_adv_loss_epoch <= current_epoch:
adv_loss = advLoss(model_outputs, discriminator)
else:
adv_loss = t.tensor(0).type(t.FloatTensor).to(pyr_loss.device)
g_total_loss = rec_loss + pyr_loss + adv_loss
g_optimizer.zero_grad()
g_total_loss.backward()
g_optimizer.step()
pnsr = calc_pnsr(model_outputs, l_gaussian)
if begin_use_adv_loss_epoch <= current_epoch:
model.eval()
discriminator.train()
with t.no_grad():
model_outputs = model(d_laplacian)
discriminator_input = t.cat([model_outputs[-1], l_gaussian[-1]], dim=0)
discriminator_label = t.cat([t.tensor([0] * model_outputs[-1].size()[0]), t.tensor([1] * model_outputs[-1].size()[0])], dim=0).type(t.LongTensor).to(discriminator_input.device)
discriminator_out = discriminator(discriminator_input)
d_total_loss = d_criterion(discriminator_out, discriminator_label)
d_optimizer.zero_grad()
d_total_loss.backward()
d_optimizer.step()
if current_step % 5 == 0:
print(
"epoch:%d/%d, step:%d/%d, rec_loss:%.5f, pyr_loss:%.5f, adv_loss:%.5f, d_loss:%.5f, g_total_loss:%.5f, psnr:%.5f" % (
current_epoch, epochs, current_step, steps, rec_loss.item(), pyr_loss.item(), adv_loss.item(), d_total_loss.item(),
g_total_loss.item(), pnsr.item()))
else:
if current_step % 5 == 0:
print(
"epoch:%d/%d, step:%d/%d, rec_loss:%.5f, pyr_loss:%.5f, adv_loss:%.5f, d_loss:%.5f, g_total_loss:%.5f, psnr:%.5f" % (
current_epoch, epochs, current_step, steps, rec_loss.item(), pyr_loss.item(), adv_loss.item(), 0,
g_total_loss.item(), pnsr.item()))
current_step += 1
print("saving epoch model......")
t.save(model.module.state_dict(), os.path.join(model_save_dir, "epoch.pth"))
return model, discriminator
def valid_epoch(model, discriminator, recLoss, pyrLoss, advLoss, valid_loader, current_eopch, begin_use_adv_loss_epoch, device_ids, is_after_prune):
global best_psnr
model.eval()
discriminator.eval()
steps = len(valid_loader)
accum_recloss = 0
accum_pyrloss = 0
accum_advloss = 0
accum_psnr = 0
for d_laplacian, l_gaussian in valid_loader:
d_laplacian = [i.cuda(device_ids[0]) for i in d_laplacian]
l_gaussian = [i.cuda(device_ids[0]) for i in l_gaussian]
with t.no_grad():
model_outputs = model(d_laplacian)
psnr = calc_pnsr(model_outputs, l_gaussian)
rec_loss = recLoss(model_outputs, l_gaussian)
pyr_loss = pyrLoss(model_outputs, l_gaussian)
if current_eopch >= begin_use_adv_loss_epoch:
adv_loss = advLoss(model_outputs, discriminator)
else:
adv_loss = t.tensor(0).type(t.FloatTensor).to(pyr_loss.device)
accum_recloss += rec_loss.item()
accum_pyrloss += pyr_loss.item()
accum_advloss += adv_loss.item()
accum_psnr += psnr.item()
avg_rec_loss = accum_recloss / steps
avg_pyr_loss = accum_pyrloss / steps
avg_adv_loss = accum_advloss / steps
avg_psnr = accum_psnr / steps
avg_total_loss = avg_rec_loss + avg_pyr_loss + avg_adv_loss
if not is_after_prune:
print("###########valid epoch:%d###########" % (current_eopch,))
else:
print("###########valid after prune epoch:%d###########" % (current_eopch,))
print("rec_loss:%.5f, pyr_loss:%.5f, adv_loss:%.5f, g_total_loss:%.5f, psnr:%.5f" % (avg_rec_loss, avg_pyr_loss, avg_adv_loss, avg_total_loss, avg_psnr))
if avg_psnr > best_psnr:
print("saving best model......")
best_psnr = avg_psnr
t.save(model.module.state_dict(), os.path.join(model_save_dir, "best.pth"))
return model, discriminator
def main():
model = ECNet(laplacian_level_count, layer_count_of_every_unet, first_layer_out_channels_of_every_unet, use_iaff, iaff_r, use_psa)
if pretrained_g_weight:
model.load_state_dict(t.load(pretrained_g_weight), strict=False)
discriminator = Discriminator(discriminator_image_size)
model = nn.DataParallel(module=model, device_ids=device_ids)
discriminator = nn.DataParallel(module=discriminator, device_ids=device_ids)
model = model.cuda(device_ids[0])
discriminator = discriminator.cuda(device_ids[0])
recLoss = RecLoss().cuda(device_ids[0])
pyrLoss = PyrLoss().cuda(device_ids[0])
advLoss = AdvLoss(laplacian_level_count).cuda(device_ids[0])
d_criterion = nn.CrossEntropyLoss().cuda(device_ids[0])
g_optimizer = optim.Adam(params=model.parameters(), lr=init_lr, weight_decay=weight_decay)
d_optimizer = optim.Adam(params=discriminator.parameters(), lr=discriminator_init_lr, weight_decay=discriminator_weight_decay)
g_lr_sch = optim.lr_scheduler.CosineAnnealingLR(g_optimizer, T_max=epochs // 2, eta_min=final_lr)
d_lr_sch = optim.lr_scheduler.CosineAnnealingLR(d_optimizer, T_max=epochs - begin_use_adv_loss_epoch + 1, eta_min=discriminator_final_lr)
for e in range(epochs):
current_epoch = e + 1
train_loader = make_loader(True, train_data_dir, image_size, color_jitter_brightness, color_jitter_saturation, batch_size, laplacian_level_count, num_workers, color_jitter_contrast, color_jitter_hue)
valid_loader = make_loader(False, valid_data_dir, image_size, color_jitter_brightness, color_jitter_saturation, batch_size, laplacian_level_count, num_workers, color_jitter_contrast, color_jitter_hue)
model, discriminator = train_epoch(model, discriminator, recLoss, pyrLoss, advLoss, d_criterion, train_loader, current_epoch, begin_use_adv_loss_epoch, g_optimizer, d_optimizer, device_ids, epochs)
model, discriminator = valid_epoch(model, discriminator, recLoss, pyrLoss, advLoss, valid_loader, current_epoch, begin_use_adv_loss_epoch, device_ids, False)
if is_prune:
model = prune_model(model, prune_amount)
valid_loader = make_loader(False, valid_data_dir, image_size, color_jitter_brightness, color_jitter_saturation, batch_size, laplacian_level_count, num_workers, color_jitter_contrast, color_jitter_hue)
model, discriminator = valid_epoch(model, discriminator, recLoss, pyrLoss, advLoss, valid_loader, current_epoch, begin_use_adv_loss_epoch, device_ids, True)
g_lr_sch.step()
if current_epoch >= begin_use_adv_loss_epoch:
d_lr_sch.step()
if __name__ == "__main__":
conf = json.load(open("conf.json", "r", encoding="utf-8"))
train_conf = conf["train"]
CUDA_VISIBLE_DEVICES = train_conf["CUDA_VISIBLE_DEVICES"]
os.environ["CUDA_VISIBLE_DEVICES"] = CUDA_VISIBLE_DEVICES
device_ids = list(range(len(CUDA_VISIBLE_DEVICES.split(","))))
best_psnr = -float("inf")
image_size = train_conf["image_size"]
discriminator_image_size = train_conf["discriminator_image_size"]
train_data_dir = train_conf["train_data_dir"]
valid_data_dir = train_conf["valid_data_dir"]
batch_size = train_conf["batch_size"]
init_lr = train_conf["init_lr"]
final_lr = train_conf["final_lr"]
discriminator_init_lr = train_conf["discriminator_init_lr"]
discriminator_final_lr = train_conf["discriminator_final_lr"]
epochs = train_conf["epochs"]
begin_use_adv_loss_epoch = train_conf["begin_use_adv_loss_epoch"]
num_workers = train_conf["num_workers"]
model_save_dir = train_conf["model_save_dir"]
laplacian_level_count = train_conf["laplacian_level_count"]
layer_count_of_every_unet = train_conf["layer_count_of_every_unet"]
first_layer_out_channels_of_every_unet = train_conf["first_layer_out_channels_of_every_unet"]
color_jitter_brightness = train_conf["color_jitter_brightness"]
color_jitter_saturation = train_conf["color_jitter_saturation"]
weight_decay = train_conf["weight_decay"]
discriminator_weight_decay = train_conf["discriminator_weight_decay"]
color_jitter_contrast = train_conf["color_jitter_contrast"]
color_jitter_hue = train_conf["color_jitter_hue"]
use_iaff = train_conf["use_iaff"]
iaff_r = train_conf["iaff_r"]
use_psa = train_conf["use_psa"]
pretrained_g_weight = train_conf["pretrained_g_weight"]
is_prune = train_conf["is_prune"]
prune_amount = train_conf["prune_amount"]
main()