-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinference_finetuning_video.py
381 lines (299 loc) · 12.8 KB
/
inference_finetuning_video.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import torch
from tqdm import tqdm
from pathlib import Path
from nlut_models import *
import torch.utils.data as data
from PIL import Image
from utils.losses import *
from parameter_finetuning import *
from torch.utils import data
import torch.nn as nn
from torchvision.utils import save_image
import time
import numpy as np
import os
import cv2
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
print(f'now device is {device}')
def train_transform():
transform_list = [
transforms.Resize(size=(256, 256)),
transforms.ToTensor()
]
return transforms.Compose(transform_list)
class AverageMeter(object):
"""
Keeps track of most recent, average, sum, and count of a metric.
"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class FlatFolderDataset(data.Dataset):
def __init__(self, root, transform):
super(FlatFolderDataset, self).__init__()
self.root = root
self.paths = os.listdir(self.root)
self.transform = transform
def __getitem__(self, index):
path = self.paths[index]
img = Image.open(os.path.join(self.root, path)).convert('RGB')
img = self.transform(img)
return img
def __len__(self):
return len(self.paths)
def name(self):
return 'FlatFolderDataset'
def InfiniteSampler(n):
# i = 0
i = n - 1
order = np.random.permutation(n)
while True:
yield order[i]
i += 1
if i >= n:
np.random.seed()
order = np.random.permutation(n)
i = 0
class InfiniteSamplerWrapper(data.sampler.Sampler):
def __init__(self, data_source):
self.num_samples = len(data_source)
def __iter__(self):
return iter(InfiniteSampler(self.num_samples))
def __len__(self):
return 2 ** 31
def adjust_learning_rate(optimizer, iteration_count, opt):
"""Imitating the original implementation"""
# lr = opt.lr / (1.0 + opt.lr_decay * iteration_count)
lr = opt.lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def p_transform():
transform_list = [transforms.ToTensor()]
return transforms.Compose(transform_list)
def train_transform2():
transform_list = [
transforms.Resize(size=(256, 256)),
transforms.RandomCrop(256),
transforms.ToTensor()
]
return transforms.Compose(transform_list)
def finetuning_train(opt, original=None, example=None):
content_tf = train_transform()
style_tf = train_transform()
if original != None:
content_images = content_tf(Image.open(
original).convert('RGB')).unsqueeze(0).to(device)
content_images = content_images.repeat(opt.batch_size, 1, 1, 1)
else:
content_dataset = FlatFolderDataset(opt.content_dir, content_tf)
content_iter = iter(data.DataLoader(
content_dataset, batch_size=opt.batch_size,
sampler=InfiniteSamplerWrapper(content_dataset),
num_workers=opt.n_threads))
if example != None:
style_images = style_tf(Image.open(
example).convert('RGB')).unsqueeze(0).to(device)
style_images = style_images.repeat(opt.batch_size, 1, 1, 1)
else:
style_dataset = FlatFolderDataset(opt.style_dir, style_tf)
style_iter = iter(data.DataLoader(
style_dataset, batch_size=opt.batch_size,
sampler=InfiniteSamplerWrapper(style_dataset),
num_workers=opt.n_threads))
if opt.batch_size == 1:
# content_images = content_images
# style_images = style_images
content_images = torch.cat([content_images, content_images], dim=0)
style_images = torch.cat([style_images, style_images], dim=0)
model = NLUTNet(opt.model, dim=opt.dim).to(device)
print('Total params: %.2fM' % (sum(p.numel()
for p in model.parameters()) / 1000000.0))
if opt.pretrained:
if os.path.isfile(opt.pretrained):
print("--------loading checkpoint----------")
print("=> loading checkpoint '{}'".format(opt.pretrained))
checkpoint = torch.load(opt.pretrained)
model.load_state_dict(checkpoint['state_dict'])
else:
print("--------no checkpoint found---------")
model.train()
TVMN_temp = TVMN(opt.dim).to(device)
# optimizer = torch.optim.Adam(model.module.parameters(), lr=opt.lr)
optimizer = torch.optim.Adam(model.parameters(), lr=opt.lr)
log_c = []
log_s = []
# log_mse = []
Time = time.time()
losses = AverageMeter()
c_losses = AverageMeter()
s_losses = AverageMeter()
# mse_losses = AverageMeter()
tv_losses = AverageMeter()
mn_losses = AverageMeter()
# -----------------------training------------------------
for i in range(opt.start_iter, opt.max_iter):
adjust_learning_rate(optimizer, iteration_count=i, opt=opt)
if original == None:
content_images = next(content_iter).to(device)
if example == None:
style_images = next(style_iter).to(device)
stylized, st_out, others = model(
content_images, content_images, style_images, TVMN=TVMN_temp)
tvmn = others.get("tvmn")
mn_cons = opt.lambda_smooth * \
(tvmn[0]+10*tvmn[2]) + opt.lambda_mn*tvmn[1]
loss_c, loss_s = model.encoder(content_images, style_images, stylized)
loss_c = loss_c.mean()
loss_s = loss_s.mean()
# loss_mse = mseloss(content_images, stylized)
loss_style = opt.content_weight*loss_c + opt.style_weight * \
loss_s + opt.mn_cons_weight*mn_cons # +tv_cons
# optimizer update
optimizer.zero_grad()
loss_style.backward()
nn.utils.clip_grad_norm_(model.parameters(), max_norm=0.2)
optimizer.step()
# update loss log
log_c.append(loss_c.item())
log_s.append(loss_s.item())
# log_mse.append(loss_mse.item())
losses.update(loss_style.item())
c_losses.update(loss_c.item())
s_losses.update(loss_s.item())
# mse_losses.update(loss_mse.item())
mn_losses.update(mn_cons.item())
# save image
if i % opt.print_interval == 0 or (i + 1) == opt.max_iter:
if opt.batch_size == 1:
content_image, style_image, stylized = content_images[
:1], style_images[:1], stylized[:1]
output_name = os.path.join(opt.save_dir, "%06d.jpg" % i)
output_images = torch.cat(
(content_image.cpu(), style_image.cpu(), stylized.cpu()), 0)
save_image(stylized.cpu(), output_name, nrow=opt.batch_size)
else:
output_name = os.path.join(opt.save_dir, "%06d.jpg" % i)
output_images = torch.cat(
(content_images.cpu(), style_images.cpu(), stylized.cpu()), 0)
current_lr = optimizer.state_dict()['param_groups'][0]['lr']
print("iter %d time/iter: %.2f lr: %.6f loss_mn: %.4f loss_c: %.4f loss_s: %.4f losses: %.4f " % (i,
(time.time(
)-Time)/opt.print_interval,
current_lr,
mn_losses.avg,
c_losses.avg, s_losses.avg,
losses.avg
))
log_c = []
log_s = []
Time = time.time()
if (i + 1) % opt.save_model_interval == 0 or (i + 1) == opt.max_iter:
state_dict = model.state_dict()
for key in state_dict.keys():
state_dict[key] = state_dict[key].to(torch.device('cpu'))
state = {'iter': i, 'state_dict': state_dict,
'optimizer': optimizer.state_dict()}
torch.save(state, opt.resume)
torch.save(state, "./"+opt.save_dir+"/" +
str(i)+"_finetuning_style_lut.pth")
def get_lut(opt, original, example):
# opt = setting.opt
model = NLUTNet(opt.model, dim=opt.dim).to(device)
print('Total params: %.2fM' % (sum(p.numel()
for p in model.parameters()) / 1000000.0))
if opt.resume:
if os.path.isfile(opt.resume):
print("--------loading checkpoint----------")
print("=> loading checkpoint '{}'".format(opt.resume))
checkpoint = torch.load(opt.resume)
opt.start_iter = checkpoint['iter']
model.load_state_dict(checkpoint['state_dict'])
else:
print("--------no checkpoint found---------")
model.train()
TVMN_temp = TVMN(opt.dim).to(device)
content_tf2 = train_transform2()
content_images = content_tf2(Image.open(
original).convert('RGB')).unsqueeze(0).to(device)
style_images = content_tf2(Image.open(
example).convert('RGB')).unsqueeze(0).to(device)
content_images = content_images.repeat(2, 1, 1, 1)
style_images = style_images.repeat(2, 1, 1, 1)
stylized, st_out, others = model(
content_images, content_images, style_images, TVMN=TVMN_temp)
# save_image(stylized, "output_name.png", nrow=opt.batch_size)
LUT = others.get("LUT")
return LUT[:1]
def draw_video(target_mask, original_path, reference_mask, corrected_mask, LUT):
sigmod_infer = nn.Sigmoid()
cap_target_src = cv2.VideoCapture(target_mask)
src_true, original = cap_target_src.read()
example = reference_mask
# 一些变换 toPIL&nb
# LUT = infer(original_path, example)
content_tf = p_transform()
TrilinearInterpo = TrilinearInterpolation()
Path(corrected_mask).parent.mkdir(parents=True, exist_ok=True)
cap_target = cv2.VideoCapture(target_mask)
cap_reference = content_tf(Image.open(
reference_mask).convert('RGB')).unsqueeze(0).to(device)
width = int(cap_target.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap_target.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = int(cap_target.get(cv2.CAP_PROP_FOURCC))
fps = cap_target.get(cv2.CAP_PROP_FPS) if fourcc else 0
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
cap_corrected = cv2.VideoWriter(
corrected_mask, fourcc, fps, (width, height))
# frame_count = int(cap_target.get(cv2.CAP_PROP_FRAME_COUNT))
frame_count = (cap_target.get(cv2.CAP_PROP_FRAME_COUNT))
all_time = 0
frame = 0
try:
with tqdm(desc=f"Frames", total=frame_count) as pbar:
while all(cap.isOpened() for cap in (cap_target, cap_corrected)):
ret_target, target = cap_target.read()
if not ret_target:
break
target = cv2.cvtColor(target, cv2.COLOR_BGR2RGB)
target = Image.fromarray(target)
target = content_tf(target).unsqueeze(0).to(device)
start_time = time.time()
img_res = TrilinearInterpo(LUT, target)
img_out = img_res+target
# img_out = sigmod_infer(img_out)
img_out = torch.squeeze(img_out, dim=0)
img_out = torch.permute(img_out, (1, 2, 0))
# 结束时间
end_time = time.time()
all_time = all_time+(end_time-start_time)
corrected = img_out.detach().cpu().numpy()*255
corrected = np.uint8(np.clip(corrected, 0, 255))
corrected = cv2.cvtColor(corrected, cv2.COLOR_RGB2BGR)
cap_corrected.write(corrected)
pbar.update(1)
frame = frame + 1
print(f'all fps: {fps/all_time}')
average_time = 1000.0*all_time/frame # ms
print(f'average time: {average_time}')
finally:
cap_target.release()
cap_corrected.release()
if __name__ == '__main__':
opt = parser.parse_args()
original = opt.content_path
example = opt.style_path
src_video = opt.src_video
dst_video = opt.dst_video
finetuning_train(opt, original, example)
lut = get_lut(opt, original, example)
draw_video(src_video, original, example, dst_video, lut)
print('save to: {}'.format(dst_video))