|
1 |
| -#!/usr/bin/env python |
2 |
| - |
3 |
| -import gc |
4 |
| - |
5 |
| -import tensorflow as tf |
6 |
| -from tensorflow.keras.models import load_model |
7 |
| - |
8 |
| -from sklearn.metrics import confusion_matrix, roc_curve, auc, average_precision_score |
9 |
| - |
10 |
| -import numpy as np |
11 |
| - |
12 |
| -tf.compat.v1.disable_eager_execution() |
13 |
| - |
14 |
| -config = tf.compat.v1.ConfigProto() |
15 |
| -config.gpu_options.allow_growth = True |
16 |
| -tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config)) |
17 |
| - |
18 |
| - |
19 |
| -fold_infos = { |
20 |
| - 'fold1': [2], |
21 |
| - 'fold2': [1], |
22 |
| - 'fold3': [0], |
23 |
| - 'all': [2, 1, 0] |
24 |
| -} |
25 |
| - |
26 |
| -model_metrics = [tf.keras.metrics.BinaryAccuracy()] |
27 |
| - |
28 |
| - |
29 |
| -def estimate_metrics(testing_fold, model_instance): |
30 |
| - threshold = 0.5 |
31 |
| - p = model_instance.predict(x=testing_fold['x'], verbose=0) |
32 |
| - p = p >= threshold |
33 |
| - matrix = confusion_matrix(testing_fold['y'], p) |
34 |
| - ap = average_precision_score(testing_fold['y'], p) |
35 |
| - fpr, tpr, thresholds = roc_curve(testing_fold['y'], p) |
36 |
| - roc = auc(fpr, tpr) |
37 |
| - return matrix, ap, roc |
38 |
| - |
39 |
| - |
40 |
| -def get_metrics_from_matrix(matrix): |
41 |
| - tp, tn, fp, fn = matrix[1, 1], matrix[0, 0], matrix[0, 1], matrix[1, 0] |
42 |
| - precision = tp / (tp + fp) |
43 |
| - recall = tp / (tp + fn) |
44 |
| - f1score = 2. * (precision * recall) / (precision + recall) |
45 |
| - return precision, recall, f1score |
46 |
| - |
47 |
| - |
48 |
| -def threefold_evaluation(dataset, model_paths_fold1, model_paths_fold2, model_paths_fold3, input_size): |
49 |
| - folds = ['fold1', 'fold2', 'fold3'] |
50 |
| - aps = [] |
51 |
| - rocs = [] |
52 |
| - recalls = [] |
53 |
| - precisions = [] |
54 |
| - f1scores = [] |
55 |
| - models = [] |
56 |
| - |
57 |
| - for fold_to_eval_on, model_paths in zip(folds, [model_paths_fold1, model_paths_fold2, model_paths_fold3]): |
58 |
| - if len(model_paths_fold1) > 1: |
59 |
| - models = [load_model(model_path, compile=False) for model_path in model_paths] |
60 |
| - img_input_l = tf.keras.Input(shape=input_size, name='img_input_L') |
61 |
| - img_input_r = tf.keras.Input(shape=input_size, name='img_input_R') |
62 |
| - tensors = [model([img_input_r, img_input_l]) for model in models] |
63 |
| - output_layer = tf.keras.layers.average(tensors) |
64 |
| - model_instance = tf.keras.Model(inputs=[img_input_r, img_input_l], outputs=output_layer) |
65 |
| - else: |
66 |
| - model_instance = load_model(model_paths[0]) |
67 |
| - model_instance.compile() |
68 |
| - |
69 |
| - testing_fold = dataset.get_training_data(fold_infos[fold_to_eval_on]) # get the testing fold subjects |
70 |
| - |
71 |
| - matrix, ap, roc = estimate_metrics(testing_fold, model_instance) |
72 |
| - aps.append(ap) |
73 |
| - rocs.append(roc) |
74 |
| - precision, recall, f1score = get_metrics_from_matrix(matrix) |
75 |
| - recalls.append(recall) |
76 |
| - precisions.append(precision) |
77 |
| - f1scores.append(f1score) |
78 |
| - |
79 |
| - del model_instance, testing_fold |
80 |
| - # noinspection PyUnusedLocal |
81 |
| - for model in models: |
82 |
| - del model |
83 |
| - gc.collect() |
84 |
| - |
85 |
| - evaluation = {'AP': {}, 'ROC': {}, 'precision': {}, 'recall': {}, 'f1score': {}} |
86 |
| - evaluation['AP']['avg'] = np.mean(np.array(aps)) |
87 |
| - evaluation['AP']['std'] = np.std(np.array(aps)) |
88 |
| - evaluation['ROC']['avg'] = np.mean(np.array(rocs)) |
89 |
| - evaluation['ROC']['std'] = np.std(np.array(rocs)) |
90 |
| - evaluation['precision']['avg'] = np.mean(np.array(precisions)) |
91 |
| - evaluation['precision']['std'] = np.std(np.array(precisions)) |
92 |
| - evaluation['recall']['avg'] = np.mean(np.array(recalls)) |
93 |
| - evaluation['recall']['std'] = np.std(np.array(recalls)) |
94 |
| - evaluation['f1score']['avg'] = np.mean(np.array(f1scores)) |
95 |
| - evaluation['f1score']['std'] = np.std(np.array(f1scores)) |
96 |
| - return evaluation |
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import gc |
| 4 | + |
| 5 | +import tensorflow as tf |
| 6 | +from tensorflow.keras.models import load_model |
| 7 | + |
| 8 | +from sklearn.metrics import confusion_matrix, roc_curve, auc, average_precision_score |
| 9 | + |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +tf.compat.v1.disable_eager_execution() |
| 13 | + |
| 14 | +config = tf.compat.v1.ConfigProto() |
| 15 | +config.gpu_options.allow_growth = True |
| 16 | +tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config)) |
| 17 | + |
| 18 | + |
| 19 | +fold_infos = { |
| 20 | + 'fold1': [2], |
| 21 | + 'fold2': [1], |
| 22 | + 'fold3': [0], |
| 23 | + 'all': [2, 1, 0] |
| 24 | +} |
| 25 | + |
| 26 | +model_metrics = [tf.keras.metrics.BinaryAccuracy()] |
| 27 | + |
| 28 | + |
| 29 | +def estimate_metrics(testing_fold, model_instance): |
| 30 | + threshold = 0.5 |
| 31 | + p = model_instance.predict(x=testing_fold['x'], verbose=0) |
| 32 | + p = p >= threshold |
| 33 | + matrix = confusion_matrix(testing_fold['y'], p) |
| 34 | + ap = average_precision_score(testing_fold['y'], p) |
| 35 | + fpr, tpr, thresholds = roc_curve(testing_fold['y'], p) |
| 36 | + roc = auc(fpr, tpr) |
| 37 | + return matrix, ap, roc |
| 38 | + |
| 39 | + |
| 40 | +def get_metrics_from_matrix(matrix): |
| 41 | + tp, tn, fp, fn = matrix[1, 1], matrix[0, 0], matrix[0, 1], matrix[1, 0] |
| 42 | + precision = tp / (tp + fp) |
| 43 | + recall = tp / (tp + fn) |
| 44 | + f1score = 2. * (precision * recall) / (precision + recall) |
| 45 | + return precision, recall, f1score |
| 46 | + |
| 47 | + |
| 48 | +def threefold_evaluation(dataset, model_paths_fold1, model_paths_fold2, model_paths_fold3, input_size): |
| 49 | + folds = ['fold1', 'fold2', 'fold3'] |
| 50 | + aps = [] |
| 51 | + rocs = [] |
| 52 | + recalls = [] |
| 53 | + precisions = [] |
| 54 | + f1scores = [] |
| 55 | + models = [] |
| 56 | + |
| 57 | + for fold_to_eval_on, model_paths in zip(folds, [model_paths_fold1, model_paths_fold2, model_paths_fold3]): |
| 58 | + if len(model_paths_fold1) > 1: |
| 59 | + models = [load_model(model_path, compile=False) for model_path in model_paths] |
| 60 | + img_input_l = tf.keras.Input(shape=input_size, name='img_input_L') |
| 61 | + img_input_r = tf.keras.Input(shape=input_size, name='img_input_R') |
| 62 | + tensors = [model([img_input_r, img_input_l]) for model in models] |
| 63 | + output_layer = tf.keras.layers.average(tensors) |
| 64 | + model_instance = tf.keras.Model(inputs=[img_input_r, img_input_l], outputs=output_layer) |
| 65 | + else: |
| 66 | + model_instance = load_model(model_paths[0]) |
| 67 | + model_instance.compile() |
| 68 | + |
| 69 | + testing_fold = dataset.get_training_data(fold_infos[fold_to_eval_on]) # get the testing fold subjects |
| 70 | + |
| 71 | + matrix, ap, roc = estimate_metrics(testing_fold, model_instance) |
| 72 | + aps.append(ap) |
| 73 | + rocs.append(roc) |
| 74 | + precision, recall, f1score = get_metrics_from_matrix(matrix) |
| 75 | + recalls.append(recall) |
| 76 | + precisions.append(precision) |
| 77 | + f1scores.append(f1score) |
| 78 | + |
| 79 | + del model_instance, testing_fold |
| 80 | + # noinspection PyUnusedLocal |
| 81 | + for model in models: |
| 82 | + del model |
| 83 | + gc.collect() |
| 84 | + |
| 85 | + evaluation = {'AP': {}, 'ROC': {}, 'precision': {}, 'recall': {}, 'f1score': {}} |
| 86 | + evaluation['AP']['avg'] = np.mean(np.array(aps)) |
| 87 | + evaluation['AP']['std'] = np.std(np.array(aps)) |
| 88 | + evaluation['ROC']['avg'] = np.mean(np.array(rocs)) |
| 89 | + evaluation['ROC']['std'] = np.std(np.array(rocs)) |
| 90 | + evaluation['precision']['avg'] = np.mean(np.array(precisions)) |
| 91 | + evaluation['precision']['std'] = np.std(np.array(precisions)) |
| 92 | + evaluation['recall']['avg'] = np.mean(np.array(recalls)) |
| 93 | + evaluation['recall']['std'] = np.std(np.array(recalls)) |
| 94 | + evaluation['f1score']['avg'] = np.mean(np.array(f1scores)) |
| 95 | + evaluation['f1score']['std'] = np.std(np.array(f1scores)) |
| 96 | + return evaluation |
0 commit comments