-
Notifications
You must be signed in to change notification settings - Fork 8
/
train.py
144 lines (140 loc) · 6.59 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
import torch
import time
import argparse
from model import fusion_net, Discriminator
from train_dataset import dehaze_train_dataset
from test_val_dataset import dehaze_test_dataset
from torch.utils.data import DataLoader
import os
from utils_test import to_psnr, to_ssim_skimage, to_rmse
from tensorboardX import SummaryWriter
import torch.nn.functional as F
from torchvision.models import vgg16
from torchvision.utils import save_image as imwrite
from pytorch_msssim import msssim
from perceptual import LossNetwork
# --- Parse hyper-parameters train --- #
parser = argparse.ArgumentParser(description='DW-GAN Dehaze')
parser.add_argument('-learning_rate', help='Set the learning rate', default=1e-4, type=float)
parser.add_argument('-train_batch_size', help='Set the training batch size', default=25, type=int)
parser.add_argument('-train_epoch', help='Set the training epoch', default=10000, type=int)
parser.add_argument('--train_dataset', type=str, default='')
parser.add_argument('--data_dir', type=str, default='')
parser.add_argument('--model_save_dir', type=str, default='./check_points')
parser.add_argument('--log_dir', type=str, default=None)
# --- Parse hyper-parameters test --- #
parser.add_argument('--test_dataset', type=str, default='')
parser.add_argument('--predict_result', type=str, default='./output_result/picture/')
parser.add_argument('-test_batch_size', help='Set the testing batch size', default=1, type=int)
args = parser.parse_args()
# --- train --- #
learning_rate = args.learning_rate
train_batch_size = args.train_batch_size
train_epoch = args.train_epoch
train_dataset = os.path.join('/your/path/')
# --- test --- #
test_dataset = os.path.join('/your/path/')
predict_result = args.predict_result
test_batch_size = args.test_batch_size
# --- output picture and check point --- #
if not os.path.exists(args.model_save_dir):
os.makedirs(args.model_save_dir)
output_dir = os.path.join('check_points/')
# --- Gpu device --- #
device_ids = [Id for Id in range(torch.cuda.device_count())]
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# --- Define the network --- #
MyEnsembleNet = fusion_net()
print('MyEnsembleNet parameters:', sum(param.numel() for param in MyEnsembleNet.parameters()))
DNet = Discriminator()
# --- Build optimizer --- #
G_optimizer = torch.optim.Adam(MyEnsembleNet.parameters(), lr=0.0001)
scheduler_G = torch.optim.lr_scheduler.MultiStepLR(G_optimizer, milestones=[3000, 5000, 8000], gamma=0.5)
D_optim = torch.optim.Adam(DNet.parameters(), lr=0.0001)
scheduler_D = torch.optim.lr_scheduler.MultiStepLR(D_optim, milestones=[5000, 7000, 8000], gamma=0.5)
# --- Load training data --- #
dataset = dehaze_train_dataset(train_dataset)
train_loader = DataLoader(dataset=dataset, batch_size=train_batch_size, num_workers=10)
# --- Load testing data --- #
test_dataset = dehaze_test_dataset(test_dataset)
test_loader = DataLoader(dataset=test_dataset, batch_size=test_batch_size, num_workers=10)
MyEnsembleNet = MyEnsembleNet.to(device)
DNet = DNet.to(device)
writer = SummaryWriter()
# --- Load the network weight --- #
try:
MyEnsembleNet.load_state_dict(torch.load(os.path.join(args.teacher_model, 'best.pkl')))
print('--- weight loaded ---')
except:
print('--- no weight loaded ---')
# --- Define the perceptual loss network --- #
vgg_model = vgg16(pretrained=True)
vgg_model = vgg_model.features[:16].to(device)
for param in vgg_model.parameters():
param.requires_grad = False
loss_network = LossNetwork(vgg_model)
loss_network.eval()
msssim_loss = msssim
# --- Strat training --- #
iteration = 0
best_psnr = 0
for epoch in range(train_epoch):
start_time = time.time()
scheduler_G.step()
scheduler_D.step()
MyEnsembleNet.train()
DNet.train()
for batch_idx, (hazy, clean) in enumerate(train_loader):
iteration += 1
hazy = hazy.to(device)
clean = clean.to(device)
output = MyEnsembleNet(hazy)
DNet.zero_grad()
real_out = DNet(clean).mean()
fake_out = DNet(output).mean()
D_loss = 1 - real_out + fake_out
D_loss.backward(retain_graph=True)
adversarial_loss = torch.mean(1 - fake_out)
MyEnsembleNet.zero_grad()
smooth_loss_l1 = F.smooth_l1_loss(output, clean)
perceptual_loss = loss_network(output, clean)
msssim_loss_ = -msssim_loss(output, clean, normalize=True)
total_loss = smooth_loss_l1 + 0.01 * perceptual_loss + 0.0005 * adversarial_loss + 0.2 * msssim_loss_
total_loss.backward()
D_optim.step()
G_optimizer.step()
writer.add_scalars('training', {'training total loss': total_loss.item()}, iteration)
writer.add_scalars('training_img', {'img loss_l1': smooth_loss_l1.item(),
'perceptual': perceptual_loss.item(),
'msssim': msssim_loss_.item()}, iteration)
writer.add_scalars('GAN_training', {'d_loss': D_loss.item(),'d_score': real_out.item(),
'g_score': fake_out.item()}, iteration)
if epoch % 5 == 0:
print('we are testing on epoch: ' + str(epoch))
with torch.no_grad():
psnr_list = []
ssim_list = []
rmse_list = []
MyEnsembleNet.eval()
for batch_idx, (hazy_up,hazy_down,hazy, clean) in enumerate(test_loader):
hazy_up = hazy_up.to(device)
hazy_down=hazy_down.to(device)
clean = clean.to(device)
frame_out_up = MyEnsembleNet(hazy_up)
frame_out_down = MyEnsembleNet(hazy_down)
frame_out=(torch.cat([frame_out_up.permute(0,2,3,1), frame_out_down[:,:,80:640,:].permute(0,2,3,1)], 1)).permute(0,3,1,2)
if not os.path.exists('output/'):
os.makedirs('output/')
imwrite(frame_out, 'output/' + str(batch_idx) + '.png', range=(0, 1))
psnr_list.extend(to_psnr(frame_out, clean))
ssim_list.extend(to_ssim_skimage(frame_out, clean))
rmse_list.extend(to_rmse(frame_out, clean))
avr_psnr = sum(psnr_list) / len(psnr_list)
avr_ssim = sum(ssim_list) / len(ssim_list)
avr_rmse = sum(rmse_list) / len(rmse_list)
writer.add_scalars('testing', {'testing psnr': avr_psnr, 'testing rmse': avr_rmse,
'testing ssim': avr_ssim}, epoch)
if avr_psnr > best_psnr:
print(f"New best PSNR achieved: {avr_psnr}. Saving model...")
torch.save(MyEnsembleNet.state_dict(), os.path.join(args.model_save_dir, 'epoch' + str(epoch) + '.pkl'))
best_psnr = avr_psnr