-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
261 lines (209 loc) · 8.04 KB
/
train.py
File metadata and controls
261 lines (209 loc) · 8.04 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from pathlib import Path
import numpy as np
import pandas as pd
import modal
from torch.utils.data import DataLoader, Dataset
import torchaudio
import torch
import torch.nn as nn
import torchaudio.transforms as T
from model import AudioCNN
import torch.optim as optim
from torch.optim.lr_scheduler import OneCycleLR
from tqdm import tqdm
from torch.utils.tensorboard import SummaryWriter
app = modal.App("audio-cnn")
image = (
modal.Image.debian_slim()
.pip_install_from_requirements("requirements.txt")
.apt_install(["wget", "unzip", "ffmpeg", "libsndfile1"])
.run_commands(
[
"cd /tmp && wget https://github.com/karolpiczak/ESC-50/archive/master.zip -O esc50.zip",
"cd /tmp && unzip esc50.zip",
"mkdir -p /opt/esc50-data",
"cp -r /tmp/ESC-50-master/* /opt/esc50-data/",
"rm -rf /tmp/esc50.zip /tmp/ESC-50-master",
]
)
.add_local_python_source("model")
)
volume = modal.Volume.from_name("esc50-data", create_if_missing=True)
model_volume = modal.Volume.from_name("esc-model", create_if_missing=True)
class ESC50Dataset(Dataset):
def __init__(self, data_dir, metadata_file, split="train", transform=None):
super().__init__()
self.data_dir = Path(data_dir)
self.metadata = pd.read_csv(metadata_file)
self.split = split
self.transform = transform
if split == "train":
self.metadata = self.metadata[self.metadata["fold"] != 5]
else:
self.metadata = self.metadata[self.metadata["fold"] == 5]
self.classes = sorted(self.metadata["category"].unique())
self.class_to_idx = {cls: idx for idx, cls in enumerate(self.classes)}
self.metadata["label"] = self.metadata["category"].map(self.class_to_idx)
def __len__(self):
return len(self.metadata)
def __getitem__(self, idx):
row = self.metadata.iloc[idx]
audio_path = self.data_dir / "audio" / row["filename"]
waveform, sample_rate = torchaudio.load(audio_path)
# waveforms - [channels, samples] - for 2 channela, take mean
if waveform.shape[0] > 1:
waveform = torch.mean(waveform, dim=0, keepdim=True)
if self.transform:
spectogram = self.transform(waveform)
else:
spectogram = waveform
return spectogram, row["label"]
def mixup_data(x, y):
# blending percentage
lam = np.random.beta(0.2, 0.2)
batch_size = x.size(0)
index = torch.randperm(batch_size).to(x.device)
# data mixing in percentages
mixed_x = lam * x + (1 - lam) * x[index, :]
y_a, y_b = y, y[index]
return mixed_x, y_a, y_b, lam
def mixup_criterion(criterion, pred, y_a, y_b, lam):
# error as if pred was 100% a and multiply by blending pct
# then same for b
return lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)
@app.function(
image=image,
gpu="A10G",
volumes={"/data": volume, "/models": model_volume},
timeout=60 * 60 * 3,
)
def train():
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_dir = f"/models/tensorboard_logs/run_{timestamp}"
writer = SummaryWriter(log_dir)
esc50_dir = Path("/opt/esc50-data")
train_transform = nn.Sequential(
T.MelSpectrogram(
sample_rate=44100,
n_fft=1024,
hop_length=512,
n_mels=128,
f_min=0,
f_max=11025,
),
T.AmplitudeToDB(),
# similar to Dropout for Audio
T.FrequencyMasking(freq_mask_param=30),
T.TimeMasking(time_mask_param=80),
)
val_transform = nn.Sequential(
T.MelSpectrogram(
sample_rate=44100,
n_fft=1024,
hop_length=512,
n_mels=128,
f_min=0,
f_max=11025,
),
T.AmplitudeToDB(),
)
train_dataset = ESC50Dataset(
data_dir=esc50_dir,
metadata_file=esc50_dir / "meta" / "esc50.csv",
split="train",
transform=train_transform,
)
val_dataset = ESC50Dataset(
data_dir=esc50_dir,
metadata_file=esc50_dir / "meta" / "esc50.csv",
split="test",
transform=val_transform,
)
print(f"training samples: {len(train_dataset)}")
print(f"validating samples: {len(val_dataset)}")
train_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_dataloader = DataLoader(val_dataset, batch_size=32, shuffle=False)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AudioCNN(num_classes=len(train_dataset.classes))
model.to(device)
num_epochs = 100
# loss function
# labels (one hot) are 1 and 0,0,0...
# we want to make this smooth -> 0.9, 0.025,0.025....
# label smoothing - makes less certain in predictions
criterion = nn.CrossEntropyLoss(label_smoothing=0.1)
optimizer = optim.AdamW(model.parameters(), lr=0.0005, weight_decay=0.01)
scheduler = OneCycleLR(
optimizer,
max_lr=0.002,
epochs=num_epochs,
steps_per_epoch=len(train_dataloader),
pct_start=0.1, # 10% of training is spent increasing lr, and rest decr.
)
best_accuracy = 0.0
print("starting trainign")
for epoch in range(num_epochs):
model.train()
epoch_loss = 0.0
progress_bar = tqdm(train_dataloader, desc=f"Epoch {epoch+1}/{num_epochs}")
for data, target in progress_bar:
data, target = data.to(device), target.to(device)
# synthetic samples to lower confidence, increase reliability
# mix two sounds - data mixing - acts as background noise
if np.random.random() > 0.7:
data, target_a, target_b, lam = mixup_data(data, target)
output = model(data)
loss = mixup_criterion(criterion, output, target_a, target_b, lam)
else: # ~70% of time no data mixing
output = model(data)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
epoch_loss += loss.item()
progress_bar.set_postfix({"Loss: ": f"{loss.item():.4f}"})
avg_epoch_loss = epoch_loss / len(train_dataloader)
writer.add_scalar("Loss/Train", avg_epoch_loss, epoch)
writer.add_scalar("Learning_Rate", optimizer.param_groups[0]["lr"], epoch)
# Validate after each epoch
model.eval()
correct = 0
total = 0
val_loss = 0
with torch.no_grad():
for data, target in val_dataloader:
data, target = data.to(device), target.to(device)
outputs = model(data)
loss = criterion(outputs, target)
val_loss += loss.item()
# pick highest scored class as pred
_, predicted = torch.max(outputs.data, 1)
total += target.size(0) # add batch size to running total
correct += (predicted == target).sum().item()
accuracy = correct / total * 100
avg_val_loss = val_loss / len(val_dataloader)
writer.add_scalar("Loss/Validation", avg_val_loss, epoch)
writer.add_scalar("Accuracy/Validation", accuracy, epoch)
print(
f"Epoch: {epoch+1} | Loss: {avg_epoch_loss:.4f} | Val Loss: {avg_val_loss:.4f} | Accuracy: {accuracy:.2f}%"
)
# best_accuracy = max(accuracy, best_accuracy)
if accuracy > best_accuracy:
best_accuracy = accuracy
torch.save(
{
"model_state_dict": model.state_dict(),
"accuracy": accuracy,
"epoch": epoch,
"classes": train_dataset.classes,
},
"/models/best_model.pth",
)
print(f"New best model saved: {accuracy:.2f}%")
writer.close()
print(f"Training Completed. Best accuracy {best_accuracy:.2f}%")
@app.local_entrypoint()
def main():
train.remote()