-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add evaluation for RT-BENE * Ignore some more models * Add testing code description to README Co-authored-by: Kevin Cortacero <cortacero.kevin@gmail.com>
- Loading branch information
1 parent
a46cec1
commit 08b1499
Showing
5 changed files
with
213 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#!/usr/bin/env python | ||
|
||
import gc | ||
|
||
import tensorflow as tf | ||
from tensorflow.keras.models import load_model | ||
|
||
from sklearn.metrics import confusion_matrix, roc_curve, auc, average_precision_score | ||
|
||
import numpy as np | ||
|
||
tf.compat.v1.disable_eager_execution() | ||
|
||
config = tf.compat.v1.ConfigProto() | ||
config.gpu_options.allow_growth = True | ||
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config)) | ||
|
||
|
||
fold_infos = { | ||
'fold1': [2], | ||
'fold2': [1], | ||
'fold3': [0], | ||
'all': [2, 1, 0] | ||
} | ||
|
||
model_metrics = [tf.keras.metrics.BinaryAccuracy()] | ||
|
||
|
||
def estimate_metrics(testing_fold, model_instance): | ||
threshold = 0.5 | ||
p = model_instance.predict(x=testing_fold['x'], verbose=0) | ||
p = p >= threshold | ||
matrix = confusion_matrix(testing_fold['y'], p) | ||
ap = average_precision_score(testing_fold['y'], p) | ||
fpr, tpr, thresholds = roc_curve(testing_fold['y'], p) | ||
roc = auc(fpr, tpr) | ||
return matrix, ap, roc | ||
|
||
|
||
def get_metrics_from_matrix(matrix): | ||
tp, tn, fp, fn = matrix[1, 1], matrix[0, 0], matrix[0, 1], matrix[1, 0] | ||
precision = tp / (tp + fp) | ||
recall = tp / (tp + fn) | ||
f1score = 2. * (precision * recall) / (precision + recall) | ||
return precision, recall, f1score | ||
|
||
|
||
def threefold_evaluation(dataset, model_paths_fold1, model_paths_fold2, model_paths_fold3, input_size): | ||
folds = ['fold1', 'fold2', 'fold3'] | ||
aps = [] | ||
rocs = [] | ||
recalls = [] | ||
precisions = [] | ||
f1scores = [] | ||
models = [] | ||
|
||
for fold_to_eval_on, model_paths in zip(folds, [model_paths_fold1, model_paths_fold2, model_paths_fold3]): | ||
if len(model_paths_fold1) > 1: | ||
models = [load_model(model_path, compile=False) for model_path in model_paths] | ||
img_input_l = tf.keras.Input(shape=input_size, name='img_input_L') | ||
img_input_r = tf.keras.Input(shape=input_size, name='img_input_R') | ||
tensors = [model([img_input_r, img_input_l]) for model in models] | ||
output_layer = tf.keras.layers.average(tensors) | ||
model_instance = tf.keras.Model(inputs=[img_input_r, img_input_l], outputs=output_layer) | ||
else: | ||
model_instance = load_model(model_paths[0]) | ||
model_instance.compile() | ||
|
||
testing_fold = dataset.get_training_data(fold_infos[fold_to_eval_on]) # get the testing fold subjects | ||
|
||
matrix, ap, roc = estimate_metrics(testing_fold, model_instance) | ||
aps.append(ap) | ||
rocs.append(roc) | ||
precision, recall, f1score = get_metrics_from_matrix(matrix) | ||
recalls.append(recall) | ||
precisions.append(precision) | ||
f1scores.append(f1score) | ||
|
||
del model_instance, testing_fold | ||
# noinspection PyUnusedLocal | ||
for model in models: | ||
del model | ||
gc.collect() | ||
|
||
evaluation = {'AP': {}, 'ROC': {}, 'precision': {}, 'recall': {}, 'f1score': {}} | ||
evaluation['AP']['avg'] = np.mean(np.array(aps)) | ||
evaluation['AP']['std'] = np.std(np.array(aps)) | ||
evaluation['ROC']['avg'] = np.mean(np.array(rocs)) | ||
evaluation['ROC']['std'] = np.std(np.array(rocs)) | ||
evaluation['precision']['avg'] = np.mean(np.array(precisions)) | ||
evaluation['precision']['std'] = np.std(np.array(precisions)) | ||
evaluation['recall']['avg'] = np.mean(np.array(recalls)) | ||
evaluation['recall']['std'] = np.std(np.array(recalls)) | ||
evaluation['f1score']['avg'] = np.mean(np.array(f1scores)) | ||
evaluation['f1score']['std'] = np.std(np.array(f1scores)) | ||
return evaluation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from evaluate_blink_model import threefold_evaluation | ||
from train_blink_model import ThreefoldTraining | ||
from dataset_manager import RTBeneDataset | ||
from pathlib import Path | ||
import argparse | ||
import pprint | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("model_save_root", help="target folder to save the models (auto-saved)") | ||
parser.add_argument("csv_subject_list", help="path to the dataset csv file") | ||
parser.add_argument("--ensemble_size", type=int, default=1, help="number of models to train for the ensemble") | ||
parser.add_argument("--batch_size", type=int, default=64) | ||
parser.add_argument("--epochs", type=int, default=15) | ||
parser.add_argument("--input_size", type=tuple, help="input size of images", default=(96, 96)) | ||
|
||
args = parser.parse_args() | ||
|
||
fold_list = ['fold1', 'fold2', 'fold3'] | ||
ensemble_size = args.ensemble_size # 1 is considered as single model | ||
epochs = args.epochs | ||
batch_size = args.batch_size | ||
input_size = args.input_size | ||
csv_subject_list = args.csv_subject_list | ||
model_save_root = args.model_save_root | ||
|
||
dataset = RTBeneDataset(csv_subject_list, input_size) | ||
|
||
threefold_training = ThreefoldTraining(dataset, epochs, batch_size, input_size) | ||
|
||
all_evaluations = {} | ||
|
||
for backbone in ['densenet121', 'resnet50', 'mobilenetv2']: | ||
models_fold1 = [] | ||
models_fold2 = [] | ||
models_fold3 = [] | ||
|
||
for i in range(1, ensemble_size + 1): | ||
model_save_path = Path(model_save_root + backbone + '/' + str(i)) | ||
model_save_path.mkdir(parents=True, exist_ok=True) | ||
threefold_training.train(backbone, str(model_save_path) + '/') | ||
|
||
models_fold1.append(str(model_save_path) + '/rt-bene_' + backbone + '_fold1_best.h5') | ||
models_fold2.append(str(model_save_path) + '/rt-bene_' + backbone + '_fold2_best.h5') | ||
models_fold3.append(str(model_save_path) + '/rt-bene_' + backbone + '_fold3_best.h5') | ||
|
||
evaluation = threefold_evaluation(dataset, models_fold1, models_fold2, models_fold3, input_size) | ||
all_evaluations[backbone] = evaluation | ||
|
||
threefold_training.free() | ||
|
||
pprint.pprint(all_evaluations) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters