|
@@ -0,0 +1,125 @@
|
|
|
|
+import torch
|
|
|
|
+
|
|
|
|
+# FOR DATA
|
|
|
|
+from torch import nn
|
|
|
|
+from torch.utils.data import DataLoader, Dataset
|
|
|
|
+from torchvision import datasets
|
|
|
|
+from torchvision.transforms import ToTensor
|
|
|
|
+import os
|
|
|
|
+import pandas as pd
|
|
|
|
+from torchvision.io import read_image
|
|
|
|
+import nonechucks as nc # Used to load data in pytorch even when images are corrupted / unavailable (skips them)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# MAYBE??
|
|
|
|
+'''
|
|
|
|
+import sys
|
|
|
|
+sys.path.append('//data/data_wnx3/data_wnx1/rschuurs/CNN+RNN-2class-1cnn-CLEAN/utils')
|
|
|
|
+
|
|
|
|
+import os
|
|
|
|
+os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
|
|
|
+os.environ["CUDA_VISIBLE_DEVICES"] = "0" # use id from $ nvidia-smi
|
|
|
|
+'''
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# LOADING DATA
|
|
|
|
+# data & training properties:
|
|
|
|
+'''
|
|
|
|
+target_rows = 91
|
|
|
|
+target_cols = 109
|
|
|
|
+depth = 91
|
|
|
|
+axis = 1
|
|
|
|
+num_clinical = 2
|
|
|
|
+CNN_drop_rate = 0.3
|
|
|
|
+RNN_drop_rate = 0.1
|
|
|
|
+CNN_w_regularizer = regularizers.l2(2e-2)
|
|
|
|
+RNN_w_regularizer = regularizers.l2(1e-6)
|
|
|
|
+CNN_batch_size = 10
|
|
|
|
+RNN_batch_size = 5
|
|
|
|
+val_split = 0.2
|
|
|
|
+optimizer = Adam(lr=1e-5)
|
|
|
|
+final_layer_size = 5
|
|
|
|
+'''
|
|
|
|
+
|
|
|
|
+# Defines Dataset class, which is instantiated to create the dataset with the required images
|
|
|
|
+class CustomImageDataset(Dataset):
|
|
|
|
+ def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
|
|
|
|
+ self.img_labels = pd.read_csv(annotations_file)
|
|
|
|
+ self.img_dir = img_dir
|
|
|
|
+ self.transform = transform
|
|
|
|
+ self.target_transform = target_transform
|
|
|
|
+
|
|
|
|
+ def __len__(self):
|
|
|
|
+ return len(self.img_labels)
|
|
|
|
+
|
|
|
|
+ def __getitem__(self, idx):
|
|
|
|
+ img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
|
|
|
|
+ image = read_image(img_path)
|
|
|
|
+ label = self.img_labels.iloc[idx, 1]
|
|
|
|
+ if self.transform:
|
|
|
|
+ image = self.transform(image)
|
|
|
|
+ if self.target_transform:
|
|
|
|
+ label = self.target_transform(label)
|
|
|
|
+ return image, label
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# Might have to replace datapaths or separate between training and testing
|
|
|
|
+model_filepath = '//data/data_wnx1/rschuurs/pytorchCNN'
|
|
|
|
+mri_datapath = './ADNI_volumes_customtemplate_float32/'
|
|
|
|
+anotationsFileDataPath = './LP_ADNIMERGE.csv'
|
|
|
|
+
|
|
|
|
+training_data = CustomImageDataset(anotationsFileDataPath, mri_datapath)
|
|
|
|
+test_data = CustomImageDataset(anotationsFileDataPath, mri_datapath)
|
|
|
|
+
|
|
|
|
+# Skips corrupted or non-present files in CSV
|
|
|
|
+safeTrainingData = nc.SafeDataset(training_data)
|
|
|
|
+safeTestData = nc.SafeDataset(test_data)
|
|
|
|
+
|
|
|
|
+print("Initial Training data length: " + str(training_data.__len__()))
|
|
|
|
+print("Safe Training data length: " + str(safeTrainingData.__len__()))
|
|
|
|
+print("Test data length: " + str(training_data.__len__()))
|
|
|
|
+
|
|
|
|
+batch_size = 64
|
|
|
|
+
|
|
|
|
+# Create data loaders.
|
|
|
|
+train_dataloader = DataLoader(training_data, batch_size=batch_size)
|
|
|
|
+test_dataloader = DataLoader(test_data, batch_size=batch_size)
|
|
|
|
+
|
|
|
|
+for X, y in test_dataloader:
|
|
|
|
+ print(f"Shape of X [N, C, H, W]: {X.shape}")
|
|
|
|
+ print(f"Shape of y: {y.shape} {y.dtype}")
|
|
|
|
+ break
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# EXTRA
|
|
|
|
+
|
|
|
|
+# will I need these params?
|
|
|
|
+'''
|
|
|
|
+params_dict = { 'CNN_w_regularizer': CNN_w_regularizer, 'RNN_w_regularizer': RNN_w_regularizer,
|
|
|
|
+ 'CNN_batch_size': CNN_batch_size, 'RNN_batch_size': RNN_batch_size,
|
|
|
|
+ 'CNN_drop_rate': CNN_drop_rate, 'epochs': 30,
|
|
|
|
+ 'gpu': "/gpu:0", 'model_filepath': model_filepath,
|
|
|
|
+ 'image_shape': (target_rows, target_cols, depth, axis),
|
|
|
|
+ 'num_clinical': num_clinical,
|
|
|
|
+ 'final_layer_size': final_layer_size,
|
|
|
|
+ 'optimizer': optimizer, 'RNN_drop_rate': RNN_drop_rate,}
|
|
|
|
+
|
|
|
|
+params = Parameters(params_dict)
|
|
|
|
+
|
|
|
|
+# WHAT WAS THIS AGAIN?
|
|
|
|
+seeds = [np.random.randint(1, 5000) for _ in range(1)]
|
|
|
|
+
|
|
|
|
+# READ THIS TO UNDERSTAND TRAIN VS VALIDATION DATA
|
|
|
|
+def evaluate_net (seed):
|
|
|
|
+ n_classes = 2
|
|
|
|
+ data_loader = DataLoader((target_rows, target_cols, depth, axis), seed = seed)
|
|
|
|
+ train_data, val_data, test_data,rnn_HdataT1,rnn_HdataT2,rnn_HdataT3,rnn_AdataT1,rnn_AdataT2,rnn_AdataT3, test_mri_nonorm = data_loader.get_train_val_test(val_split, mri_datapath)
|
|
|
|
+
|
|
|
|
+ print('Length Val Data[0]: ',len(val_data[0]))
|
|
|
|
+'''
|