This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
evaluate.py
105 lines (90 loc) · 3.15 KB
/
evaluate.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
import argparse
import os
import json
import torch
import yaml
import numpy as np
import torch.nn as nn
from torch.utils.data import DataLoader
from utils.tools import to_device, log, synth_one_sample
from model import DiffSingerLoss
from data_utils import Dataset
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def evaluate(args, model, step, configs, logger=None, vocoder=None, losses=None):
preprocess_config, model_config, train_config = configs
# Get dataset
dataset = Dataset(
preprocess_config["path"]["val_filelist"], preprocess_config, train_config, sort=False, drop_last=False
)
batch_size = train_config["optimizer"]["batch_size"]
loader = DataLoader(
dataset,
batch_size=2,
shuffle=False,
collate_fn=dataset.collate_fn,
)
# Get loss function
Loss = DiffSingerLoss(args, preprocess_config, model_config, train_config).to(device)
loss_sums = [{k:0 for k in loss.keys()} if isinstance(loss, dict) else 0 for loss in losses]
for batchs in loader:
for batch in batchs:
batch = to_device(batch, device)
pitches = batch[8].clone()
with torch.no_grad():
# Forward
output = model(*(batch[1:]))
# Cal Loss
losses = Loss(batch, output)
for i in range(len(losses)):
if isinstance(losses[i], dict):
for k in loss_sums[i].keys():
loss_sums[i][k] += losses[i][k].item() * len(batch[0])
else:
loss_sums[i] += losses[i].item() * len(batch[0])
loss_means = []
loss_means_ = []
for loss_sum in loss_sums:
if isinstance(loss_sum, dict):
loss_mean = {k:v / len(dataset) for k, v in loss_sum.items()}
loss_means.append(loss_mean)
loss_means_.append(sum(loss_mean.values()))
else:
loss_means.append(loss_sum / len(dataset))
loss_means_.append(loss_sum / len(dataset))
message = "Validation Step {}, Total Loss: {:.4f}, Mel Loss: {:.4f}, Noise Loss: {:.4f}, ".format(
*([step] + [l for l in loss_means_])
)
if logger is not None:
figs, wav_reconstruction, wav_prediction, tag = synth_one_sample(
args,
batch,
pitches,
output,
vocoder,
model_config,
preprocess_config,
model.module.diffusion,
)
log(logger, step, losses=loss_means)
log(
logger,
step,
figs=figs,
tag="Validation",
)
sampling_rate = preprocess_config["preprocessing"]["audio"]["sampling_rate"]
log(
logger,
audio=wav_reconstruction,
sampling_rate=sampling_rate,
tag="Validation/{}_reconstructed".format(tag),
step=step
)
log(
logger,
audio=wav_prediction,
sampling_rate=sampling_rate,
tag="Validation/{}_synthesized".format(tag),
step=step
)
return message