baseline.py 3.7 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
51

        # init bn
        x = self.bn_init(x)
52
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
53
54
        # layer 1
        x = self.mp_1(nn.ELU()(self.bn_1(self.conv_1(x))))
55
        # print(x.shape)
Verena Praher's avatar
Verena Praher committed
56
57

        # layer 2
58
59
60
        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
61
62
63

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

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

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

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

        return logit

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

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    def forward_full_song(self, x, y):
        # print(x.shape)
        #TODO full song???
        return self.forward(x[:, :, :, :512])
        # y_hat = torch.zeros((x.shape[0], 56), requires_grad=True).cuda()
        # hop_size = 256
        # i=0
        # count = 0
        # while i < x.shape[-1]:
        #     y_hat += self.forward(x[:,:,:,i:i+512])
        #     i += hop_size
        #     count += 1
        # return y_hat/count

    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
109
        return validation_step(self, data_batch, batch_nb)
110
111

    def test_step(self, data_batch, batch_nb):
Verena Praher's avatar
Verena Praher committed
112
        return test_step(self, data_batch, batch_nb)
113
114
115
116
117

    def test_end(self, outputs):
        test_metrics = test_end(outputs)
        self.experiment.log(test_metrics)
        return test_metrics
118
119

    def validation_end(self, outputs):
120
        return validation_end(outputs)
121

122
    def configure_optimizers(self):
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
123
124
125
126
        return [torch.optim.Adam(self.parameters(), lr=1e-4)]  # from their code

    @pl.data_loader
    def tng_dataloader(self):
127
        return tng_dataloader()
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
128
129
130

    @pl.data_loader
    def val_dataloader(self):
131
        return val_dataloader()
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
132
133
134

    @pl.data_loader
    def test_dataloader(self):
135
        return test_dataloader()
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
136
137

    @staticmethod
138
    def add_model_specific_args(parent_parser):
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
139
140
        return parent_parser
        pass