-
Notifications
You must be signed in to change notification settings - Fork 4
/
hyperblood_classification_svm.py
446 lines (362 loc) · 15.3 KB
/
hyperblood_classification_svm.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
# -*- 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
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
************************************************************************
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'
SVM experiments
"""
import sys
import numpy as np
import matplotlib.pyplot as plt
import time
from data_library import get_data,get_anno
from sklearn.model_selection import StratifiedKFold
from two_stage_svm import TwoStageSVM
from sklearn.metrics._classification import accuracy_score
from sklearn.preprocessing import scale
from os.path import isfile
from sklearn.metrics import cohen_kappa_score
from sklearn.decomposition import PCA
from data_loader import blood_loader
from data_paths import PATH_DATA,PATH_ANNO
DS = ['E_1', 'E_7', 'E_21', 'F_1', 'F_1a', 'F_2', 'F_2k', 'F_7', 'F_21']
DS_I = [('F_1', 'E_1'), ('F_1a', 'E_1'), ('F_2', 'E_7'), ('F_2k', 'E_7')
,('F_7', 'E_7'), ('F_21', 'E_21'), ('F_2', 'F_2k'), ('F_2k', 'F_2')]
#************************** UTILITIES *******************************************
def calculate_average_accuracy(ground_truth,
prediction):
'''
Calculate accuracy for each class in ground truth
(without background) and average accuracy
Arguments:
ground_truth: map of ground truth of a given image
prediction: map of prediction of an algorithm
Returns:
classes_accuracies: a list of accuracies for each class
without background
average_accuracy: average accuracy (mean from consecutive classes)
'''
# all classes without background
classes = np.unique(ground_truth)
classes = np.delete(classes, np.where(classes == 0))
classes_accuracies = []
# for each class
for no in classes:
class_occurences = np.argwhere(ground_truth == no)
# calculate total number of pixels from a given class
total_occurences = class_occurences.shape[0]
correct_occurences = 0
for occurence in class_occurences:
if(prediction[tuple(occurence)] == no):
correct_occurences += 1
class_accuracy = (correct_occurences / total_occurences)
classes_accuracies.append(class_accuracy)
# average accuracy
average_accuracy = np.mean(classes_accuracies)
return average_accuracy
def calculate_kappa_coefficient(ground_truth,
prediction):
'''
Kamil's function
Arguments:
ground_truth: map of ground truth of a given image (2D)
prediction: map of prediction of an algorithm (2D)
Returns:
kappa: value of Cohen's kappa coefficient
'''
# Prepare a proper form of results
ground_truth = ground_truth.reshape(-1)
prediction = prediction.reshape(-1)
# Remove background indices
background = np.argwhere(ground_truth == 0)
ground_truth = np.delete(ground_truth, background)
prediction = np.delete(prediction, background)
# Calculate Cohen's kappa value
kappa = cohen_kappa_score(ground_truth, prediction)
return kappa
def spectra_normalisation(X,mode='median'):
"""
performs spectra normalisation, dividing each spectrum by their median/mean
parameters:
X: 2D data array
mode: per-spectra normalisation[median,mean,max]
returns:
2D array copy with normalised spectra
"""
X2 = X.copy()
norms = None
if mode == 'median':
norms = np.median(X2,axis=1)
elif mode == 'mean':
norms = np.mean(X2,axis=1)
elif mode == 'max':
norms = np.max(X2,axis=1)
else:
raise NotImplementedError
for i in range(len(norms)):
X2[i]=X2[i]/norms[i]
return X2
def preprocess(X_in,preprocessing='none'):
"""
perform spectra normalisation and applies selected preprocessing e.g. scaling
parameters:
X_in: 2D data array
preprocessing: type of prepoecessing ['none','scale']
returns:
2D array copy with preprocessed and normalised spectra
"""
X=X_in.copy()
X = spectra_normalisation(X)
if preprocessing == 'scale':
X=scale(X)
elif preprocessing == 'none':
pass
else:
raise NotImplementedError
return X
def load_ds(name='F(1)',exp_index=0):
"""
loads a dataset image with annotation
parameters:
name: image name
exp_index: no. experiment (e.g. 0-9)
returns:
data,train_gt,test_gt,anno
"""
data,_ = get_data(name)
anno = get_anno(name)
train_gt = np.load("{}{}/train_gt_{}.npz".format(PATH_ANNO,name,exp_index))['gt']
test_gt = np.load("{}{}/test_gt_{}.npz".format(PATH_ANNO,name,exp_index))['gt']
return data,train_gt,test_gt,anno
#************************** EXPERIMENTS *******************************************
def experiment_transductive(name='E(1)',exp_index=0,preprocessing='none'):
"""
performs transductive experiment for frame/target pair
parameters:
name: image name
exp_index: no. experiment (e.g. 0-9)
preprocessing: preprocessing mode
returns:
True/False
"""
start = time.time()
outfile = 'results/{}_{}_{}.npz'.format(name,exp_index,preprocessing)
data,train_gt,test_gt,_ = load_ds(name,exp_index)
#preprocessing
X = data.reshape(-1,data.shape[2])
X=preprocess(X,preprocessing=preprocessing)
data = X.reshape(data.shape)
X_train = data[train_gt>0]
y_train = train_gt[train_gt>0]
time_prepare = time.time()-start
start = time.time()
svm=TwoStageSVM()
svm.fit(X_train,y_train)
time_train = time.time()-start
X_test = data[test_gt>0]
y_test = test_gt[test_gt>0]
start = time.time()
y_pred = svm.predict(X_test)
time_test = time.time()-start
y_pred_all = svm.predict(X)
acc = accuracy_score(y_test,y_pred)
np.savez_compressed(outfile,y_test=y_test,y_pred=y_pred,sh=data.shape,best_params=[svm.params_['C'],svm.params_['gamma']],acc=[acc],cls_score=[svm.score_],y_pred_all=y_pred_all,time_train=time_train,time_test=time_test,time_prepare=time_prepare)
print (outfile,acc,time_train,time_test)
def experiment_inductive(name_train='hyperblood_frame_day_1_afternoon',name_test='hyperblood_comparison_day_1',exp_index=0,preprocessing='none'):
"""
performs inductive experiment for frame/target pair
parameters:
name_train: image name (for training)
name_test: image name (for testing)
exp_index: no. experiment (e.g. 0-9)
preprocessing: preprocessing mode
"""
print (name_train,name_test)
start = time.time()
outfile = 'results_i/{}_{}_{}_{}.npz'.format(name_train,name_test,exp_index,preprocessing)
data_train,train_gt,_,_ = load_ds(name_train,exp_index)
data_test,_,test_gt,_ = load_ds(name_test,exp_index)
_,anno_test, _,_,_ = blood_loader(PATH_DATA, name_test)
test_gt = np.asarray(test_gt,dtype=np.int32)
anno_test = np.asarray(anno_test,dtype=np.int32)
assert np.sum(np.unique(anno_test)-np.unique(test_gt))==0
#preprocessing
X = data_train.reshape(-1,data_train.shape[2])
X=preprocess(X,preprocessing=preprocessing)
data_train = X.reshape(data_train.shape)
X = data_test.reshape(-1,data_test.shape[2])
X=preprocess(X,preprocessing=preprocessing)
data_test = X.reshape(data_test.shape)
X_train = data_train[train_gt>0]
y_train = train_gt[train_gt>0]
time_prepare = time.time()-start
start = time.time()
svm=TwoStageSVM()
svm.fit(X_train,y_train)
time_train = time.time()-start
#second version of results
X_test_gt = data_test.copy()[anno_test>0]
y_test_gt = anno_test.copy()[anno_test>0]
X_test = data_test[test_gt>0]
y_test = test_gt[test_gt>0]
y_pred = svm.predict(X_test)
y_pred_all = svm.predict(X)
start = time.time()
y_pred_gt = svm.predict(X_test_gt)
time_test = time.time()-start
acc = accuracy_score(y_test,y_pred)
acc_gt = accuracy_score(y_test_gt,y_pred_gt)
np.savez_compressed(outfile,y_test=y_test,y_pred=y_pred,sh_train=data_train.shape,sh_test=data_test.shape,best_params=[svm.params_['C'],svm.params_['gamma']],acc=[acc],cls_score=[svm.score_],y_pred_all=y_pred_all,y_test_gt=y_test_gt,y_pred_gt=y_pred_gt,acc_gt=[acc_gt],time_train=time_train,time_test=time_test,time_prepare=time_prepare)
print (outfile,acc,acc_gt,time_train,time_test)
#************************** RUN & SEE *******************************************
def see_all_tra(name='E(1)',preprocessing='none',stat='acc'):
"""
presents transductive experiment result
parameters:
name: image name
preprocessing: preprocessing mode
stat: performance measure ['acc','aa','kappa']
"""
acc = []
aa = []
kappa=[]
t_train=[]
t_test=[]
for i in range(10):
outfile = 'results/{}_{}_{}.npz'.format(name,i,preprocessing)
res = np.load(outfile)
acc.append(res['acc'][0])
aa.append(calculate_average_accuracy(res['y_test'],res['y_pred']))
kappa.append(calculate_kappa_coefficient(res['y_test'],res['y_pred']))
t_train.append(res['time_train']+res['time_prepare'])
t_test.append(res['time_test']+res['time_prepare'])
t_train=np.asarray(t_train)
t_test=np.asarray(t_test)
res=None
assert stat in ['acc','aa','kappa']
res = acc
if stat=='aa':
res=aa
elif stat=='kappa':
res=kappa
if stat!='kappa':
print ("{}:{:0.2f}({:0.2f}) , time_train:${:0.1f}\pm{:0.1f}$, time_test:${:0.1f}\pm{:0.1f}$".format(name,np.mean(res)*100
,np.std(res)*100
,np.mean(t_train)
,np.std(t_train)
,np.mean(t_test)
,np.std(t_test)))
else:
print ("{}:{:0.2f}({:0.2f})".format(name,np.mean(res),np.std(res)))
def see_all_ind(name_train='F(1)',name_test='E(1)',preprocessing='none',stat='acc'):
"""
presents inductive experiment result
parameters:
name: image name
preprocessing: preprocessing mode
stat: performance measure ['acc','aa','kappa']
"""
acc = []
acc_gt = []
aa = []
kappa=[]
t_train=[]
t_test=[]
for i in range(10):
outfile = 'results_i/{}_{}_{}_{}.npz'.format(name_train,name_test,i,preprocessing)
try:
res = np.load(outfile)
acc.append(res['acc'][0])
acc_gt.append(res['acc_gt'][0])
aa.append(calculate_average_accuracy(res['y_test_gt'],res['y_pred_gt']))
kappa.append(calculate_kappa_coefficient(res['y_test_gt'],res['y_pred_gt']))
t_train.append(res['time_train']+res['time_prepare'])
t_test.append(res['time_test']+res['time_prepare'])
except:
pass
#print ("no", outfile)
t_train=np.asarray(t_train)
t_test=np.asarray(t_test)
if len(acc)==0:
print ("{}->{}: not yet computed".format(name_train,name_test))
return 1
res=None
assert stat in ['acc','aa','kappa']
res = acc_gt
if stat=='aa':
res=aa
elif stat=='kappa':
res=kappa
#print ("{}->{}: {:0.2f}({:0.2f}), all: {:0.2f}({:0.2f})".format(name_train,name_test,np.mean(acc)*100,np.std(acc)*100,np.mean(acc_gt)*100,np.std(acc_gt)*100))
if stat=='kappa':
print ("{}->{}: {:0.2f}({:0.2f})".format(name_train,name_test,np.mean(res),np.std(res)))
else:
print ("{}->{}: {:0.2f}({:0.2f}) , time_train:${:0.1f}\pm{:0.1f}$, time_test:${:0.1f}\pm{:0.1f}$".format(name_train
,name_test
,np.mean(res)*100
,np.std(res)*100
,np.mean(t_train)
,np.std(t_train)
,np.mean(t_test)
,np.std(t_test)))
return 0
def fire_inductive(exp_index=0,preprocessing='none'):
"""
fires inductive experiment for all images
parameters:
exp_index: no. experiment (e.g. 0-9)
preprocessing: preprocessing mode
"""
for n in DS_I:
experiment_inductive(name_train=n[0],name_test=n[1],exp_index=exp_index,preprocessing=preprocessing)
def fire_transductive(exp_index=0,preprocessing='none'):
"""
fires all transductive experiments
parameters:
exp_index: no. experiment (e.g. 0-9)
preprocessing: preprocessing mode
"""
for n in DS:
experiment_transductive(name=n,exp_index=exp_index,preprocessing=preprocessing)
def results_ind(preprocessing='none',stat='acc'):
"""
printes results of inductive experiments
parameters:
preprocessing: preprocessing mode
stat: performance measure ['acc','aa','kappa']
"""
for n in DS_I:
see_all_ind(name_train=n[0],name_test=n[1],preprocessing=preprocessing,stat=stat)
def results_tra(preprocessing='none',stat='acc'):
"""
printes results of transductive expetimens experiments
parameters:
preprocessing: preprocessing mode
stat: performance measure ['acc','aa','kappa']
"""
for n in DS:
see_all_tra(name=n,preprocessing=preprocessing,stat=stat)
if __name__ == '__main__':
preprocessing='none'
if True:
for exp_index in range(10):
fire_inductive(exp_index,preprocessing=preprocessing)
fire_transductive(exp_index,preprocessing=preprocessing)
results_ind(preprocessing=preprocessing,stat='acc')
results_tra(preprocessing=preprocessing,stat='acc')