CNN.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from torch import device, cuda
  2. import torch.nn as nn
  3. import utils.CNN_Layers as CustomLayers
  4. class CNN_Net(nn.Module):
  5. def __init__(self, prps, final_layer_size=5):
  6. super(CNN_Net, self).__init__()
  7. self.final_layer_size = final_layer_size
  8. self.device = device('cuda:0' if cuda.is_available() else 'cpu')
  9. print("CNN Initialized. Using: " + str(self.device))
  10. # LAYERS
  11. print(f"CNN Model Initialization")
  12. self.conv1 = CustomLayers.Conv_elu_maxpool_drop(1, 192, (11, 13, 11), stride=(4,4,4), pool=True, prps=prps)
  13. self.conv2 = CustomLayers.Conv_elu_maxpool_drop(192, 384, (5, 6, 5), stride=(1,1,1), pool=True, prps=prps)
  14. self.conv3_mid_flow = CustomLayers.Mid_flow(384, 384, prps=prps)
  15. self.conv4_sepConv = CustomLayers.Conv_elu_maxpool_drop(384, 96,(3, 4, 3), stride=(1,1,1), pool=True, prps=prps,
  16. sep_conv=True)
  17. self.conv5_sepConv = CustomLayers.Conv_elu_maxpool_drop(96, 48, (3, 4, 3), stride=(1, 1, 1), pool=True,
  18. prps=prps, sep_conv=True)
  19. self.fc1 = CustomLayers.Fc_elu_drop(113568, 20, prps=prps, softmax=False) # TODO, concatenate clinical data after this
  20. self.fc2 = CustomLayers.Fc_elu_drop(20, final_layer_size, prps=prps, softmax=True) # For now this works as output layer, though may be incorrect
  21. # FORWARDS
  22. def forward(self, x):
  23. x = self.conv1(x)
  24. x = self.conv2(x)
  25. x = self.conv3_mid_flow(x)
  26. x = self.conv4_sepConv(x)
  27. x = self.conv5_sepConv(x)
  28. # FLATTEN x
  29. flatten_size = x.size(1) * x.size(2) * x.size(3) * x.size(4)
  30. x = x.view(-1, flatten_size)
  31. x = self.fc1(x)
  32. x = self.fc2(x)
  33. return x