-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
407 lines (340 loc) · 13.9 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
# -*- coding: utf-8 -*-
"""
************************************************************************
Copyright 2020 Institute of Theoretical and Applied Informatics,
Polish Academy of Sciences (ITAI PAS) https://www.iitis.pl
author: K. Książek, P.Głomb, M. Romaszewski
The code in this file is based on the code from library: https://github.com/nshaud/DeepHyperX
for paper
N. Audebert, B. Le Saux and S. Lefevre, "Deep Learning for Classification of Hyperspectral Data: A Comparative Review,"
in IEEE Geoscience and Remote Sensing Magazine, vol. 7, no. 2, pp. 159-173, June 2019.
The code is used for RESEARCH AND NON COMMERCIAL PURPOSES under the licence:
https://github.com/nshaud/DeepHyperX/blob/master/License
Therefore, the original authors license is used for the code in this file.
************************************************************************
Code for experiments in the paper by
K. Książek, M. Romaszewski, P. Głomb, B. Grabowski, M. Cholewa
`Blood Stains Classification with Hyperspectral
Imaging and Deep Neural Networks'
Various functions using during calculations.
"""
import os
import re
import torch
import random
import numpy as np
import sklearn.model_selection
import seaborn as sns
import itertools
import spectral
import matplotlib.pyplot as plt
from scipy import io, misc
from sklearn.metrics import confusion_matrix
# ----------------------------------------------------------------------------
def get_device(ordinal):
"""
Returns the devide: GPU/CPU
"""
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):
"""
smart file for different data formats
"""
_, 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).load()
return img
elif ext == '.npz':
try:
labels = np.load(dataset)['gt']
return labels
except KeyError:
img = np.load(dataset)['data']
return img
else:
raise ValueError("Unknown file format: {}".format(ext))
# ----------------------------------------------------------------------------
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 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]
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)):
denominator = np.sum(cm[i, :]) + np.sum(cm[:, i])
if denominator == 0:
F1 = 0.
else:
F1 = 2. * cm[i, i] / denominator
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, model_name, dataset_name, path, preprocessing, label_values=None, training_image=None, agregated=False):
text = ""
# create folders if don't exist
os.makedirs("./" + path + "/Confusion_matrix/", exist_ok=True)
os.makedirs("./" + path + "/Accuracy/", exist_ok=True)
# stream of results into files
confusion_filename = "confusion_matrix_" + model_name + "_" + "train_" + training_image + "_pre_" + preprocessing + "_test_" + dataset_name
confusion_stream = open("./" + path + "/Confusion_matrix/" + confusion_filename + ".csv", "a")
accuracy_filename = "accuracy_" + model_name + "_" + "train_" + training_image + "_pre_" + preprocessing + "_test_" + dataset_name
accuracy_stream = open("./" + path + "/Accuracy/" + accuracy_filename + ".csv", "a")
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"]
accuracy_stream.write(str(accuracy) + "\n")
F1scores = results["F1 scores"]
kappa = results["Kappa"]
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)
print(text)
confusion_stream.write(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[tuple(train_indices)] = gt[tuple(train_indices)]
test_gt[tuple(test_indices)] = gt[tuple(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()
# ----------------------------------------------------------------------------
if __name__ == "__main__":
pass