62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
if __name__ == '__main__':
|
|
import numpy as np
|
|
from tqdm import tqdm
|
|
from cfg import *
|
|
from mimii import MIMII
|
|
from models.ae import AE, SubSpecCAE
|
|
import torch.nn as nn
|
|
import torch.optim as optim
|
|
import random
|
|
from models.layers import Subspectrogram
|
|
|
|
torch.manual_seed(42)
|
|
torch.cuda.manual_seed(42)
|
|
np.random.seed(42)
|
|
random.seed(42)
|
|
|
|
dataset_path = ALL_DATASET_PATHS[5]
|
|
print(f'Training on {dataset_path.name}')
|
|
mimii = MIMII(dataset_path=dataset_path, machine_id=0)
|
|
mimii.to(DEVICE)
|
|
#mimii.preprocess(n_fft=1024, hop_length=256, n_mels=80, center=False, power=2.0) # 80 x 80
|
|
tfms = Subspectrogram(SUB_SPEC_HEIGT, SUB_SPEC_HOP)
|
|
|
|
dl = mimii.train_dataloader(
|
|
segment_len=NUM_SEGMENTS,
|
|
hop_len=NUM_SEGMENT_HOPS,
|
|
batch_size=BATCH_SIZE,
|
|
num_workers=NUM_WORKERS,
|
|
shuffle=True,
|
|
transform=tfms
|
|
)
|
|
|
|
model = SubSpecCAE().to(DEVICE)
|
|
model.init_weights()
|
|
|
|
# print(model(torch.randn(128, 1, 20, 80).to(DEVICE)).shape)
|
|
|
|
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
|
|
|
|
|
for epoch in range(NUM_EPOCHS):
|
|
print(f'EPOCH #{epoch+1}')
|
|
losses = []
|
|
for batch in tqdm(dl):
|
|
data, labels = batch
|
|
data = data.to(DEVICE) # torch.Size([128, 4, 20, 80]) batch x subs_specs x height x width
|
|
|
|
loss = model.train_loss(data)
|
|
|
|
optimizer.zero_grad()
|
|
loss.backward()
|
|
optimizer.step()
|
|
|
|
losses.append(loss.item())
|
|
print(f'Loss: {np.mean(losses)}')
|
|
|
|
auc = mimii.evaluate_model(model, NUM_SEGMENTS, NUM_SEGMENTS, transform=tfms)
|
|
print(f'AUC: {auc}')
|
|
|
|
|
|
|