This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
executable file
·246 lines (226 loc) · 7.68 KB
/
run.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
#!/usr/bin/env python
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace
from pathlib import Path
from typing import Optional
import cv2
import numpy as np
import torch
from config import Config, load_config
from dataloaders.loader import get_transform
from models.encoder import EncoderCNN
from models.rendering import RenderingCNN
from train import Trainer
from utils import (
crop_resize,
extend_bbox,
fmo_detect_maxarea,
imread,
imwrite,
rev_crop_resize,
rgba2hs,
)
class Runner:
def __init__(self, config: Config, load_folder: Path):
self.config = config
torch.backends.cudnn.benchmark = True
self.encoder = EncoderCNN()
self.rendering = RenderingCNN(config)
encoder_name = f"{Trainer.ENC_PREFIX}{Trainer.BEST_SUFFIX}.pt"
rendering_name = f"{Trainer.RENDER_PREFIX}{Trainer.BEST_SUFFIX}.pt"
self.encoder.load_state_dict(
torch.load(load_folder / encoder_name, map_location="cpu")
)
self.rendering.load_state_dict(
torch.load(load_folder / rendering_name, map_location="cpu")
)
self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
self.encoder = self.encoder.to(self.device)
self.rendering = self.rendering.to(self.device)
self.encoder.eval()
self.rendering.eval()
def process_image(
self,
im_path: Path,
bgr_path: Path,
output_folder: Path,
steps: int,
) -> None:
im = imread(str(im_path))
bgr = imread(str(bgr_path))
tsr = self._run_defmo(im, bgr, steps)
# generate results
out = cv2.VideoWriter(
str(output_folder / "tsr.avi"),
cv2.VideoWriter_fourcc(*"MJPG"),
6,
(im.shape[1], im.shape[0]),
True,
)
for ki in range(steps):
imwrite(tsr[..., ki], str(output_folder / f"tsr{ki}.png"))
out.write((tsr[:, :, [2, 1, 0], ki] * 255).astype(np.uint8))
out.release()
def process_video(
self,
video_path: Path,
output_folder: Path,
median: int,
steps: int,
) -> None:
# estimate initial background
Ims = []
cap = cv2.VideoCapture(str(video_path))
while cap.isOpened():
ret, frame = cap.read()
Ims.append(frame)
if len(Ims) >= median:
break
bgr = np.median(np.asarray(Ims) / 255, 0)[:, :, [2, 1, 0]]
# run DeFMO
out = cv2.VideoWriter(
str(output_folder / "tsr.avi"),
cv2.VideoWriter_fourcc(*"MJPG"),
6,
(bgr.shape[1], bgr.shape[0]),
True,
)
tsr0: Optional[np.ndarray] = None
frmi = 0
while cap.isOpened():
if frmi < median:
frame = Ims[frmi]
else:
ret, frame = cap.read()
if not ret:
break
Ims = Ims[1:]
Ims.append(frame)
# update background (running median)
bgr = np.median(np.asarray(Ims) / 255, 0)[:, :, [2, 1, 0]]
frmi += 1
im = frame[:, :, [2, 1, 0]] / 255
tsr = self._run_defmo(im, bgr, steps)
if frmi == 1:
tsr0 = tsr
continue
if frmi == 2:
assert tsr0 is not None
forward = np.min(
[
np.mean((tsr0[..., -1] - tsr[..., -1]) ** 2),
np.mean((tsr0[..., -1] - tsr[..., 0]) ** 2),
]
)
backward = np.min(
[
np.mean((tsr0[..., 0] - tsr[..., -1]) ** 2),
np.mean((tsr0[..., 0] - tsr[..., 0]) ** 2),
]
)
if backward < forward:
# reverse time direction for better alignment
tsr0 = tsr0[..., ::-1]
assert tsr0 is not None
for ki in range(steps):
out.write(
(tsr0[:, :, [2, 1, 0], ki] * 255).astype(np.uint8)
)
assert tsr0 is not None
if np.mean((tsr0[..., -1] - tsr[..., -1]) ** 2) < np.mean(
(tsr0[..., -1] - tsr[..., 0]) ** 2
):
# reverse time direction for better alignment
tsr = tsr[..., ::-1]
for ki in range(steps):
out.write((tsr[:, :, [2, 1, 0], ki] * 255).astype(np.uint8))
tsr0 = tsr
cap.release()
out.release()
def _run_defmo(
self,
im: np.ndarray,
bgr: np.ndarray,
steps: int,
) -> np.ndarray:
preprocess = get_transform(self.config.normalize)
bbox, radius = fmo_detect_maxarea(im, bgr, maxarea=0.03)
bbox = extend_bbox(
bbox.copy(),
4 * np.max(radius),
self.config.resolution_y / self.config.resolution_x,
im.shape,
)
im_crop = crop_resize(
im, bbox, (self.config.resolution_x, self.config.resolution_y)
)
bgr_crop = crop_resize(
bgr, bbox, (self.config.resolution_x, self.config.resolution_y)
)
input_batch = (
torch.cat((preprocess(im_crop), preprocess(bgr_crop)), 0)
.to(self.device)
.unsqueeze(0)
.float()
)
with torch.no_grad():
latent = self.encoder(input_batch)
times = torch.linspace(0, 1, steps).to(self.device)
renders = self.rendering(latent, times[None])
renders_rgba = (
renders[0].data.cpu().detach().numpy().transpose(2, 3, 1, 0)
)
tsr_crop = rgba2hs(renders_rgba, bgr_crop)
tsr = rev_crop_resize(tsr_crop, bbox, bgr.copy())
tsr[tsr > 1] = 1
tsr[tsr < 0] = 0
return tsr
def main(args: Namespace) -> None:
config = load_config(args.config)
output_folder = args.output_folder.expanduser()
if not output_folder.exists():
output_folder.mkdir(parents=True)
runner = Runner(config, args.load_folder.expanduser())
if args.im is not None and args.bgr is not None:
runner.process_image(
args.im,
args.bgr,
output_folder,
args.steps,
)
elif args.video is not None:
runner.process_video(
args.video,
output_folder,
args.median,
args.steps,
)
else:
print("You should either provide both --im and --bgr, or --video.")
if __name__ == "__main__":
parser = ArgumentParser(
description="Run the DeFMO model on input images/videos",
epilog="You must either give both --im and --bgr, or give --video",
formatter_class=ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"load_folder", type=Path, help="Path from where to load saved models"
)
parser.add_argument("--im", type=Path, help="Path to the input image")
parser.add_argument(
"--bgr", type=Path, help="Path to the input background"
)
parser.add_argument("--video", type=Path, help="Path to the input video")
parser.add_argument(
"--config", type=Path, help="Path to the TOML hyper-param config"
)
parser.add_argument(
"--output-folder",
type=Path,
default="output",
help="Path where to dump outputs",
)
parser.add_argument("--steps", type=int, default=24)
parser.add_argument("--median", type=int, default=7)
main(parser.parse_args())