-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_split.py
144 lines (119 loc) · 5.26 KB
/
data_split.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
# -*- 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'
File with data splitting for stratifying the image during training.
"""
import numpy as np
import matplotlib.pyplot as plt
from utils import open_file
# ----------------------------------------------------------------------------
def get_fixed_sets(run, folder_name, dataset_name, mode='both'):
"""
Load fixed training and test set from the file.
Arguments:
run - number of a current run
folder_name - folder with files
dataset_name - name of a loaded dataset
mode - 'both': train and test set,
'train': only training set
'test': only test set
"""
if mode in ('both', 'train'):
train_gt = open_file(folder_name + dataset_name + '/train_gt_' + str(run) + '.npz')
if mode == 'train':
return train_gt
if mode in ('both', 'test'):
test_gt = open_file(folder_name + dataset_name + '/test_gt_' + str(run) + '.npz')
if mode == 'test':
return test_gt
else:
return train_gt, test_gt
else:
print('Wrong name of the mode parameter!')
pass
# ----------------------------------------------------------------------------
def select_subset(gt, size=500, min_n_class=5):
"""
stratified selection of a random subset from image
parameters:
gt: labels
size: if size>1: no. examples/class, if size<1: ratio of examples/class
min_n_class: minimum number of examples/class (if exists)
returns:
training set, test set
"""
all_gt_classes = np.unique(gt)
trash_classes = np.array([0, 255])
# all classes which are in ground truth
classes = np.setdiff1d(all_gt_classes, trash_classes)
indices_train, indices_test = [], []
train_set = np.zeros_like(gt)
test_set = np.zeros_like(gt)
# select samples for each class
for current_class in classes:
indices = np.nonzero(gt == current_class)
X = list(zip(*indices))
np.random.shuffle(X)
# number of elements of current_class to seek
to_seek = int(size) if size > 1 else int(len(indices[0]) * size)
if to_seek < min_n_class:
to_seek = np.min([min_n_class,
np.count_nonzero(gt == current_class)])
train_sample = X[0:to_seek]
indices_train = indices_train + train_sample
test_sample = X[to_seek:]
indices_test = indices_test + test_sample
# prepare tables with training and test set
for element in indices_train:
train_set[element] = gt[element]
for element in indices_test:
test_set[element] = gt[element]
return train_set, test_set
# ----------------------------------------------------------------------------
def check_split_correctness(validation_gt, train_gt, test_gt, no_of_classes=6, mode='general'):
"""
Function for checking the correctness of the set split
mode - 'general': division into the training and the test set inside DeepHyperX
'fixed': loading prepared files
"""
if mode != 'fixed':
# first level: does sum of train and test set equal to whole gt?
check_gt = np.array(train_gt + test_gt)
diff = validation_gt - check_gt
assert((diff == 0).all())
assert((check_gt == validation_gt).all())
classes = np.arange(1, no_of_classes + 1, 1)
for no in classes:
test_class = np.count_nonzero(train_gt == no)
check_class = np.count_nonzero(test_gt == no)
print('In train set are {} samples of {} class.'.format(test_class, no))
print('In test set are {} samples of {} class.'.format(check_class, no))
no_of_border = np.count_nonzero(train_gt == 255)
no_of_classes_train = np.count_nonzero(train_gt) - no_of_border
print('Length of training set: {}'.format(no_of_classes_train))
print('Length of test set: {}'.format(np.count_nonzero(test_gt)))
print('Ratio of non-zero elements in training set to the non-zero elements'
' in the whole set: {}'.format(
no_of_classes_train / (no_of_classes_train + np.count_nonzero(test_gt))
))
# ----------------------------------------------------------------------------
if __name__ == "__main__":
pass