-
Notifications
You must be signed in to change notification settings - Fork 5
/
forward_podcast.py
146 lines (135 loc) · 4.42 KB
/
forward_podcast.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
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
import argparse
from torch.utils.data.dataset import Dataset
import yaml
import os
import sys
import torch
import soundfile as sf
from asteroid.utils import tensors_to_device
import numpy as np
from tqdm import tqdm
import torchaudio
from utils.my_import import my_import
class PodcastLoader(Dataset):
dataset_name = "PodcastMix"
def __init__(self, csv_dir, sample_rate=44100, segment=3):
self.segment = segment
self.sample_rate = sample_rate
self.paths = [os.path.join(csv_dir, f) for f in os.listdir(csv_dir) if (os.path.isfile(os.path.join(csv_dir, f)) and '.wav' in f)]
self.paths = sorted(self.paths, key=lambda i: int(os.path.splitext(os.path.basename(i))[0]))
torchaudio.set_audio_backend(backend='soundfile')
def __len__(self):
return len(self.paths)
def __getitem__(self, index):
starting_second = 0
podcast_path = self.paths[index]
audio_signal, _ = torchaudio.load(
podcast_path,
frame_offset=starting_second * self.sample_rate,
num_frames=self.segment * self.sample_rate,
normalize=True
)
audio_signal = torch.mean(audio_signal, dim=0)
return audio_signal
parser = argparse.ArgumentParser()
parser.add_argument(
"--test_dir",
type=str,
required=True,
help="Test directory including the csv files"
)
parser.add_argument(
"--use_gpu",
type=int,
default=0,
help="Whether to use the GPU for model execution"
)
parser.add_argument(
"--target_model",
type=str,
required=True,
help="Asteroid model to use"
)
parser.add_argument(
"--exp_dir",
default="exp/tmp",
help="Best serialized model path"
)
parser.add_argument(
"--out_dir",
type=str,
default='ConvTasNet/eval/tmp',
required=True,
help="Directory where the eval results" " will be stored",
)
parser.add_argument(
"--segment",
type=int,
default=2,
required=True,
help="Number of seconds to separate",
)
parser.add_argument(
"--sample_rate",
type=int,
default=8192,
required=True,
help="Sample rate",
)
def main(conf):
model_path = os.path.join(conf["exp_dir"], "best_model.pth")
if conf["target_model"] == "UNet":
sys.path.append('UNet_model')
AsteroidModelModule = my_import("unet_model.UNet")
else:
sys.path.append('ConvTasNet_model')
AsteroidModelModule = my_import("conv_tasnet_norm.ConvTasNetNorm")
model = AsteroidModelModule.from_pretrained(model_path, sample_rate=conf["sample_rate"])
if conf["use_gpu"]:
model.cuda()
model_device = next(model.parameters()).device
test_set = PodcastLoader(
csv_dir=conf["test_dir"],
sample_rate=conf["sample_rate"],
segment=conf["segment"]
)
eval_save_dir = os.path.join(conf["exp_dir"], conf["out_dir"])
ex_save_dir = os.path.join(eval_save_dir, "examples_podcast/")
torch.no_grad().__enter__()
for idx in tqdm(range(len(test_set))):
# Forward the network on the mixture.
mix = test_set[idx]
mix = tensors_to_device(mix, device=model_device)
if conf["target_model"] == "UNet":
est_sources = model(mix.unsqueeze(0)).squeeze(0)
else:
est_sources = model(mix)
mix_np = mix.cpu().data.numpy()
est_sources_np = est_sources.squeeze(0).cpu().data.numpy()
# Save some examples in a folder. Wav files and metrics as text.
local_save_dir = os.path.join(ex_save_dir, "ex_{}/".format(idx + 1))
os.makedirs(local_save_dir, exist_ok=True)
sf.write(
local_save_dir + "mixture.wav",
mix_np,
conf["sample_rate"]
)
# Loop over the estimates sources
for src_idx, est_src in enumerate(est_sources_np):
est_src *= np.max(np.abs(mix_np)) / np.max(np.abs(est_src))
sf.write(
local_save_dir + "s{}_estimate.wav".format(src_idx),
est_src,
conf["sample_rate"],
)
if __name__ == "__main__":
args = parser.parse_args()
arg_dic = dict(vars(args))
# Load training config
conf_path = os.path.join(args.exp_dir, "conf.yml")
with open(conf_path) as f:
train_conf = yaml.safe_load(f)
arg_dic["sample_rate"] = train_conf["data"]["sample_rate"]
# arg_dic["segment"] = train_conf["data"]["segment"]
arg_dic["train_conf"] = train_conf
main(arg_dic)