fp16 build of ProPainter
Browse files- fp16/fc_decoder.onnx +3 -0
- fp16/fc_encoder.onnx +3 -0
- fp16/fc_step_backward.onnx +3 -0
- fp16/fc_step_forward.onnx +3 -0
- fp16/gen_encoder.onnx +3 -0
- fp16/gen_step_backward.onnx +3 -0
- fp16/gen_step_forward.onnx +3 -0
- fp16/gen_transformer.onnx +3 -0
- fp16/img_step.onnx +3 -0
- fp16/pipeline.py +255 -0
- fp16/raft.onnx +3 -0
fp16/fc_decoder.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:48750e6e2291ba335513357ee2ae7250f12fa71bb4cd54267859358b468d6c36
|
| 3 |
+
size 644994
|
fp16/fc_encoder.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:269ee000d9d511208b3c37af4031d74317b6f28c910f3478add9fda673cb97de
|
| 3 |
+
size 1333238
|
fp16/fc_step_backward.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cf55f6d4fa1de7143655d080a4d1bb57f0749405eb4e7e96af3985fe12a80eb6
|
| 3 |
+
size 7924110
|
fp16/fc_step_forward.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:338b54350aacbfd04fe99849ac631a3e5343f08a4c3f33d94bceb7b019672419
|
| 3 |
+
size 8513271
|
fp16/gen_encoder.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:29bcde1a374d458281416af37a95419b53e6e1200d2340911e87809ddc922f2b
|
| 3 |
+
size 8431664
|
fp16/gen_step_backward.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a3e60860c29319cf251e447343a79f259bc23cb8c17874aec3085d4cf211b893
|
| 3 |
+
size 6788743
|
fp16/gen_step_forward.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f33655516654f6d5e7c54a8f834823900675d55242e84681263dcf1b1c6db0c2
|
| 3 |
+
size 6788022
|
fp16/gen_transformer.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e4d7aec32f7f84bb849e9aeaa74bb8c49c3aedef9a992c7190da4414d0bc3bf6
|
| 3 |
+
size 66604583
|
fp16/img_step.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:49973fad918fdb012e1f84603351e504ec125ee5454ddd515f9ef05e35c7d9c0
|
| 3 |
+
size 17812
|
fp16/pipeline.py
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ProPainter video inpainting (object removal) with ONNX Runtime: RAFT optical flow, the recurrent flow-completion
|
| 2 |
+
network and the ProPainter generator as ONNX graphs (the recurrences split into one graph per step), numpy around
|
| 3 |
+
them, following the repository's inference_propainter.py (video_inpainting mode, its defaults: mask dilation 4,
|
| 4 |
+
neighbour length 10, reference stride 10, sub-video length 80, 20 RAFT iterations).
|
| 5 |
+
Needs numpy, scipy, onnxruntime (or onnxruntime-gpu) and Pillow.
|
| 6 |
+
|
| 7 |
+
python fp16/pipeline.py frames_dir masks_dir out_dir [--cuda] (one mask per frame, or a single mask image)
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
GRAPHS = ("raft", "fc_encoder", "fc_step_backward", "fc_step_forward", "fc_decoder", "img_step", "gen_encoder",
|
| 17 |
+
"gen_step_backward", "gen_step_forward", "gen_transformer")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def masks_from(images, length, size, flow_dilate=4, mask_dilate=4):
|
| 21 |
+
"""read_mask: each mask resized (nearest) and dilated (scipy binary_dilation) -> flow masks and dilated masks."""
|
| 22 |
+
from scipy import ndimage
|
| 23 |
+
from PIL import Image
|
| 24 |
+
fm, md = [], []
|
| 25 |
+
for m in images:
|
| 26 |
+
a = np.asarray(m.resize(size, Image.NEAREST).convert("L"))
|
| 27 |
+
fm.append(ndimage.binary_dilation(a, iterations=flow_dilate).astype(np.float32) if flow_dilate > 0
|
| 28 |
+
else (a > 0.1).astype(np.float32))
|
| 29 |
+
md.append(ndimage.binary_dilation(a, iterations=mask_dilate).astype(np.float32) if mask_dilate > 0
|
| 30 |
+
else (a > 0.1).astype(np.float32))
|
| 31 |
+
if len(images) == 1:
|
| 32 |
+
fm, md = fm * length, md * length
|
| 33 |
+
return np.stack(fm)[:, None], np.stack(md)[:, None]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def down4(x, mode):
|
| 37 |
+
"""F.interpolate(scale_factor=1/4): bilinear (align_corners=False: the mean of rows / columns 4i+1, 4i+2) or nearest
|
| 38 |
+
(row / column 4i)."""
|
| 39 |
+
if mode == "nearest":
|
| 40 |
+
return x[..., ::4, ::4]
|
| 41 |
+
x = 0.5 * (x[..., 1::4, :] + x[..., 2::4, :])
|
| 42 |
+
return 0.5 * (x[..., 1::4] + x[..., 2::4])
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def ref_index(mid, neighbours, length, stride=10, num=-1):
|
| 46 |
+
out = []
|
| 47 |
+
if num == -1:
|
| 48 |
+
return [i for i in range(0, length, stride) if i not in neighbours]
|
| 49 |
+
s, e = max(0, mid - stride * (num // 2)), min(length, mid + stride * (num // 2))
|
| 50 |
+
for i in range(s, e, stride):
|
| 51 |
+
if i not in neighbours:
|
| 52 |
+
if len(out) > num:
|
| 53 |
+
break
|
| 54 |
+
out.append(i)
|
| 55 |
+
return out
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class ProPainter:
|
| 59 |
+
def __init__(self, folder, provider="cpu"):
|
| 60 |
+
import onnxruntime as ort
|
| 61 |
+
folder = Path(folder)
|
| 62 |
+
if provider != "cpu" and hasattr(ort, "preload_dlls"):
|
| 63 |
+
try:
|
| 64 |
+
ort.preload_dlls()
|
| 65 |
+
except Exception:
|
| 66 |
+
pass
|
| 67 |
+
so = ort.SessionOptions()
|
| 68 |
+
so.log_severity_level = 3
|
| 69 |
+
pv = ["CPUExecutionProvider"] if provider == "cpu" else [
|
| 70 |
+
("CUDAExecutionProvider", {"arena_extend_strategy": "kSameAsRequested"}), "CPUExecutionProvider"]
|
| 71 |
+
self.s = {n: ort.InferenceSession(str(folder / f"{n}.onnx"), so, providers=pv) for n in GRAPHS}
|
| 72 |
+
self.ro = ort.RunOptions()
|
| 73 |
+
if provider != "cpu": # Windows: shapes vary, and the GPU arena would keep every size's buffers
|
| 74 |
+
self.ro.add_run_config_entry("memory.enable_memory_arena_shrinkage", "gpu:0")
|
| 75 |
+
self.dt = {n: {i.name: (np.float16 if i.type == "tensor(float16)" else np.float32) for i in s.get_inputs()}
|
| 76 |
+
for n, s in self.s.items()}
|
| 77 |
+
|
| 78 |
+
def run(self, name, **feeds):
|
| 79 |
+
feeds = {k: np.ascontiguousarray(v, self.dt[name][k]) for k, v in feeds.items()}
|
| 80 |
+
out = self.s[name].run(None, feeds, self.ro)
|
| 81 |
+
out = [o.astype(np.float32) for o in out]
|
| 82 |
+
return out[0] if len(out) == 1 else out
|
| 83 |
+
|
| 84 |
+
# -- flow ------------------------------------------------------------------------------------------------------
|
| 85 |
+
def raft(self, frames):
|
| 86 |
+
"""Forward / backward flows between consecutive frames, in clips (12 frames up to 640 px wide, ...) as the
|
| 87 |
+
original does."""
|
| 88 |
+
T, _, H, W = frames.shape
|
| 89 |
+
clip = 12 if W <= 640 else 8 if W <= 720 else 4 if W <= 1280 else 2
|
| 90 |
+
fw, bw = [], []
|
| 91 |
+
for f in range(0, T, clip):
|
| 92 |
+
seg = frames[f:min(T, f + clip)] if f == 0 else frames[f - 1:min(T, f + clip)]
|
| 93 |
+
a, b = seg[:-1], seg[1:]
|
| 94 |
+
fw.append(self.run("raft", image1=a, image2=b))
|
| 95 |
+
bw.append(self.run("raft", image1=b, image2=a))
|
| 96 |
+
return np.concatenate(fw), np.concatenate(bw)
|
| 97 |
+
|
| 98 |
+
def _fc_direction(self, flows, masks):
|
| 99 |
+
"""RecurrentFlowCompleteNet.forward on one direction: flows [T, 2, H, W] (masked), masks [T, 1, H, W]."""
|
| 100 |
+
feat_mid, feat_e1 = self.run("fc_encoder", flows=flows[None], masks=masks[None])
|
| 101 |
+
x = feat_mid[0] # [T, 128, h, w]
|
| 102 |
+
T = len(x)
|
| 103 |
+
zero = np.zeros_like(x[0:1])
|
| 104 |
+
feats = {}
|
| 105 |
+
for name in ("backward", "forward"):
|
| 106 |
+
order = range(T - 1, -1, -1) if name == "backward" else range(T)
|
| 107 |
+
out, prop = [], zero
|
| 108 |
+
for i, idx in enumerate(order):
|
| 109 |
+
n2 = out[-2] if i > 1 else zero
|
| 110 |
+
kw = {"feat_prop": prop, "feat_n2": n2, "feat_current": x[idx:idx + 1],
|
| 111 |
+
"first": np.array([1.0 if i == 0 else 0.0], np.float32)}
|
| 112 |
+
if name == "forward":
|
| 113 |
+
kw["feat_backward"] = feats["backward"][idx]
|
| 114 |
+
prop = self.run(f"fc_step_{name}", **kw)
|
| 115 |
+
out.append(prop)
|
| 116 |
+
feats[name] = out[::-1] if name == "backward" else out
|
| 117 |
+
return self.run("fc_decoder", feat_mid=x, feat_b=np.concatenate(feats["backward"]),
|
| 118 |
+
feat_f=np.concatenate(feats["forward"]), feat_e1=feat_e1)
|
| 119 |
+
|
| 120 |
+
def complete_flows(self, fw, bw, fmask, sub=80):
|
| 121 |
+
"""forward_bidirect_flow + combine_flow, in sub-videos of 80 flows padded by 5."""
|
| 122 |
+
L = len(fw)
|
| 123 |
+
|
| 124 |
+
def one(f, b, m):
|
| 125 |
+
mf, mb = m[:-1], m[1:]
|
| 126 |
+
pf = self._fc_direction(f * (1 - mf), mf)
|
| 127 |
+
pb = self._fc_direction((b * (1 - mb))[::-1], mb[::-1])[::-1]
|
| 128 |
+
return pf * mf + f * (1 - mf), pb * mb + b * (1 - mb)
|
| 129 |
+
if L <= sub:
|
| 130 |
+
return one(fw, bw, fmask)
|
| 131 |
+
pfs, pbs, pad = [], [], 5
|
| 132 |
+
for f in range(0, L, sub):
|
| 133 |
+
s, e = max(0, f - pad), min(L, f + sub + pad)
|
| 134 |
+
ps, pe = f - s, e - min(L, f + sub)
|
| 135 |
+
a, b = one(fw[s:e], bw[s:e], fmask[s:e + 1])
|
| 136 |
+
pfs.append(a[ps:e - s - pe]); pbs.append(b[ps:e - s - pe])
|
| 137 |
+
return np.concatenate(pfs), np.concatenate(pbs)
|
| 138 |
+
|
| 139 |
+
# -- image propagation (no learned weights) ---------------------------------------------------------------------
|
| 140 |
+
def _img_prop(self, x, ff, fb, m):
|
| 141 |
+
"""BidirectionalPropagation(learnable=False), 'nearest': backward then forward pass over the frames; returns the
|
| 142 |
+
forward pass's frames and masks."""
|
| 143 |
+
T = len(x)
|
| 144 |
+
cur_f, cur_m = list(x), list(m)
|
| 145 |
+
for name in ("backward", "forward"):
|
| 146 |
+
order = list(range(T - 1, -1, -1)) if name == "backward" else list(range(T))
|
| 147 |
+
flow_idx = order if name == "backward" else list(range(-1, T - 1))
|
| 148 |
+
prop_flows, check_flows = (ff, fb) if name == "backward" else (fb, ff)
|
| 149 |
+
nf, nm = [], []
|
| 150 |
+
for i, idx in enumerate(order):
|
| 151 |
+
if i == 0:
|
| 152 |
+
fp, mp = cur_f[idx], cur_m[idx]
|
| 153 |
+
else:
|
| 154 |
+
j = flow_idx[i]
|
| 155 |
+
fp, mp = self.run("img_step", feat_prop=fp[None], mask_prop=mp[None], feat_current=cur_f[idx][None],
|
| 156 |
+
mask_current=cur_m[idx][None], flow_prop=prop_flows[j:j + 1],
|
| 157 |
+
flow_check=check_flows[j:j + 1])
|
| 158 |
+
fp, mp = fp[0], mp[0]
|
| 159 |
+
nf.append(fp); nm.append(mp)
|
| 160 |
+
if name == "backward":
|
| 161 |
+
nf, nm = nf[::-1], nm[::-1]
|
| 162 |
+
cur_f, cur_m = nf, nm
|
| 163 |
+
return np.stack(cur_f), np.stack(cur_m)
|
| 164 |
+
|
| 165 |
+
def propagate_images(self, frames, masks, pf, pb, sub=80):
|
| 166 |
+
masked = frames * (1 - masks)
|
| 167 |
+
T, sub = len(frames), min(100, sub)
|
| 168 |
+
if T <= sub:
|
| 169 |
+
p, um = self._img_prop(masked, pf, pb, masks)
|
| 170 |
+
return frames * (1 - masks) + p * masks, um
|
| 171 |
+
fr, ms, pad = [], [], 10
|
| 172 |
+
for f in range(0, T, sub):
|
| 173 |
+
s, e = max(0, f - pad), min(T, f + sub + pad)
|
| 174 |
+
ps, pe = f - s, e - min(T, f + sub)
|
| 175 |
+
p, um = self._img_prop(masked[s:e], pf[s:e - 1], pb[s:e - 1], masks[s:e])
|
| 176 |
+
fr.append((frames[s:e] * (1 - masks[s:e]) + p * masks[s:e])[ps:e - s - pe]); ms.append(um[ps:e - s - pe])
|
| 177 |
+
return np.concatenate(fr), np.concatenate(ms)
|
| 178 |
+
|
| 179 |
+
# -- the generator ----------------------------------------------------------------------------------------------
|
| 180 |
+
def generate(self, imgs, masks, upd, ff, fb, l_t):
|
| 181 |
+
"""InpaintGenerator.forward on one window: imgs / masks / upd [t, ...] (local frames first), flows [l_t - 1, ...]."""
|
| 182 |
+
enc = self.run("gen_encoder", frames=imgs, masks_in=masks, masks_updated=upd) # [t, 128, h, w]
|
| 183 |
+
local, ref = enc[:l_t], enc[l_t:]
|
| 184 |
+
dff, dfb = down4(ff, "bilinear") / 4.0, down4(fb, "bilinear") / 4.0
|
| 185 |
+
dm_in = down4(masks, "nearest")[:l_t]
|
| 186 |
+
dm_up = down4(upd[:l_t], "nearest")
|
| 187 |
+
pm = np.concatenate([dm_in, dm_up], 1) # [l_t, 2, h, w]
|
| 188 |
+
outs = {}
|
| 189 |
+
cur = list(local)
|
| 190 |
+
for name in ("backward", "forward"):
|
| 191 |
+
order = list(range(l_t - 1, -1, -1)) if name == "backward" else list(range(l_t))
|
| 192 |
+
flow_idx = order if name == "backward" else list(range(-1, l_t - 1))
|
| 193 |
+
pflows, cflows = (dff, dfb) if name == "backward" else (dfb, dff)
|
| 194 |
+
res, fp = [], None
|
| 195 |
+
zf = np.zeros_like(pflows[0:1]) if len(pflows) else np.zeros((1, 2) + local.shape[-2:], np.float32)
|
| 196 |
+
for i, idx in enumerate(order):
|
| 197 |
+
first = i == 0
|
| 198 |
+
j = flow_idx[i]
|
| 199 |
+
fp = self.run(f"gen_step_{name}", feat_prop=cur[idx][None] if first else fp,
|
| 200 |
+
feat_current=cur[idx][None], flow_prop=zf if first else pflows[j:j + 1],
|
| 201 |
+
flow_check=zf if first else cflows[j:j + 1], mask_current=pm[idx:idx + 1],
|
| 202 |
+
first=np.array([1.0 if first else 0.0], np.float32))
|
| 203 |
+
res.append(fp)
|
| 204 |
+
if name == "backward":
|
| 205 |
+
res = res[::-1]
|
| 206 |
+
outs[name] = np.concatenate(res)
|
| 207 |
+
cur = list(outs[name])
|
| 208 |
+
return self.run("gen_transformer", local_feat=local, feat_b=outs["backward"], feat_f=outs["forward"],
|
| 209 |
+
prop_mask=pm, ref_feat=ref, mask_local=dm_in) # [l_t, 3, H, W] in [-1, 1]
|
| 210 |
+
|
| 211 |
+
def __call__(self, frames, mask_images, neighbor_length=10, ref_stride=10, sub=80):
|
| 212 |
+
"""frames: list of PIL RGB images; mask_images: one per frame or a single one. Returns uint8 frames."""
|
| 213 |
+
from PIL import Image
|
| 214 |
+
size = frames[0].size
|
| 215 |
+
psize = (size[0] - size[0] % 8, size[1] - size[1] % 8)
|
| 216 |
+
if psize != size:
|
| 217 |
+
frames = [f.resize(psize) for f in frames]
|
| 218 |
+
T = len(frames)
|
| 219 |
+
fmask, mdil = masks_from(mask_images, T, psize)
|
| 220 |
+
inp = np.stack([np.asarray(f, np.uint8) for f in frames]) # [T, H, W, 3]
|
| 221 |
+
x = inp.transpose(0, 3, 1, 2).astype(np.float32) / 255.0 * 2 - 1
|
| 222 |
+
fw, bw = self.raft(x)
|
| 223 |
+
pf, pb = self.complete_flows(fw, bw, fmask, sub)
|
| 224 |
+
upd, umask = self.propagate_images(x, mdil, pf, pb, sub)
|
| 225 |
+
stride = neighbor_length // 2
|
| 226 |
+
num = sub // ref_stride if T > sub else -1
|
| 227 |
+
comp = [None] * T
|
| 228 |
+
bm = mdil[:, 0, :, :, None].astype(np.uint8)
|
| 229 |
+
for f in range(0, T, stride):
|
| 230 |
+
nb = list(range(max(0, f - stride), min(T, f + stride + 1)))
|
| 231 |
+
ids = nb + ref_index(f, nb, T, ref_stride, num)
|
| 232 |
+
out = self.generate(upd[ids], mdil[ids], umask[ids], pf[nb[:-1]], pb[nb[:-1]], len(nb))
|
| 233 |
+
out = (out + 1) / 2 * 255
|
| 234 |
+
for i, idx in enumerate(nb):
|
| 235 |
+
img = out[i].transpose(1, 2, 0).astype(np.uint8) * bm[idx] + inp[idx] * (1 - bm[idx])
|
| 236 |
+
comp[idx] = img if comp[idx] is None else \
|
| 237 |
+
(comp[idx].astype(np.float32) * 0.5 + img.astype(np.float32) * 0.5).astype(np.uint8)
|
| 238 |
+
if psize != size:
|
| 239 |
+
comp = [np.asarray(Image.fromarray(c).resize(size, Image.BICUBIC)) for c in comp]
|
| 240 |
+
return comp
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
if __name__ == "__main__":
|
| 244 |
+
import argparse
|
| 245 |
+
from PIL import Image
|
| 246 |
+
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
| 247 |
+
ap.add_argument("frames"); ap.add_argument("masks"); ap.add_argument("out")
|
| 248 |
+
ap.add_argument("--dir", default=str(Path(__file__).resolve().parent)); ap.add_argument("--cuda", action="store_true")
|
| 249 |
+
a = ap.parse_args()
|
| 250 |
+
fr = [Image.open(p).convert("RGB") for p in sorted(Path(a.frames).iterdir())]
|
| 251 |
+
mp = Path(a.masks)
|
| 252 |
+
ms = [Image.open(p) for p in sorted(mp.iterdir())] if mp.is_dir() else [Image.open(mp)]
|
| 253 |
+
Path(a.out).mkdir(parents=True, exist_ok=True)
|
| 254 |
+
for i, c in enumerate(ProPainter(a.dir, "cuda" if a.cuda else "cpu")(fr, ms)):
|
| 255 |
+
Image.fromarray(c).save(Path(a.out) / f"{i:05d}.png")
|
fp16/raft.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2b8b0cb0b202b79fc13fbd32ab811a248071c5b10dc1594bea24f72249b011f3
|
| 3 |
+
size 21558132
|