def plot_roc(): for domain in domains: for model_name in model_names: for init_opt in init_opts: for preprocess_opt in preprocess_opts: scores = [] labels = [] for ind_fold, fold in enumerate(folds): K.clear_session() dh = DatasetHandler(domain) dataset_fold = dh.get_fold(ind_fold, preprocess_opt) model_path = os.path.join('log', domain, model_name, init_opt, preprocess_opt, fold, 'stage_5.h5') model = model_loader.load_full_model(model_name, no_cats=2) model.load_weights(model_path) scores.append( model.predict(dataset_fold['test_data'], batch_size=10)) labels.append(dataset_fold['test_labels']) scores = np.concatenate(scores)[:, 1] labels = np.concatenate(labels)[:, 1] fpr, tpr, _ = roc_curve(labels, scores) roc_auc = auc(fpr, tpr) savemat( os.path.join( 'log', 'roc', domain + '_' + model_name + '_' + init_opt + '_' + preprocess_opt + '.mat'), { 'fpr': fpr, 'tpr': tpr, 'roc_auc': roc_auc }) plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc) plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating characteristics curve') plt.legend(loc="lower right") plt.savefig( os.path.join( 'log', 'roc', domain + '_' + model_name + '_' + init_opt + '_' + preprocess_opt)) plt.close()
def main(): for domain in domains: for model_name in model_names: for init_opt in init_opts: for preprocess_opt in preprocess_opts: log_path = os.path.join(log_path_main, domain, model_name, init_opt) if not os.path.exists(log_path): os.makedirs(log_path) for ind_fold, fold in enumerate(folds): K.clear_session() dh = DatasetHandler(domain) dataset_fold = dh.get_fold(ind_fold, preprocess_opt) model_path = os.path.join('log', domain, model_name, init_opt, preprocess_opt, fold, 'stage_5.h5') model = model_loader.load_full_model(model_name, no_cats=2) model.load_weights(model_path) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) guided_bprop = GuidedBackprop(model) for ind_image, image in enumerate( dataset_fold['test_data']): if domain == 'VL': image = np.dot(image[..., :3], [0.299, 0.587, 0.114]) image = image[:, :, np.newaxis] image = np.repeat(image, 3, axis=2) mask = guided_bprop.get_mask(image) mask = np.power(mask, 2) mask = np.sum(mask, axis=2) mask = np.sqrt(mask) mask -= np.min(mask) norm_max = np.max(mask) if norm_max == 0: norm_max = 1 mask /= norm_max mask *= 255 mask = np.uint8(mask) img = Image.fromarray(mask, 'L') im_name = dataset_fold['test_image_names'][ ind_image].split('.')[0] img.save(os.path.join(log_path, im_name + '.png'), 'PNG')
def main(): # initialize log file file_log = open(os.path.join(log_path, 'log.txt'), 'w') file_log.write(domain + ' - ' + str(ind_fold) + '\n') file_log.write(model_name + '\n') file_log.write(init_method + '\n') file_log.write(preprocess_method + '\n') # read dataset dh = DatasetHandler(domain) dataset_fold = dh.get_fold(ind_fold, preprocess_method) if init_method == 'random': model = model_loader.load_full_model(model_name, random_weights=True, no_cats=2, weight_decay=0.001) elif init_method == 'ImageNet': model = model_loader.load_full_model(model_name, random_weights=False, no_cats=2, weight_decay=0.001) # train the last layer accs = [] false_images = [] model = model_loader.set_trainable_layers(model, model_name, 'final') learning_rate = 0.1 for ind_iter in range(5): model = trainer.train_model(model, dataset_fold, learning_rate) learning_rate /= 2 acc, false_image = trainer.test_model(model, dataset_fold) accs.append(acc) false_images.append(false_image) model.save_weights(os.path.join(log_path, 'final_layer.h5')) # fine-tune stage 5 and onwards model = model_loader.set_trainable_layers(model, model_name, '5') learning_rate = 0.01 for ind_iter in range(5): model = trainer.train_model(model, dataset_fold, learning_rate) learning_rate /= 2 acc, false_image = trainer.test_model(model, dataset_fold) accs.append(acc) false_images.append(false_image) model.save_weights(os.path.join(log_path, 'stage_5.h5')) # record accuracies file_log.write('Final layer\n') file_log.write(str(accs[0]) + '\n') file_log.write('Stage 5\n') file_log.write(str(accs[1]) + '\n') # record falsely classified images file_log.write('Final layer\n') for fi in false_images[0]: file_log.write(fi + '\n') file_log.write('Stage 5\n') for fi in false_images[1]: file_log.write(fi + '\n') file_log.close()