forked from aiff22/PyNET-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
198 lines (134 loc) · 6.57 KB
/
train_model.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
# Copyright 2020 by Andrey Ignatov. All Rights Reserved.
from torch.utils.data import DataLoader
from torchvision import transforms
from torch.optim import Adam
import torch
import imageio
import numpy as np
import math
import sys
from load_data import LoadData, LoadVisualData
from msssim import MSSSIM
from model import PyNET
from vgg import vgg_19
from utils import normalize_batch, process_command_args
to_image = transforms.Compose([transforms.ToPILImage()])
np.random.seed(0)
torch.manual_seed(0)
# Processing command arguments
level, batch_size, learning_rate, restore_epoch, num_train_epochs, dataset_dir = process_command_args(sys.argv)
dslr_scale = float(1) / (2 ** (level - 1))
# Dataset size
TRAIN_SIZE = 46839
TEST_SIZE = 1204
def train_model():
torch.backends.cudnn.deterministic = True
device = torch.device("cuda")
print("CUDA visible devices: " + str(torch.cuda.device_count()))
print("CUDA Device Name: " + str(torch.cuda.get_device_name(device)))
# Creating dataset loaders
train_dataset = LoadData(dataset_dir, TRAIN_SIZE, dslr_scale, test=False)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, num_workers=1,
pin_memory=True, drop_last=True)
test_dataset = LoadData(dataset_dir, TEST_SIZE, dslr_scale, test=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=1, shuffle=False, num_workers=1,
pin_memory=True, drop_last=False)
visual_dataset = LoadVisualData(dataset_dir, 10, dslr_scale, level)
visual_loader = DataLoader(dataset=visual_dataset, batch_size=1, shuffle=False, num_workers=0,
pin_memory=True, drop_last=False)
# Creating image processing network and optimizer
generator = PyNET(level=level, instance_norm=True, instance_norm_level_1=True).to(device)
generator = torch.nn.DataParallel(generator)
optimizer = Adam(params=generator.parameters(), lr=learning_rate)
# Restoring the variables
if level < 5:
generator.load_state_dict(torch.load("models/pynet_level_" + str(level + 1) +
"_epoch_" + str(restore_epoch) + ".pth"), strict=False)
# Losses
VGG_19 = vgg_19(device)
MSE_loss = torch.nn.MSELoss()
MS_SSIM = MSSSIM()
# Train the network
for epoch in range(num_train_epochs):
torch.cuda.empty_cache()
train_iter = iter(train_loader)
for i in range(len(train_loader)):
optimizer.zero_grad()
x, y = next(train_iter)
x = x.to(device, non_blocking=True)
y = y.to(device, non_blocking=True)
enhanced = generator(x)
# MSE Loss
loss_mse = MSE_loss(enhanced, y)
# VGG Loss
if level < 5:
enhanced_vgg = VGG_19(normalize_batch(enhanced))
target_vgg = VGG_19(normalize_batch(y))
loss_content = MSE_loss(enhanced_vgg, target_vgg)
# Total Loss
if level == 5 or level == 4:
total_loss = loss_mse
if level == 3 or level == 2:
total_loss = loss_mse * 10 + loss_content
if level == 1:
total_loss = loss_mse * 10 + loss_content
if level == 0:
loss_ssim = MS_SSIM(enhanced, y)
total_loss = loss_mse + loss_content + (1 - loss_ssim) * 0.4
# Perform the optimization step
total_loss.backward()
optimizer.step()
if i == 0:
# Save the model that corresponds to the current epoch
generator.eval().cpu()
torch.save(generator.state_dict(), "models/pynet_level_" + str(level) + "_epoch_" + str(epoch) + ".pth")
generator.to(device).train()
# Save visual results for several test images
generator.eval()
with torch.no_grad():
visual_iter = iter(visual_loader)
for j in range(len(visual_loader)):
torch.cuda.empty_cache()
raw_image = next(visual_iter)
raw_image = raw_image.to(device, non_blocking=True)
enhanced = generator(raw_image.detach())
enhanced = np.asarray(to_image(torch.squeeze(enhanced.detach().cpu())))
imageio.imwrite("results/pynet_img_" + str(j) + "_level_" + str(level) + "_epoch_" +
str(epoch) + ".jpg", enhanced)
# Evaluate the model
loss_mse_eval = 0
loss_psnr_eval = 0
loss_vgg_eval = 0
loss_ssim_eval = 0
generator.eval()
with torch.no_grad():
test_iter = iter(test_loader)
for j in range(len(test_loader)):
x, y = next(test_iter)
x = x.to(device, non_blocking=True)
y = y.to(device, non_blocking=True)
enhanced = generator(x)
loss_mse_temp = MSE_loss(enhanced, y).item()
loss_mse_eval += loss_mse_temp
loss_psnr_eval += 20 * math.log10(1.0 / math.sqrt(loss_mse_temp))
if level < 2:
loss_ssim_eval += MS_SSIM(y, enhanced)
if level < 5:
enhanced_vgg_eval = VGG_19(normalize_batch(enhanced)).detach()
target_vgg_eval = VGG_19(normalize_batch(y)).detach()
loss_vgg_eval += MSE_loss(enhanced_vgg_eval, target_vgg_eval).item()
loss_mse_eval = loss_mse_eval / TEST_SIZE
loss_psnr_eval = loss_psnr_eval / TEST_SIZE
loss_vgg_eval = loss_vgg_eval / TEST_SIZE
loss_ssim_eval = loss_ssim_eval / TEST_SIZE
if level < 2:
print("Epoch %d, mse: %.4f, psnr: %.4f, vgg: %.4f, ms-ssim: %.4f" % (epoch,
loss_mse_eval, loss_psnr_eval, loss_vgg_eval, loss_ssim_eval))
elif level < 5:
print("Epoch %d, mse: %.4f, psnr: %.4f, vgg: %.4f" % (epoch,
loss_mse_eval, loss_psnr_eval, loss_vgg_eval))
else:
print("Epoch %d, mse: %.4f, psnr: %.4f" % (epoch, loss_mse_eval, loss_psnr_eval))
generator.train()
if __name__ == '__main__':
train_model()