12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- from torch import nn
- from torchvision.transforms import ToTensor
- import os
- import pandas as pd
- import numpy as np
- import utils.layers as ly
- import torch
- import torchvision
- class Parameters:
- def __init__(self, param_dict):
- self.CNN_w_regularizer = param_dict["CNN_w_regularizer"]
- self.RNN_w_regularizer = param_dict["RNN_w_regularizer"]
- self.CNN_batch_size = param_dict["CNN_batch_size"]
- self.RNN_batch_size = param_dict["RNN_batch_size"]
- self.CNN_drop_rate = param_dict["CNN_drop_rate"]
- self.RNN_drop_rate = param_dict["RNN_drop_rate"]
- self.epochs = param_dict["epochs"]
- self.gpu = param_dict["gpu"]
- self.model_filepath = param_dict["model_filepath"] + "/net.h5"
- self.num_clinical = param_dict["num_clinical"]
- self.image_shape = param_dict["image_shape"]
- self.final_layer_size = param_dict["final_layer_size"]
- self.optimizer = param_dict["optimizer"]
- class CNN_Net(nn.Module):
- def __init__(self, image_channels, clin_data_channels, droprate):
- super().__init__()
- # Initial Convolutional Blocks
- self.conv1 = ly.ConvolutionalBlock(
- image_channels, 192, (11, 13, 11), stride=(4, 4, 4), droprate=droprate, pool=True
- )
- self.conv2 = ly.ConvolutionalBlock(
- 192, 384, (5, 6, 5), droprate=droprate, pool=True
- )
- # Midflow Block
- self.midflow = ly.MidFlowBlock(384, droprate)
- # Combine
- self.combined = nn.Sequential(self.conv1, self.conv2, self.midflow)
- # Split Convolutional Block
- self.splitconv = ly.SplitConvBlock(384, 192, 96, 4, droprate)
- #Fully Connected Block
- self.fc1 = ly.FullyConnectedBlock(96, 20, droprate=droprate)
- self.image_layers = nn.Sequential(self.combined, self.splitconv).double()
- #Data Layers, fully connected
- self.fc1 = ly.FullyConnectedBlock(clin_data_channels, 64, droprate=droprate)
- self.fc2 = ly.FullyConnectedBlock(64, 20, droprate=droprate)
-
- #Connect Data
- self.data_layers = nn.Sequential(self.fc1, self.fc2).double()
- #Final Dense Layer
- self.dense1 = nn.Linear(40, 5)
- self.dense2 = nn.Linear(5, 2)
- self.softmax = nn.Softmax()
- self.final_layers = nn.Sequential(self.dense1, self.dense2, self.softmax)
- def forward(self, x):
- image, clin_data = x
- print(image.shape)
-
- image = self.image_layers(image)
- x = torch.cat((image, clin_data), dim=1)
- x = self.final_layers(x)
- return x
-
|