-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmain.py
172 lines (151 loc) · 5 KB
/
main.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
import argparse
import pytorch_lightning as pl
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from pytorch_lightning import Trainer
from pytorch_lightning.loggers import TensorBoardLogger
from torch.utils.data import DataLoader
# Audio Augmentations
from torchaudio_augmentations import (
RandomApply,
ComposeMany,
RandomResizedCrop,
PolarityInversion,
Noise,
Gain,
HighLowPass,
Delay,
PitchShift,
Reverb,
)
from clmr.data import ContrastiveDataset
from clmr.datasets import get_dataset
from clmr.evaluation import evaluate
from clmr.models import SampleCNN
from clmr.modules import ContrastiveLearning, SupervisedLearning
from clmr.utils import yaml_config_hook
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="CLMR")
parser = Trainer.add_argparse_args(parser)
config = yaml_config_hook("./config/config.yaml")
for k, v in config.items():
parser.add_argument(f"--{k}", default=v, type=type(v))
args = parser.parse_args()
pl.seed_everything(args.seed)
# ------------
# data augmentations
# ------------
if args.supervised:
train_transform = [RandomResizedCrop(n_samples=args.audio_length)]
num_augmented_samples = 1
else:
train_transform = [
RandomResizedCrop(n_samples=args.audio_length),
RandomApply([PolarityInversion()], p=args.transforms_polarity),
RandomApply([Noise()], p=args.transforms_noise),
RandomApply([Gain()], p=args.transforms_gain),
RandomApply(
[HighLowPass(sample_rate=args.sample_rate)], p=args.transforms_filters
),
RandomApply([Delay(sample_rate=args.sample_rate)], p=args.transforms_delay),
RandomApply(
[
PitchShift(
n_samples=args.audio_length,
sample_rate=args.sample_rate,
)
],
p=args.transforms_pitch,
),
RandomApply(
[Reverb(sample_rate=args.sample_rate)], p=args.transforms_reverb
),
]
num_augmented_samples = 2
# ------------
# dataloaders
# ------------
train_dataset = get_dataset(args.dataset, args.dataset_dir, subset="train")
valid_dataset = get_dataset(args.dataset, args.dataset_dir, subset="valid")
contrastive_train_dataset = ContrastiveDataset(
train_dataset,
input_shape=(1, args.audio_length),
transform=ComposeMany(
train_transform, num_augmented_samples=num_augmented_samples
),
)
contrastive_valid_dataset = ContrastiveDataset(
valid_dataset,
input_shape=(1, args.audio_length),
transform=ComposeMany(
train_transform, num_augmented_samples=num_augmented_samples
),
)
train_loader = DataLoader(
contrastive_train_dataset,
batch_size=args.batch_size,
num_workers=args.workers,
drop_last=True,
shuffle=True,
)
valid_loader = DataLoader(
contrastive_valid_dataset,
batch_size=args.batch_size,
num_workers=args.workers,
drop_last=True,
shuffle=False,
)
# ------------
# encoder
# ------------
encoder = SampleCNN(
strides=[3, 3, 3, 3, 3, 3, 3, 3, 3],
supervised=args.supervised,
out_dim=train_dataset.n_classes,
)
# ------------
# model
# ------------
if args.supervised:
module = SupervisedLearning(args, encoder, output_dim=train_dataset.n_classes)
else:
module = ContrastiveLearning(args, encoder)
logger = TensorBoardLogger("runs", name="CLMRv2-{}".format(args.dataset))
if args.checkpoint_path:
module = module.load_from_checkpoint(
args.checkpoint_path, encoder=encoder, output_dim=train_dataset.n_classes
)
else:
# ------------
# training
# ------------
if args.supervised:
early_stopping = EarlyStopping(monitor="Valid/loss", patience=20)
else:
early_stopping = None
trainer = Trainer.from_argparse_args(
args,
logger=logger,
sync_batchnorm=True,
max_epochs=args.max_epochs,
log_every_n_steps=10,
check_val_every_n_epoch=1,
accelerator=args.accelerator,
)
trainer.fit(module, train_loader, valid_loader)
if args.supervised:
test_dataset = get_dataset(args.dataset, args.dataset_dir, subset="test")
contrastive_test_dataset = ContrastiveDataset(
test_dataset,
input_shape=(1, args.audio_length),
transform=None,
)
device = "cuda:0" if args.gpus else "cpu"
results = evaluate(
module.encoder,
None,
contrastive_test_dataset,
args.dataset,
args.audio_length,
device=device,
)
print(results)