-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_conv.py
72 lines (59 loc) · 2.13 KB
/
train_conv.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
import os
import torch
import torch.nn as nn
from torch.utils.data import BatchSampler, RandomSampler
from torch.optim import Adam
from torch.utils.tensorboard import SummaryWriter
from songnet.audio.loader import AudioLoader
from songnet.models.convolution.waveconv import WaveConvolution
AUDIO_DIR = "data/converted"
FREQ = 44100
BATCH_SIZE = 8
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def sample(src, target):
for idx in BatchSampler(RandomSampler(range(src.size(1))), BATCH_SIZE, False):
yield src[:,idx], target[:,idx]
def sample_simple(src):
for idx in BatchSampler(RandomSampler(range(len(src))), BATCH_SIZE, False):
yield [src[i] for i in idx]
def pad(x):
padd = nn.ConstantPad1d((0,44100*360-x.size(1)), 0)
return padd(x)
def main():
writer = SummaryWriter("runs/exp1")
net = WaveConvolution().to(DEVICE)
optimizer = Adam(net.parameters())
mse = torch.nn.SmoothL1Loss()
files = os.listdir(AUDIO_DIR)
for epoch in range(10):
avg_epoch_loss = 0
waveforms = []
batches = 0
for fil in sample_simple(files):
waveforms = []
for f in fil:
f = f"{AUDIO_DIR}/{f}"
# print(torchaudio.info(f))
loader = AudioLoader()
waveform = loader.load_resample(f, FREQ)
# print(audio)
waveform = pad(waveform)
waveforms.append(waveform)
# print(seq.size(), target.size())
# target = target[1:]
# print(target)
avg_loss = 0
wave = torch.stack(waveforms, dim=0).to(DEVICE)
print(wave.size())
output = net(wave)
avg_loss = mse(output, wave)
print(f"Epoch: {epoch}, Batch: {batches}, Loss: {avg_loss.item()}")
avg_loss.backward()
optimizer.step()
avg_epoch_loss += avg_loss.item()
batches += 1
avg_epoch_loss /= batches
writer.add_scalar("Loss/Train: ", avg_epoch_loss, epoch)
# loader = AudioLoader("")
if __name__ == "__main__":
main()