-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
551 lines (458 loc) · 19.1 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
# -*- coding: utf-8 -*-
import random
import numpy as np
import seaborn as sns
from sklearn.metrics import confusion_matrix
import sklearn.model_selection
import itertools
import spectral
import visdom
import matplotlib.pyplot as plt
from scipy import io, misc
import os
import re
import torch
from collections import Counter
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) # 哪个标签对应什么样的颜色(RGB三个值)
Returns:
arr_3d: int 2D images of color-encoded labels in RGB format # RGB三通道图像
"""
arr_3d = np.zeros((arr_2d.shape[0], arr_2d.shape[1], 3), dtype=np.uint8) # 确定维度和编码方式,行列为arr_2d的行列,编码方式为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=""): # 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) # 从SpyFile对象或numpy数组中提取RGB数据以供显示。
rgb /= np.max(rgb) # 最大值化处理
rgb = np.asarray(255 * rgb, dtype='uint8') # 转为ndarray类型
# Display the RGB composite image 显示RGB合成图像
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 可以是list类型
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] # 检查维度是否相符,比如PaviaU的mat和gt都是(610, 340)
for label in np.unique(gt):
if label in ignored_labels:
continue
else:
indices = np.nonzero(gt == label) # 返回同一类标签的全部索引。(对gt每个元素判断是否为label,是的话为1否则为0,然后提取全部的非零元素的索引
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两个点构成一个网格
w, h = window_shape
W, H = img.shape[:2] # 获取img的前两个维度
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. # 通过n个元素对n个元素进行分组来浏览iterable
Args:
n: int, size of the groups
iterable: the iterable to Browse 迭代
Yields:
chunk of n elements from the iterable 可迭代的n个元素块
"""
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 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部分没看懂要干啥
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"]
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 (r,c)形式的位置的索引
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 / 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
# ???
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()
# ======================================================================================================================
# --------------------------------------------------- self added -------------------------------------------------------
# ======================================================================================================================
def show_Hyperparameter(input):
'''
:param input: args(命令行解析器) or hyperparameter(字典类型)
:return: None
'''
print('the settings are as following:')
if isinstance(input, dict): # hyperparameter 字典类型
for key in input:
print(key, ':', input[key])
else:
argsDict = input.__dict__ # args 命令行解析器
for key in argsDict:
print(key, ':', argsDict[key])
def samples_per_class(labels, label_map=None, print_rst=True):
'''
:param labels: labels of samples to count (encoded label)
:param label_map: (optional) map between raw label and encoded label
:param print_rst: bool, print count result, default = True
:return: count result in input encoded label
'''
if type(labels) is not np.ndarray: # tensor转ndarray
labels = labels.cpu().detach().numpy()
count_result = Counter(labels) # 对labels各label的数量进行计数, key为1,2,……
# print(count_result)
# 对字典按key排序, dict 2 list 2 dict
count_result = dict(sorted(count_result.items(), key=lambda x: x[0], reverse=False))
# print(count_result)
if print_rst:
if label_map is not None:
# 有 label_map
for (key, value) in count_result.items():
print(list(label_map.keys())[list(label_map.values()).index(key)], ': ', value)
else:
# 无 label_map
for (key, value) in count_result.items():
print(key, ': ', value)
print('\n')
return count_result