This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 476
/
eval.py
177 lines (133 loc) · 5.03 KB
/
eval.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
"""
Converts a Video to SuperSloMo version
"""
from time import time
import click
import cv2
import torch
from PIL import Image
import numpy as np
import model
from torchvision import transforms
from torch.functional import F
torch.set_grad_enabled(False)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
trans_forward = transforms.ToTensor()
trans_backward = transforms.ToPILImage()
if device != "cpu":
mean = [0.429, 0.431, 0.397]
mea0 = [-m for m in mean]
std = [1] * 3
trans_forward = transforms.Compose([trans_forward, transforms.Normalize(mean=mean, std=std)])
trans_backward = transforms.Compose([transforms.Normalize(mean=mea0, std=std), trans_backward])
flow = model.UNet(6, 4).to(device)
interp = model.UNet(20, 5).to(device)
back_warp = None
def setup_back_warp(w, h):
global back_warp
with torch.set_grad_enabled(False):
back_warp = model.backWarp(w, h, device).to(device)
def load_models(checkpoint):
states = torch.load(checkpoint, map_location='cpu')
interp.load_state_dict(states['state_dictAT'])
flow.load_state_dict(states['state_dictFC'])
def interpolate_batch(frames, factor):
frame0 = torch.stack(frames[:-1])
frame1 = torch.stack(frames[1:])
i0 = frame0.to(device)
i1 = frame1.to(device)
ix = torch.cat([i0, i1], dim=1)
flow_out = flow(ix)
f01 = flow_out[:, :2, :, :]
f10 = flow_out[:, 2:, :, :]
frame_buffer = []
for i in range(1, factor):
t = i / factor
temp = -t * (1 - t)
co_eff = [temp, t * t, (1 - t) * (1 - t), temp]
ft0 = co_eff[0] * f01 + co_eff[1] * f10
ft1 = co_eff[2] * f01 + co_eff[3] * f10
gi0ft0 = back_warp(i0, ft0)
gi1ft1 = back_warp(i1, ft1)
iy = torch.cat((i0, i1, f01, f10, ft1, ft0, gi1ft1, gi0ft0), dim=1)
io = interp(iy)
ft0f = io[:, :2, :, :] + ft0
ft1f = io[:, 2:4, :, :] + ft1
vt0 = F.sigmoid(io[:, 4:5, :, :])
vt1 = 1 - vt0
gi0ft0f = back_warp(i0, ft0f)
gi1ft1f = back_warp(i1, ft1f)
co_eff = [1 - t, t]
ft_p = (co_eff[0] * vt0 * gi0ft0f + co_eff[1] * vt1 * gi1ft1f) / \
(co_eff[0] * vt0 + co_eff[1] * vt1)
frame_buffer.append(ft_p)
return frame_buffer
def load_batch(video_in, batch_size, batch, w, h):
if len(batch) > 0:
batch = [batch[-1]]
for i in range(batch_size):
ok, frame = video_in.read()
if not ok:
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = Image.fromarray(frame)
frame = frame.resize((w, h), Image.ANTIALIAS)
frame = frame.convert('RGB')
frame = trans_forward(frame)
batch.append(frame)
return batch
def denorm_frame(frame, w0, h0):
frame = frame.cpu()
frame = trans_backward(frame)
frame = frame.resize((w0, h0), Image.BILINEAR)
frame = frame.convert('RGB')
return np.array(frame)[:, :, ::-1].copy()
def convert_video(source, dest, factor, batch_size=10, output_format='mp4v', output_fps=30):
vin = cv2.VideoCapture(source)
count = vin.get(cv2.CAP_PROP_FRAME_COUNT)
w0, h0 = int(vin.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vin.get(cv2.CAP_PROP_FRAME_HEIGHT))
codec = cv2.VideoWriter_fourcc(*output_format)
vout = cv2.VideoWriter(dest, codec, float(output_fps), (w0, h0))
w, h = (w0 // 32) * 32, (h0 // 32) * 32
setup_back_warp(w, h)
done = 0
batch = []
while True:
batch = load_batch(vin, batch_size, batch, w, h)
if len(batch) == 1:
break
done += len(batch) - 1
intermediate_frames = interpolate_batch(batch, factor)
intermediate_frames = list(zip(*intermediate_frames))
for fid, iframe in enumerate(intermediate_frames):
vout.write(denorm_frame(batch[fid], w0, h0))
for frm in iframe:
vout.write(denorm_frame(frm, w0, h0))
try:
yield len(batch), done, count
except StopIteration:
break
vout.write(denorm_frame(batch[0], w0, h0))
vin.release()
vout.release()
@click.command('Evaluate Model by converting a low-FPS video to high-fps')
@click.argument('input')
@click.option('--checkpoint', help='Path to model checkpoint')
@click.option('--output', help='Path to output file to save')
@click.option('--batch', default=2, help='Number of frames to process in single forward pass')
@click.option('--scale', default=4, help='Scale Factor of FPS')
@click.option('--fps', default=30, help='FPS of output video')
def main(input, checkpoint, output, batch, scale, fps):
avg = lambda x, n, x0: (x * n/(n+1) + x0 / (n+1), n+1)
load_models(checkpoint)
t0 = time()
n0 = 0
fpx = 0
for dl, fd, fc in convert_video(input, output, int(scale), int(batch), output_fps=int(fps)):
fpx, n0 = avg(fpx, n0, dl / (time() - t0))
prg = int(100*fd/fc)
eta = (fc - fd) / fpx
print('\rDone: {:03d}% FPS: {:05.2f} ETA: {:.2f}s'.format(prg, fpx, eta) + ' '*5, end='')
t0 = time()
if __name__ == '__main__':
main()