-
Notifications
You must be signed in to change notification settings - Fork 0
/
datamodules.py
124 lines (90 loc) · 3.48 KB
/
datamodules.py
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
import numpy as np
import pandas as pd
import pytorch_lightning as pl
import torch
from torch.utils.data import Dataset
from sklearn.metrics import confusion_matrix
from torch import nn, optim
from utils import recall_score
class ECGDataset(Dataset):
def __init__(self, X, y) -> None:
super().__init__()
self.X = X
self.y = y
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
return self.X[idx].astype(np.float32), self.y[idx]
class Net(pl.LightningModule):
def __init__(self, input_layer, model, lr):
super().__init__()
print("Making Model...")
self.input = input_layer
self.model = model
self.criterion = nn.CrossEntropyLoss()
self.optimizer = optim.Adam(
self.model.parameters(),
lr=lr,
betas=(0.9, 0.999),
amsgrad=True,
)
self.cm = np.zeros([4, 4])
def forward(self, input):
x = self.input(input)
output = self.model(x)
return output
def loss_fn(self, pred, label):
pred = pred.reshape(pred.shape[0], -1)
label = label.view(-1).to(torch.int64)
loss = self.criterion(pred, label)
return loss
def training_step(self, batch, batch_idx):
input, label = batch
pred = self.forward(input)
loss = self.loss_fn(pred, label)
self.log("loss", loss)
return {"loss": loss}
def validation_step(self, batch, batch_idx):
input, label = batch
batch_size = input.shape[0]
label = label.to("cpu")
pred = self.forward(input)
pred = torch.argmax(pred, dim=1).to("cpu")
pred = pred.squeeze(-1)
acc = (pred == label).sum().item() / batch_size
pred = pred.view(-1)
label = label.view(-1)
recall = recall_score(label, pred, average="macro").tolist()
return {"acc": acc, "recall": recall}
def validation_epoch_end(self, outputs):
ave_acc = torch.tensor([x["acc"] for x in outputs]).to(torch.float).mean()
ave_recall = torch.tensor([x["recall"] for x in outputs]).to(torch.float).mean()
self.log("acc", ave_acc)
self.log("recall", ave_recall)
self.log("lr", self.optimizer.param_groups[0]["lr"])
return {"acc": ave_acc}
def test_step(self, batch, batch_idx):
input, label = batch
batch_size = input.shape[0]
label = label.to("cpu")
pred = self.forward(input)
pred = torch.argmax(pred, dim=1).to("cpu")
pred = pred.squeeze(-1)
acc = (pred == label).sum().item() / batch_size
pred = pred.view(-1)
label = label.view(-1)
recall = recall_score(label, pred, average="macro").tolist()
label_name = list(range(4))
self.cm += confusion_matrix(label, pred, labels=label_name)
self.log("test_acc", acc)
self.log("test_recall", recall)
def test_epoch_end(self, outputs) -> None:
cm = self.cm
label_name = ["N", "V", "S", "F"]
columns_labels = ["pred_" + l_n for l_n in label_name]
index_labels = ["act_" + l_n for l_n in label_name]
cm = pd.DataFrame(cm, columns=columns_labels, index=index_labels)
print(cm.to_markdown())
return super().test_epoch_end(outputs)
def configure_optimizers(self):
return [self.optimizer]