Spaces:
Runtime error
Runtime error
File size: 7,130 Bytes
71bd54f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchinfo import summary
# Not in use yet
class Conv1d_layer(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size) -> None:
super().__init__()
self.conv = nn.Conv1d(in_channels=in_channel, out_channels=out_channel, kernel_size=kernel_size)
self.batch_norm = torch.nn.BatchNorm1d(out_channel)
self.dropout = nn.Dropout1d(p=0.5)
def forward(self, x):
x= self.conv(x)
x = self.batch_norm(x)
x = self.dropout(x)
return x
class CNN(nn.Module):
def __init__(self, ecg_channels=12):
super(CNN, self).__init__()
self.name = "CNN"
self.conv1 = nn.Conv1d(ecg_channels, 16, 7)
self.pool1 = nn.MaxPool1d(2, 2)
self.conv2 = nn.Conv1d(16, 32, 5)
self.pool2 = nn.MaxPool1d(2, 2)
self.conv3 = nn.Conv1d(32, 48, 3)
self.pool3 = nn.MaxPool1d(2, 2)
self.fc0 = nn.Linear(5856, 512)
self.fc1 = nn.Linear(512, 128)
self.fc2 = nn.Linear(128, 5)
self.activation = nn.ReLU()
def forward(self, x, notes=None):
x = self.pool1(self.activation(self.conv1(x)))
x = self.pool2(self.activation(self.conv2(x)))
x = self.pool3(self.activation(self.conv3(x)))
x = x.view(x.size(0),-1)
x = self.activation(self.fc0(x))
x = self.activation(self.fc1(x))
x = self.fc2(x)
x = x.squeeze(1)
return x
class MMCNN_SUM(nn.Module):
def __init__(self, ecg_channels=12):
super(MMCNN_SUM, self).__init__()
# ECG processing Layers
self.name = "MMCNN_SUM"
self.conv1 = Conv1d_layer(ecg_channels, 16, 7)
self.pool1 = nn.MaxPool1d(2, 2)
self.conv2 = Conv1d_layer(16, 32, 5)
self.pool2 = nn.MaxPool1d(2, 2)
self.conv3 = Conv1d_layer(32, 48, 3)
self.pool3 = nn.MaxPool1d(2, 2)
self.fc0 = nn.Linear(5856, 512)
self.fc1 = nn.Linear(512, 128)
self.fc2 = nn.Linear(128, 5)
# Clinical Notes Processing Layers
self.fc_emb = nn.Linear(768, 128)
self.norm = nn.LayerNorm(128)
self.activation = nn.ReLU()
def forward(self, x, notes):
# ECG Processing
x = self.pool1(self.activation(self.conv1(x)))
x = self.pool2(self.activation(self.conv2(x)))
x = self.pool3(self.activation(self.conv3(x)))
x = x.view(x.size(0),-1)
x = self.activation(self.fc0(x))
x = self.activation(self.fc1(x))
# Notes Processing
notes = notes.view(notes.size(0),-1)
notes = self.activation(self.fc_emb(notes))
x = self.fc2(self.norm(x + notes))
x = x.squeeze(1)
return x
class MMCNN_CAT(nn.Module):
def __init__(self, ecg_channels=12):
super(MMCNN_CAT, self).__init__()
# ECG processing Layers
self.name = "MMCNN_CAT"
self.conv1 = nn.Conv1d(ecg_channels, 16, 7)
self.pool1 = nn.MaxPool1d(2, 2)
self.conv2 = nn.Conv1d(16, 32, 5)
self.pool2 = nn.MaxPool1d(2, 2)
self.conv3 = nn.Conv1d(32, 48, 3)
self.pool3 = nn.MaxPool1d(2, 2)
self.fc0 = nn.Linear(5856, 512)
self.fc1 = nn.Linear(512, 128)
self.fc2 = nn.Linear(256, 5)
# Clinical Notes Processing Layers
self.fc_emb = nn.Linear(768, 128)
self.norm = nn.LayerNorm(128)
self.activation = nn.ReLU()
def forward(self, x, notes):
# ECG Processing
x = self.pool1(self.activation(self.conv1(x)))
x = self.pool2(self.activation(self.conv2(x)))
x = self.pool3(self.activation(self.conv3(x)))
x = x.view(x.size(0),-1)
x = self.activation(self.fc0(x))
x = self.activation(self.fc1(x))
# Notes Processing
notes = notes.view(notes.size(0),-1)
notes = self.activation(self.fc_emb(notes))
x = self.fc2(torch.cat((x,notes),dim=1))
x = x.squeeze(1)
return x
class MMCNN_ATT(nn.Module):
def __init__(self, ecg_channels=12):
super(MMCNN_ATT, self).__init__()
# ECG processing Layers
self.name = "MMCNN_ATT"
self.conv1 = nn.Conv1d(ecg_channels, 16, 7)
self.pool1 = nn.MaxPool1d(2, 2)
self.conv2 = nn.Conv1d(16, 32, 5)
self.pool2 = nn.MaxPool1d(2, 2)
self.conv3 = nn.Conv1d(32, 48, 3)
self.pool3 = nn.MaxPool1d(2, 2)
self.fc0 = nn.Linear(5856, 512)
self.fc1 = nn.Linear(512, 128)
self.fc2 = nn.Linear(128, 5)
# Clinical Notes Processing Layers
self.fc_emb = nn.Linear(768, 128)
self.norm1 = nn.LayerNorm(128)
self.norm2 = nn.LayerNorm(128)
self.attention = nn.MultiheadAttention(128, 8, batch_first=True)
self.activation = nn.ReLU()
def forward(self, x, notes):
# ECG Processing
x = self.pool1(self.activation(self.conv1(x)))
x = self.pool2(self.activation(self.conv2(x)))
x = self.pool3(self.activation(self.conv3(x)))
x = x.view(x.size(0),-1)
x = self.activation(self.fc0(x))
x = self.activation(self.fc1(x))
x = self.norm1(x)
# Notes Processing
notes = notes.view(notes.size(0),-1)
notes = self.activation(self.fc_emb(notes))
notes = self.norm2(notes)
notes=notes.unsqueeze(1)
x=x.unsqueeze(1)
x,_= self.attention(notes, x, x)
x = self.fc2(x)
x = x.squeeze(1)
return x
class MMCNN_SUM_ATT(nn.Module):
def __init__(self, ecg_channels=12):
super(MMCNN_SUM_ATT, self).__init__()
# ECG processing Layers
self.name = "MMCNN_SUM_ATT"
self.conv1 = nn.Conv1d(ecg_channels, 16, 7)
self.pool1 = nn.MaxPool1d(2, 2)
self.conv2 = nn.Conv1d(16, 32, 5)
self.pool2 = nn.MaxPool1d(2, 2)
self.conv3 = nn.Conv1d(32, 48, 3)
self.pool3 = nn.MaxPool1d(2, 2)
self.fc0 = nn.Linear(5856, 512)
self.fc1 = nn.Linear(512, 128)
self.fc2 = nn.Linear(128, 5)
# Clinical Notes Processing Layers
self.fc_emb = nn.Linear(768, 128)
self.norm = nn.LayerNorm(128)
self.attention = nn.MultiheadAttention(128, 8, batch_first=True)
self.activation = nn.ReLU()
def forward(self, x, notes):
# ECG Processing
x = self.pool1(self.activation(self.conv1(x)))
x = self.pool2(self.activation(self.conv2(x)))
x = self.pool3(self.activation(self.conv3(x)))
x = x.view(x.size(0),-1)
x = self.activation(self.fc0(x))
x = self.activation(self.fc1(x))
# Notes Processing
notes = notes.view(notes.size(0),-1)
notes = self.activation(self.fc_emb(notes))
x = self.norm(x + notes)
x=x.unsqueeze(1)
# print(x.shape)
x,_= self.attention(x, x, x)
x = self.fc2(x)
x = x.squeeze(1)
return x
if __name__ == "__main__":
model = CNN()
# model = Conv1d_layer(12, 16, 7)
summary(model, input_size = (1, 12, 1000))
|