models.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from torch import nn
  2. from torchvision.transforms import ToTensor
  3. import os
  4. import pandas as pd
  5. import numpy as np
  6. import utils.layers as ly
  7. import torch
  8. import torchvision
  9. class Parameters:
  10. def __init__(self, param_dict):
  11. self.CNN_w_regularizer = param_dict["CNN_w_regularizer"]
  12. self.RNN_w_regularizer = param_dict["RNN_w_regularizer"]
  13. self.CNN_batch_size = param_dict["CNN_batch_size"]
  14. self.RNN_batch_size = param_dict["RNN_batch_size"]
  15. self.CNN_drop_rate = param_dict["CNN_drop_rate"]
  16. self.RNN_drop_rate = param_dict["RNN_drop_rate"]
  17. self.epochs = param_dict["epochs"]
  18. self.gpu = param_dict["gpu"]
  19. self.model_filepath = param_dict["model_filepath"] + "/net.h5"
  20. self.num_clinical = param_dict["num_clinical"]
  21. self.image_shape = param_dict["image_shape"]
  22. self.final_layer_size = param_dict["final_layer_size"]
  23. self.optimizer = param_dict["optimizer"]
  24. class CNN_Net(nn.Module):
  25. def __init__(self, image_channels, clin_data_channels, droprate):
  26. super().__init__()
  27. # Initial Convolutional Blocks
  28. self.conv1 = ly.ConvolutionalBlock(
  29. image_channels, 192, (11, 13, 11), stride=(4, 4, 4), droprate=droprate, pool=True
  30. )
  31. self.conv2 = ly.ConvolutionalBlock(
  32. 192, 384, (5, 6, 5), droprate=droprate, pool=True
  33. )
  34. # Midflow Block
  35. self.midflow = ly.MidFlowBlock(384, droprate)
  36. # Combine
  37. self.combined = nn.Sequential(self.conv1, self.conv2, self.midflow)
  38. # Split Convolutional Block
  39. self.splitconv = ly.SplitConvBlock(384, 192, 96, 4, droprate)
  40. #Fully Connected Block
  41. self.fc1 = ly.FullyConnectedBlock(96, 20, droprate=droprate)
  42. self.image_layers = nn.Sequential(self.combined, self.splitconv).double()
  43. #Data Layers, fully connected
  44. self.fc1 = ly.FullyConnectedBlock(clin_data_channels, 64, droprate=droprate)
  45. self.fc2 = ly.FullyConnectedBlock(64, 20, droprate=droprate)
  46. #Connect Data
  47. self.data_layers = nn.Sequential(self.fc1, self.fc2).double()
  48. #Final Dense Layer
  49. self.dense1 = nn.Linear(40, 5)
  50. self.dense2 = nn.Linear(5, 2)
  51. self.softmax = nn.Softmax()
  52. self.final_layers = nn.Sequential(self.dense1, self.dense2, self.softmax)
  53. def forward(self, x):
  54. image, clin_data = x
  55. print(image.shape)
  56. image = self.image_layers(image)
  57. x = torch.cat((image, clin_data), dim=1)
  58. x = self.final_layers(x)
  59. return x