MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / bilinear_sample

Function bilinear_sample

diffusers/examples/community/rerender_a_video.py:59–80  ·  view source on GitHub ↗
(img, sample_coords, mode="bilinear", padding_mode="zeros", return_mask=False)

Source from the content-addressed store, hash-verified

57
58
59def bilinear_sample(img, sample_coords, mode="bilinear", padding_mode="zeros", return_mask=False):
60 # img: [B, C, H, W]
61 # sample_coords: [B, 2, H, W] in image scale
62 if sample_coords.size(1) != 2: # [B, H, W, 2]
63 sample_coords = sample_coords.permute(0, 3, 1, 2)
64
65 b, _, h, w = sample_coords.shape
66
67 # Normalize to [-1, 1]
68 x_grid = 2 * sample_coords[:, 0] / (w - 1) - 1
69 y_grid = 2 * sample_coords[:, 1] / (h - 1) - 1
70
71 grid = torch.stack([x_grid, y_grid], dim=-1) # [B, H, W, 2]
72
73 img = F.grid_sample(img, grid, mode=mode, padding_mode=padding_mode, align_corners=True)
74
75 if return_mask:
76 mask = (x_grid >= -1) & (y_grid >= -1) & (x_grid <= 1) & (y_grid <= 1) # [B, H, W]
77
78 return img, mask
79
80 return img
81
82
83def flow_warp(feature, flow, mask=False, mode="bilinear", padding_mode="zeros"):

Callers 1

flow_warpFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected