-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
178 lines (140 loc) · 6.33 KB
/
test.py
File metadata and controls
178 lines (140 loc) · 6.33 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
import os
import sys
sys.path.append('../')
import time
import logging
import numpy as np
import soundfile as sf
from pathlib import Path
from importlib import import_module
import torch
from torch.utils.data import DataLoader
from data_loader import WaveSplitDataset
from WaveSplit.models.wavesplit import WaveSplit
from losses import SingleSrcNegSDR,PITminLoss,PairwiseNegSDR
from pit_wrapper import PITLossWrapper
import ipdb
class Tester(object):
def __init__(self, test_config, spks_config, seps_config, filt_config):
if test_config['checkpoint_path'] is "":
test_config['checkpoint_path'] = 'best.pth'
checkpoint_path = os.path.join(test_config['checkpoint_directory'],test_config['checkpoint_path'])
model = WaveSplit(n_src=2,**spks_config, **seps_config, **filt_config)
#print(model)
self.model = model.cuda()
checkpoint_data = torch.load(checkpoint_path, map_location='cpu')
self.model.load_state_dict(checkpoint_data['model'])
def test(self, input):
self.model.eval()
input = input.cuda()
wave = self.model(input)
return wave
def test_cal(self, mix, clean):
self.model.eval()
mix = mix.cuda()
clean = clean.cuda()
with torch.no_grad():
wave,SISNR,SISNR_O = self.model([mix,clean])
#print(wave.shape)
wave = wave.squeeze(0).cpu().detach().numpy()
return - SISNR.cpu().numpy() , -SISNR_O.cpu().numpy() ,wave
def test(test_config):
# Initial
checkpoint_directory = test_config.get('checkpoint_directory', '')
batch_size = test_config.get('batch_size', 16)
checkpoint_path = test_config.get('checkpoint_path', '')
output_directory = test_config.get('output_directory', 'Exp')
# Setup
tester = Tester(test_config, spks_config, seps_config, filt_config)
print("Testing directory:",data_config['test_dir'])
with open(data_config['test_dir']+"data.json") as g:
test_data = g.read()
test_info = json.loads(test_data)
SNR_List = []
SNRO_List = []
print(test_info[1][0]['Src'])
for i in range(len(test_info)):
mix_data, _ = sf.read('../'+test_info[i][0]['Src'], start=0, stop=None, dtype='float32')
clean1, _ = sf.read('../'+test_info[i][3]['Src'], start=0, stop=None, dtype='float32')
clean2, _ = sf.read('../'+test_info[i][4]['Src'], start=0, stop=None, dtype='float32')
mix = torch.from_numpy(mix_data).float().unsqueeze(0)
clean1 = torch.from_numpy(clean1).float().unsqueeze(0)
clean2 = torch.from_numpy(clean2).float().unsqueeze(0)
clean = torch.cat((clean1,clean2),0)
clean = clean.unsqueeze(0)
SISNR,SISNR_O,wave = tester.test_cal(torch.from_numpy(mix_data).float().unsqueeze(0),clean)
clean_cpu = clean.squeeze(0).cpu().detach().numpy()
SNR_List.append(SISNR)
SNRO_List.append(SISNR_O)
print(SISNR)
output_directory = Path(output_directory)
output_directory.mkdir(parents=True, exist_ok=True)
outputpath = str(output_directory / "{ex}_Mix.wav".format(ex=i+1))
print(outputpath)
max_sample = np.max(np.abs(mix_data))
norm_wave = mix_data.copy() / max_sample
#sf.write(outputpath,norm_wave,8000)
for j in range(2):
outputpath = str(output_directory / "{ex}_Sep_{x}_tt_best.wav".format(ex=i+1,x=j+1))
#print(outputpath)
max_sample = np.max(np.abs(wave[j]))
norm_wave = wave[j].copy() / max_sample if max_sample >= 1 else wave[j].copy()
#sf.write(outputpath,norm_wave,8000)
outputpath = str(output_directory / "{ex}_Sep_{x}_tt_Truth.wav".format(ex=i+1,x=j+1))
#print(outputpath)
max_sample = np.max(np.abs(clean_cpu[j]))
norm_wave = clean_cpu[j].copy() / max_sample
#sf.write(outputpath,norm_wave,8000)
#exit()
if (i+1)%20 is 0:
# SNR_Arr = np.array(SNR_List)
# q = np.median(SNR_Arr)
# w = np.percentile(SNR_Arr, [25, 50, 75])
# print(q,w)
exit()
print("{cur}/{tot}: {avg}, {avg_o}".format(cur=i+1,tot=len(test_info),avg=(sum(SNR_List)/len(SNR_List)),avg_o=(sum(SNRO_List)/len(SNRO_List))),end='\r')
print("Testing Set Avg. SISNR:",sum(SNR_List)/len(SNR_List),"mix Avg. SISNR:",sum(SNRO_List)/len(SNRO_List))
SNR_Arr = np.array(SNR_List)
np.save("ValidationResult.npy",SNR_Arr)
q = np.median(SNR_Arr)
w = np.percentile(SNR_Arr, [13, 25, 50, 75, 88])
print(q,w)
print(np.std(SNR_Arr))
if __name__ == "__main__":
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default='config_WS.json',
help='JSON file for configuration')
parser.add_argument('-o', '--output_directory', type=str, default=None,
help='Directory for checkpoint output')
parser.add_argument('-p', '--checkpoint_path', type=str, default=None,
help='checkpoint path to keep training')
parser.add_argument('-T', '--training_dir', type=str, default=None,
help='Traininig dictionary path')
parser.add_argument('-g', '--gpu', type=str, default='0',
help='Using gpu #')
args = parser.parse_args()
# Parse configs. Globals nicer in this case
with open(args.config) as f:
data = f.read()
config = json.loads(data)
test_config = config["test_config"]
global data_config
data_config = config["data"]
global spks_config
spks_config = config["speakerstack"]
global seps_config
seps_config = config["separationstack"]
global filt_config
filt_config = config["filterbank"]
if args.output_directory is not None:
train_config['output_directory'] = args.output_directory
if args.checkpoint_path is not None:
train_config['checkpoint_path'] = args.checkpoint_path
if args.training_dir is not None:
data_config['training_dir'] = args.training_dir
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
test(test_config)