-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
142 lines (103 loc) · 3.8 KB
/
utils.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
import os
import random
import numpy as np
import torch
from torch.autograd import Variable
#### General purpose
def seed_everything(seed=0):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
def safe_mkdir(path):
"""Create a directory if there isn't one already."""
try:
os.makedirs(path)
except OSError:
pass
#### Data preprocessing
def prepare(datalist, device):
return [data.float().to(device) for data in datalist]
def contrast_normalization(datalist, std_bias=0):
for i in range(len(datalist)):
m = torch.mean(datalist[i].float(), dim=(2, 3), keepdims=True)
s = torch.std(datalist[i].float(), dim=(2, 3), keepdims=True)
datalist[i] = (datalist[i] - m) / (s + std_bias)
return datalist
def clone_(datalist):
return [data.clone() for data in datalist]
def generate_easy_flow(batch_size=64, shape=128, range=1):
# Return random shift
flow = torch.zeros(batch_size, 2, shape, shape)
flow = flow + torch.randint(-range, range + 1, (batch_size, 2, 1, 1))
return flow
def roll_batch_slow(batch, shifts):
# batch: b, 1, h, w
# shifts: b, 2
for i in range(len(shifts)):
batch[i] = torch.roll(batch[i], tuple(shifts[i].tolist()), dims=(-2, -1))
return batch
def roll_batch(batch, shifts):
# batch: b, 1, h, w
# shifts: b, 2
b, _, h, w = batch.size()
shift_y, shift_x = shifts[:, 0], shifts[:, 1]
# Calculate the rolled coordinates
idx_y = (torch.arange(h).view(1, h, 1) - shift_y.view(b, 1, 1)) % h
idx_x = (torch.arange(w).view(1, 1, w) - shift_x.view(b, 1, 1)) % w
# Use advanced indexing to obtain rolled_batch
rolled_batch = batch[torch.arange(b)[:, None, None], :, idx_y, idx_x]
# Rearrange the dimensions to match the input batch
rolled_batch = rolled_batch.permute(0, 3, 1, 2)
return rolled_batch
def crop_border(datalist, cropborder=1):
for i in range(len(datalist)):
datalist[i] = datalist[i][..., cropborder:-cropborder, cropborder:-cropborder]
return datalist
def self_registered(im, csr):
# bands registration
im_r = im.clone().cpu()
imT = contrast_normalization([im])[0]
concat = torch.cat((imT[1:2].expand(3, -1, -1, -1), imT[[0, 2, 3]]), 1)
flow = csr(concat).cpu().detach() # 3, 2, h, w
im_r[[0, 2, 3]] = warp(
im[[0, 2, 3]].cpu(), flow.cpu(), mode="bicubic", padding_mode="reflection"
)
return im_r.cpu().numpy().astype(np.uint16) # , flow.numpy()
def super_resolve(im, rec):
sr = rec(im / 400.0)
return (sr.detach().cpu().numpy() * 400).astype(np.uint16)
#####
#### Warping
def create_grid(flo):
B, _, H, W = flo.shape
# mesh grid
xx = torch.arange(0, W).view(1, -1).repeat(H, 1)
yy = torch.arange(0, H).view(-1, 1).repeat(1, W)
xx = xx.view(1, 1, H, W).repeat(B, 1, 1, 1)
yy = yy.view(1, 1, H, W).repeat(B, 1, 1, 1)
grid = torch.cat((xx, yy), 1).float()
grid = grid.to(flo.device)
vgrid = Variable(grid) + flo
# scale grid to [-1,1]
vgrid[:, 0, :, :] = 2.0 * vgrid[:, 0, :, :].clone() / max(W - 1, 1) - 1.0
vgrid[:, 1, :, :] = 2.0 * vgrid[:, 1, :, :].clone() / max(H - 1, 1) - 1.0
vgrid = vgrid.permute(0, 2, 3, 1)
return vgrid
def warp(x, flo, mode="bilinear", padding_mode="zeros"):
"""
warp an image/tensor (im2) back to im1, according to the optical flow
x: [B, C, H, W] (im2)
flo: [B, 2, H, W] flow
"""
if torch.sum(flo * flo) == 0:
return x
else:
B, _, H, W = x.size()
vgrid = create_grid(flo)
output = torch.nn.functional.grid_sample(
x, vgrid, align_corners=True, mode=mode, padding_mode=padding_mode
)
return output