-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse_me.py
201 lines (150 loc) · 6.39 KB
/
use_me.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Usage script.
"""
from __future__ import print_function
import argparse
import os
import time
import numpy as np
import torch
from helpers.data_feeder import data_feeder_testing, data_process_results_testing
from helpers.settings import debug, hyper_parameters, output_states_path, training_constants, \
usage_output_string_per_example, usage_output_string_total
from modules import RNNEnc, RNNDec, FNNMasker, FNNDenoiser
__author__ = ['Konstantinos Drossos -- TUT', 'Stylianos Mimilakis -- Fraunhofer IDMT']
__docformat__ = 'reStructuredText'
__all__ = ['use_me_process']
def use_me_process(sources_list, output_file_names):
"""The usage process.
:param sources_list: The file names to be used.
:type sources_list: list[str]
:param output_file_names: The output file names to be used.
:type output_file_names: list[list[str]]
"""
print('\n-- Welcome to MaD TwinNet.')
if debug:
print('\n-- Cannot proceed in debug mode. Please set debug=False at the settings file.')
print('-- Exiting.')
exit(-1)
print('-- Now I will extract the voice and the background music from the provided files')
device = 'cuda' if not debug and torch.cuda.is_available() else 'cpu'
# Masker modules
rnn_enc = RNNEnc(hyper_parameters['reduced_dim'], hyper_parameters['context_length'], debug)
rnn_dec = RNNDec(hyper_parameters['rnn_enc_output_dim'], debug)
fnn = FNNMasker(
hyper_parameters['rnn_enc_output_dim'],
hyper_parameters['original_input_dim'],
hyper_parameters['context_length']
)
# Denoiser modules
denoiser = FNNDenoiser(hyper_parameters['original_input_dim'])
rnn_enc.load_state_dict(torch.load(output_states_path['rnn_enc']))
rnn_enc.to(device)
rnn_dec.load_state_dict(torch.load(output_states_path['rnn_dec']))
rnn_dec.to(device)
fnn.load_state_dict(torch.load(output_states_path['fnn']))
fnn.to(device)
denoiser.load_state_dict(torch.load(output_states_path['denoiser']))
denoiser.to(device)
testing_it = data_feeder_testing(
window_size=hyper_parameters['window_size'], fft_size=hyper_parameters['fft_size'],
hop_size=hyper_parameters['hop_size'], seq_length=hyper_parameters['seq_length'],
context_length=hyper_parameters['context_length'], batch_size=1,
debug=debug, sources_list=sources_list
)
print('-- Let\'s go!\n')
total_time = 0
for index, data in enumerate(testing_it()):
s_time = time.time()
mix, mix_magnitude, mix_phase, voice_true, bg_true = data
voice_predicted = np.zeros(
(
mix_magnitude.shape[0],
hyper_parameters['seq_length'] - hyper_parameters['context_length'] * 2,
hyper_parameters['window_size']
),
dtype=np.float32
)
for batch in range(int(mix_magnitude.shape[0] / training_constants['batch_size'])):
b_start = batch * training_constants['batch_size']
b_end = (batch + 1) * training_constants['batch_size']
v_in = torch.from_numpy(mix_magnitude[b_start:b_end, :, :]).to(device)
tmp_voice_predicted = rnn_enc(v_in)
tmp_voice_predicted = rnn_dec(tmp_voice_predicted)
tmp_voice_predicted = fnn(tmp_voice_predicted, v_in)
tmp_voice_predicted = denoiser(tmp_voice_predicted)
voice_predicted[b_start:b_end, :, :] = tmp_voice_predicted.data.cpu().numpy()
data_process_results_testing(
index=index, voice_true=voice_true, bg_true=bg_true,
voice_predicted=voice_predicted,
window_size=hyper_parameters['window_size'], mix=mix, mix_magnitude=mix_magnitude,
mix_phase=mix_phase, hop=hyper_parameters['hop_size'],
context_length=hyper_parameters['context_length'],
output_file_name=output_file_names[index]
)
e_time = time.time()
print(usage_output_string_per_example.format(
f=sources_list[index],
t=e_time - s_time
))
total_time += e_time - s_time
print('\n-- Testing finished\n')
print(usage_output_string_total.format(
t=total_time
))
print('-- That\'s all folks!')
def _make_target_file_names(sources_list):
"""Makes the target file names for the sources list.
:param sources_list: The sources list.
:type sources_list: list[str]
:return: The target names.
:rtype: list[list[str]]
"""
targets_list = []
for source in sources_list:
f_name = os.path.splitext(source)[0]
targets_list.append(['{}_voice.wav'.format(f_name), '{}_bg_music.wav'.format(f_name)])
return targets_list
def _get_file_names_from_file(file_name):
"""Reads line by line a txt file and returns the contents.
:param file_name: The file name of the txt file.
:type file_name: str
:return: The contents of the file, in a line-by-line fashion.
:rtype: list[str]
"""
with open(file_name) as f:
return [line.strip() for line in f.readlines()]
def main(x):
cmd_arg_parser = argparse.ArgumentParser(
usage='python scripts/use_me [-w the_file.wav]|[-l the_files.txt]',
description='Script to use the MaD TwinNet with your own files. Remember to set up properly'
'the PYTHONPATH environmental variable'
)
cmd_arg_parser.add_argument(
'--input-wav', '-w', action='store', dest='input_wav', default='',
help='Specify one wav file to be processed.'
)
cmd_arg_parser.add_argument(
'--input-list', '-l', action='store', dest='input_list', default=[],
help='Specify one txt file with each line to be one path for a wav file.'
)
cmd_args = cmd_arg_parser.parse_args()
input_wav = x
input_list = cmd_args.input_list
if (input_wav == '' and len(input_list) == 0) or (input_wav != '' and len(input_list) != 0):
print('-- Please specify **either** a wav file (with -w) **or** give'
'a txt file with file names in each line (with -l). ')
print('-- Exiting.')
exit(-1)
if len(input_list) == 0:
input_list = [input_wav]
else:
input_list = _get_file_names_from_file(input_list)
use_me_process(
sources_list=input_list,
output_file_names=_make_target_file_names(input_list)
)
if __name__ == '__main__':
main(x)
# EOF