-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
288 lines (216 loc) · 6.94 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
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
import math
import os
import random
import numpy as np
import skimage.color
import skimage.draw
import skimage.exposure
import skimage.filters
import skimage.filters.rank
import skimage.io
import skimage.morphology
import skimage.transform
import skimage.util
from PIL import Image
import torch
from torchvision import transforms, utils
INCEPTION_SIZE = 299
IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD = [0.229, 0.224, 0.225]
transforms_imagenet_inception = transforms.Compose([
transforms.Resize(INCEPTION_SIZE),
transforms.CenterCrop(INCEPTION_SIZE),
transforms.ToTensor(),
transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
])
def center_crop(array):
width, height = array.shape[0], array.shape[1]
if width == height:
return array
if width < height:
diff = height - width
start = math.floor(diff / 2)
end = math.ceil(diff / 2)
return array[start:-end, :]
if width > height:
diff = width - height
start = math.floor(diff / 2)
end = math.ceil(diff / 2)
return array[:, start:-end]
def open_image_properly(path, arch):
# Open image. Results in values between 0 and 255.
array = skimage.io.imread(path)
# Crop.
array = center_crop(array)
# Resize.
if arch == 'inception':
length = INCEPTION_SIZE
else:
raise ValueError()
array = skimage.transform.resize(array, (length, length))
# Convert to float. Results in values between 0 and 1.
array = skimage.util.img_as_float(array)
return array
def array_to_tensor(array):
# Apply imagenet normalization.
array = array.copy()
array = array - IMAGENET_MEAN
array = array / IMAGENET_STD
# Rearrange.
array = array.transpose([2, 0, 1])
# Add dimension.
array = np.expand_dims(array, 0)
# Convert to tensor and return.
tensor = torch.Tensor(array)
return tensor
def image2tensor(image, arch='inception'):
'''
Turns a PIL image into a pytorch tensor for the
pretrained inception network trained on imagenet.
'''
if arch == 'inception':
tensor = transforms_imagenet_inception(image)
else:
raise ValueError()
tensor.unsqueeze_(0)
return tensor
def tensor2array(tensor):
'''
Turns a pytorch tensor, prepared for the pretrained
inception network trained on imagenet into a numpy array
with values between zero and one.
'''
# Turn tensor into numpy array.
array = tensor.clone().detach()[0].numpy()
# Rearrange from (c, w, h) to (w, h, c).
array = array.transpose([1, 2, 0])
# Denormalize.
array *= IMAGENET_STD
array += IMAGENET_MEAN
# Clip.
array = array.clip(0, 1)
return array
def array2tensor(array):
'''
Turns a numpy array with values between zero and one
into a pytorch tensor for the pretrained inception
network trained on imagenet.
'''
# Clone.
array = array.copy()
# Add first dimension.
dim = len(array.shape)
if dim == 3:
array = np.expand_dims(array, 0)
elif dim == 4:
pass
else:
raise ValueError()
# Normalize.
array -= IMAGENET_MEAN
array /= IMAGENET_STD
# Rearrange.
array = array.transpose([0, 3, 1, 2])
# Turn into tensor.
tensor = torch.Tensor(array)
return tensor
def open_image_as_tensor(path, arch='inception'):
'''
Opens an image using PIL and turns it into a pytorch
tensor for the pretrained inception network trained on
imagenet.
'''
image = Image.open(path)
tensor = image2tensor(image, arch)
return tensor
def save_tensor_as_image(tensor, path):
'''
Saves a pytorch tensor as an image file.
'''
array = tensor2array(tensor)
skimage.io.imsave(path, array)
class ImageNetLabelDecoder():
def __init__(self, root):
path_classes = os.path.join(root, 'imagenet_classes.txt')
path_synsets = os.path.join(root, 'imagenet_synsets.txt')
# Load synsets.
with open(path_synsets, 'r') as handle:
synsets = handle.readlines()
synsets = [line.strip().split() for line in synsets]
self.names = {synset[0]: ' '.join(synset[1:]) for synset in synsets}
# Load classes.
with open(path_classes, 'r') as handle:
classes = handle.readlines()
self.classes = [line.strip() for line in classes]
def decode(self, idx):
try:
return self.names[self.classes[int(idx)]]
except TypeError:
try:
return [self.decode(i) for i in idx]
except:
raise
def __call__(self, idx):
return self.decode(idx)
def print_top_predictions(self, predictions, n_predictions=5):
# Select top scoring predictions.
top_idx = np.argpartition(predictions, -n_predictions)[-n_predictions:]
# Sort top scoring predictions.
top_idx = top_idx[np.argsort(predictions[top_idx])][::-1]
# Calculate certainties.
sm = torch.nn.Softmax(0)
certainties = sm(torch.Tensor(predictions))
# Print.
for i in top_idx:
print('{:3d}\t{:04.2f}\t{}'.format(i, certainties[i],
self.decode(i)))
def image2mask(image, size=3):
gray = skimage.color.rgb2gray(image)
mask = skimage.filters.rank.entropy(gray, skimage.morphology.disk(size))
mask = skimage.exposure.equalize_hist(mask)
mask -= mask.min()
mask /= mask.max()
mask = np.power(mask, 4)
return mask
def random_mask(shape, radius):
# Initialize empty mask.
mask = np.zeros(shape)
# Determine center of circle.
(width, height) = shape
radius = int(radius)
x = random.randint(radius, width - radius)
y = random.randint(radius, height - radius)
# Draw circle.
row_idx, col_idx = skimage.draw.circle(x, y, radius, shape)
mask[row_idx, col_idx] = 1
# Blur.
mask = skimage.filters.gaussian(mask, radius * 0.1)
return mask
def flat_idx_to_matrix_idx(idx, shape):
(_, n_cols) = shape
row_idx = idx // n_cols
col_idx = idx % n_cols
return row_idx, col_idx
def binary_mask_from_highest_values(matrix, n_ones):
# Reduce.
matrix = np.linalg.norm(matrix, axis=0)
# Find indices of highest values.
flat = matrix.flatten()
flat = np.abs(flat)
perm = flat.argsort()[::-1]
highest = perm[:n_ones]
row_idx, col_idx = flat_idx_to_matrix_idx(highest, matrix.shape)
# Initialize empty mask and fill in ones.
mask = np.zeros_like(matrix)
mask[row_idx, col_idx] = 1
return mask
def open_image_as_segments(path):
img = skimage.io.imread(path)
img = skimage.color.rgb2gray(img)
segments = np.zeros_like(img, dtype=int)
for i, val in enumerate(np.unique(img)):
segments[img == val] = i
return segments
def relative_total_energy_from_map(energy_map):
energy = np.sum(energy_map) / np.prod(energy_map.shape)
return energy