-
Notifications
You must be signed in to change notification settings - Fork 7
/
propainter_inference.py
341 lines (294 loc) · 12.1 KB
/
propainter_inference.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
from dataclasses import dataclass, field
import numpy as np
from numpy.typing import NDArray
import torch
from tqdm import tqdm
from .model.modules.flow_comp_raft import RAFT_bi
from .model.recurrent_flow_completion import (
RecurrentFlowCompleteNet,
)
from ComfyUI_ProPainter_Nodes.utils.model_utils import Models
from .model.propainter import InpaintGenerator
@dataclass
class ProPainterConfig:
ref_stride: int
neighbor_length: int
subvideo_length: int
raft_iter: int
fp16: str
video_length: int
device: torch.device
process_size: tuple[int, int]
use_half: bool = field(init=False)
def __post_init__(self) -> None:
"""Initialize use-half."""
self.use_half = self.fp16 == "enable"
if self.device == torch.device("cpu"):
self.use_half = False
def get_ref_index(
mid_neighbor_id: int,
neighbor_ids: list[int],
config: ProPainterConfig,
ref_num: int = -1,
) -> list[int]:
"""Calculate reference indices for frames based on the provided parameters."""
ref_index = []
if ref_num == -1:
for i in range(0, config.video_length, config.ref_stride):
if i not in neighbor_ids:
ref_index.append(i)
else:
start_idx = max(0, mid_neighbor_id - config.ref_stride * (ref_num // 2))
end_idx = min(
config.video_length, mid_neighbor_id + config.ref_stride * (ref_num // 2)
)
for i in range(start_idx, end_idx, config.ref_stride):
if i not in neighbor_ids:
if len(ref_index) > ref_num:
break
ref_index.append(i)
return ref_index
def compute_flow(
raft_model: RAFT_bi, frames: torch.Tensor, config: ProPainterConfig
) -> tuple[torch.Tensor, torch.Tensor]:
"""Compute forward and backward optical flows using the RAFT model."""
if frames.size(dim=-1) <= 640:
short_clip_len = 12
elif frames.size(dim=-1) <= 720:
short_clip_len = 8
elif frames.size(dim=-1) <= 1280:
short_clip_len = 4
else:
short_clip_len = 2
# use fp32 for RAFT
if frames.size(dim=1) > short_clip_len:
gt_flows_f_list, gt_flows_b_list = [], []
for chunck in range(0, config.video_length, short_clip_len):
end_f = min(config.video_length, chunck + short_clip_len)
if chunck == 0:
flows_f, flows_b = raft_model(
frames[:, chunck:end_f], iters=config.raft_iter
)
else:
flows_f, flows_b = raft_model(
frames[:, chunck - 1 : end_f], iters=config.raft_iter
)
gt_flows_f_list.append(flows_f)
gt_flows_b_list.append(flows_b)
torch.cuda.empty_cache()
gt_flows_f = torch.cat(gt_flows_f_list, dim=1)
gt_flows_b = torch.cat(gt_flows_b_list, dim=1)
gt_flows_bi = (gt_flows_f, gt_flows_b)
else:
gt_flows_bi = raft_model(frames, iters=config.raft_iter)
torch.cuda.empty_cache()
return gt_flows_bi
def complete_flow(
recurrent_flow_model: RecurrentFlowCompleteNet,
flows_tuple: tuple[torch.Tensor, torch.Tensor],
flow_masks: torch.Tensor,
subvideo_length: int,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Complete and refine optical flows using a recurrent flow completion model.
This function processes optical flows in chunks if the total length exceeds the specified
subvideo length. It uses a recurrent model to complete and refine the flows, combining
forward and backward flows into bidirectional flows.
"""
flow_length = flows_tuple[0].size(dim=1)
if flow_length > subvideo_length:
pred_flows_f_list, pred_flows_b_list = [], []
pad_len = 5
for f in range(0, flow_length, subvideo_length):
s_f = max(0, f - pad_len)
e_f = min(flow_length, f + subvideo_length + pad_len)
pad_len_s = max(0, f) - s_f
pad_len_e = e_f - min(flow_length, f + subvideo_length)
pred_flows_bi_sub, _ = recurrent_flow_model.forward_bidirect_flow(
(flows_tuple[0][:, s_f:e_f], flows_tuple[1][:, s_f:e_f]),
flow_masks[:, s_f : e_f + 1],
)
pred_flows_bi_sub = recurrent_flow_model.combine_flow(
(flows_tuple[0][:, s_f:e_f], flows_tuple[1][:, s_f:e_f]),
pred_flows_bi_sub,
flow_masks[:, s_f : e_f + 1],
)
pred_flows_f_list.append(
pred_flows_bi_sub[0][:, pad_len_s : e_f - s_f - pad_len_e]
)
pred_flows_b_list.append(
pred_flows_bi_sub[1][:, pad_len_s : e_f - s_f - pad_len_e]
)
torch.cuda.empty_cache()
pred_flows_f = torch.cat(pred_flows_f_list, dim=1)
pred_flows_b = torch.cat(pred_flows_b_list, dim=1)
pred_flows_bi = (pred_flows_f, pred_flows_b)
else:
pred_flows_bi, _ = recurrent_flow_model.forward_bidirect_flow(
flows_tuple, flow_masks
)
pred_flows_bi = recurrent_flow_model.combine_flow(
flows_tuple, pred_flows_bi, flow_masks
)
torch.cuda.empty_cache()
return pred_flows_bi
def image_propagation(
inpaint_model: InpaintGenerator,
frames: torch.Tensor,
masks_dilated: torch.Tensor,
prediction_flows: tuple[torch.Tensor, torch.Tensor],
config: ProPainterConfig,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Propagate inpainted images across video frames.
If the video length exceeds a defined threshold, the process is segmented and handled in chunks.
"""
process_width, process_height = config.process_size
masked_frames = frames * (1 - masks_dilated)
subvideo_length_img_prop = min(
100, config.subvideo_length
) # ensure a minimum of 100 frames for image propagation
if config.video_length > subvideo_length_img_prop:
updated_frames_list, updated_masks_list = [], []
pad_len = 10
for f in range(0, config.video_length, subvideo_length_img_prop):
s_f = max(0, f - pad_len)
e_f = min(config.video_length, f + subvideo_length_img_prop + pad_len)
pad_len_s = max(0, f) - s_f
pad_len_e = e_f - min(config.video_length, f + subvideo_length_img_prop)
b, t, _, _, _ = masks_dilated[:, s_f:e_f].size()
pred_flows_bi_sub = (
prediction_flows[0][:, s_f : e_f - 1],
prediction_flows[1][:, s_f : e_f - 1],
)
prop_imgs_sub, updated_local_masks_sub = inpaint_model.img_propagation(
masked_frames[:, s_f:e_f],
pred_flows_bi_sub,
masks_dilated[:, s_f:e_f],
"nearest",
)
updated_frames_sub = (
frames[:, s_f:e_f] * (1 - masks_dilated[:, s_f:e_f])
+ prop_imgs_sub.view(b, t, 3, process_height, process_width)
* masks_dilated[:, s_f:e_f]
)
updated_masks_sub = updated_local_masks_sub.view(
b, t, 1, process_height, process_width
)
updated_frames_list.append(
updated_frames_sub[:, pad_len_s : e_f - s_f - pad_len_e]
)
updated_masks_list.append(
updated_masks_sub[:, pad_len_s : e_f - s_f - pad_len_e]
)
torch.cuda.empty_cache()
updated_frames = torch.cat(updated_frames_list, dim=1)
updated_masks = torch.cat(updated_masks_list, dim=1)
else:
b, t, _, _, _ = masks_dilated.size()
prop_imgs, updated_local_masks = inpaint_model.img_propagation(
masked_frames, prediction_flows, masks_dilated, "nearest"
)
updated_frames = (
frames * (1 - masks_dilated)
+ prop_imgs.view(b, t, 3, process_height, process_width) * masks_dilated
)
updated_masks = updated_local_masks.view(b, t, 1, process_height, process_width)
torch.cuda.empty_cache()
return updated_frames, updated_masks
def feature_propagation(
inpaint_model: InpaintGenerator,
updated_frames: torch.Tensor,
updated_masks: torch.Tensor,
masks_dilated: torch.Tensor,
prediction_flows: tuple[torch.Tensor, torch.Tensor],
original_frames: list[NDArray],
config: ProPainterConfig,
) -> list[NDArray]:
"""Propagate inpainted features across video frames.
The process is segmented and handled in chunks if the video length exceeds a defined threshold.
"""
# TODO: Refactor function may be too
process_width, process_height = config.process_size
# TODO: Refacator how composed frames is initialized
composed_frames: list[NDArray | None] = [None] * config.video_length
neighbor_stride = config.neighbor_length // 2
ref_num = (
config.subvideo_length // config.ref_stride
if config.video_length > config.subvideo_length
else -1
)
for f in tqdm(range(0, config.video_length, neighbor_stride)):
neighbor_ids = list(
range(
max(0, f - neighbor_stride),
min(config.video_length, f + neighbor_stride + 1),
)
)
ref_ids = get_ref_index(f, neighbor_ids, config, ref_num)
selected_imgs = updated_frames[:, neighbor_ids + ref_ids, :, :, :]
selected_masks = masks_dilated[:, neighbor_ids + ref_ids, :, :, :]
if config.use_half:
selected_masks = selected_masks.half()
selected_update_masks = updated_masks[:, neighbor_ids + ref_ids, :, :, :]
selected_pred_flows_bi = (
prediction_flows[0][:, neighbor_ids[:-1], :, :, :],
prediction_flows[1][:, neighbor_ids[:-1], :, :, :],
)
with torch.no_grad():
# 1.0 indicates mask
l_t = len(neighbor_ids)
pred_img = inpaint_model(
selected_imgs,
selected_pred_flows_bi,
selected_masks,
selected_update_masks,
l_t,
)
pred_img = pred_img.view(-1, 3, process_height, process_width)
pred_img = (pred_img + 1) / 2
pred_img = pred_img.cpu().permute(0, 2, 3, 1).numpy() * 255
binary_masks = (
masks_dilated[0, neighbor_ids, :, :, :]
.cpu()
.permute(0, 2, 3, 1)
.numpy()
.astype(np.uint8)
)
for i, idx in enumerate(neighbor_ids):
# idx = neighbor_ids[i]
img = np.array(pred_img[i]).astype(np.uint8) * binary_masks[
i
] + original_frames[idx] * (1 - binary_masks[i])
if composed_frames[idx] is None:
composed_frames[idx] = img
else:
composed_frames[idx] = (
composed_frames[idx].astype(np.float32) * 0.5
+ img.astype(np.float32) * 0.5
)
composed_frames[idx] = composed_frames[idx].astype(np.uint8)
torch.cuda.empty_cache()
return composed_frames
def process_inpainting(
models: Models,
frames: torch.Tensor,
flow_masks: torch.Tensor,
masks_dilated: torch.Tensor,
config: ProPainterConfig,
) -> tuple[torch.Tensor, torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
"""Apply inpainting on video using recurrent flow and ProPainter model."""
with torch.no_grad():
gt_flows_bi = compute_flow(models.raft_model, frames, config)
if config.use_half:
frames, flow_masks, masks_dilated = (
frames.half(),
flow_masks.half(),
masks_dilated.half(),
)
gt_flows_bi = (gt_flows_bi[0].half(), gt_flows_bi[1].half())
pred_flows_bi = complete_flow(
models.flow_model, gt_flows_bi, flow_masks, config.subvideo_length
)
updated_frames, updated_masks = image_propagation(
models.inpaint_model, frames, masks_dilated, pred_flows_bi, config
)
return updated_frames, updated_masks, pred_flows_bi