-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNN_finishedpython.py
More file actions
211 lines (168 loc) · 6.07 KB
/
Copy pathCNN_finishedpython.py
File metadata and controls
211 lines (168 loc) · 6.07 KB
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
# IMPORTS:
import numpy as np
import os
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset, DataLoader, TensorDataset
import matplotlib.pyplot as plt
from scipy.signal import detrend
import pandas as pd
from torch.utils.data import random_split
from sklearn.preprocessing import MinMaxScaler
# MY DATASET.CLASS:
class ECGData(Dataset):
def __init__(self):
rwma_labels = np.loadtxt(
"/home/ngsci/datasets/silent-cchs-ecg/csv/rwma-outcomes.csv",
delimiter=",",
dtype=np.float32,
skiprows=1,
)
npy_filepath = "/home/ngsci/datasets/silent-cchs-ecg/npy"
dir_list = os.listdir(npy_filepath)
npy_arrays = []
for each in dir_list:
file = f"{npy_filepath}/{each}"
npy_arrays.append(np.load(file))
stacked = np.stack(npy_arrays, axis=1)
self.X = torch.from_numpy(stacked)
self.y = torch.from_numpy(rwma_labels[:, 1:])
def __len__(self):
return self.X.shape[0]
def __getitem__(self, idx):
return (self.X[idx, :, :, :], self.y[idx])
# DATA SPLIT / PREPROCESS / PREPARE / INITIATE...
data = ECGData() #instanz of above Custom Class
### DATA SPLIT FUNCKTION and INIT.
def split_data(data):
indexRange = torch.arange(0, len(data))
train_size = int(0.8 * len(data))
train_split, test_split = random_split(
indexRange, [train_size, len(data) - train_size]
)
X_train = data[train_split][0].squeeze(dim=2)
X_test = data[test_split][0].squeeze(dim=2)
y_train = data[train_split][1]
y_test = data[test_split][1]
return X_train, X_test, y_train, y_test
X_train, X_test, y_train, y_test = split_data(data)
## Data NORMALIZATION
##
def preprocess_data(X_train, X_test):
# Min-max scaling
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(
X_train.reshape(-1, X_train.shape[-1])
).reshape(X_train.shape)
X_test_scaled = scaler.transform(X_test.reshape(-1, X_test.shape[-1])).reshape(
X_test.shape
)
return X_train_scaled, X_test_scaled
X_train_preprocessed, X_test_preprocessed = preprocess_data(X_train, X_test)
X_train = torch.tensor(
X_train_preprocessed[:, :, :5000], dtype=torch.float32, requires_grad=False
)
X_test = torch.tensor(
X_test_preprocessed[:, :, :5000], dtype=torch.float32, requires_grad=False
)
y_train = torch.tensor(y_train[:5000], requires_grad=False)
y_test = torch.tensor(y_test[:5000], requires_grad=False)
train_dataset = TensorDataset(X_train, y_train)
test_dataset = TensorDataset(X_test, y_test)
train_loader = DataLoader(train_dataset, batch_size=1, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False)
for inputs, targets in train_loader:
print(inputs, targets)
break
# CNN MODEL CLASS & Instance INIT.:
import torch.optim as optim
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self).__init__()
self.conv1 = nn.Conv1d(12, 32, kernel_size=3)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool1d(kernel_size=2)
self.flatten = nn.Flatten()
self.fc1 = nn.Linear(32 * 2499, 50)
self.fc2 = nn.Linear(50, 50)
self.fc3 = nn.Linear(50, 50)
self.fc4 = nn.Linear(50, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.flatten(x)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
x = self.relu(x)
x = self.fc4(x)
x = self.sigmoid(x)
return x
# Create an instance of the model
cnn_model = CNNModel()
from sklearn.metrics import confusion_matrix
def accuracy(outputs, targets):
predicted = (outputs > 0.5).float()
correct = (predicted == targets).float()
acc = correct.sum() / len(targets)
return acc.item()
# Modify the target labels to binary values using a threshold
threshold = 0.5
y_train_binary = (y_train >= threshold).float()
y_test_binary = (y_test >= threshold).float()
# Define the binary cross-entropy loss
criterion = nn.BCELoss()
# Define the optimizer
optimizer = optim.Adam(cnn_model.parameters(), lr=0.001)
# Training loop
num_epochs = 5
train_loss_history = []
val_loss_history = []
train_acc_history = []
val_acc_history = []
for epoch in range(num_epochs):
# Training
cnn_model.train()
train_loss = 0
train_correct = 0
total = 0
for inputs, targets in train_loader:
optimizer.zero_grad()
outputs = cnn_model(inputs)
loss = criterion(outputs.squeeze(), targets.squeeze())
loss.backward()
optimizer.step()
# Compute training accuracy
predicted = (outputs >= threshold).float()
train_correct += (predicted.squeeze() == targets.squeeze()).sum().item()
total += targets.size(0)
train_loss += loss.item()
train_acc = train_correct / total
train_loss_history.append(train_loss / len(train_loader))
train_acc_history.append(train_acc)
# Validation
cnn_model.eval()
val_loss = 0
val_correct = 0
total = 0
for inputs, targets in test_loader:
outputs = cnn_model(inputs)
loss = criterion(outputs.squeeze(), targets.squeeze())
# Compute validation accuracy
predicted = (outputs >= threshold).float()
val_correct += (predicted.squeeze() == targets.squeeze()).sum().item()
total += targets.size(0)
val_loss += loss.item()
val_acc = val_correct / total
val_loss_history.append(val_loss / len(test_loader))
val_acc_history.append(val_acc)
print(f"Epoch [{epoch+1}/{num_epochs}], Train Loss: {train_loss_history[-1]:.4f}, Train Acc: {train_acc:.4f}")
print(f"Epoch [{epoch+1}/{num_epochs}], Val Loss: {val_loss_history[-1]:.4f}, Val Acc: {val_acc:.4f}")
print("Confusion matrix:")
print(confusion_matrix(y_test_binary, (cnn_model(X_test) >= threshold).float()))