-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
340 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import argparse | ||
from pathlib import Path | ||
|
||
import torch | ||
from mmflow.apis import inference_model, init_model | ||
from mmflow.datasets import visualize_flow | ||
from PIL import Image | ||
from torchvision import transforms | ||
from torchvision.utils import save_image | ||
|
||
from rspy.solver import cubic_flow, linear_flow, quadratic_flow | ||
from rspy.utils import feats_sampling | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--input", type=str, help="input file or directory") | ||
parser.add_argument("--output", type=str, help="output directory") | ||
parser.add_argument("--model", type=str, default="linear", help="linear | quadratic | cubic") | ||
parser.add_argument("--gamma", type=float, default=0.9, help="the readout reatio") | ||
parser.add_argument("--tau", type=int, default=0, help="the timestamp warping to") | ||
parser.add_argument("--fconfig", type=str, default="raft_8x2_100k_mixed_368x768", help="mmflow config file") | ||
parser.add_argument("--device", type=str, default="cuda:0", help="cpu | cuda:0") | ||
args = parser.parse_args() | ||
|
||
|
||
def main(): | ||
assert args.model in ["linear", "quadratic", "cubic"] | ||
input, output = Path(args.input), Path(args.output) | ||
image_paths = sorted(list(input.iterdir())) | ||
|
||
# init a optical-flow model | ||
config_file, checkpoint_file = f"{args.fconfig}.py", f"{args.fconfig}.pth" | ||
model = init_model(config_file, checkpoint_file, device=args.device) | ||
|
||
# * inference flow | ||
num = {"linear": 2, "quadratic": 3, "cubic": 4}[args.model] | ||
flows = inference_model(model, image_paths[: num - 1], image_paths[1:num]) # list of numpy.ndarray | ||
torch_flows = [torch.from_numpy(flow).unsqueeze(0).to(args.device) for flow in flows] # * list (1,h,w,2) | ||
for i, flow in enumerate(flows): | ||
visualize_flow(flow, output / f"{i:04d}.png") | ||
|
||
# * rolling shutter correctin | ||
solver = eval(f"{args.model}_flow") | ||
F0tau = solver(*torch_flows[:num], args.gamma, args.tau) # * (1,h,w,2) | ||
|
||
# * warp image | ||
rs_path = image_paths[num // 2] | ||
tsfm = transforms.Compose([transforms.ToTensor()]) | ||
rs_image = tsfm(Image.open(rs_path).convert("RGB")).unsqueeze(0).to(args.device) # * (1,3,h,w) | ||
rsc_image = feats_sampling(rs_image, -F0tau) | ||
|
||
# * save image | ||
save_image(rsc_image, output / f"rsc_{rs_path.stem}.png") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import torch | ||
from einops import rearrange | ||
|
||
|
||
def linear_flow(F01: torch.Tensor, gamma: float, tau: float) -> torch.Tensor: | ||
"""solve the linear motion matrix and predict the correction feild. | ||
Args: | ||
F01: torch.Tensor (b,h,w,2), flow 0 -> 1 | ||
gamma: float, the readout reatio | ||
tau: float, the timestamp warping to | ||
Returns: | ||
torch.Tensor: the correction feild to tau. | ||
""" | ||
h, w = F01.shape[1:3] | ||
t01 = 1 + gamma / h * F01[:, :, :, 1] # * (b, h, w) | ||
|
||
# solve the linear motion matrix | ||
M = F01 / rearrange(t01, "b h w -> b h w 1") # * (b, h, w, 2) | ||
|
||
# predict the correction feild | ||
grid_y, _ = torch.meshgrid( | ||
torch.arange(0, h, device=F01.device, requires_grad=False), | ||
torch.arange(0, w, device=F01.device, requires_grad=False), | ||
) # * (h, w) | ||
|
||
t0tau = tau - gamma / h * grid_y # * (h, w) | ||
F0tau = rearrange(t0tau, "h w -> h w 1") * M # * (b, h, w, 2) | ||
|
||
return F0tau | ||
|
||
|
||
def quadratic_flow(F0n1: torch.Tensor, F01: torch.Tensor, gamma: float, tau: float) -> torch.Tensor: | ||
"""solve the quadratic motion matrix and predict the correction feild. | ||
Args: | ||
F0n1: torch.Tensor (b,h,w,2), flow 0 -> -1 | ||
F01: torch.Tensor (b,h,w,2), flow 0 -> 1 | ||
gamma (float): the readout reatio | ||
tau (float): the timestamp warping to | ||
Returns: | ||
torch.Tensor: the correction feild to tau. | ||
""" | ||
h, w = F0n1.shape[1:3] | ||
t0n1 = -1 + gamma / h * F0n1[:, :, :, 1] # * (b, h, w) | ||
t01 = 1 + gamma / h * F01[:, :, :, 1] # * (b, h, w) | ||
|
||
# solve the quadratic motion matrix | ||
A = rearrange( | ||
torch.stack([t0n1, 0.5 * t0n1 ** 2, t01, 0.5 * t01 ** 2], dim=-1), | ||
"b h w (m n) -> b h w m n", | ||
m=2, | ||
n=2, | ||
) # * (b, h, w, 2, 2) | ||
|
||
B = torch.stack([F0n1, F01], dim=-2) # * (b, h, w, 2, 2) | ||
M = torch.linalg.solve(A, B) # * (b, h, w, 2, 2) | ||
|
||
# predict the correction feild | ||
grid_y, _ = torch.meshgrid( | ||
torch.arange(0, h, device=F0n1.device, requires_grad=False), | ||
torch.arange(0, w, device=F0n1.device, requires_grad=False), | ||
) | ||
t0tau = tau - gamma / h * grid_y # * (h, w) | ||
|
||
Atau = rearrange(torch.stack([t0tau, 0.5 * t0tau ** 2], dim=-1), "h w m -> h w 1 m") # * (h, w, 1, 2) | ||
F0tau = rearrange(Atau @ M, "b h w 1 n -> b h w n") # * (b, h, w, 2) | ||
|
||
return F0tau | ||
|
||
|
||
def cubic_flow(F0n2: torch.Tensor, F0n1: torch.Tensor, F01: torch.Tensor, gamma: float, tau: float) -> torch.Tensor: | ||
"""solve the cubic motion matrix and predict the correction feild. | ||
Args: | ||
F0n1: torch.Tensor (b,h,w,2): flow 0 -> -1 | ||
F01: torch.Tensor (b,h,w,2): flow 0 -> 1 | ||
F02: torch.Tensor (b,h,w,2): flow 0 -> 2 | ||
gamma: (float): the readout reatio | ||
tau: (float): the timestamp warping to | ||
Returns: | ||
torch.Tensor: the correction feild to tau. | ||
""" | ||
h, w = F0n1.shape[1:3] | ||
t0n2 = -2 + gamma / h * F0n2[:, :, :, 1] | ||
t0n1 = -1 + gamma / h * F0n1[:, :, :, 1] | ||
t01 = 1 + gamma / h * F01[:, :, :, 1] | ||
|
||
# solve the quadratic motion matrix | ||
A = rearrange( | ||
torch.stack( | ||
[ | ||
t0n2, | ||
0.5 * t0n2 ** 2, | ||
1 / 6 * t0n2 ** 3, | ||
t0n1, | ||
0.5 * t0n1 ** 2, | ||
1 / 6 * t0n1 ** 3, | ||
t01, | ||
0.5 * t01 ** 2, | ||
1 / 6 * t01 ** 3, | ||
], | ||
dim=-1, | ||
), | ||
"b h w (m n) -> b h w m n", | ||
m=3, | ||
n=3, | ||
) # * (b, h, w, 3, 3) | ||
B = torch.stack([F0n2, F0n1, F01], dim=-2) # * (b, h, w, 3, 2) | ||
M = torch.linalg.solve(A, B) # * (b, h, w, 3, 2) | ||
|
||
# predict the correction feild | ||
grid_y, _ = torch.meshgrid( | ||
torch.arange(0, h, device=F0n1.device, requires_grad=False), | ||
torch.arange(0, w, device=F0n1.device, requires_grad=False), | ||
) | ||
t0tau = tau - gamma / h * grid_y # * (h, w) | ||
|
||
Atau = rearrange(torch.stack([t0tau, 0.5 * t0tau ** 2, 1 / 6 * t0tau ** 3], dim=-1), "h w m -> h w 1 m") | ||
F0tau = rearrange(Atau @ M, "b h w 1 n -> b h w n") # * (b, h, w, 2) | ||
|
||
return F0tau |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import numpy as np | ||
import torch | ||
import torch.nn.functional as F | ||
|
||
|
||
def feats_sampling( | ||
x, | ||
flow, | ||
interpolation="bilinear", | ||
padding_mode="zeros", | ||
align_corners=True, | ||
): | ||
"""return warped images with flows in shape(B, C, H, W) | ||
Args: | ||
x: shape(B, C, H, W) | ||
flow: shape(B, H, W, 2) | ||
""" | ||
if x.size()[-2:] != flow.size()[1:3]: | ||
raise ValueError( | ||
f"The spatial sizes of input ({x.size()[-2:]}) and " f"flow ({flow.size()[1:3]}) are not the same." | ||
) | ||
h, w = x.shape[-2:] | ||
|
||
# create mesh grid | ||
grid_y, grid_x = torch.meshgrid(torch.arange(0, h), torch.arange(0, w)) | ||
grid = torch.stack((grid_x, grid_y), 2).type_as(x) #! (h, w, 2) | ||
grid.requires_grad = False | ||
grid_flow = grid + flow | ||
|
||
# scale grid_flow to [-1,1] | ||
grid_flow_x = 2.0 * grid_flow[:, :, :, 0] / max(w - 1, 1) - 1.0 | ||
grid_flow_y = 2.0 * grid_flow[:, :, :, 1] / max(h - 1, 1) - 1.0 | ||
grid_flow = torch.stack((grid_flow_x, grid_flow_y), dim=3) | ||
output = F.grid_sample( | ||
x, | ||
grid_flow, | ||
mode=interpolation, | ||
padding_mode=padding_mode, | ||
align_corners=align_corners, | ||
) | ||
return output |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.