forked from dong03/MVSS-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
354 lines (279 loc) · 16 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import argparse
import os
from sys import platform
from torch.utils.data import DataLoader
from torch.autograd import Variable
from models import *
from datasets import *
import torch.nn as nn
import torch
from datetime import datetime
from torch.utils.tensorboard import SummaryWriter
from models.mvssnet import get_mvss
def dice_loss(gt, out, smooth=1.0):
gt = gt.view(-1)
out = out.view(-1)
intersection = (gt * out).sum()
dice = (2.0 * intersection + smooth) / (torch.square(gt).sum() + torch.square(
out).sum() + smooth) # TODO: need to confirm this matches what the paper says, and also the calculation/result is correct
return 1.0 - dice
def bgr_to_rgb(t):
b, g, r = torch.unbind(t, 1)
return torch.stack((r, g, b), 1)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--epoch", type=int, default=0, help="epoch to start training from")
parser.add_argument("--n_epochs", type=int, default=200, help="number of epochs of training")
parser.add_argument("--train_paths_file", type=str, default="../FaceForensics/train_files.txt",
help="path to the file with training input paths") # each line of this file should contain "/path/to/image.ext /path/to/mask.ext /path/to/edge.ext 1 (for fake)/0 (for real)"; for real image.ext, set /path/to/mask.ext and /path/to/edge.ext as a string None
parser.add_argument("--valid_paths_file", type=str, default="../FaceForensics/valid_files.txt",
help="path to the file with validation input paths")
parser.add_argument("--image_size", type=int, default=512, help="size of the images")
parser.add_argument("--batch_size", type=int, default=12,
help="size of the batches") # no default value given by paper
parser.add_argument("--lr", type=float, default=1e-4, help="adam: learning rate")
parser.add_argument("--b1", type=float, default=0.9, help="adam: decay of first order momentum of gradient")
parser.add_argument("--b2", type=float, default=0.999, help="adam: decay of first order momentum of gradient")
parser.add_argument("--workers", type=int, default=4, help="number of cpu threads to use during batch generation")
parser.add_argument("--channels", type=int, default=3, help="number of image channels")
parser.add_argument('--decay_epoch', type=int, default=50, help='decay')
parser.add_argument("--lambda_seg", type=float, default=0.16, help="pixel-scale loss weight (alpha)")
parser.add_argument("--lambda_clf", type=float, default=0.04, help="image-scale loss weight (beta)")
parser.add_argument("--run_name", type=str, default="MVSS-Net", help="run name")
parser.add_argument("--log_interval", type=int, default=100, help="interval between saving image samples")
parser.add_argument("--checkpoint_interval", type=int, default=1000,
help="batch interval between model checkpoints")
parser.add_argument('--load_path', type=str, default=None, help='pretrained model or checkpoint for continued training')
parser.add_argument('--nGPU', type=int, default=1, help='number of gpus') # TODO: multiple GPU support
args = parser.parse_args()
print(args)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Initialize model
model = get_mvss(backbone='resnet50',
pretrained_base=True,
nclass=1,
sobel=True,
constrain=True,
n_input=args.channels,
).to(device)
# Losses that are built-in in PyTorch
criterion_clf = nn.BCEWithLogitsLoss().to(device)
# Load pretrained models
if args.load_path != None:
print('Load pretrained model: ' + args.load_path)
model.load_state_dict(torch.load(args.load_path))
# Tensor
Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.Tensor
# Time for log
logtm = datetime.now().strftime("%Y%m%d%H%M%S")
# Dataset
train_dataset = Datasets(args.train_paths_file, args.image_size)
valid_dataset = Datasets(args.valid_paths_file, args.image_size)
train_dataloader = DataLoader(dataset=train_dataset, batch_size=args.batch_size, num_workers=args.workers, shuffle=True,
pin_memory=True, drop_last=True)
valid_dataloader = DataLoader(dataset=valid_dataset, batch_size=args.batch_size, num_workers=args.workers, shuffle=False,
pin_memory=True, drop_last=True)
# Conversion from epoch to step/iter
decay_iter = args.decay_epoch * len(train_dataloader)
# Optimizers
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, betas=(args.b1, args.b2))
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer=optimizer,
step_size=decay_iter,
gamma=0.5)
# ----------
# Training
# ----------
os.makedirs("logs", exist_ok=True)
writer = SummaryWriter("logs/" + logtm + "_" + args.run_name)
checkpoint_dir = "checkpoints/" + logtm + "_" + args.run_name
os.makedirs(checkpoint_dir, exist_ok=True)
for epoch in range(args.epoch, args.n_epochs):
print("Starting Epoch ", epoch + 1)
# Record loss sum for training and validation per epoch by summing over dataloader
# training
train_epoch_total_seg = 0 # Pixel-scale loss
train_epoch_total_clf = 0 # Image-scale loss
train_epoch_total_edg = 0 # Edge loss
train_epoch_total_model = 0 # Total loss
train_epoch_steps = 0 # Track total number of iterations
# validation
valid_epoch_total_seg = 0 # Pixel-scale loss
valid_epoch_total_clf = 0 # Image-scale loss
valid_epoch_total_edg = 0 # Edge loss
valid_epoch_total_model = 0 # Total loss
valid_epoch_steps = 0 # Track total number of iterations
# Iterate over train_loader
for step, data in enumerate(train_dataloader):
curr_steps = epoch * len(train_dataloader) + step + 1
# Read from train_dataloader
train_in_imgs = Variable(data["input"].type(Tensor))
train_in_masks = Variable(data["mask"].type(Tensor))
train_in_edges = Variable(data["edge"].type(Tensor))
train_in_labels = Variable(data["label"].type(Tensor))
# ------------------
# Train Generators
# ------------------
optimizer.zero_grad()
# Prediction
train_out_edges, train_out_masks = model(train_in_imgs)
train_out_edges = torch.sigmoid(train_out_edges)
train_out_masks = torch.sigmoid(train_out_masks)
# Pixel-scale loss
loss_seg = dice_loss(train_in_masks, train_out_masks)
# Edge loss
# TODO: is it the same as the paper?
loss_edg = dice_loss(train_in_edges, train_out_edges)
# Image-scale loss (with GMP)
# TODO: GeM from MVSS-Net++
gmp = nn.MaxPool2d(args.image_size)
out_labels = gmp(train_out_masks).squeeze()
loss_clf = criterion_clf(train_in_labels, out_labels)
# Total loss
alpha = args.lambda_seg
beta = args.lambda_clf
weighted_loss_seg = alpha * loss_seg
weighted_loss_clf = beta * loss_clf
weighted_loss_edg = (1.0 - alpha - beta) * loss_edg
loss = weighted_loss_seg + weighted_loss_clf + weighted_loss_edg
# backward prop and step
loss.backward()
optimizer.step()
lr_scheduler.step()
# log losses for epoch
train_epoch_steps += 1
train_epoch_total_seg += weighted_loss_seg
train_epoch_total_clf += weighted_loss_clf
train_epoch_total_edg += weighted_loss_edg
train_epoch_total_model += loss
# --------------
# Log Progress (for certain steps)
# --------------
if step % args.log_interval == 0:
print(f"[Epoch {epoch + 1}/{args.n_epochs}] [Batch {step + 1}/{len(train_dataloader)}] "
f"[Total Loss {loss:.3f}]"
f"[Pixel-scale Loss {weighted_loss_seg:.3e}]"
f"[Edge Loss {weighted_loss_edg:.3e}]"
f"[Image-scale Loss {weighted_loss_clf:.3e}]"
f"")
writer.add_scalar("LearningRate", lr_scheduler.get_last_lr()[0],
curr_steps)
writer.add_scalar("Loss/Total Loss", loss, epoch * len(train_dataloader) + step)
writer.add_scalar("Loss/Pixel-scale", weighted_loss_seg, curr_steps)
writer.add_scalar("Loss/Edge", weighted_loss_edg, curr_steps)
writer.add_scalar("Loss/Image-scale", weighted_loss_clf, curr_steps)
in_imgs_rgb = bgr_to_rgb(train_in_imgs.clone().detach())
writer.add_images('Input Img', in_imgs_rgb, epoch * len(train_dataloader) + step)
writer.add_images('Input Mask', train_in_masks, epoch * len(train_dataloader) + step)
writer.add_images('Output Mask', train_out_masks, epoch * len(train_dataloader) + step)
writer.add_images('Input Edge', train_in_edges, epoch * len(train_dataloader) + step)
writer.add_images('Output Edge', train_out_edges, epoch * len(train_dataloader) + step)
# save model parameters
# TODO: you can change when the parameters are saved
if step % args.checkpoint_interval == 0:
tm = datetime.now().strftime("%Y%m%d%H%M%S")
torch.save(model.state_dict(),
os.path.join(checkpoint_dir, tm + '_' + args.run_name + '_' + str(
epoch + 1) + "_" + str(step + 1) + '.pth'))
# Iterate over valid_loader
with torch.no_grad():
for step, data in enumerate(valid_dataloader):
# Read from train_dataloader
valid_in_imgs = Variable(data["input"].type(Tensor))
valid_in_masks = Variable(data["mask"].type(Tensor))
valid_in_edges = Variable(data["edge"].type(Tensor))
valid_in_labels = Variable(data["label"].type(Tensor))
# Prediction
valid_out_edges, valid_out_masks = model(valid_in_imgs)
valid_out_edges = torch.sigmoid(valid_out_edges)
valid_out_masks = torch.sigmoid(valid_out_masks)
# Pixel-scale loss
loss_seg = dice_loss(valid_in_masks, valid_out_masks)
# Edge loss
loss_edg = dice_loss(valid_in_edges, valid_out_edges)
# Image-scale loss (with GMP)
gmp = nn.MaxPool2d(args.image_size)
out_labels = gmp(valid_out_masks).squeeze()
loss_clf = criterion_clf(valid_in_labels, out_labels)
# Total loss
alpha = args.lambda_seg
beta = args.lambda_clf
weighted_loss_seg = alpha * loss_seg
weighted_loss_clf = beta * loss_clf
weighted_loss_edg = (1.0 - alpha - beta) * loss_edg
loss = weighted_loss_seg + weighted_loss_clf + weighted_loss_edg
# log losses for epoch
valid_epoch_steps += 1
valid_epoch_total_seg += weighted_loss_seg
valid_epoch_total_clf += weighted_loss_clf
valid_epoch_total_edg += weighted_loss_edg
valid_epoch_total_model += loss
# Add images to tensorboard
if step % args.log_interval == 0:
in_imgs_rgb = bgr_to_rgb(valid_in_imgs.clone().detach())
writer.add_images('Input Img (valid set)', in_imgs_rgb, epoch * len(valid_dataloader) + step)
writer.add_images('Input Mask (valid set)', valid_in_masks, epoch * len(valid_dataloader) + step)
writer.add_images('Output Mask (valid set)', valid_out_masks, epoch * len(valid_dataloader) + step)
writer.add_images('Input Edge (valid set)', valid_in_edges, epoch * len(valid_dataloader) + step)
writer.add_images('Output Edge (valid set)', valid_out_edges, epoch * len(valid_dataloader) + step)
# --------------
# Log Progress (for epoch)
# --------------
# Training loss average for epoch
if (train_epoch_steps != 0):
train_epoch_avg_seg = train_epoch_total_seg / train_epoch_steps
train_epoch_avg_edg = train_epoch_total_edg / train_epoch_steps
train_epoch_avg_clf = train_epoch_total_clf / train_epoch_steps
train_epoch_avg_model = train_epoch_total_model / train_epoch_steps
print(f"[Epoch {epoch + 1}/{args.n_epochs}]"
f"[===== Train set ====]"
f"[Epoch Total Loss {train_epoch_avg_model:.3f}]"
f"[Epoch Pixel-scale Loss {train_epoch_avg_seg:.3e}]"
f"[Epoch Edge Loss {train_epoch_avg_edg:.3e}]"
f"[Epoch Image-scale Loss {train_epoch_avg_clf:.3e}]"
f"")
writer.add_scalar("Epoch LearningRate", lr_scheduler.get_last_lr()[0],
epoch)
# in_imgs_rgb = bgr_to_rgb(train_in_imgs.clone().detach())
# writer.add_images('Epoch Input Img (train set)', in_imgs_rgb, epoch)
# writer.add_images('Epoch Input Mask (train set)', train_in_masks, epoch)
# writer.add_images('Epoch Output Mask (train set)', train_out_masks, epoch)
# writer.add_images('Epoch Input Edge (train set)', train_in_edges, epoch)
# writer.add_images('Epoch Output Edge (train set)', train_out_edges, epoch)
# Validation loss average for epoch
if (valid_epoch_steps != 0):
valid_epoch_avg_seg = valid_epoch_total_seg / valid_epoch_steps
valid_epoch_avg_edg = valid_epoch_total_edg / valid_epoch_steps
valid_epoch_avg_clf = valid_epoch_total_clf / valid_epoch_steps
valid_epoch_avg_model = valid_epoch_total_model / valid_epoch_steps
print(f"[Epoch {epoch + 1}/{args.n_epochs}]"
f"[===== Validation set ====]"
f"[Epoch Total Loss {valid_epoch_avg_model:.3f}]"
f"[Epoch Pixel-scale Loss {valid_epoch_avg_seg:.3e}]"
f"[Epoch Edge Loss {valid_epoch_avg_edg:.3e}]"
f"[Epoch Image-scale Loss {valid_epoch_avg_clf:.3e}]"
f"")
# in_imgs_rgb = bgr_to_rgb(valid_in_imgs.clone().detach())
# writer.add_images('Epoch Input Img (valid set)', in_imgs_rgb, epoch)
# writer.add_images('Epoch Input Mask (valid set)', valid_in_masks, epoch)
# writer.add_images('Epoch Output Mask (valid set)', valid_out_masks, epoch)
# writer.add_images('Epoch Input Edge (valid set)', valid_in_edges, epoch)
# writer.add_images('Epoch Output Edge (valid set)', valid_out_edges, epoch)
# Write train and validation loss
writer.add_scalars('Epoch Loss/Total Loss',
{'train': train_epoch_avg_model,
'valid': valid_epoch_avg_model}, epoch)
writer.add_scalars('Epoch Loss/Pixel-scale',
{'train': train_epoch_avg_seg,
'valid': valid_epoch_avg_seg}, epoch)
writer.add_scalars('Epoch Loss/Edge',
{'train': train_epoch_avg_edg,
'valid': valid_epoch_avg_edg}, epoch)
writer.add_scalars('Epoch Loss/Image-scale',
{'train': train_epoch_avg_clf,
'valid': valid_epoch_avg_clf}, epoch)
print("Finished training")
if platform == "win32":
if __name__ == '__main__':
main()
else:
main()