-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils_HSI.py
573 lines (477 loc) · 18.4 KB
/
utils_HSI.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
# -*- coding: utf-8 -*-
import random
import numpy as np
from sklearn.metrics import confusion_matrix
import sklearn.model_selection
import itertools
import spectral
import matplotlib.pyplot as plt
from scipy import io
import imageio
import os
import re
import torch
import numpy as np
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 seed_worker(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
np.random.seed(seed) # Numpy module.
random.seed(seed) # Python random module.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def open_file(dataset):
_, ext = os.path.splitext(dataset)
ext = ext.lower()
if ext == '.mat':
# Load Matlab array
# matlab v5.0 files using "io.loadmat"
# return io.loadmat(dataset)
# Solve bug: NotImplementedError: Please use HDF reader for matlab v7.3 files, e.g. h5py
import h5py
return h5py.File(dataset)
elif ext == '.tif' or ext == '.tiff':
# Load TIFF file
return imageio.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 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 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 sliding_window(image, 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 = image.shape[:2]
offset_w = (W - w) % step
offset_h = (H - h) % step
for x in range(0, W - w + offset_w, step):
if x + w > W:
x = W - w
for y in range(0, H - h + offset_h, step):
if y + h > H:
y = H - h
if with_data:
yield image[x:x + w, y:y + h], x, y, w, h
else:
yield x, y, w, h
def count_sliding_window(top, 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, 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] -1
# 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
FP = cm.sum(axis=0) - np.diag(cm)
FN = cm.sum(axis=1) - np.diag(cm)
TP = np.diag(cm)
TN = cm.sum() - (FP + FN + TP)
FP = FP.astype(float)
FN = FN.astype(float)
TP = TP.astype(float)
TN = TN.astype(float)
# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
results["TPR"] = TPR
# 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 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
results["prediction"] = prediction
results["label"] = target
return results
def show_results(results, vis, label_values=None, agregated=False):
text = ""
if agregated:
accuracies = [r["Accuracy"] for r in results]
kappas = [r["Kappa"] for r in results]
F1_scores = [r["F1_scores"] for r in results]
F1_scores_mean = np.mean(F1_scores, axis=0)
F1_scores_std = np.std(F1_scores, 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"]
kappa = results["Kappa"]
#label_values = label_values[1:]
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: {:.03f} +- {:.03f}\n".format(np.mean(accuracies),
np.std(accuracies)))
else:
text += "Accuracy : {:.03f}%\n".format(accuracy)
text += "---\n"
text += "F1_scores :\n"
if agregated:
for label, score, std in zip(label_values, F1_scores_mean,
F1_scores_std):
text += "\t{}: {:.03f} +- {:.03f}\n".format(label, score, std)
else:
for label, score in zip(label_values, F1scores):
text += "\t{}: {:.03f}\n".format(label, score)
text += "---\n"
if agregated:
text += ("Kappa: {:.03f} +- {:.03f}\n".format(np.mean(kappas),
np.std(kappas)))
else:
text += "Kappa: {:.03f}\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)
train_label = []
test_label = []
if mode == 'random':
if train_size == 1:
random.shuffle(X)
train_indices = [list(t) for t in zip(*X)]
[train_label.append(i) for i in gt[tuple(train_indices)]]
train_set = np.column_stack((train_indices[0],train_indices[1],train_label))
train_gt[tuple(train_indices)] = gt[tuple(train_indices)]
test_gt = []
test_set = []
else:
train_indices, test_indices = sklearn.model_selection.train_test_split(X, train_size=train_size, stratify=y, random_state=23)
train_indices = [list(t) for t in zip(*train_indices)]
test_indices = [list(t) for t in zip(*test_indices)]
train_gt[tuple(train_indices)] = gt[tuple(train_indices)]
test_gt[tuple(test_indices)] = gt[tuple(test_indices)]
[train_label.append(i) for i in gt[tuple(train_indices)]]
train_set = np.column_stack((train_indices[0],train_indices[1],train_label))
[test_label.append(i) for i in gt[tuple(test_indices)]]
test_set = np.column_stack((test_indices[0],test_indices[1],test_label))
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 / second_half_count
if ratio > 0.9 * train_size and ratio < 1.1 * 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, train_set, test_set
def sample_gt_fixed(gt, train_size_list, 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)
train_label = []
test_label = []
print("Sampling {} with train size = {}".format(mode, train_size_list))
train_indices, test_indices = [], []
train_label = []
test_label = []
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_list[c-1], random_state=23)
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]
[train_label.append(i) for i in gt[train_indices]]
train_set = np.column_stack(
(train_indices[0], train_indices[1], train_label))
[test_label.append(i) for i in gt[test_indices]]
test_set = np.column_stack((test_indices[0], test_indices[1], test_label))
return train_gt, test_gt, train_set, test_set
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()