-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
221 lines (183 loc) · 9.52 KB
/
trainer.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import os
import time
import datetime
import numpy as np
import cv2
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader
import network
import train_dataset
import utils
def WGAN_trainer(opt):
# ----------------------------------------
# Initialize training parameters
# ----------------------------------------
# cudnn benchmark accelerates the network
cudnn.benchmark = opt.cudnn_benchmark
# configurations
save_folder = opt.save_path
sample_folder = opt.sample_path
if not os.path.exists(save_folder):
os.makedirs(save_folder)
if not os.path.exists(sample_folder):
os.makedirs(sample_folder)
# Build networks
generator = utils.create_generator(opt)
discriminator = utils.create_discriminator(opt)
perceptualnet = utils.create_perceptualnet()
# Loss functions
L1Loss = nn.L1Loss()
MSELoss = nn.MSELoss()
# Optimizers
optimizer_g = torch.optim.Adam(generator.parameters(), lr = opt.lr_g, betas = (opt.b1, opt.b2), weight_decay = opt.weight_decay)
optimizer_d = torch.optim.Adam(discriminator.parameters(), lr = opt.lr_d, betas = (opt.b1, opt.b2), weight_decay = opt.weight_decay)
# Learning rate decrease
def adjust_learning_rate(lr_in, optimizer, epoch, opt):
"""Set the learning rate to the initial LR decayed by "lr_decrease_factor" every "lr_decrease_epoch" epochs"""
lr = lr_in * (opt.lr_decrease_factor ** (epoch // opt.lr_decrease_epoch))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
# Save the two-stage generator model
def save_model_generator(net, epoch, opt):
"""Save the model at "checkpoint_interval" and its multiple"""
model_name = 'deepfillv2_WGAN_G_epoch%d_batchsize%d.pth' % (epoch, opt.batch_size)
model_name = os.path.join(save_folder, model_name)
if opt.multi_gpu == True:
if epoch % opt.checkpoint_interval == 0:
torch.save(net.module.state_dict(), model_name)
print('The trained model is successfully saved at epoch %d' % (epoch))
else:
if epoch % opt.checkpoint_interval == 0:
torch.save(net.state_dict(), model_name)
print('The trained model is successfully saved at epoch %d' % (epoch))
# Save the dicriminator model
def save_model_discriminator(net, epoch, opt):
"""Save the model at "checkpoint_interval" and its multiple"""
model_name = 'deepfillv2_WGAN_D_epoch%d_batchsize%d.pth' % (epoch, opt.batch_size)
model_name = os.path.join(save_folder, model_name)
if opt.multi_gpu == True:
if epoch % opt.checkpoint_interval == 0:
torch.save(net.module.state_dict(), model_name)
print('The trained model is successfully saved at epoch %d' % (epoch))
else:
if epoch % opt.checkpoint_interval == 0:
torch.save(net.state_dict(), model_name)
print('The trained model is successfully saved at epoch %d' % (epoch))
# load the model
def load_model(net, epoch, opt, type='G'):
"""Save the model at "checkpoint_interval" and its multiple"""
if type == 'G':
model_name = 'deepfillv2_WGAN_G_epoch%d_batchsize%d.pth' % (epoch, opt.batch_size)
else:
model_name = 'deepfillv2_WGAN_D_epoch%d_batchsize%d.pth' % (epoch, opt.batch_size)
model_name = os.path.join(save_folder, model_name)
pretrained_dict = torch.load(model_name)
net.load_state_dict(pretrained_dict)
if opt.resume:
load_model(generator, opt.resume_epoch, opt, type='G')
load_model(discriminator, opt.resume_epoch, opt, type='D')
print('--------------------Pretrained Models are Loaded--------------------')
# To device
if opt.multi_gpu == True:
generator = nn.DataParallel(generator)
discriminator = nn.DataParallel(discriminator)
perceptualnet = nn.DataParallel(perceptualnet)
generator = generator.cuda()
discriminator = discriminator.cuda()
perceptualnet = perceptualnet.cuda()
else:
generator = generator.cuda()
discriminator = discriminator.cuda()
perceptualnet = perceptualnet.cuda()
# ----------------------------------------
# Initialize training dataset
# ----------------------------------------
# Define the dataset
trainset = train_dataset.InpaintDataset(opt)
print('The overall number of images equals to %d' % len(trainset))
# Define the dataloader
dataloader = DataLoader(trainset, batch_size = opt.batch_size, shuffle = True, num_workers = opt.num_workers, pin_memory = True, drop_last=True)
# ----------------------------------------
# Training
# ----------------------------------------
# Initialize start time
prev_time = time.time()
# Tensor type
Tensor = torch.cuda.FloatTensor
# Training loop
for epoch in range(opt.resume_epoch, opt.epochs):
for batch_idx, (img, height, width) in enumerate(dataloader):
img = img.cuda()
# set the same free form masks for each batch
mask = torch.empty(img.shape[0], 1, img.shape[2], img.shape[3]).cuda()
for i in range(opt.batch_size):
mask[i] = torch.from_numpy(train_dataset.InpaintDataset.random_ff_mask(
shape=(height[0], width[0])).astype(np.float32)).cuda()
# LSGAN vectors
valid = Tensor(np.ones((img.shape[0], 1, height[0]//32, width[0]//32)))
fake = Tensor(np.zeros((img.shape[0], 1, height[0]//32, width[0]//32)))
zero = Tensor(np.zeros((img.shape[0], 1, height[0]//32, width[0]//32)))
### Train Discriminator
optimizer_d.zero_grad()
# Generator output
first_out, second_out = generator(img, mask)
# forward propagation
first_out_wholeimg = img * (1 - mask) + first_out * mask # in range [0, 1]
second_out_wholeimg = img * (1 - mask) + second_out * mask # in range [0, 1]
# Fake samples
fake_scalar = discriminator(second_out_wholeimg.detach(), mask)
# True samples
true_scalar = discriminator(img, mask)
# Loss and optimize
loss_fake = -torch.mean(torch.min(zero, -valid-fake_scalar))
loss_true = -torch.mean(torch.min(zero, -valid+true_scalar))
# Overall Loss and optimize
loss_D = 0.5 * (loss_fake + loss_true)
loss_D.backward()
optimizer_d.step()
### Train Generator
optimizer_g.zero_grad()
# L1 Loss
first_L1Loss = (first_out - img).abs().mean()
second_L1Loss = (second_out - img).abs().mean()
# GAN Loss
fake_scalar = discriminator(second_out_wholeimg, mask)
GAN_Loss = -torch.mean(fake_scalar)
# Get the deep semantic feature maps, and compute Perceptual Loss
img_featuremaps = perceptualnet(img) # feature maps
second_out_featuremaps = perceptualnet(second_out)
second_PerceptualLoss = L1Loss(second_out_featuremaps, img_featuremaps)
# Compute losses
loss = opt.lambda_l1 * first_L1Loss + opt.lambda_l1 * second_L1Loss + \
opt.lambda_perceptual * second_PerceptualLoss + opt.lambda_gan * GAN_Loss
loss.backward()
optimizer_g.step()
# Determine approximate time left
batches_done = epoch * len(dataloader) + batch_idx
batches_left = opt.epochs * len(dataloader) - batches_done
time_left = datetime.timedelta(seconds = batches_left * (time.time() - prev_time))
prev_time = time.time()
# Print log
print("\r[Epoch %d/%d] [Batch %d/%d] [first Mask L1 Loss: %.5f] [second Mask L1 Loss: %.5f]" %
((epoch + 1), opt.epochs, batch_idx, len(dataloader), first_L1Loss.item(), second_L1Loss.item()))
print("\r[D Loss: %.5f] [G Loss: %.5f] [Perceptual Loss: %.5f] time_left: %s" %
(loss_D.item(), GAN_Loss.item(), second_PerceptualLoss.item(), time_left))
masked_img = img * (1 - mask) + mask
mask = torch.cat((mask, mask, mask), 1)
if (batch_idx + 1) % 40 == 0:
img_list = [img, mask, masked_img, first_out, second_out]
name_list = ['gt', 'mask', 'masked_img', 'first_out', 'second_out']
utils.save_sample_png(sample_folder = sample_folder, sample_name = 'epoch%d' % (epoch + 1), img_list = img_list, name_list = name_list, pixel_max_cnt = 255)
# Learning rate decrease
adjust_learning_rate(opt.lr_g, optimizer_g, (epoch + 1), opt)
adjust_learning_rate(opt.lr_d, optimizer_d, (epoch + 1), opt)
# Save the model
save_model_generator(generator, (epoch + 1), opt)
save_model_discriminator(discriminator, (epoch + 1), opt)
### Sample data every epoch
if (epoch + 1) % 1 == 0:
img_list = [img, mask, masked_img, first_out, second_out]
name_list = ['gt', 'mask', 'masked_img', 'first_out', 'second_out']
utils.save_sample_png(sample_folder = sample_folder, sample_name = 'epoch%d' % (epoch + 1), img_list = img_list, name_list = name_list, pixel_max_cnt = 255)