|
@@ -1,3 +1,5 @@
|
|
|
+from symbol import parameters
|
|
|
+
|
|
|
import torch
|
|
|
|
|
|
# FOR DATA
|
|
@@ -16,6 +18,11 @@ import platform
|
|
|
import time
|
|
|
current_time = time.localtime()
|
|
|
|
|
|
+
|
|
|
+# INTERPRETABILITY
|
|
|
+from captum.attr import GuidedGradCam
|
|
|
+
|
|
|
+
|
|
|
print(time.strftime("%Y-%m-%d_%H:%M", current_time))
|
|
|
print("--- RUNNING ---")
|
|
|
print("Pytorch Version: " + torch. __version__)
|
|
@@ -46,13 +53,13 @@ CNN_filepath = '/data/data_wnx1/rschuurs/Pytorch_CNN-RNN/cnn_net.pth' # cn
|
|
|
# mri_datapath = '/data/data_wnx1/rschuurs/Pytorch_CNN-RNN/PET_volumes_customtemplate_float32/' # Small Test
|
|
|
# big dataset
|
|
|
mri_datapath = '/data/data_wnx1/_Data/AlzheimersDL/CNN+RNN-2class-1cnn+data/PET_volumes_customtemplate_float32/' # Real data
|
|
|
-annotations_datapath = './data/data_wnx1/rschuurs/Pytorch_CNN-RNN/LP_ADNIMERGE.csv'
|
|
|
+csv_datapath = 'LP_ADNIMERGE.csv'
|
|
|
|
|
|
# annotations_file = pd.read_csv(annotations_datapath) # DataFrame
|
|
|
# show_image(17508)
|
|
|
|
|
|
# TODO: Datasets include multiple labels, such as medical info
|
|
|
-training_data, val_data, test_data = prepare_datasets(mri_datapath, val_split, seed)
|
|
|
+training_data, val_data, test_data = prepare_datasets(mri_datapath, csv_datapath, val_split, seed)
|
|
|
|
|
|
# Create data loaders
|
|
|
train_dataloader = DataLoader(training_data, batch_size=properties['batch_size'], shuffle=True, drop_last=True)
|
|
@@ -63,7 +70,7 @@ test_dataloader = DataLoader(test_data, batch_size=properties['batch_size'], shu
|
|
|
# loads a few images to test
|
|
|
x = 0
|
|
|
while x < 0:
|
|
|
- train_features, train_labels = next(iter(train_dataloader))[0]
|
|
|
+ train_features, train_labels = next(iter(train_dataloader))
|
|
|
# print(f"Feature batch shape: {train_features.size()}")
|
|
|
img = train_features[0].squeeze()
|
|
|
print(f"Feature batch shape: {img.size()}")
|
|
@@ -72,23 +79,43 @@ while x < 0:
|
|
|
label = train_labels[0]
|
|
|
print(f"Label: {label}")
|
|
|
plt.imshow(image, cmap="gray")
|
|
|
- plt.savefig(f"./Image{x}_IS:{label}.png")
|
|
|
+ # plt.savefig(f"./Image{x}_IS:{label}.png")
|
|
|
plt.show()
|
|
|
|
|
|
x = x+1
|
|
|
|
|
|
|
|
|
-epochs = 20
|
|
|
roc = True
|
|
|
CNN = CNN_Net(prps=properties, final_layer_size=2)
|
|
|
CNN.cuda()
|
|
|
|
|
|
-train(CNN, train_dataloader, val_dataloader, CNN_filepath, epochs, graphs=True)
|
|
|
-# load(CNN, CNN_filepath)
|
|
|
-evaluate(CNN, test_dataloader)
|
|
|
-predict(CNN, test_dataloader)
|
|
|
+# train(CNN, train_dataloader, val_dataloader, CNN_filepath, properties, graphs=True)
|
|
|
+load(CNN, CNN_filepath)
|
|
|
+# evaluate(CNN, test_dataloader)
|
|
|
+# predict(CNN, test_dataloader)
|
|
|
+
|
|
|
+print(CNN)
|
|
|
+CNN.eval()
|
|
|
+
|
|
|
+guided_gc = GuidedGradCam(CNN, CNN.conv5_sepConv) # Performed on LAST convolution layer
|
|
|
+# input = torch.randn(1, 1, 91, 109, 91, requires_grad=True).cuda()
|
|
|
+
|
|
|
+# TODO MAKE BATCH SIZE 1 FOR THIS TO WORK??
|
|
|
+train_features, train_labels = next(iter(train_dataloader))
|
|
|
+while(train_labels[0] == 0):
|
|
|
+ train_features, train_labels = next(iter(train_dataloader))
|
|
|
+
|
|
|
+attr = guided_gc.attribute(train_features.cuda(), 0) #, interpolate_mode="area")
|
|
|
|
|
|
+# draw the attributes
|
|
|
+attr = attr.unsqueeze(0)
|
|
|
+attr = attr.cpu().detach().numpy()
|
|
|
+attr = np.clip(attr, 0, 1)
|
|
|
+plt.imshow(attr)
|
|
|
+plt.show()
|
|
|
|
|
|
+print("Done w/ attributions")
|
|
|
+print(attr)
|
|
|
|
|
|
# EXTRA
|
|
|
|