forked from jingyonghou/Audiomer-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiments.py
219 lines (181 loc) · 6.84 KB
/
experiments.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
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
211
212
213
214
215
216
217
218
219
from einops import rearrange
from datamodules import *
from Audiomer import AudiomerClassification
from functools import partial
import pytorch_lightning as pl
from argparse import ArgumentParser
import numpy as np
import torch.nn.functional as F
import torch
import torchmetrics
def single_label_cross_entropy(y_pred, y):
return F.nll_loss(y_pred, y)
class PartialModule(torch.nn.Module):
def __init__(self, func, **kwargs):
super().__init__()
self.func = func
self.kwargs = kwargs
def forward(self, x):
return self.func(x, **self.kwargs)
class Experiment(pl.LightningModule):
def __init__(self, **kwargs):
super().__init__()
self.model_params = dict(
expansion_factor=2,
mlp_dropout=0.2,
num_heads=2,
depth=1,
dim_head=32,
use_residual=kwargs['no_residual'],
use_attention=kwargs['no_attention'],
equal_strides=kwargs['unequal_strides'],
use_se=kwargs['no_se'],
)
self.activation_function = torch.nn.Identity()
self.model_params['input_size'] = 8192
self.loss_fn = single_label_cross_entropy
self.activation_function = PartialModule(torch.log_softmax, dim=-1)
self.model_params['pool'] = 'cls'
# networks
if kwargs['dataset'] == 'SC35':
self.model_params['num_classes'] = 35
elif kwargs['dataset'] == 'SC12':
self.model_params['num_classes'] = 12
elif kwargs['dataset'] == 'SC20':
self.model_params['num_classes'] = 20
if kwargs['model'] == "L":
self.model_params['config'] = [1, 4, 8, 16, 16, 32, 32, 64, 64, 96, 96, 192]
elif kwargs['model'] == "M":
self.model_params['config'] = [1, 4, 8, 16, 16, 32, 64, 128]
elif kwargs['model'] == "S":
self.model_params['config'] = [1, 4, 8, 8, 16, 16, 32, 32, 64, 64]
self.model_params['kernel_sizes'] = [5] * \
(len(self.model_params['config'])-1)
self.model_params['mlp_dim'] = self.model_params['config'][-1]
self.model = AudiomerClassification(**self.model_params)
self.train_acc = torchmetrics.Accuracy(num_classes=self.model_params['num_classes'])
self.val_acc = torchmetrics.Accuracy(num_classes=self.model_params['num_classes'])
self.test_acc = torchmetrics.Accuracy(num_classes=self.model_params['num_classes'])
# makes self.hparams under the hood and saves to ckpt
for k, v in self.model_params.items():
self.hparams[k] = v
print(self.model_params)
self.save_hyperparameters()
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, y = batch
y_pred = self.activation_function(self(x))
loss = self.loss_fn(y_pred, y)
self.train_acc(y_pred.argmax(-1), y)
self.log("ACC/train", self.train_acc, prog_bar=True)
self.log("LOSS/train", loss)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_pred = self.activation_function(self(x))
loss = self.loss_fn(y_pred, y)
self.val_acc(y_pred.argmax(-1), y)
self.log("ACC/val", self.val_acc, prog_bar=True)
self.log("LOSS/val", loss, prog_bar=True)
return loss
def test_step(self, batch, batch_idx):
x, y = batch
y_pred = self.activation_function(self(x))
loss = self.loss_fn(y_pred, y)
self.test_acc(y_pred.argmax(-1), y)
self.log("ACC/test", self.test_acc, prog_bar=True)
self.log("LOSS/test", loss, prog_bar=True)
return loss
def configure_optimizers(self):
lr = self.hparams.learning_rate
opt = torch.optim.Adam(self.model.parameters(),
lr=lr)
schedule = torch.optim.lr_scheduler.CosineAnnealingLR(
opt, T_max=320)
return [opt], [schedule]
@ staticmethod
def add_model_specific_args(parent_parser):
parser = ArgumentParser(parents=[parent_parser], add_help=False)
parser.add_argument(
"--learning_rate", type=float, default=0.005, help="adam: learning rate"
)
parser.add_argument(
"--batch_size",
type=int,
default=32,
help="Batch size",
)
parser.add_argument(
"--num_workers",
type=int,
default=12,
help="num_workers",
)
parser.add_argument(
"--pin_memory",
type=bool,
default=False,
help="Pin memory",
)
return parser
from pytorch_lightning import loggers
from pytorch_lightning.callbacks import ModelCheckpoint
def cli_main(args=None):
parser = ArgumentParser()
parser.add_argument("--dataset", required=True, choices=["SC35", "SC20", "SC12"],
help="SC35, SC12, SC20")
parser.add_argument("--model", required=True, choices=["L", "M", "S"],
help="L, M, S")
parser.add_argument("--logdir", required=True, type=str)
parser.add_argument(
"--no_se",
default=True,
action='store_false',
help="Whether to use squeeze excitation or not",
)
parser.add_argument(
"--unequal_strides",
default=False,
action='store_true',
help="Whether to use equal strides or not",
)
parser.add_argument(
"--no_attention",
default=True,
action='store_false',
help="Whether to use performer attention or not",
)
parser.add_argument(
"--no_residual",
default=True,
action='store_false',
help="Whether to use residual connections or not",
)
script_args, _ = parser.parse_known_args(args)
if script_args.dataset == "SC35":
dm_cls = SpeechCommands35DataModule
elif script_args.dataset == "SC12":
dm_cls = SpeechCommands12DataModule
elif script_args.dataset == "SC20":
dm_cls = SpeechCommands20DataModule
parser = dm_cls.add_argparse_args(parser)
parser = pl.Trainer.add_argparse_args(parser)
parser = Experiment.add_model_specific_args(parser)
args, _ = parser.parse_known_args(args)
print(args)
dm = dm_cls(batch_size=args.batch_size, num_workers=args.num_workers,
pin_memory=args.pin_memory, augmentation=True)
model = Experiment(**vars(args))
logger = loggers.TensorBoardLogger(args.logdir)
checkpointer = ModelCheckpoint(monitor="LOSS/val", mode="min", save_top_k=10)
trainer = pl.Trainer.from_argparse_args(
args, max_epochs=300,
precision=16,
stochastic_weight_avg=True,
logger=logger,
callbacks=[checkpointer]
)
trainer.fit(model, dm) # dont test when training
if __name__ == "__main__":
cli_main()