baseline.py 2.97 KB
Newer Older
Verena Praher's avatar
Verena Praher committed
1
2
3
import torch.nn as nn

from utils import *
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
4
from datasets.datasets import HDF5Dataset
Verena Praher's avatar
Verena Praher committed
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import pytorch_lightning as pl

from sklearn.metrics import roc_auc_score

# 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
        self.dense = nn.Linear(64, num_class)
        self.dropout = nn.Dropout(0.5)

    def forward(self, x):
        x = x.unsqueeze(1)

        # init bn
        x = self.bn_init(x)

        # layer 1
        x = self.mp_1(nn.ELU()(self.bn_1(self.conv_1(x))))

        # layer 2
        x = self.mp_2(nn.ELU()(self.bn_2(self.conv_2(x))))

        # layer 3
        x = self.mp_3(nn.ELU()(self.bn_3(self.conv_3(x))))

        # layer 4
        x = self.mp_4(nn.ELU()(self.bn_4(self.conv_4(x))))

        # layer 5
        x = self.mp_5(nn.ELU()(self.bn_5(self.conv_5(x))))

        # classifier
        x = x.view(x.size(0), -1)
        x = self.dropout(x)
        logit = nn.Sigmoid()(self.dense(x))

        return logit

    def my_loss(self, y_hat, y):
81
82
83
        return F.binary_cross_entropy(y_hat, y)

    def configure_optimizers(self):
Shreyan Chowdhury's avatar
Shreyan Chowdhury committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        return [torch.optim.Adam(self.parameters(), lr=1e-4)]  # from their code

    @pl.data_loader
    def tng_dataloader(self):
        trainset = HDF5Dataset(os.path.join(PATH_MELSPEC_DOWNLOADED_HDF5, 'train.h5'), recursive=False, load_data=False)
        return DataLoader(dataset=trainset, batch_size=32, shuffle=True)

    @pl.data_loader
    def val_dataloader(self):
        validationset = HDF5Dataset(os.path.join(PATH_MELSPEC_DOWNLOADED_HDF5, 'val.h5'), recursive=False, load_data=False)
        return DataLoader(dataset=validationset, batch_size=128, shuffle=True)

    @pl.data_loader
    def test_dataloader(self):
        testset = HDF5Dataset(os.path.join(PATH_MELSPEC_DOWNLOADED_HDF5, 'test.h5'), recursive=False, load_data=False)
        return DataLoader(dataset=testset, batch_size=32, shuffle=True)

    @staticmethod
    def add_model_specific_args(parent_parser, root_dir):
        return parent_parser
        pass