Spaces:
Build error
Build error
File size: 6,807 Bytes
8235b4f |
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 |
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
# Authors: Eliya Nachmani (enk100), Yossi Adi (adiyoss), Lior Wolf and Alexandre Defossez (adefossez)
import argparse
from concurrent.futures import ProcessPoolExecutor
import json
import logging
import sys
import numpy as np
from pesq import pesq
from pystoi import stoi
import torch
from .models.sisnr_loss import cal_loss
from .data.data import Validset
from . import distrib
from .utils import bold, deserialize_model, LogProgress
logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser(
'Evaluate separation performance using MulCat blocks')
parser.add_argument('model_path',
help='Path to model file created by training')
parser.add_argument('data_dir',
help='directory including mix.json, s1.json, s2.json, ... files')
parser.add_argument('--device', default="cuda")
parser.add_argument('--sdr', type=int, default=0)
parser.add_argument('--sample_rate', default=16000,
type=int, help='Sample rate')
parser.add_argument('--num_workers', type=int, default=5)
parser.add_argument('-v', '--verbose', action='store_const', const=logging.DEBUG,
default=logging.INFO, help="More loggging")
def evaluate(args, model=None, data_loader=None, sr=None):
total_sisnr = 0
total_pesq = 0
total_stoi = 0
total_cnt = 0
updates = 5
# Load model
if not model:
pkg = torch.load(args.model_path, map_location=args.device)
if 'model' in pkg:
model = pkg['model']
else:
model = pkg
model = deserialize_model(model)
if 'best_state' in pkg:
model.load_state_dict(pkg['best_state'])
logger.debug(model)
model.eval()
model.to(args.device)
# Load data
if not data_loader:
dataset = Validset(args.data_dir)
data_loader = distrib.loader(
dataset, batch_size=1, num_workers=args.num_workers)
sr = args.sample_rate
pendings = []
with ProcessPoolExecutor(args.num_workers) as pool:
with torch.no_grad():
iterator = LogProgress(logger, data_loader, name="Eval estimates")
for i, data in enumerate(iterator):
# Get batch data
mixture, lengths, sources = [x.to(args.device) for x in data]
# Forward
with torch.no_grad():
mixture /= mixture.max()
estimate = model(mixture)[-1]
sisnr_loss, snr, estimate, reorder_estimate = cal_loss(
sources, estimate, lengths)
reorder_estimate = reorder_estimate.cpu()
sources = sources.cpu()
mixture = mixture.cpu()
pendings.append(
pool.submit(_run_metrics, sources, reorder_estimate, mixture, None,
sr=sr))
total_cnt += sources.shape[0]
for pending in LogProgress(logger, pendings, updates, name="Eval metrics"):
sisnr_i, pesq_i, stoi_i = pending.result()
total_sisnr += sisnr_i
total_pesq += pesq_i
total_stoi += stoi_i
metrics = [total_sisnr, total_pesq, total_stoi]
sisnr, pesq, stoi = distrib.average(
[m/total_cnt for m in metrics], total_cnt)
logger.info(
bold(f'Test set performance: SISNRi={sisnr:.2f} PESQ={pesq}, STOI={stoi}.'))
return sisnr, pesq, stoi
def _run_metrics(clean, estimate, mix, model, sr, pesq=False):
if model is not None:
torch.set_num_threads(1)
# parallel evaluation here
with torch.no_grad():
estimate = model(estimate)[-1]
estimate = estimate.numpy()
clean = clean.numpy()
mix = mix.numpy()
sisnr = cal_SISNRi(clean, estimate, mix)
if pesq:
pesq_i = cal_PESQ(clean, estimate, sr=sr)
stoi_i = cal_STOI(clean, estimate, sr=sr)
else:
pesq_i = 0
stoi_i = 0
return sisnr.mean(), pesq_i, stoi_i
def cal_SISNR(ref_sig, out_sig, eps=1e-8):
"""Calcuate Scale-Invariant Source-to-Noise Ratio (SI-SNR)
Args:
ref_sig: numpy.ndarray, [B, T]
out_sig: numpy.ndarray, [B, T]
Returns:
SISNR
"""
assert len(ref_sig) == len(out_sig)
B, T = ref_sig.shape
ref_sig = ref_sig - np.mean(ref_sig, axis=1).reshape(B, 1)
out_sig = out_sig - np.mean(out_sig, axis=1).reshape(B, 1)
ref_energy = (np.sum(ref_sig ** 2, axis=1) + eps).reshape(B, 1)
proj = (np.sum(ref_sig * out_sig, axis=1).reshape(B, 1)) * \
ref_sig / ref_energy
noise = out_sig - proj
ratio = np.sum(proj ** 2, axis=1) / (np.sum(noise ** 2, axis=1) + eps)
sisnr = 10 * np.log(ratio + eps) / np.log(10.0)
return sisnr.mean()
def cal_PESQ(ref_sig, out_sig, sr):
"""Calculate PESQ.
Args:
ref_sig: numpy.ndarray, [B, C, T]
out_sig: numpy.ndarray, [B, C, T]
Returns
PESQ
"""
B, C, T = ref_sig.shape
ref_sig = ref_sig.reshape(B*C, T)
out_sig = out_sig.reshape(B*C, T)
pesq_val = 0
for i in range(len(ref_sig)):
pesq_val += pesq(sr, ref_sig[i], out_sig[i], 'nb')
return pesq_val / (B*C)
def cal_STOI(ref_sig, out_sig, sr):
"""Calculate STOI.
Args:
ref_sig: numpy.ndarray, [B, C, T]
out_sig: numpy.ndarray, [B, C, T]
Returns:
STOI
"""
B, C, T = ref_sig.shape
ref_sig = ref_sig.reshape(B*C, T)
out_sig = out_sig.reshape(B*C, T)
try:
stoi_val = 0
for i in range(len(ref_sig)):
stoi_val += stoi(ref_sig[i], out_sig[i], sr, extended=False)
return stoi_val / (B*C)
except:
return 0
def cal_SISNRi(src_ref, src_est, mix):
"""Calculate Scale-Invariant Source-to-Noise Ratio improvement (SI-SNRi)
Args:
src_ref: numpy.ndarray, [B, C, T]
src_est: numpy.ndarray, [B, C, T], reordered by best PIT permutation
mix: numpy.ndarray, [T]
Returns:
average_SISNRi
"""
avg_SISNRi = 0.0
B, C, T = src_ref.shape
for c in range(C):
sisnr = cal_SISNR(src_ref[:, c], src_est[:, c])
sisnrb = cal_SISNR(src_ref[:, c], mix)
avg_SISNRi += (sisnr - sisnrb)
avg_SISNRi /= C
return avg_SISNRi
def main():
args = parser.parse_args()
logging.basicConfig(stream=sys.stderr, level=args.verbose)
logger.debug(args)
sisnr, pesq, stoi = evaluate(args)
json.dump({'sisnr': sisnr,
'pesq': pesq, 'stoi': stoi}, sys.stdout)
sys.stdout.write('\n')
if __name__ == '__main__':
main()
|