-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
178 lines (157 loc) · 6.43 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
from torch.utils.tensorboard import SummaryWriter
from torch import optim
from torch.optim.lr_scheduler import LambdaLR
from data import load_data
from ssf import VideoCompressionModel
from utils import get_batch_psnr
import torch
import torch.nn.functional as F
import config
import os
import argparse
import shutil
parser = argparse.ArgumentParser(description='values from bash script')
parser.add_argument('--beta', type=float, required=True, help='beta value for single rate model')
parser.add_argument('--device', type=int, required=True, help='cuda device')
parser.add_argument('--mtype', type=str, required=True, help='mode type')
args = parser.parse_args()
def schedule_func(ep):
return max(config.decay ** ep, config.minf)
# model and data config
model = VideoCompressionModel(
config.filter_size,
config.hyper_filter_size,
True if "transform" in args.mtype else False,
True if "cond" in args.mtype else False,
vbr_dim=0
)
train_data, val_data = load_data(
config.data_config, config.batch_size, pin_memory=False, num_workers=8
)
model_name = f'{config.data_config["dataset_name"]}-type_{args.mtype}-beta_{args.beta}-vbr_0'
if not os.path.isdir("./model_params"):
os.mkdir("./model_params")
if config.model_checkpoint:
loaded = torch.load(f"./model_params/{model_name}.pt", map_location=lambda storage, loc: storage)
model.load_state_dict(loaded)
print("model loaded")
model = model.to(args.device)
# training config
median_params = model.median_params()
optimizer = optim.Adam(
[
{"params": model.main_params(), "lr": config.lr},
{"params": median_params[0], "lr": config.lr * 10},
{"params": median_params[1], "lr": config.lr * 10},
{"params": median_params[2], "lr": config.lr * 10}
]
)
scheduler = LambdaLR(optimizer, lr_lambda=[schedule_func for i in range(4)])
if config.opt_checkpoint:
loaded = torch.load(f"./model_params/{model_name}-opt.pt", map_location=lambda storage, loc: storage)
optimizer.load_state_dict(loaded)
print("optimizer loaded")
def train(batch, beta):
model.train()
optimizer.zero_grad()
batch = batch.to(args.device)
state = model(batch)
mse = F.mse_loss(state["output"], batch)
bpp = state["bpp"].mean()
loss = beta * bpp + mse
loss.backward()
median_losses = model.compute_extra_loss()
for mloss in median_losses:
mloss.backward()
torch.nn.utils.clip_grad_norm_(model.main_params(), config.grad_clip)
optimizer.step()
with torch.no_grad():
psnr = [get_batch_psnr(state["output"][i].clamp(0, 1), batch[i], 1.0) for i in range(batch.shape[0])]
state["psnr"] = torch.stack(psnr, 0)
state["loss"] = loss
state["mse"] = mse
return state
def test(batch):
model.eval()
with torch.no_grad():
batch = batch.to(args.device)
state = model(batch)
psnr = [get_batch_psnr(state["output"][i].clamp(0, 1), batch[i], 1.0) for i in range(batch.shape[0])]
state["psnr"] = torch.stack(psnr, 0)
return state
finish = False
if os.path.isdir(f'{config.tbdir}/{args.mtype}/{model_name}'):
shutil.rmtree(f'{config.tbdir}/{args.mtype}/{model_name}')
writer = SummaryWriter(f"{config.tbdir}/{args.mtype}/{model_name}")
step = 0
while True:
for train_batch_idx, train_batch in enumerate(train_data):
state = train(train_batch, args.beta)
if step % config.log_checkpoint_step == 0:
writer.add_images(
"train/recon_sample", state["output"][:, 0, ...].clamp(0, 1), step // config.log_checkpoint_step
)
writer.add_images(
"train/warped_sample",
state["warped"][:, 0, ...],
step // config.log_checkpoint_step,
)
writer.add_scalar("train/loss", state["loss"], step // config.log_checkpoint_step)
writer.add_scalar("train/mse", state["mse"], step // config.log_checkpoint_step)
writer.add_scalar("train/bpp", state["bpp"].mean(), step // config.log_checkpoint_step)
writer.add_text(
"train/bpp_seq_per_batch",
"--".join("%.4f" % e for e in state["bpp"].tolist()),
step // config.log_checkpoint_step,
)
writer.add_text(
"train/psnr_seq_per_batch",
"--".join("%.3f" % e for e in state["psnr"].tolist()),
step // config.log_checkpoint_step,
)
# validation
num_of_sample = 0
bpp_seq = 0
psnr_seq = 0
bpp = 0
for test_batch_idx, test_batch in enumerate(val_data):
state = test(test_batch)
if test_batch_idx == 0:
writer.add_images(
"val/recon_sample",
state["output"][:, 0, ...].clamp(0, 1),
step // config.log_checkpoint_step,
)
writer.add_images(
"val/warped_sample",
state["warped"][:, 0, ...],
step // config.log_checkpoint_step,
)
num_of_sample += test_batch.shape[1]
bpp_seq += state["bpp"] * test_batch.shape[1]
psnr_seq += state["psnr"] * test_batch.shape[1]
bpp += state["bpp"].mean() * test_batch.shape[1]
writer.add_scalar("val/bpp", bpp / num_of_sample, step // config.log_checkpoint_step)
writer.add_text(
"val/bpp_seq_per_batch",
"--".join("%.4f" % e for e in (bpp_seq / num_of_sample).tolist()),
step // config.log_checkpoint_step,
)
writer.add_text(
"val/psnr_seq_per_batch",
"--".join("%.3f" % e for e in (psnr_seq / num_of_sample).tolist()),
step // config.log_checkpoint_step,
)
step += 1
if step % config.save_step == 0:
torch.save(model.state_dict(), f"./model_params/{model_name}.pt")
torch.save(optimizer.state_dict(), f"./model_params/{model_name}-opt.pt")
if (step % config.scheduler_step == 0) and (step != 0):
scheduler.step()
if step > config.n_step:
finish = True
break
if finish:
break
torch.save(model.state_dict(), f"./model_params/{model_name}.pt")
torch.save(optimizer.state_dict(), f"./model_params/{model_name}-opt.pt")