-
Notifications
You must be signed in to change notification settings - Fork 18
/
train.py
296 lines (250 loc) · 9.3 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
import os
import shutil
import datetime
import argparse
import torch
import numpy as np
from torch.utils.data import DataLoader
import utility
from loss import Loss
from datasets import Vimeo90K_interp
from mytest import Middlebury_other
from models.cdfi_adacof import CDFI_adacof
def parse_args():
parser = argparse.ArgumentParser(
description="Compression-Driven Frame Interpolation Training"
)
# parameters
# Model Selection
parser.add_argument("--model", type=str, default="cdfi_adacof")
# Hardware Setting
parser.add_argument("--gpu_id", type=int, default=0)
parser.add_argument("--use_cudnn", type=bool, default=True)
# Directory Setting
parser.add_argument("--data_dir", type=str, default="./vimeo_triplet/")
parser.add_argument("--uid", type=str, default=None)
parser.add_argument("--checkpoint", type=str, default=None)
parser.add_argument(
"--force", action="store_true", help="force to override the given uid"
)
parser.add_argument(
"--test_input", type=str, default="./test_data/middlebury_others/input"
)
parser.add_argument(
"--test_gt", type=str, default="./test_data/middlebury_others/gt"
)
# Learning Options
parser.add_argument("--epochs", type=int, default=100, help="Max Epochs")
parser.add_argument("--batch_size", type=int, default=8, help="Batch size")
parser.add_argument(
"--loss",
type=str,
default="1*Charb+0.01*g_Spatial+0.005*VGG",
help="loss function configuration",
)
parser.add_argument(
"--num_training_samples", type=int, default=-1, help="Traning sub dataset size"
)
# Optimization specifications
parser.add_argument("--lr", type=float, default=0.001, help="learning rate")
parser.add_argument(
"--lr_decay", type=int, default=20, help="learning rate decay per N epochs"
)
parser.add_argument(
"--decay_type", type=str, default="step", help="learning rate decay type"
)
parser.add_argument(
"--gamma",
type=float,
default=0.5,
help="learning rate decay factor for step decay",
)
parser.add_argument(
"--optimizer",
default="ADAMax",
choices=("SGD", "ADAM", "RMSprop", "ADAMax", "OBProxSG"),
help="optimizer to use (SGD | ADAM | RMSprop | ADAMax | OBProxSG)",
)
parser.add_argument("--weight_decay", type=float, default=0, help="weight decay")
parser.add_argument(
"--lambda_",
type=float,
default=1e-4,
help="regularization parameter for L1 optimization",
)
parser.add_argument(
"--Np", type=int, default=5, help="number of P-steps in OBProxSG"
)
# Options for AdaCoF
parser.add_argument("--kernel_size", type=int, default=11)
parser.add_argument("--dilation", type=int, default=2)
parser.add_argument("--inactive", action="store_true", default=None)
args = parser.parse_args()
if not args.inactive:
if args.uid is None:
unique_id = str(np.random.randint(0, 100000))
print("revise the unique id to a random number " + str(unique_id))
args.uid = unique_id
timestamp = datetime.datetime.now().strftime("%a-%b-%d-%H-%M")
save_path = "./model_weights/" + args.uid + "-" + timestamp
else:
save_path = "./model_weights/" + str(args.uid)
if not os.path.exists(save_path + "/best" + ".pth"):
os.makedirs(save_path, exist_ok=True)
else:
if not args.force:
raise ("please use another uid ")
else:
print("override this uid" + args.uid)
for m in range(1, 10):
if not os.path.exists(save_path + "/log.txt.bk" + str(m)):
shutil.copy(
save_path + "/log.txt", save_path + "/log.txt.bk" + str(m)
)
shutil.copy(
save_path + "/args.txt", save_path + "/args.txt.bk" + str(m)
)
break
parser.add_argument(
"--save_path", default=save_path, help="the output dir of weights"
)
parser.add_argument(
"--log", default=save_path + "/log.txt", help="the log file in training"
)
parser.add_argument(
"--arg", default=save_path + "/args.txt", help="the args used"
)
args = parser.parse_args()
with open(args.log, "w") as f:
f.close()
with open(args.arg, "w") as f:
print(args)
print(args, file=f)
f.close()
if args.use_cudnn:
print("cudnn is used")
torch.backends.cudnn.benchmark = True
else:
print("cudnn is not used")
torch.backends.cudnn.benchmark = False
return args
class Trainer:
def __init__(
self, args, train_loader, test_loader, my_model, my_loss, start_epoch=1
):
self.args = args
self.train_loader = train_loader
self.max_step = self.train_loader.__len__()
self.test_loader = test_loader
self.model = my_model
self.loss = my_loss
self.current_epoch = start_epoch
self.save_path = args.save_path
self.optimizer = utility.make_optimizer(args, self.model)
self.scheduler = utility.make_scheduler(args, self.optimizer)
if not os.path.exists(args.save_path):
os.makedirs(args.save_path)
self.result_dir = args.save_path + "/results"
self.ckpt_dir = args.save_path + "/checkpoints"
if not os.path.exists(self.result_dir):
os.makedirs(self.result_dir)
if not os.path.exists(self.ckpt_dir):
os.makedirs(self.ckpt_dir)
self.logfile = open(args.log, "w")
# Initial Test
self.model.eval()
self.best_psnr = self.test_loader.test(
self.model,
self.result_dir,
output_name=str(self.current_epoch).zfill(3),
file_stream=self.logfile,
)
def train(self):
# Train
self.model.train()
for batch_idx, (frame0, frame1, frame2) in enumerate(self.train_loader):
frame0 = frame0.cuda()
frame1 = frame1.cuda()
frame2 = frame2.cuda()
self.optimizer.zero_grad()
output = self.model(frame0, frame2)
loss = self.loss(output, frame1)
loss.backward()
self.optimizer.step()
if batch_idx % 100 == 0:
torch.save(
{
"epoch": self.current_epoch,
"state_dict": self.model.state_dict(),
},
self.ckpt_dir + "/real_time.pth",
)
utility.print_and_save(
"{:<13s}{:<14s}{:<6s}{:<16s}{:<12s}{:<20.16f}".format(
"Train Epoch: ",
"["
+ str(self.current_epoch)
+ "/"
+ str(self.args.epochs)
+ "]",
"Step: ",
"[" + str(batch_idx) + "/" + str(self.max_step) + "]",
"train loss: ",
loss.item(),
),
self.logfile,
)
self.current_epoch += 1
self.scheduler.step()
utility.print_and_save(
"===== current lr: %f =====" % (self.optimizer.param_groups[0]["lr"]),
self.logfile,
)
def test(self):
utility.print_and_save("Testing...", self.logfile)
# Test
self.model.eval()
tmp_psnr = self.test_loader.test(
self.model,
self.result_dir,
output_name=str(self.current_epoch).zfill(3),
file_stream=self.logfile,
)
if tmp_psnr > self.best_psnr:
self.best_psnr = tmp_psnr
torch.save(
{"epoch": self.current_epoch, "state_dict": self.model.state_dict()},
self.ckpt_dir
+ "/model_epoch_"
+ str(self.current_epoch).zfill(3)
+ ".pth",
)
def terminate(self):
return self.current_epoch >= self.args.epochs
def close(self):
self.logfile.close()
def main():
args = parse_args()
torch.cuda.set_device(args.gpu_id)
# prepare training data
train_dataset, val_dataset = Vimeo90K_interp(args.data_dir)
train_loader = DataLoader(
dataset=train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=8
)
# val_loader = DataLoader(dataset=val_dataset, batch_size=args.batch_size, shuffle=True, num_workers=8)
# prepare test data
test_db = Middlebury_other(args.test_input, args.test_gt)
# initialize our model
model = CDFI_adacof(args).cuda()
print("# of model parameters is: " + str(utility.count_network_parameters(model)))
# prepare the loss
loss = Loss(args)
# prepare the trainer
my_trainer = Trainer(args, train_loader, test_db, model, loss)
# start training
while not my_trainer.terminate():
my_trainer.train()
my_trainer.test()
my_trainer.close()
if __name__ == "__main__":
main()