diff --git a/image_ref/config.py b/image_ref/config.py index c679960fc07d12c8a04a0e5b9b5179ecdec90974..7ca99992f26eb6ab8ee3b786353772dc936ac504 100644 --- a/image_ref/config.py +++ b/image_ref/config.py @@ -4,20 +4,20 @@ import argparse def load_args_contrastive(): parser = argparse.ArgumentParser() - parser.add_argument('--epoches', type=int, default=1) + parser.add_argument('--epoches', type=int, default=0) parser.add_argument('--save_inter', type=int, default=50) parser.add_argument('--eval_inter', type=int, default=1) parser.add_argument('--noise_threshold', type=int, default=0) parser.add_argument('--lr', type=float, default=0.001) parser.add_argument('--batch_size', type=int, default=16) parser.add_argument('--positive_prop', type=int, default=None) - parser.add_argument('--model', type=str, default='ResNet50') + parser.add_argument('--model', type=str, default='ResNet18') parser.add_argument('--dataset_train_dir', type=str, default='../data/processed_data/npy_image/data_training_contrastive') parser.add_argument('--dataset_val_dir', type=str, default='../data/processed_data/npy_image/data_test_contrastive') parser.add_argument('--dataset_ref_dir', type=str, default='../image_ref/img_ref') - parser.add_argument('--output', type=str, default='output/out_contrastive.csv') - parser.add_argument('--save_path', type=str, default='output/best_model_constrastive.pt') - parser.add_argument('--pretrain_path', type=str, default='../output/best_model_constrastive.pt') + parser.add_argument('--output', type=str, default='../output/out_contrastive.csv') + parser.add_argument('--save_path', type=str, default='../output/best_model_constrastive.pt') + parser.add_argument('--pretrain_path', type=str, default='../saved_model/baseline_resnet18_contrastive_prop_30.pt') args = parser.parse_args() return args \ No newline at end of file diff --git a/image_ref/dataset_ref.py b/image_ref/dataset_ref.py index 88913c55df2199b99f67c267e0e110183a3073a0..3472d627e5a61a4c7d5f2a7f0768486c80081375 100644 --- a/image_ref/dataset_ref.py +++ b/image_ref/dataset_ref.py @@ -114,11 +114,12 @@ def make_dataset_custom( class ImageFolderDuo(data.Dataset): def __init__(self, root, transform=None, target_transform=None, - flist_reader=make_dataset_custom, loader=npy_loader, ref_dir = None, positive_prop=None): + flist_reader=make_dataset_custom, loader=npy_loader, ref_dir = None, positive_prop=None, ref_transform=None): self.root = root self.imlist = flist_reader(root) self.transform = transform self.target_transform = target_transform + self.ref_transform = ref_transform self.loader = loader self.classes = torchvision.datasets.folder.find_classes(root)[0] self.ref_dir = ref_dir @@ -144,7 +145,7 @@ class ImageFolderDuo(data.Dataset): if self.transform is not None: imgAER = self.transform(imgAER) imgANA = self.transform(imgANA) - img_ref = self.transform(img_ref) + img_ref = self.ref_transform(img_ref) contrastive_target = 0 if target == label_ref else 1 return imgAER, imgANA, img_ref, contrastive_target @@ -169,6 +170,8 @@ def load_data_duo(base_dir_train, base_dir_test, batch_size, shuffle=True, noise ref_transform = transforms.Compose( [transforms.Resize((224, 224)), + Threshold_noise(noise_threshold), + Log_normalisation(), transforms.Normalize(0.5, 0.5)]) print('Default val transform') diff --git a/image_ref/grad_cam.py b/image_ref/grad_cam.py index 57cd4e2fb91eaa22d0a0674ef53f8a0dbdddf7c4..a2de3c3ea15e1ebb60306a65778a6c76750be3e5 100644 --- a/image_ref/grad_cam.py +++ b/image_ref/grad_cam.py @@ -25,18 +25,18 @@ def compute_class_activation_map(): path_aer ='../data/processed_data/npy_image/data_test_contrastive/Citrobacter freundii/CITFRE17_AER.npy' path_ana ='../data/processed_data/npy_image/data_test_contrastive/Citrobacter freundii/CITFRE17_ANA.npy' - path_ref ='../image_ref/img_ref/Citrobacter freundii.npy' - + # path_ref ='../image_ref/img_ref/Citrobacter freundii.npy' #positive + path_ref = '../image_ref/img_ref/Enterobacter hormaechei.npy' #negative + # path_ref = '../image_ref/img_ref/Proteus mirabilis.npy' # negative tensor_aer = npy_loader(path_aer) tensor_ana = npy_loader(path_ana) tensor_ref = npy_loader(path_ref) img_ref = np.load(path_ref) - tensor_aer = transform(tensor_aer) tensor_ana = transform(tensor_ana) - tensor_ref = ref_transform(tensor_ref) + tensor_ref = transform(tensor_ref) tensor_aer = torch.unsqueeze(tensor_aer, dim=0) tensor_ana = torch.unsqueeze(tensor_ana, dim=0) @@ -70,6 +70,8 @@ def compute_class_activation_map(): # Perform the forward pass model.eval() # Set the model to evaluation mode output = model(tensor_aer,tensor_ana,tensor_ref) + + print(output) pred_class = output.argmax(dim=1).item() # Zero the gradients @@ -77,6 +79,7 @@ def compute_class_activation_map(): # Backward pass to compute gradients output[:, pred_class].backward() + print('Predicted class ',pred_class) # Compute the weights weights = torch.mean(gradients[0], dim=[2, 3]) diff --git a/image_ref/main.py b/image_ref/main.py index ef6382ea822a3061bd47818011fe3f27aa26abfc..2a6bdc04954cc097cdafea544ca926e6e0f82a40 100644 --- a/image_ref/main.py +++ b/image_ref/main.py @@ -81,6 +81,7 @@ def run_duo(args): model.double() #load weight if args.pretrain_path is not None : + 'Model weight loaded' load_model(model,args.pretrain_path) #move parameters to GPU if torch.cuda.is_available(): @@ -134,12 +135,12 @@ def run_duo(args): plt.show() - plt.savefig('output/training_plot_contrastive_{}.png'.format(args.positive_prop)) + plt.savefig('../output/training_plot_contrastive_{}.png'.format(args.positive_prop)) #load and evaluate best model load_model(model, args.save_path) - make_prediction_duo(model,data_test_batch, 'output/confusion_matrix_contractive_{}_bis.png'.format(args.positive_prop), - 'output/confidence_matrix_contractive_{}_bis.png'.format(args.positive_prop)) + make_prediction_duo(model,data_test_batch, '../output/confusion_matrix_contractive_{}_bis.png'.format(args.positive_prop), + '../output/confidence_matrix_contractive_{}_bis.png'.format(args.positive_prop)) def make_prediction_duo(model, data, f_name, f_name2): @@ -167,6 +168,7 @@ def make_prediction_duo(model, data, f_name, f_name2): img_ref = img_ref.cuda() label = label.cuda() output = model(imaer,imana,img_ref) + print(output) confidence = soft_max(output) confidence_pred_list[specie].append(confidence[:,0].data.cpu().numpy()) #Mono class output (only most postive paire)