main.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import torch
  2. import torchvision
  3. # FOR DATA
  4. from utils.preprocess import prepare_datasets, prepare_predict
  5. from utils.show_image import show_image
  6. from utils.CNN import CNN_Net
  7. from torch.utils.data import DataLoader
  8. from torchvision import datasets
  9. from torch import nn
  10. from torchvision.transforms import ToTensor
  11. # import nonechucks as nc # Used to load data in pytorch even when images are corrupted / unavailable (skips them)
  12. # FOR IMAGE VISUALIZATION
  13. import nibabel as nib
  14. # GENERAL PURPOSE
  15. import os
  16. import pandas as pd
  17. import numpy as np
  18. import matplotlib.pyplot as plt
  19. import glob
  20. import platform
  21. print("--- RUNNING ---")
  22. print("Pytorch Version: " + torch. __version__)
  23. print("Python Version: " + platform.python_version())
  24. # LOADING DATA
  25. # data & training properties:
  26. val_split = 0.2 # % of val and test, rest will be train
  27. seed = 12 # TODO Randomize seed
  28. # params = {
  29. # "target_rows": 91,
  30. # "target_cols": 109,
  31. # "depth": 91,
  32. # "axis": 1,
  33. # "num_clinical": 2,
  34. # "CNN_drop_rate": 0.3,
  35. # "RNN_drop_rate": 0.1,
  36. # # "CNN_w_regularizer": regularizers.l2(2e-2),
  37. # # "RNN_w_regularizer": regularizers.l2(1e-6),
  38. # "CNN_batch_size": 10,
  39. # "RNN_batch_size": 5,
  40. # "val_split": 0.2,
  41. # "final_layer_size": 5
  42. # }
  43. properties = {
  44. "batch_size":6,
  45. "padding":0,
  46. "dilation":1,
  47. "groups":1,
  48. "bias":True,
  49. "padding_mode":"zeros",
  50. "drop_rate":0
  51. }
  52. # Might have to replace datapaths or separate between training and testing
  53. model_filepath = '/data/data_wnx1/rschuurs/Pytorch_CNN-RNN'
  54. CNN_filepath = '/data/data_wnx1/rschuurs/Pytorch_CNN-RNN/cnn_net.pth' # cnn_net.pth
  55. # mri_datapath = '/data/data_wnx1/rschuurs/Pytorch_CNN-RNN/PET_volumes_customtemplate_float32/' # Small Test
  56. mri_datapath = '/data/data_wnx1/_Data/AlzheimersDL/CNN+RNN-2class-1cnn+data/PET_volumes_customtemplate_float32/' # Real data
  57. annotations_datapath = './data/data_wnx1/rschuurs/Pytorch_CNN-RNN/LP_ADNIMERGE.csv'
  58. # annotations_file = pd.read_csv(annotations_datapath) # DataFrame
  59. # show_image(17508)
  60. # TODO: Datasets include multiple labels, such as medical info
  61. training_data, val_data, test_data = prepare_datasets(mri_datapath, val_split, seed)
  62. # Create data loaders
  63. train_dataloader = DataLoader(training_data, batch_size=properties['batch_size'], shuffle=True, drop_last=True)
  64. test_dataloader = DataLoader(test_data, batch_size=properties['batch_size'], shuffle=True)
  65. val_dataloader = DataLoader(val_data, batch_size=properties['batch_size'], shuffle=True)
  66. # for X, y in train_dataloader:
  67. # print(f"Shape of X [Channels (colors), Y, X, Z]: {X.shape}") # X & Y are from TOP LOOKING DOWN
  68. # print(f"Shape of Y (Dataset?): {y.shape} {y.dtype}")
  69. # break
  70. # Display 4 images and labels.
  71. x = 0
  72. while x < 0:
  73. train_features, train_labels = next(iter(train_dataloader))
  74. print(f"Feature batch shape: {train_features.size()}")
  75. img = train_features[0].squeeze()
  76. print(f"Feature batch shape: {img.size()}")
  77. image = img[:, :, 40]
  78. print(f"Feature batch shape: {image.size()}")
  79. label = train_labels[0]
  80. print(f"Label: {label}")
  81. plt.imshow(image, cmap="gray")
  82. plt.savefig(f"./Image{x}_IS:{label}.png")
  83. plt.show()
  84. x = x+1
  85. train = False
  86. predict = False
  87. CNN = CNN_Net(prps=properties, final_layer_size=2)
  88. CNN.cuda()
  89. # RUN CNN
  90. if(train):
  91. CNN.train_model(train_dataloader, test_dataloader, CNN_filepath, epochs=20)
  92. CNN.evaluate_model(val_dataloader, roc=True)
  93. else:
  94. CNN.load_state_dict(torch.load(CNN_filepath))
  95. CNN.evaluate_model(val_dataloader, roc=True)
  96. # PREDICT MODE TO TEST INDIVIDUAL IMAGES
  97. if(predict):
  98. on = True
  99. print("---- Predict mode ----")
  100. print("Integer for image")
  101. print("x or X for exit")
  102. while(on):
  103. inp = input("Next image: ")
  104. if(inp == None or inp.lower() == 'x' or not inp.isdigit()): on = False
  105. else:
  106. dataloader = DataLoader(prepare_predict(mri_datapath, [inp]), batch_size=properties['batch_size'], shuffle=True)
  107. prediction = CNN.predict(dataloader)
  108. features, labels = next(iter(dataloader), )
  109. img = features[0].squeeze()
  110. image = img[:, :, 40]
  111. print(f"Expected class: {labels}")
  112. print(f"Prediction: {prediction}")
  113. plt.imshow(image, cmap="gray")
  114. plt.show()
  115. print("--- END ---")
  116. # EXTRA
  117. # will I need these params?
  118. '''
  119. params_dict = { 'CNN_w_regularizer': CNN_w_regularizer, 'RNN_w_regularizer': RNN_w_regularizer,
  120. 'CNN_batch_size': CNN_batch_size, 'RNN_batch_size': RNN_batch_size,
  121. 'CNN_drop_rate': CNN_drop_rate, 'epochs': 30,
  122. 'gpu': "/gpu:0", 'model_filepath': model_filepath,
  123. 'image_shape': (target_rows, target_cols, depth, axis),
  124. 'num_clinical': num_clinical,
  125. 'final_layer_size': final_layer_size,
  126. 'optimizer': optimizer, 'RNN_drop_rate': RNN_drop_rate,}
  127. params = Parameters(params_dict)
  128. # WHAT WAS THIS AGAIN?
  129. seeds = [np.random.randint(1, 5000) for _ in range(1)]
  130. # READ THIS TO UNDERSTAND TRAIN VS VALIDATION DATA
  131. def evaluate_net (seed):
  132. n_classes = 2
  133. data_loader = DataLoader((target_rows, target_cols, depth, axis), seed = seed)
  134. 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)
  135. print('Length Val Data[0]: ',len(val_data[0]))
  136. '''