Spaces:
Sleeping
Sleeping
File size: 7,789 Bytes
f9cbc98 |
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 |
import os
import numpy as np
from PIL import Image
import torch
from torch.utils.data import Dataset
from torchvision.transforms import ToTensor
def get_mgrid(sidelen, vmin=-1, vmax=1):
if type(vmin) is not list:
vmin = [vmin for _ in range(len(sidelen))]
if type(vmax) is not list:
vmax = [vmax for _ in range(len(sidelen))]
tensors = tuple([torch.linspace(vmin[i], vmax[i], steps=sidelen[i]) for i in range(len(sidelen))])
mgrid = torch.stack(torch.meshgrid(*tensors), dim=-1)
mgrid = mgrid.reshape(-1, len(sidelen))
return mgrid
def apply_homography(x, h):
h = torch.cat([h, torch.ones_like(h[:, [0]])], -1)
h = h.view(-1, 3, 3)
x = torch.cat([x, torch.ones_like(x[:, 0]).unsqueeze(-1)], -1).unsqueeze(-1)
o = torch.bmm(h, x).squeeze(-1)
o = o[:, :-1] / o[:, [-1]]
return o
def jacobian(y, x):
B, N = y.shape
jacobian = list()
for i in range(N):
v = torch.zeros_like(y)
v[:, i] = 1.
dy_i_dx = torch.autograd.grad(y,
x,
grad_outputs=v,
retain_graph=True,
create_graph=True)[0] # shape [B, N]
jacobian.append(dy_i_dx)
jacobian = torch.stack(jacobian, dim=1).requires_grad_()
return jacobian
def overlap_mix(img1, img2, img_order, overlap_num):
w1 = np.linspace(0, 1, overlap_num)[::-1]
w2 = 1 - w1
return w1[img_order] * img1 + w2[img_order] * img2
class VideoFitting(Dataset):
def __init__(self, path, transform=None):
super().__init__()
self.path = path
if transform is None:
self.transform = ToTensor()
else:
self.transform = transform
self.video = self.get_video_tensor()
self.num_frames, _, self.H, self.W = self.video.size()
self.pixels = self.video.permute(2, 3, 0, 1).contiguous().view(-1, 3)
self.coords = get_mgrid([self.H, self.W, self.num_frames])
shuffle = torch.randperm(len(self.pixels))
self.pixels = self.pixels[shuffle]
self.coords = self.coords[shuffle]
def get_video_tensor(self):
frames = sorted(os.listdir(self.path))
video = []
for i in range(len(frames)):
img = Image.open(os.path.join(self.path, frames[i]))
img = self.transform(img)
video.append(img)
return torch.stack(video, 0)
def __len__(self):
return 1
def __getitem__(self, idx):
if idx > 0: raise IndexError
return self.coords, self.pixels
class TestVideoFitting(Dataset):
def __init__(self, path, transform=None):
super().__init__()
self.path = path
if transform is None:
self.transform = ToTensor()
else:
self.transform = transform
self.video = self.get_video_tensor()
self.num_frames, _, self.H, self.W = self.video.size()
self.pixels = self.video.permute(2, 3, 0, 1).contiguous().view(-1, 3)
self.coords = get_mgrid([self.H, self.W, self.num_frames])
def get_video_tensor(self):
frames = sorted(os.listdir(self.path))
video = []
for i in range(len(frames)):
img = Image.open(os.path.join(self.path, frames[i]))
img = self.transform(img)
video.append(img)
return torch.stack(video, 0)
def __len__(self):
return 1
def __getitem__(self, idx):
if idx > 0: raise IndexError
return self.coords, self.pixels
class GroupVideoFitting(Dataset):
def __init__(self, path, mask_path, transform=None, mask_transform=None):
super().__init__()
self.path = path
self.mask_path = mask_path
if transform is None:
self.transform = ToTensor()
else:
self.transform = transform
if mask_transform is None:
self.mask_transform = ToTensor()
else:
self.mask_transform = mask_transform
self.video = self.get_video_tensor()
self.mask = self.get_mask_tensor()
self.num_frames, _, self.H, self.W = self.video.size()
self.pixels = self.video.permute(2, 3, 0, 1).contiguous().view(-1, 3)
self.mask_pixels = self.mask.permute(2, 3, 0, 1).contiguous().view(-1, 1)
self.coords = get_mgrid([self.H, self.W, self.num_frames])
shuffle = torch.randperm(len(self.pixels))
self.pixels = self.pixels[shuffle]
self.coords = self.coords[shuffle]
self.mask_pixels = self.mask_pixels[shuffle]
def get_video_tensor(self):
frames = sorted(os.listdir(self.path))
video = []
for i in range(len(frames)):
img = Image.open(os.path.join(self.path, frames[i]))
img = self.transform(img)
video.append(img)
return torch.stack(video, 0)
def get_mask_tensor(self):
masks = sorted(os.listdir(self.mask_path))
all_mask = []
for i in range(len(masks)):
mask = Image.open(os.path.join(self.mask_path, masks[i]))
mask = self.mask_transform(mask)
all_mask.append(mask)
return torch.stack(all_mask, 0)
def __len__(self):
return 1
def __getitem__(self, idx):
if idx > 0: raise IndexError
return self.coords, self.pixels, self.mask_pixels
class TestGroupVideoFitting(Dataset):
def __init__(self, path, mask_path, back_mask_path, transform=None, mask_transform=None):
super().__init__()
self.path = path
self.mask_path = mask_path
self.back_mask_path = back_mask_path
if transform is None:
self.transform = ToTensor()
else:
self.transform = transform
if mask_transform is None:
self.mask_transform = ToTensor()
else:
self.mask_transform = mask_transform
self.video = self.get_video_tensor()
self.mask = self.get_mask_tensor()
self.back_mask = self.get_back_mask_tensor()
self.num_frames, _, self.H, self.W = self.video.size()
self.pixels = self.video.permute(2, 3, 0, 1).contiguous().view(-1, 3)
self.mask_pixels = self.mask.permute(2, 3, 0, 1).contiguous().view(-1, 1)
self.back_mask_pixels = self.back_mask.permute(2, 3, 0, 1).contiguous().view(-1, 1)
self.coords = get_mgrid([self.H, self.W, self.num_frames])
def get_video_tensor(self):
frames = sorted(os.listdir(self.path))
video = []
for i in range(len(frames)):
img = Image.open(os.path.join(self.path, frames[i]))
img = self.transform(img)
video.append(img)
return torch.stack(video, 0)
def get_mask_tensor(self):
masks = sorted(os.listdir(self.mask_path))
all_mask = []
for i in range(len(masks)):
mask = Image.open(os.path.join(self.mask_path, masks[i]))
mask = self.mask_transform(mask)
all_mask.append(mask)
return torch.stack(all_mask, 0)
def get_back_mask_tensor(self):
masks = sorted(os.listdir(self.back_mask_path))
all_mask = []
for i in range(len(masks)):
mask = Image.open(os.path.join(self.back_mask_path, masks[i]))
mask = self.mask_transform(mask)
all_mask.append(mask)
return torch.stack(all_mask, 0)
def __len__(self):
return 1
def __getitem__(self, idx):
if idx > 0: raise IndexError
return self.coords, self.pixels, self.mask_pixels, self.back_mask_pixels |