-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsampling.py
executable file
·238 lines (215 loc) · 8.91 KB
/
sampling.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
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
234
235
236
import torch
from einops import rearrange
import numpy as np
operation_seed_counter = 0
def generate_mask_pair(img):
# prepare masks (N x C x H/2 x W/2)
n, c, t, h, w = img.shape
mask1 = torch.zeros(size=(n * t * h // 2 * w // 2 * 4, ),
dtype=torch.bool,
device=img.device)
mask2 = torch.zeros(size=(n * t * h // 2 * w // 2 * 4, ),
dtype=torch.bool,
device=img.device)
mask3 = torch.zeros(size=(n * t * h // 2 * w // 2 * 4, ),
dtype=torch.bool,
device=img.device)
# prepare random mask pairs
idx_pair = torch.tensor([
[0, 1, 2], [0, 2, 1],
[1, 0, 3], [1, 3, 0],
[2, 0, 3], [2, 3, 0],
[3, 2, 1], [3, 1, 2]],
dtype=torch.int64,
device=img.device)
rd_idx = torch.zeros(size=(n * t * h // 2 * w // 2, ),
dtype=torch.int64,
device=img.device)
torch.randint(low=0,
high=8,
size=(n * t * h // 2 * w // 2, ),
generator=get_generator(),
out=rd_idx)
# [n * h // 2 * w // 2, ]
rd_pair_idx = idx_pair[rd_idx]
# [n * t * h // 2 * w // 2, 2]
rd_pair_idx += torch.arange(start=0,
end=n * t * h // 2 * w // 2 * 4,
step=4,
dtype=torch.int64,
device=img.device).reshape(-1, 1)
# get masks
mask1[rd_pair_idx[:, 0]] = 1
mask2[rd_pair_idx[:, 1]] = 1
mask3[rd_pair_idx[:, 2]] = 1
return mask1, mask2, mask3
def generate_subimages(img, mask):
n, c, t, h, w = img.shape
img = rearrange(img, 'b c s h w -> (b s) c h w')
subimage = torch.zeros(n*t,
c,
h // 2,
w // 2,
dtype=img.dtype,
layout=img.layout,
device=img.device)
# per channel
for i in range(c):
img_per_channel = space_to_depth(img[:, i:i + 1, :, :], block_size=2)
img_per_channel = img_per_channel.permute(0, 2, 3, 1).reshape(-1)
subimage[:, i:i + 1, :, :] = img_per_channel[mask].reshape(
n*t, h // 2, w // 2, c).permute(0, 3, 1, 2)
subimage = rearrange(subimage, '(n t) c h w -> n c t h w', n=n, t=t)
return subimage
def get_generator():
global operation_seed_counter
operation_seed_counter += 1
# cuda
g_cuda_generator = torch.Generator(device="cuda")
g_cuda_generator.manual_seed(operation_seed_counter)
return g_cuda_generator
def space_to_depth(x, block_size):
n, c, h, w = x.size()
unfolded_x = torch.nn.functional.unfold(x, block_size, stride=block_size)
return unfolded_x.view(n, c * block_size**2, h // block_size,
w // block_size)
class AugmentNoise_np(object):
def __init__(self, style):
print(style)
if style.startswith('gauss'):
self.params = [
float(p) / 255.0 for p in style.replace('gauss', '').split('_')
]
if len(self.params) == 1:
self.style = "gauss_fix"
elif len(self.params) == 2:
self.style = "gauss_range"
elif style.startswith('poisson'):
self.params = [
float(p) for p in style.replace('poisson', '').split('_')
]
if len(self.params) == 1:
self.style = "poisson_fix"
elif len(self.params) == 2:
self.style = "poisson_range"
def add_train_noise(self, x):
shape = x.shape
if self.style == "gauss_fix":
std = self.params[0]
std = std * np.ones((shape[0], 1, 1))
noise = np.random.normal(
loc=0.0,
scale=std,
size=shape
)
return x + noise
elif self.style == "gauss_range":
min_std, max_std = self.params
std = np.random.rand((shape[0], 1, 1)) * (max_std - min_std) + min_std
noise = np.random.normal(
loc=0.0,
scale=std,
size=shape
)
return x + noise
elif self.style == "poisson_fix":
lam = self.params[0]
lam = lam * np.ones((shape[0], 1, 1))
noised = np.random.poisson(lam * x, size=shape) / lam
return noised
elif self.style == "poisson_range":
min_lam, max_lam = self.params
lam = np.random.rand((shape[0], 1, 1)) * (max_lam - min_lam) + min_lam
noised = np.random.poisson(lam * x, size=shape) / lam
return noised
def add_valid_noise(self, x):
shape = x.shape
if self.style == "gauss_fix":
std = self.params[0]
return np.array(x + np.random.normal(size=shape) * std,
dtype=np.float32)
elif self.style == "gauss_range":
min_std, max_std = self.params
std = np.random.uniform(low=min_std, high=max_std, size=(1, 1, 1))
return np.array(x + np.random.normal(size=shape) * std,
dtype=np.float32)
elif self.style == "poisson_fix":
lam = self.params[0]
return np.array(np.random.poisson(lam * x) / lam, dtype=np.float32)
elif self.style == "poisson_range":
min_lam, max_lam = self.params
lam = np.random.uniform(low=min_lam, high=max_lam, size=(1, 1, 1))
return np.array(np.random.poisson(lam * x) / lam, dtype=np.float32)
class AugmentNoise(object):
def __init__(self, style):
print(style)
if style.startswith('gauss'):
self.params = [
float(p) / 255.0 for p in style.replace('gauss', '').split('_')
]
if len(self.params) == 1:
self.style = "gauss_fix"
elif len(self.params) == 2:
self.style = "gauss_range"
elif style.startswith('poisson'):
self.params = [
float(p) for p in style.replace('poisson', '').split('_')
]
if len(self.params) == 1:
self.style = "poisson_fix"
elif len(self.params) == 2:
self.style = "poisson_range"
def add_train_noise(self, x):
shape = x.shape
if self.style == "gauss_fix":
std = self.params[0]
std = std * torch.ones((shape[0], 1, 1), device=x.device)
noise = torch.cuda.FloatTensor(shape, device=x.device)
torch.normal(mean=0.0,
std=std,
generator=get_generator(),
out=noise)
return x + noise
elif self.style == "gauss_range":
min_std, max_std = self.params
std = torch.rand(size=(shape[0], 1, 1),
device=x.device) * (max_std - min_std) + min_std
noise = torch.cuda.FloatTensor(shape, device=x.device)
torch.normal(mean=0, std=std, generator=get_generator(), out=noise)
return x + noise
elif self.style == "poisson_fix":
lam = self.params[0]
lam = lam * torch.ones((shape[0], 1, 1), device=x.device)
noised = torch.poisson(lam * x, generator=get_generator()) / lam
return noised
elif self.style == "poisson_range":
min_lam, max_lam = self.params
lam = torch.rand(size=(shape[0], 1, 1),
device=x.device) * (max_lam - min_lam) + min_lam
noised = torch.poisson(lam * x, generator=get_generator()) / lam
return noised
def add_valid_noise(self, x):
shape = x.shape
if self.style == "gauss_fix":
std = self.params[0]
return np.array(x + np.random.normal(size=shape) * std,
dtype=np.float32)
elif self.style == "gauss_range":
min_std, max_std = self.params
std = np.random.uniform(low=min_std, high=max_std, size=(1, 1, 1))
return np.array(x + np.random.normal(size=shape) * std,
dtype=np.float32)
elif self.style == "poisson_fix":
lam = self.params[0]
return np.array(np.random.poisson(lam * x) / lam, dtype=np.float32)
elif self.style == "poisson_range":
min_lam, max_lam = self.params
lam = np.random.uniform(low=min_lam, high=max_lam, size=(1, 1, 1))
return np.array(np.random.poisson(lam * x) / lam, dtype=np.float32)
if __name__ == "__main__":
img = torch.randn((2, 10, 16, 64, 64))
mask1, mask2 = generate_mask_pair(img)
noisy_sub1 = generate_subimages(img, mask1)
noisy_sub2 = generate_subimages(img, mask2)
print(noisy_sub1.shape)
print(noisy_sub2.shape)