-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
635 lines (526 loc) · 20.6 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# -*- coding: utf-8 -*-
import random
import numpy as np
from sklearn.metrics import confusion_matrix
import sklearn.model_selection
import seaborn as sns
import itertools
import spectral
import visdom
import matplotlib.pyplot as plt
from scipy import io, misc
import os
import re
import torch
def get_device(ordinal):
# Use GPU ?
if ordinal < 0:
print("Computation on CPU")
device = torch.device('cpu')
elif torch.cuda.is_available():
print("Computation on CUDA GPU device {}".format(ordinal))
device = torch.device('cuda:{}'.format(ordinal))
else:
print("/!\\ CUDA was requested but is not available! Computation will go on CPU. /!\\")
device = torch.device('cpu')
return device
def open_file(dataset):
_, ext = os.path.splitext(dataset)
ext = ext.lower()
if ext == '.mat':
# Load Matlab array
return io.loadmat(dataset)
elif ext == '.tif' or ext == '.tiff':
# Load TIFF file
return misc.imread(dataset)
elif ext == '.hdr':
img = spectral.open_image(dataset)
return img.load()
else:
raise ValueError("Unknown file format: {}".format(ext))
def convert_to_color_(arr_2d, palette=None):
"""Convert an array of labels to RGB color-encoded image.
Args:
arr_2d: int 2D array of labels
palette: dict of colors used (label number -> RGB tuple)
Returns:
arr_3d: int 2D images of color-encoded labels in RGB format
"""
arr_3d = np.zeros((arr_2d.shape[0], arr_2d.shape[1], 3), dtype=np.uint8)
if palette is None:
raise Exception("Unknown color palette")
for c, i in palette.items():
m = arr_2d == c
arr_3d[m] = i
return arr_3d
def convert_from_color_(arr_3d, palette=None):
"""Convert an RGB-encoded image to grayscale labels.
Args:
arr_3d: int 2D image of color-coded labels on 3 channels
palette: dict of colors used (RGB tuple -> label number)
Returns:
arr_2d: int 2D array of labels
"""
if palette is None:
raise Exception("Unknown color palette")
arr_2d = np.zeros((arr_3d.shape[0], arr_3d.shape[1]), dtype=np.uint8)
for c, i in palette.items():
m = np.all(arr_3d == np.array(c).reshape(1, 1, 3), axis=2)
arr_2d[m] = i
return arr_2d
def display_predictions(pred, vis, gt=None, caption=""):
if gt is None:
vis.images([np.transpose(pred, (2, 0, 1))],
opts={'caption': caption})
else:
vis.images([np.transpose(pred, (2, 0, 1)),
np.transpose(gt, (2, 0, 1))],
nrow=2,
opts={'caption': caption})
def display_dataset(img, gt, bands, labels, palette, vis):
"""Display the specified dataset.
Args:
img: 3D hyperspectral image
gt: 2D array labels
bands: tuple of RGB bands to select
labels: list of label class names
palette: dict of colors
display (optional): type of display, if any
"""
print("Image 1 has dimensions {}x{} and {} channels".format(*img.shape))
rgb = spectral.get_rgb(img, bands)
rgb /= np.max(rgb)
rgb = np.asarray(255 * rgb, dtype='uint8')
# Display the RGB composite image
caption = "RGB (bands {}, {}, {})".format(*bands)
# send to visdom server
vis.images([np.transpose(rgb, (2, 0, 1))],
opts={'caption': caption})
def display_lidar_data(img, vis):
"""Display the LiDAR data.
Args:
img: 2D LiDAR image
"""
print("Image 2 has dimensions {}x{} and {} channels".format(*img.shape))
gray = img / np.max(img)
gray = np.asarray(255 * gray, dtype='uint8')
# Display the lidar composite image
caption = "LiDAR"
# send to visdom server
vis.images([np.transpose(gray, (2, 0, 1))],
opts={'caption': caption})
def explore_spectrums(img, complete_gt, class_names, vis,
ignored_labels=None):
"""Plot sampled spectrums with mean + std for each class.
Args:
img: 3D hyperspectral image
complete_gt: 2D array of labels
class_names: list of class names
ignored_labels (optional): list of labels to ignore
vis : Visdom display
Returns:
mean_spectrums: dict of mean spectrum by class
"""
mean_spectrums = {}
for c in np.unique(complete_gt):
if c in ignored_labels:
continue
mask = complete_gt == c
class_spectrums = img[mask].reshape(-1, img.shape[-1])
step = max(1, class_spectrums.shape[0] // 100)
fig = plt.figure()
plt.title(class_names[c])
# Sample and plot spectrums from the selected class
for spectrum in class_spectrums[::step, :]:
plt.plot(spectrum, alpha=0.25)
mean_spectrum = np.mean(class_spectrums, axis=0)
std_spectrum = np.std(class_spectrums, axis=0)
lower_spectrum = np.maximum(0, mean_spectrum - std_spectrum)
higher_spectrum = mean_spectrum + std_spectrum
# Plot the mean spectrum with thickness based on std
plt.fill_between(range(len(mean_spectrum)), lower_spectrum,
higher_spectrum, color="#3F5D7D")
plt.plot(mean_spectrum, alpha=1, color="#FFFFFF", lw=2)
vis.matplot(plt)
mean_spectrums[class_names[c]] = mean_spectrum
return mean_spectrums
def plot_spectrums(spectrums, vis, title=""):
"""Plot the specified dictionary of spectrums.
Args:
spectrums: dictionary (name -> spectrum) of spectrums to plot
vis: Visdom display
"""
win = None
for k, v in spectrums.items():
n_bands = len(v)
update = None if win is None else 'append'
win = vis.line(X=np.arange(n_bands), Y=v, name=k, win=win, update=update,
opts={'title': title})
def build_dataset(mat, gt, ignored_labels=None):
"""Create a list of training samples based on an image and a mask.
Args:
mat: 3D hyperspectral matrix to extract the spectrums from
gt: 2D ground truth
ignored_labels (optional): list of classes to ignore, e.g. 0 to remove
unlabeled pixels
return_indices (optional): bool set to True to return the indices of
the chosen samples
"""
samples = []
labels = []
# Check that image and ground truth have the same 2D dimensions
assert mat.shape[:2] == gt.shape[:2]
for label in np.unique(gt):
if label in ignored_labels:
continue
else:
indices = np.nonzero(gt == label)
samples += list(mat[indices])
labels += len(indices[0]) * [label]
return np.asarray(samples), np.asarray(labels)
def get_random_pos(img, window_shape):
""" Return the corners of a random window in the input image
Args:
img: 2D (or more) image, e.g. RGB or grayscale image
window_shape: (width, height) tuple of the window
Returns:
xmin, xmax, ymin, ymax: tuple of the corners of the window
"""
w, h = window_shape
W, H = img.shape[:2]
x1 = random.randint(0, W - w - 1)
x2 = x1 + w
y1 = random.randint(0, H - h - 1)
y2 = y1 + h
return x1, x2, y1, y2
def padding_image(image, patch_size=None, mode="symmetric", constant_values=None):
"""Padding an input image.
Modified at 2020.11.16. If you find any issues, please email at [email protected] with details.
Args:
image: 2D+ image with a shape of [h, w, ...],
The array to pad
patch_size: optional, a list include two integers, default is [1, 1] for pure spectra algorithm,
The patch size of the algorithm
mode: optional, str or function, default is "symmetric",
Including 'constant', 'reflect', 'symmetric', more details see np.pad()
constant_values: optional, sequence or scalar, default is 0,
Used in 'constant'. The values to set the padded values for each axis
Returns:
padded_image with a shape of [h + patch_size[0] // 2 * 2, w + patch_size[1] // 2 * 2, ...]
"""
if patch_size is None:
patch_size = [1, 1]
h = patch_size[0] // 2
w = patch_size[1] // 2
pad_width = [[h, h], [w, w]]
[pad_width.append([0, 0]) for i in image.shape[2:]]
padded_image = np.pad(image, pad_width, mode=mode)
return padded_image
def restore_from_padding(image, patch_size=None):
if patch_size is None:
patch_size = [1, 1]
h = patch_size[0] // 2
w = patch_size[1] // 2
W, H = image.shape[:2]
restore_img = image[w:W-w, h:H-h, :]
return restore_img
def sliding_window(image1, image2, step=10, window_size=(20, 20), with_data=True):
"""Sliding window generator over an input image.
Args:
image: 2D+ image to slide the window on, e.g. RGB or hyperspectral
step: int stride of the sliding window
window_size: int tuple, width and height of the window
with_data (optional): bool set to True to return both the data and the
corner indices
Yields:
([data], x, y, w, h) where x and y are the top-left corner of the
window, (w,h) the window size
"""
# slide a window across the image
w, h = window_size
W, H = image1.shape[:2]
offset_w = (W - w) % step
offset_h = (H - h) % step
"""
Compensate one for the stop value of range(...). because this function does not include the stop value.
Two examples are listed as follows.
When step = 1, supposing w = h = 3, W = H = 7, and step = 1.
Then offset_w = 0, offset_h = 0.
In this case, the x should have been ranged from 0 to 4 (4-6 is the last window),
i.e., x is in range(0, 5) while W (7) - w (3) + offset_w (0) + 1 = 5. Plus one !
Range(0, 5, 1) equals [0, 1, 2, 3, 4].
When step = 2, supposing w = h = 3, W = H = 8, and step = 2.
Then offset_w = 1, offset_h = 1.
In this case, x is in [0, 2, 4] while W (8) - w (3) + offset_w (1) + 1 = 6. Plus one !
Range(0, 6, 2) equals [0, 2, 4]/
Same reason to H, h, offset_h, and y.
"""
for x in range(0, W - w + offset_w + 1, step):
if x + w > W:
x = W - w
for y in range(0, H - h + offset_h + 1, step):
if y + h > H:
y = H - h
if with_data:
yield image1[x:x + w, y:y + h], image2[x:x + w, y:y + h], x, y, w, h
else:
yield x, y, w, h
def count_sliding_window(top, top2, step=10, window_size=(20, 20)):
""" Count the number of windows in an image.
Args:
image: 2D+ image to slide the window on, e.g. RGB or hyperspectral, ...
step: int stride of the sliding window
window_size: int tuple, width and height of the window
Returns:
int number of windows
"""
sw = sliding_window(top, top2, step, window_size, with_data=False)
return sum(1 for _ in sw)
def grouper(n, iterable):
""" Browse an iterable by grouping n elements by n elements.
Args:
n: int, size of the groups
iterable: the iterable to Browse
Yields:
chunk of n elements from the iterable
"""
it = iter(iterable)
while True:
chunk = tuple(itertools.islice(it, n))
if not chunk:
return
yield chunk
def metrics(prediction, target, ignored_labels=[], n_classes=None):
"""Compute and print metrics (accuracy, confusion matrix and F1 scores).
Args:
prediction: list of predicted labels
target: list of target labels
ignored_labels (optional): list of labels to ignore, e.g. 0 for undef
n_classes (optional): number of classes, max(target) by default
Returns:
accuracy, F1 score by class, confusion matrix
"""
ignored_mask = np.zeros(target.shape[:2], dtype=np.bool)
for l in ignored_labels:
ignored_mask[target == l] = True
ignored_mask = ~ignored_mask
target = target[ignored_mask]
prediction = prediction[ignored_mask]
results = {}
n_classes = np.max(target) + 1 if n_classes is None else n_classes
cm = confusion_matrix(
target,
prediction,
labels=range(n_classes))
results["Confusion matrix"] = cm
# Compute global accuracy
total = np.sum(cm)
accuracy = sum([cm[x][x] for x in range(len(cm))])
accuracy *= 100 / float(total)
results["Accuracy"] = accuracy
# Compute F1 score
F1scores = np.zeros(len(cm))
for i in range(len(cm)):
try:
F1 = 2. * cm[i, i] / (np.sum(cm[i, :]) + np.sum(cm[:, i]))
except ZeroDivisionError:
F1 = 0.
F1scores[i] = F1
results["F1 scores"] = F1scores
# Compute precision for every class
Precisions = np.zeros(len(cm))
for i in range(len(cm)):
try:
Precision = 1. * cm[i, i] / np.sum(cm[i, :])
except ZeroDivisionError:
Precision = 0.
Precisions[i] = Precision
results["Precisions"] = Precisions
# Compute Average Accuracy (AA)
AAs = []
for i in range(len(cm)):
try:
recall = cm[i][i] / np.sum(cm[i, :])
if np.isnan(recall):
continue
except ZeroDivisionError:
recall = 0.
AAs.append(recall)
results['AA'] = np.mean(AAs)
# Compute kappa coefficient
pa = np.trace(cm) / float(total)
pe = np.sum(np.sum(cm, axis=0) * np.sum(cm, axis=1)) / \
float(total * total)
kappa = (pa - pe) / (1 - pe)
results["Kappa"] = kappa
return results
def show_results(results, vis, label_values=None, agregated=False):
text = ""
if agregated:
accuracies = [r["Accuracy"] for r in results]
AAs = [r["AA"] for r in results]
kappas = [r["Kappa"] for r in results]
F1_scores = [r["F1 scores"] for r in results]
Precisions = [r["Precisions"] for r in results]
F1_scores_mean = np.mean(F1_scores, axis=0)
F1_scores_std = np.std(F1_scores, axis=0)
Precisions_mean = np.mean(Precisions, axis=0)
Precisions_std = np.std(Precisions, axis=0)
cm = np.mean([r["Confusion matrix"] for r in results], axis=0)
text += "Agregated results :\n"
else:
cm = results["Confusion matrix"]
accuracy = results["Accuracy"]
F1scores = results["F1 scores"]
Precision = results['Precisions']
AA = results['AA']
kappa = results["Kappa"]
vis.heatmap(cm, opts={'title': "Confusion matrix",
'marginbottom': 150,
'marginleft': 150,
'width': 500,
'height': 500,
'rownames': label_values, 'columnnames': label_values})
text += "Confusion matrix :\n"
text += str(cm)
text += "---\n"
if agregated:
text += ("Accuracy: {:.04f} +- {:.04f}\n".format(np.mean(accuracies),
np.std(accuracies)))
# for label, score, std in zip(label_values, accuracies_mean,
# accuracies_std):
# text += "\t{}: {:.03f} +- {:.03f}\n".format(label, score, std)
else:
text += "Accuracy : {:.04f}%\n".format(accuracy)
# for label, score in zip(label_values, accuracy):
# text += "\t{}: {:.03f}\n".format(label, score)
text += "---\n"
text += "F1 scores :\n"
if agregated:
for label, score, std in zip(label_values, F1_scores_mean,
F1_scores_std):
text += "\t{}: {:.04f} +- {:.04f}\n".format(label, score, std)
else:
for label, score in zip(label_values, F1scores):
text += "\t{}: {:.04f}\n".format(label, score)
text += "---\n"
text += "Precisions :\n"
if agregated:
for label, score, std in zip(label_values, Precisions_mean,
Precisions_std):
text += "\t{}: {:.04f} +- {:.04f}\n".format(label, score, std)
else:
for label, score in zip(label_values, Precision):
text += "\t{}: {:.04f}\n".format(label, score)
text += "---\n"
if agregated:
text += ("AA: {:.04f} +- {:.04f}\n".format(np.mean(AAs),
np.std(AAs)))
# for label, score, std in zip(label_values, accuracies_mean,
# accuracies_std):
# text += "\t{}: {:.03f} +- {:.03f}\n".format(label, score, std)
else:
text += "AA : {:.04f}\n".format(AA)
if agregated:
text += ("Kappa: {:.04f} +- {:.04f}\n".format(np.mean(kappas),
np.std(kappas)))
else:
text += "Kappa: {:.04f}\n".format(kappa)
vis.text(text.replace('\n', '<br/>'))
print(text)
def sample_gt(gt, train_size, mode='random'):
"""Extract a fixed percentage of samples from an array of labels.
Args:
gt: a 2D array of int labels
percentage: [0, 1] float
Returns:
train_gt, test_gt: 2D arrays of int labels
"""
indices = np.nonzero(gt)
X = list(zip(*indices)) # x,y features
y = gt[indices].ravel() # classes
train_gt = np.zeros_like(gt)
test_gt = np.zeros_like(gt)
if train_size > 1:
train_size = int(train_size)
if mode == 'random':
train_indices, test_indices = sklearn.model_selection.train_test_split(X, train_size=train_size, stratify=y)
train_indices = [list(t) for t in zip(*train_indices)]
test_indices = [list(t) for t in zip(*test_indices)]
train_gt[train_indices] = gt[train_indices]
test_gt[test_indices] = gt[test_indices]
elif mode == 'fixed':
print("Sampling {} with train size = {}".format(mode, train_size))
train_indices, test_indices = [], []
for c in np.unique(gt):
if c == 0:
continue
indices = np.nonzero(gt == c)
X = list(zip(*indices)) # x,y features
train, test = sklearn.model_selection.train_test_split(X, train_size=train_size)
train_indices += train
test_indices += test
train_indices = [list(t) for t in zip(*train_indices)]
test_indices = [list(t) for t in zip(*test_indices)]
train_gt[train_indices] = gt[train_indices]
test_gt[test_indices] = gt[test_indices]
elif mode == 'disjoint':
train_gt = np.copy(gt)
test_gt = np.copy(gt)
for c in np.unique(gt):
mask = gt == c
for x in range(gt.shape[0]):
first_half_count = np.count_nonzero(mask[:x, :])
second_half_count = np.count_nonzero(mask[x:, :])
try:
ratio = first_half_count / (first_half_count + second_half_count)
if ratio > 0.9 * train_size:
break
except ZeroDivisionError:
continue
mask[:x, :] = 0
train_gt[mask] = 0
test_gt[train_gt > 0] = 0
else:
raise ValueError("{} sampling is not implemented yet.".format(mode))
return train_gt, test_gt
def compute_imf_weights(ground_truth, n_classes=None, ignored_classes=[]):
""" Compute inverse median frequency weights for class balancing.
For each class i, it computes its frequency f_i, i.e the ratio between
the number of pixels from class i and the total number of pixels.
Then, it computes the median m of all frequencies. For each class the
associated weight is m/f_i.
Args:
ground_truth: the annotations array
n_classes: number of classes (optional, defaults to max(ground_truth))
ignored_classes: id of classes to ignore (optional)
Returns:
numpy array with the IMF coefficients
"""
n_classes = np.max(ground_truth) if n_classes is None else n_classes
weights = np.zeros(n_classes)
frequencies = np.zeros(n_classes)
for c in range(0, n_classes):
if c in ignored_classes:
continue
frequencies[c] = np.count_nonzero(ground_truth == c)
# Normalize the pixel counts to obtain frequencies
frequencies /= np.sum(frequencies)
# Obtain the median on non-zero frequencies
idx = np.nonzero(frequencies)
median = np.median(frequencies[idx])
weights[idx] = median / frequencies[idx]
weights[frequencies == 0] = 0.
return weights
def camel_to_snake(name):
s = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s).lower()
def seed_torch(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True