-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification.py
More file actions
executable file
·456 lines (384 loc) · 14.8 KB
/
classification.py
File metadata and controls
executable file
·456 lines (384 loc) · 14.8 KB
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
#!/usr/bin/env python3
# Copyright (c) 2019-2023
# Shell-ML Project
# Pedro Ribeiro Mendes Júnior <pedrormjunior@gmail.com> et al.
# Artificial Intelligence Lab. Recod.ai
# Institute of Computing (IC)
# University of Campinas (Unicamp)
# Campinas, São Paulo, Brazil
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Perform classification on the extracted features."""
from typing import List, Dict
import random
import argparse
import itertools as it
import numpy as np
import sklearn.ensemble
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
import parmapper as pm
import rocklib
import rocklibdata
import rockmeasures
import rocklongfiles
import rockresults
from rocklib import logger
import envconfig
import experiments.main as rockexperiment
SAVE_PREDICTS = True
SAVE_RESULTS = True
envconfig._makedir(envconfig.results_dir)
res = rockresults.Results(envconfig.results_filename)
predres = rockresults.Results(envconfig.predicts_filename)
class FeatureDimensionError(ValueError):
"""Exception pretty much for Softmax classifier."""
def softmax(vector):
"""Simply calculates the Softmax function."""
e = np.exp(vector)
ret = e / e.sum(axis=-1, keepdims=True)
return ret
class Softmax():
"""Softmax classifier."""
def __init__(self, classes, quant_classes):
self.classes = classes
self.quant_classes = quant_classes
self.labels_to_use = classes[:quant_classes]
def fit(self, features, _):
"""Fit the classifier. Fitting soft Softmax means doing nothing, just
asserting given data is as expected for a classifier.
"""
if features.shape[1] > len(self.classes):
raise FeatureDimensionError((features.shape,
self.quant_classes))
def predict_proba(self, features):
"""Predict probabilities."""
if features.shape[1] > len(self.classes):
raise FeatureDimensionError((features.shape,
self.quant_classes))
indexes = np.array([self.classes.index(x)
for x in self.labels_to_use])
ret = features[:, indexes]
ret = softmax(ret)
return ret
def get_classifier_model(classifier: str,
classes: List[int],
quant_classes: int,
):
"""`classes` and `quant_classes` are required only for Softmax classifier,
which needs know which portion of the feature vector on which to perform
the Softmax calculation.
"""
assert isinstance(classifier, str), type(classifier)
if classifier.endswith('nn') and classifier[:-2].isnumeric():
# Using the nearest neighbor classifier.
n_neighbors = int(classifier[:-2])
model = KNeighborsClassifier(
n_neighbors=n_neighbors,
algorithm='brute',
metric='euclidean',
)
elif classifier == 'svm':
model = SVC(
gamma='auto',
probability=True,
decision_function_shape='ovo',
)
elif classifier == 'rf':
model = sklearn.ensemble.RandomForestClassifier()
elif classifier == 'softmax':
model = Softmax(classes, quant_classes)
else:
raise Exception('Unknown classifier: {}'.format(classifier))
return model
def normalize_features(data, scale=None):
assert isinstance(data, np.ndarray), type(data)
assert len(data.shape) == 2, data.shape
assert scale is None or (isinstance(scale, tuple) and len(scale) == 2), \
scale
if scale:
datamin, diff = scale
else:
datamin = data.min(axis=0)
diff = data.max(axis=0) - datamin
ret = (data - datamin) / diff
if scale:
return ret
else:
return ret, (datamin, diff)
def classify(key,
quant_classes,
classes,
augment_classification,
classifier,
topk,
normalize,
features_file_part_train_lst,
features_file_part_val_lst,
features_file_part_test_lst,
override: bool,
experiment: Dict[str, str],
):
"""Given the parameters of a classification experiment, perform it and save
the results.
"""
assert isinstance(quant_classes, int), type(quant_classes)
assert quant_classes >= 2, quant_classes
assert isinstance(classes, list), type(classes)
assert all(isinstance(x, int) for x in classes), classes
assert isinstance(augment_classification, bool), \
type(augment_classification)
assert isinstance(classifier, str), type(classifier)
assert isinstance(topk, int), type(topk)
assert topk > 0, topk
assert isinstance(normalize, bool), type(normalize)
assert isinstance(override, bool), type(override)
logger.info('Results will be stored with KEY on '
f'{envconfig.results_filename}. KEY: {key}')
if key in res and key in predres:
if override:
logger.warning('OVERRIDE previous key.')
else:
logger.warning('This classification were performed before. '
'So, skipping a new one.')
return
# Preparing the dataset.
assert len(features_file_part_train_lst) == 1, \
('TODO: It needs the implementation for multi-scale training, however '
'it is deprecated, as a better approach seems to be implementing '
"multi-scale through PyTorch's data loader. See "
'https://pytorch.org/tutorials/beginner/finetuning_torchvision_models'
'_tutorial.html#load-data '
'for more details.')
assert len(features_file_part_val_lst) == 1, features_file_part_val_lst
assert len(features_file_part_test_lst) == 1, features_file_part_test_lst
shelve_extension = '.db'
assert features_file_part_train_lst[0].endswith(shelve_extension)
assert features_file_part_val_lst[0].endswith(shelve_extension)
assert features_file_part_test_lst[0].endswith(shelve_extension)
map_file = {
'train': rocklongfiles.shortfile(
features_file_part_train_lst[0][:-len(shelve_extension)]) + shelve_extension,
'val': rocklongfiles.shortfile(
features_file_part_val_lst[0][:-len(shelve_extension)]) + shelve_extension,
'test': rocklongfiles.shortfile(
features_file_part_test_lst[0][:-len(shelve_extension)]) + shelve_extension,
}
del shelve_extension
image_datasets = {
part: rocklib.RockDataset(
filename=map_file[part],
classes=classes,
datatype='features',
)
for part in map_file
}
del map_file
features = {
part: [image_datasets[part][i]
for i in range(len(image_datasets[part]))]
for part in image_datasets
}
def get_data_as_numpy(lst):
def get_info(features_metainfo):
features, label, metainfo = features_metainfo
filepath = metainfo['path']
# label = rocklib.filename_to_label(filepath)
well = rocklibdata.well_name_to_num[
rocklib.filename_to_well(filepath)]
depth = rocklib.filename_to_depth(filepath)
dirtype = rocklib.filename_to_dirtype(filepath)
return features, label, well, depth, dirtype
return {
key: data
for key, data in zip(
['features', 'label', 'well', 'depth', 'dirtype'],
map(np.array, zip(*map(get_info, lst))),
)
}
features_info = {
part: get_data_as_numpy(features[part])
for part in features
}
# Now, limiting the experiment to the first `quant_classes`, i.e., use only
# data (feature vectors, etc.) referring to those classes.
for part in features_info:
indexes = features_info[part]['label'] < quant_classes
for dictkey in features_info[part]:
features_info[part][dictkey] = \
features_info[part][dictkey][indexes]
model = get_classifier_model(classifier, classes, quant_classes)
logger.debug(f'Model: {model}')
if experiment['normalize']:
logger.debug('Normalizing features.')
features_info['train']['features'], scale = normalize_features(
features_info['train']['features']
)
features_info['val']['features'] = normalize_features(
features_info['val']['features'],
scale,
)
features_info['test']['features'] = normalize_features(
features_info['test']['features'],
scale,
)
logger.debug('Fitting classifier.')
try:
model.fit(
features_info['train']['features'],
features_info['train']['label'],
)
except FeatureDimensionError:
# Occurs for softmax when the dimensionality of the feature
# vectors is bigger than the number of classes.
pass
logger.debug('Predicting for train, val, and test.')
pred_probs = {
part: model.predict_proba(features_info[part]['features'])
for part in features_info
}
logger.debug('Predicting done.')
logger.debug('Performing top-k analysis')
labels_to_use = np.array(classes[:quant_classes])
def top_k(probs_matrix):
def top_k_per_vector(probs):
assert topk >= 1, topk
ret = [x[1] for x in sorted(zip(probs, range(len(probs))),
reverse=True)][:topk]
ret = labels_to_use[ret]
return ret
return np.array([top_k_per_vector(probs) for probs in probs_matrix])
pred_top = {
part: top_k(pred_probs[part])
for part in pred_probs
}
# At this point, predicted labels are the original labels instead of the
# index-based labels (i.e., 0, ..., N-1). Then should also convert the
# ground-truth labels to the original labels.
for part in features_info:
features_info[part]['label'] = \
labels_to_use[features_info[part]['label']]
# The data on the internal `zip` will be used to group feature vectors from
# the same image. The combination of those four keys is enough information
# to separate each image.
logger.debug('Grouping feature vectors for fusion.')
data = {
part: list(zip(zip(features_info[part]['well'],
features_info[part]['depth'],
features_info[part]['dirtype'],
features_info[part]['label']),
pred_top[part]))
for part in pred_top
}
def group_features(data):
groups = []
uniquekeys = []
data = [(x[0], list(x[1])) for x in data]
data_sorted = sorted(data)
data_sorted = [(x[0], np.array(x[1])) for x in data_sorted]
for key, group in it.groupby(data_sorted, lambda x: x[0]):
groups.append(np.array([x[1] for x in group]))
uniquekeys.append(key)
return {'groups': groups,
'uniquekeys': uniquekeys}
data_groups = {
part: group_features(data[part])
for part in data
}
def max_vote(group):
assert len(group) >= 1, len(group)
group = group.flatten()
counts = sorted([((group == x).sum(), x) for x in set(group)],
reverse=True)
ret = [x[1] for x in counts[:topk]]
return ret
logger.debug('Performing max-voting fusion.')
votes = {
part: {
'votes': np.array([
max_vote(group)
for group in data_groups[part]['groups']
]),
'labels': np.array([
label
for _, _, _, label in data_groups[part]['uniquekeys']
]),
}
for part in data_groups
}
for part in votes:
assert len(votes[part]['votes']) == len(votes[part]['labels'])
for part in pred_top:
assert len(pred_top[part]) == len(features_info[part]['label'])
preds = {
part: {
'img': {'true': votes[part]['labels'],
'pred': votes[part]['votes']},
'pat': {'true': features_info[part]['label'],
'pred': pred_top[part]},
}
for part in votes
}
results_dict = {
part: {
metric: {
imgpat: func(preds[part][imgpat]['true'],
preds[part][imgpat]['pred'])
for imgpat in preds[part]
}
for metric, func in zip(['acc', 'na'],
[rockmeasures.acc, rockmeasures.na])
}
for part in preds
}
if SAVE_PREDICTS:
logger.debug('Saving predictions.')
predres[key] = preds
else:
logger.warning('Predictions are not being saved.')
if SAVE_RESULTS:
logger.debug('Saving results.')
res[key] = results_dict
else:
logger.warning('Results are not being saved.')
logger.info(f'Results for {key}: {results_dict}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='[rocklib] classification.py')
parser.add_argument(
'--partition',
type=int,
help='''Partition to run classification for. Should be an integer.''',
default=argparse.SUPPRESS,
)
parser.add_argument(
'--model_name',
type=str,
help='''Name of the network model used for feature extraction. Should
be a string.''',
default=argparse.SUPPRESS,
)
args = parser.parse_args()
random.seed(0.8640890499471714)
Experiment = rockexperiment.Experiment
Experiment.experiment = 'classification'
Experiment.update(args)
Experiment.shuffle()
Experiment(classify, mapf=pm.parmap, send_dict=True)
# Experiment(classify, mapf=map, send_dict=True)
logger.info('READY')