-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
329 lines (271 loc) · 10.5 KB
/
Copy pathutils.py
File metadata and controls
329 lines (271 loc) · 10.5 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Harbin Institute of Technology Bachelor Thesis
# Author: HIT Michael_Bryant
# Mail: 1137892110@qq.com
import matplotlib.pyplot as plt
from torch.autograd import Variable
import math
from typing import Tuple
import torch
from torch._C import StringType, device
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as datasets
import torchvision.transforms.functional as TF
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
import torch.optim as optim
import torchvision
import torch.distributions as td
import torch
"""
Functions for padding the images to admissible input shapes of the U-Net
"""
def scale_array(array, min_value, max_value): # 找到数组中的最小值和最大值
min_val = np.min(array)
max_val = np.max(array) # 将数组的元素缩放到目标范围
scaled_array = min_value + (max_value - min_value) * (array - min_val) / (max_val - min_val)
return scaled_array
def data_normal(origin_data):
d_min = origin_data.min()
if d_min < 0:
origin_data += torch.abs(d_min)
d_min = origin_data.min()
d_max = origin_data.max()
dst = d_max - d_min
norm_data = (origin_data - d_min).true_divide(dst)
return norm_data
def pad_to_admissible_size(x, image_size, admissible_size):
if isinstance(image_size, tuple):
v_margin = admissible_size[0] - image_size[0]
h_margin = admissible_size[1] - image_size[1]
# if margin is not even, pad one more pixel on the right/up than on the left/down
pads = (
int(math.floor(h_margin / 2)),
int(math.ceil(h_margin / 2)),
int(math.floor(v_margin / 2)),
int(math.ceil(v_margin / 2)),
)
out = F.pad(x, pads, "constant", 0)
else:
margin = admissible_size - image_size
# if margin is not even, pad one more pixel on the right/up than on the left/down
pads = (
int(math.floor(margin / 2)),
int(math.ceil(margin / 2)),
int(math.floor(margin / 2)),
int(math.ceil(margin / 2)),
)
out = F.pad(x, pads, "constant", 0)
return out, pads
def pad_to_image_size(output, image_size, output_size):
if isinstance(image_size, tuple):
v_margin = image_size[0] - output_size[0]
h_margin = image_size[1] - output_size[1]
# if margin is not even, pad one more pixel on the right/up than on the left/down
pads = (
int(math.floor(h_margin / 2)),
int(math.ceil(h_margin / 2)),
int(math.floor(v_margin / 2)),
int(math.ceil(v_margin / 2)),
)
out = F.pad(output, pads, "constant", 0)
else:
margin = image_size - output_size
# if margin is not even, pad one more pixel on the right/up than on the left/down
pads = (
int(math.floor(margin / 2)),
int(math.ceil(margin / 2)),
int(math.floor(margin / 2)),
int(math.ceil(margin / 2)),
)
out = F.pad(output, pads, "constant", 0)
return out, pads
def unpad(x, pad):
if pad[2] + pad[3] > 0:
x = x[:, :, pad[2] : -pad[3], :]
if pad[0] + pad[1] > 0:
x = x[:, :, :, pad[0] : -pad[1]]
return x
def get_pads_to_original_size(image_size, output_size):
"""
Args
image_size: (int) size of input image
output_size: (int) size of the image that comes out of the U-Net
Returns:
pads: (tuple) padding that is neccesary to get to the input size from the output_size
"""
if isinstance(image_size, tuple):
v_margin = image_size[0] - output_size[0]
h_margin = image_size[1] - output_size[1]
# if margin is not even, pad one more pixel on the right/up than on the left/down
pads = (
int(math.floor(h_margin / 2)),
int(math.ceil(h_margin / 2)),
int(math.floor(v_margin / 2)),
int(math.ceil(v_margin / 2)),
)
return pads
else:
margin = image_size - output_size
# if margin is not even, pad one more pixel on the right/up than on the left/down
pads = (
int(math.floor(margin / 2)),
int(math.ceil(margin / 2)),
int(math.floor(margin / 2)),
int(math.ceil(margin / 2)),
)
return pads
"""
Functions for plotting
"""
def make_image_grid(images, masks, predictions, required_padding):
"""
Args
X_batch: (torch.tensor BxCxHxW) Tensor contains the input images
target_batch: (torch.tensor BxCxHxW) Tensor contains the target segmentations
pred_batch: (torch.tensor BxCxHxW) Tensor contains the predictions
Returns:
grid: grid object to be plotted in wandb
"""
grid_img = torchvision.utils.make_grid(images, len(images))
grid_target = torchvision.utils.make_grid(
F.pad(masks, required_padding, "constant", 0), len(masks)
)
grid_pred = torchvision.utils.make_grid(
F.pad(predictions, required_padding, "constant", 0), len(predictions)
)
grid = torch.stack([grid_img, grid_target, grid_pred])
grid = torchvision.utils.make_grid(grid, 1)
return grid
def make_image_grid_with_heatmaps(images, masks, predictions, required_padding):
"""
Args
X_batch: (torch.tensor BxCxHxW) Tensor contains the input images
target_batch: (torch.tensor BxCxHxW) Tensor contains the target segmentations
pred_batch: (torch.tensor BxCxHxW) Tensor contains the predictions
Returns:
grid: grid object to be plotted in wandb
"""
grid_img = torchvision.utils.make_grid(images, len(images))
grid_target = torchvision.utils.make_grid(masks, len(masks))
grid_pred = torchvision.utils.make_grid(
F.pad(predictions, required_padding, "constant", 0), len(predictions)
)
grid = torch.stack([grid_img, grid_target, grid_pred])
grid = torchvision.utils.make_grid(grid, 1)
return grid
"""
Functions for Probabilistic U-Net based on implementation of https://github.com/stefanknegt/Probabilistic-Unet-Pytorch/blob/master/utils.py
"""
def truncated_normal_(tensor, mean=0, std=1):
size = tensor.shape
tmp = tensor.new_empty(size + (4,)).normal_()
valid = (tmp < 2) & (tmp > -2)
ind = valid.max(-1, keepdim=True)[1]
tensor.data.copy_(tmp.gather(-1, ind).squeeze(-1))
tensor.data.mul_(std).add_(mean)
def init_weights(m):
if type(m) == nn.Conv2d or type(m) == nn.ConvTranspose2d:
nn.init.kaiming_normal_(m.weight, mode="fan_in", nonlinearity="relu")
# nn.init.normal_(m.weight, std=0.001)
# nn.init.normal_(m.bias, std=0.001)
truncated_normal_(m.bias, mean=0, std=0.001)
def init_weights_orthogonal_normal(m):
if type(m) == nn.Conv2d or type(m) == nn.ConvTranspose2d:
nn.init.orthogonal_(m.weight)
truncated_normal_(m.bias, mean=0, std=0.001)
# nn.init.normal_(m.bias, std=0.001)
def l2_regularisation(m):
l2_reg = None
for W in m.parameters():
if l2_reg is None:
l2_reg = W.norm(2)
else:
l2_reg = l2_reg + W.norm(2)
return l2_reg
def save_mask_prediction_example(mask, pred, iter):
plt.imshow(pred[0, :, :], cmap="Greys")
plt.savefig("images/" + str(iter) + "_prediction.png")
plt.imshow(mask[0, :, :], cmap="Greys")
plt.savefig("images/" + str(iter) + "_mask.png")
"""
SSN Implementation https://github.com/biomedia-mira/stochastic_segmentation_networks/blob/master/ssn/
"""
class SSNCrossEntropyLoss(nn.CrossEntropyLoss):
def __init__(
self,
weight=None,
size_average=None,
ignore_index=-100,
reduce=None,
reduction="mean",
):
super().__init__(weight, size_average, ignore_index, reduce, reduction)
def forward(self, logits: torch.tensor, target: torch.tensor, **kwargs):
return super().forward(logits, target)
class StochasticSegmentationNetworkLossMCIntegral(nn.Module):
def __init__(self, num_mc_samples: int = 1):
super().__init__()
self.num_mc_samples = num_mc_samples
@staticmethod
def fixed_re_parametrization_trick(dist, num_samples):
assert num_samples % 2 == 0
samples = dist.rsample((num_samples // 2,))
mean = dist.mean.unsqueeze(0)
samples = samples - mean
return torch.cat([samples, -samples]) + mean
def forward(self, logits, target, distribution, **kwargs):
batch_size = logits.shape[0]
num_classes = logits.shape[1]
logit_sample = self.fixed_re_parametrization_trick(
distribution, self.num_mc_samples
)
target = target.unsqueeze(1)
target = target.expand((self.num_mc_samples,) + target.shape)
flat_size = self.num_mc_samples * batch_size
logit_sample = logit_sample.view((flat_size, num_classes, -1))
target = target.reshape((flat_size, -1))
target = target.unsqueeze(1)
log_prob = -F.binary_cross_entropy_with_logits(
logit_sample, target, reduction="none"
).view((self.num_mc_samples, batch_size, -1))
loglikelihood = torch.mean(
torch.logsumexp(torch.sum(log_prob, dim=-1), dim=0)
- math.log(self.num_mc_samples)
)
loss = -loglikelihood
return loss
class ReshapedDistribution(td.Distribution):
def __init__(
self,
base_distribution: td.Distribution,
new_event_shape: Tuple[int, ...],
validate_args=None,
):
super().__init__(
batch_shape=base_distribution.batch_shape,
event_shape=new_event_shape,
validate_args=validate_args,
)
self.base_distribution = base_distribution
self.new_shape = base_distribution.batch_shape + new_event_shape
@property
def support(self):
return self.base_distribution.support
@property
def arg_constraints(self):
return self.base_distribution.arg_constraints()
@property
def mean(self):
return self.base_distribution.mean.view(self.new_shape)
@property
def variance(self):
return self.base_distribution.variance.view(self.new_shape)
def rsample(self, sample_shape=torch.Size()):
return self.base_distribution.rsample(sample_shape).view(
sample_shape + self.new_shape
)
def log_prob(self, value):
return self.base_distribution.log_prob(value.view(self.batch_shape + (-1,)))
def entropy(self):
return self.base_distribution.entropy()