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(): domains = ['IR', 'VL'] preprocess_methods = ['mean_subtraction', 'scaling'] init_methods = ['random', 'ImageNet'] model_names = ['ResNet50', 'VGG19'] stages = ['stage_1', 'stage_2', 'stage_3', 'stage_4', 'stage_5'] ResNet50_layer_names = [ 'max_pooling2d_1', 'activation_10', 'activation_22', 'activation_40', 'activation_49' ] VGG19_layer_names = [ 'block1_pool', 'block2_pool', 'block3_pool', 'block4_pool', 'block5_pool' ] out_dict = {} sampling_method = 'pca' for domain in domains: for preprocess_method in preprocess_methods: # load dataset dh = DatasetHandler(domain) dataset_all = dh.get_all(preprocess_method) for init_method in init_methods: for model_name in model_names: for ind_stage, stage in enumerate(stages): # load model keras.backend.clear_session() 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) # strip layers if model_name == 'ResNet50': end_layer = ResNet50_layer_names[ind_stage] elif model_name == 'VGG19': end_layer = VGG19_layer_names[ind_stage] model = keras.models.Model( inputs=model.input, outputs=model.get_layer(end_layer).output) feats = model.predict(dataset_all['data']) feats = np.reshape(feats, (dataset_all['data'].shape[0], -1)) if sampling_method == 'pca': pca = PCA(1024) feats = pca.fit_transform(feats) elif sampling_method == 'uniform': if feats.shape[1] > 16384: sample_indices = np.round( np.linspace(0, feats.shape[1] - 1, 16384)) feats = feats[:, sample_indices.astype(int)] name = domain + '_' + preprocess_method + '_' + init_method + '_' + model_name + '_' + stage out_dict[name + '_feats'] = feats out_dict[name + '_labels'] = dataset_all['labels'] savemat('feats_' + sampling_method + '.mat', out_dict, do_compression=True)
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()