baseline.py 3.3 KB
Newer Older
Verena Praher's avatar
Verena Praher committed
1
2
3
from utils import *
import pytorch_lightning as pl

4
5
from models.shared_stuff import *

6
from sklearn import metrics
Verena Praher's avatar
Verena Praher committed
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

# TODO pr-auc
# TODO f1-score

class CNN(pl.LightningModule):
    def __init__(self, num_class):
        super(CNN, self).__init__()

        # init bn
        self.bn_init = nn.BatchNorm2d(1)

        # layer 1
        self.conv_1 = nn.Conv2d(1, 64, 3, padding=1)
        self.bn_1 = nn.BatchNorm2d(64)
        self.mp_1 = nn.MaxPool2d((2, 4))

        # layer 2
        self.conv_2 = nn.Conv2d(64, 128, 3, padding=1)
        self.bn_2 = nn.BatchNorm2d(128)
        self.mp_2 = nn.MaxPool2d((2, 4))

        # layer 3
        self.conv_3 = nn.Conv2d(128, 128, 3, padding=1)
        self.bn_3 = nn.BatchNorm2d(128)
        self.mp_3 = nn.MaxPool2d((2, 4))

        # layer 4
        self.conv_4 = nn.Conv2d(128, 128, 3, padding=1)
        self.bn_4 = nn.BatchNorm2d(128)
        self.mp_4 = nn.MaxPool2d((3, 5))

        # layer 5
        self.conv_5 = nn.Conv2d(128, 64, 3, padding=1)
        self.bn_5 = nn.BatchNorm2d(64)
        self.mp_5 = nn.MaxPool2d((4, 4))

        # classifier
44
        self.dense = nn.Linear(320, num_class)
Verena Praher's avatar
Verena Praher committed
45
46
47
        self.dropout = nn.Dropout(0.5)

    def forward(self, x):
48
        # x = x.unsqueeze(1)
Verena Praher's avatar
Verena Praher committed
49

50
        x = x[:, :, :, :512]
Verena Praher's avatar
Verena Praher committed
51
52
        # init bn
        x = self.bn_init(x)
53
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
54
55
        # layer 1
        x = self.mp_1(nn.ELU()(self.bn_1(self.conv_1(x))))
56
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
57
58

        # layer 2
59
60
61
        x = nn.ELU()(self.bn_2(self.conv_2(x)))
        # x = self.mp_2(nn.ELU()(self.bn_2(self.conv_2(x))))
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
62
63
64

        # layer 3
        x = self.mp_3(nn.ELU()(self.bn_3(self.conv_3(x))))
65
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
66
67

        # layer 4
68
        # x = nn.ELU()(self.bn_4(self.conv_4(x)))
Verena Praher's avatar
Verena Praher committed
69
        x = self.mp_4(nn.ELU()(self.bn_4(self.conv_4(x))))
70
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
71
72
73

        # layer 5
        x = self.mp_5(nn.ELU()(self.bn_5(self.conv_5(x))))
74
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
75
76
77

        # classifier
        x = x.view(x.size(0), -1)
78
        # print("Lin input", x.shape)
Verena Praher's avatar
Verena Praher committed
79
80
        x = self.dropout(x)
        logit = nn.Sigmoid()(self.dense(x))
81
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
82
83
84
85

        return logit

    def my_loss(self, y_hat, y):
86
        return my_loss(y_hat, y)
87

88
89
90
91
92
93
94
95
    def training_step(self, data_batch, batch_nb):
        x, _, y = data_batch
        y_hat = self.forward_full_song(x, y)
        y = y.float()
        y_hat = y_hat.float()
        return {'loss':self.my_loss(y_hat, y)}

    def validation_step(self, data_batch, batch_nb):
Verena Praher's avatar
Verena Praher committed
96
        return validation_step(self, data_batch, batch_nb)
97
98

    def test_step(self, data_batch, batch_nb):
Verena Praher's avatar
Verena Praher committed
99
        return test_step(self, data_batch, batch_nb)
100
101
102
103
104

    def test_end(self, outputs):
        test_metrics = test_end(outputs)
        self.experiment.log(test_metrics)
        return test_metrics
105
106

    def validation_end(self, outputs):
107
        return validation_end(outputs)
108

109
    def configure_optimizers(self):
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
110
111
112
113
        return [torch.optim.Adam(self.parameters(), lr=1e-4)]  # from their code

    @pl.data_loader
    def tng_dataloader(self):
114
        return tng_dataloader()
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
115
116
117

    @pl.data_loader
    def val_dataloader(self):
118
        return val_dataloader()
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
119
120
121

    @pl.data_loader
    def test_dataloader(self):
122
        return test_dataloader()
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
123
124

    @staticmethod
125
    def add_model_specific_args(parent_parser):
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
126
127
        return parent_parser
        pass