-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
209 lines (156 loc) · 6.98 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
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
import torch
import numpy as np
from torchvision.datasets.folder import has_file_allowed_extension
from torchvision.transforms.functional import to_pil_image, to_tensor, _is_pil_image
from PIL import Image
IMG_EXTENSIONS = ['.png']
def denormalize(imgs, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
c, w, h = imgs.size()[1:]
if len(mean) != len(std):
raise ValueError('The length of mean and var should be the same')
if c != len(mean):
raise ValueError(f'The length of mean and var should be {3}, but got {len(mean)}')
if not isinstance(mean, torch.Tensor):
mean = torch.FloatTensor(mean)
if not isinstance(std, torch.Tensor):
std = torch.FloatTensor(std)
mean = mean.view(-1, 1, 1).expand(c, w, h).unsqueeze(0).to(imgs.device)
std = std.view(-1, 1, 1).expand(c, w, h).unsqueeze(0).to(imgs.device)
return imgs * std + mean
def is_image_file(filename):
"""Checks if a file is an allowed image extension.
Args:
filename (string): path to a file
Returns:
bool: True if the filename ends with a known image extension
"""
return has_file_allowed_extension(filename, IMG_EXTENSIONS)
def pil_loader(path):
img = Image.open(path)
img.convert('RGB')
return img
def fluore_to_tensor(pic):
"""Convert a ``PIL Image`` to tensor. Range stays the same.
Only output one channel, if RGB, convert to grayscale as well.
Current data is 8 bit depth.
Args:
pic (PIL Image): Image to be converted to Tensor.
Returns:
Tensor: only one channel, Tensor type consistent with bit-depth.
"""
if not (_is_pil_image(pic)):
raise TypeError('pic should be PIL Image. Got {}'.format(type(pic)))
# handle PIL Image
if pic.mode == 'I':
img = torch.from_numpy(np.array(pic, np.int32, copy=False))
elif pic.mode == 'I;16':
img = torch.from_numpy(np.array(pic, np.int16, copy=False))
elif pic.mode == 'F':
img = torch.from_numpy(np.array(pic, np.float32, copy=False))
elif pic.mode == '1':
img = 255 * torch.from_numpy(np.array(pic, np.uint8, copy=False))
else:
# all 8-bit: L, P, RGB, YCbCr, RGBA, CMYK
img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))
# PIL image mode: L, P, I, F, RGB, YCbCr, RGBA, CMYK
if pic.mode == 'YCbCr':
nchannel = 3
elif pic.mode == 'I;16':
nchannel = 1
else:
nchannel = len(pic.mode)
img = img.view(pic.size[1], pic.size[0], nchannel)
if nchannel == 1:
img = img.squeeze(-1).unsqueeze(0)
elif pic.mode in ('RGB', 'RGBA'):
# RBG to grayscale:
# https://en.wikipedia.org/wiki/Luma_%28video%29
ori_dtype = img.dtype
rgb_weights = torch.tensor([0.2989, 0.5870, 0.1140])
img = (img[:, :, [0, 1, 2]].float() * rgb_weights).sum(-1).unsqueeze(0)
img = img.to(ori_dtype)
else:
# other type not supported yet: YCbCr, CMYK
raise TypeError('Unsupported image type {}'.format(pic.mode))
return img
class MaskGenerator:
def __init__(self, input_size=192, mask_patch_size=32, model_patch_size=4, mask_ratio=0.6):
self.input_size = input_size
self.mask_patch_size = mask_patch_size
self.model_patch_size = model_patch_size
self.mask_ratio = mask_ratio
assert self.input_size % self.mask_patch_size == 0
assert self.mask_patch_size % self.model_patch_size == 0
self.rand_size = self.input_size // self.mask_patch_size
self.scale = self.mask_patch_size // self.model_patch_size
self.token_count = self.rand_size ** 2
self.mask_count = int(np.ceil(self.token_count * self.mask_ratio))
def __call__(self):
mask_idx = np.random.permutation(self.token_count)[:self.mask_count]
mask = np.zeros(self.token_count, dtype=int)
mask[mask_idx] = 1
mask = mask.reshape((self.rand_size, self.rand_size))
mask = mask.repeat(self.scale, axis=0).repeat(self.scale, axis=1)
return torch.from_numpy(mask)
class Masker():
"""Object for masking and demasking"""
def __init__(self, width=3, mode='zero', infer_single_pass=False, include_mask_as_input=False, channel=3):
self.grid_size = width
self.n_masks = width ** 2
self.mode = mode
self.infer_single_pass = infer_single_pass
self.include_mask_as_input = include_mask_as_input
self.channel = channel
def mask(self, X, i):
phasex = i % self.grid_size
phasey = (i // self.grid_size) % self.grid_size
mask = self.pixel_grid_mask(X[0, 0].shape, self.grid_size, phasex, phasey)
mask = mask.to(X.device)
mask_inv = torch.ones(mask.shape).to(X.device) - mask
if self.mode == 'interpolate':
masked = self.interpolate_mask(X, mask, mask_inv, channel=self.channel)
elif self.mode == 'zero':
masked = X * mask_inv
else:
raise NotImplementedError
if self.include_mask_as_input:
net_input = torch.cat((masked, mask.repeat(X.shape[0], 1, 1, 1)), dim=1)
else:
net_input = masked
return net_input, mask
def __len__(self):
return self.n_masks
def infer_full_image(self, X, model):
if self.infer_single_pass:
if self.include_mask_as_input:
net_input = torch.cat((X, torch.zeros(X[:, 0:1].shape).to(X.device)), dim=1)
else:
net_input = X
net_output = model(net_input)
return net_output
else:
net_input, mask = self.mask(X, 0)
net_output = model(net_input)
acc_tensor = torch.zeros(net_output.shape).cpu()
for i in range(self.n_masks):
net_input, mask = self.mask(X, i)
net_output = model(net_input)
acc_tensor = acc_tensor + (net_output * mask).cpu()
return acc_tensor
def pixel_grid_mask(self, shape, patch_size, phase_x, phase_y):
A = torch.zeros(shape[-2:])
for i in range(shape[-2]):
for j in range(shape[-1]):
if (i % patch_size == phase_x and j % patch_size == phase_y):
A[i, j] = 1
return torch.Tensor(A)
def interpolate_mask(self, tensor, mask, mask_inv, channel):
device = tensor.device
mask = mask.to(device)
kernel = np.array([[0.5, 1.0, 0.5], [1.0, 0.0, 1.0], (0.5, 1.0, 0.5)])
kernel = kernel[np.newaxis, np.newaxis, :, :]
kernel = np.repeat(kernel, channel, 1)
kernel = torch.Tensor(kernel).to(device)
kernel = kernel / kernel.sum()
filtered_tensor = torch.nn.functional.conv2d(tensor, kernel, stride=1, padding=1)
return filtered_tensor * mask + tensor * mask_inv