1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # Convert FEATURES with trained models.
5
+ # By Wen-Chin Huang 2019.06
6
+
1
7
import json
2
8
import os
3
9
4
10
import tensorflow as tf
5
11
import numpy as np
6
12
7
- from datetime import datetime
8
13
from importlib import import_module
9
14
10
15
import argparse
11
16
import logging
12
17
13
18
import sys
14
19
from preprocessing .vcc2018 .feature_reader import Whole_feature_reader
15
- from preprocessing .normalizer import MinMaxScaler
20
+ from preprocessing .normalizer import MinMaxScaler , StandardScaler
16
21
from preprocessing .utils import read_hdf5 , read_txt
17
22
from util .wrapper import load , get_default_logdir_output
18
23
19
24
def main ():
20
25
21
26
parser = argparse .ArgumentParser (
22
- description = "convert files ." )
27
+ description = "Conversion ." )
23
28
parser .add_argument (
24
29
"--logdir" , required = True , type = str ,
25
30
help = "path of log directory" )
26
31
parser .add_argument (
27
32
"--checkpoint" , default = None , type = str ,
28
33
help = "path of checkpoint" )
34
+
29
35
parser .add_argument (
30
36
"--src" , default = None , required = True , type = str ,
31
37
help = "source speaker" )
32
38
parser .add_argument (
33
39
"--trg" , default = None , required = True , type = str ,
34
40
help = "target speaker" )
35
- parser .add_argument ('--stat_dir' , type = str ,
36
- default = '/mnt/md1/datasets/vcc2018/world/etc-new' ,
37
- help = 'configuration directory' )
38
- parser .add_argument ('--file_pattern' , type = str ,
39
- default = '/mnt/md1/datasets/vcc2018/world/bin-dynamic/no_VAD/ev/{}/*.bin' ,
40
- help = 'file pattern' )
41
+ parser .add_argument (
42
+ "--type" , default = 'test' , type = str ,
43
+ help = "test or valid (default is test)" )
44
+
45
+
46
+ parser .add_argument (
47
+ "--input_feat" , required = True ,
48
+ type = str , help = "input feature type" )
49
+ parser .add_argument (
50
+ "--output_feat" , required = True ,
51
+ type = str , help = "output feature type" )
52
+ parser .add_argument (
53
+ "--mcd" , action = 'store_true' ,
54
+ help = "calculate mcd or not" )
55
+ parser .add_argument (
56
+ "--syn" , action = 'store_true' ,
57
+ help = "synthesize voice or not" )
41
58
args = parser .parse_args ()
42
59
43
60
# make exp directory
@@ -70,41 +87,42 @@ def main():
70
87
module = import_module (arch ['model_module' ], package = None )
71
88
MODEL = getattr (module , arch ['model' ])
72
89
73
- input_feat = arch [ 'conversion' ][ 'input' ]
90
+ input_feat = args . input_feat
74
91
input_feat_dim = arch ['feat_param' ]['dim' ][input_feat ]
75
- output_feat = arch [ 'conversion' ][ 'output' ]
92
+ output_feat = args . output_feat
76
93
77
94
# read speakers
78
95
spk_list = read_txt (arch ['spklist' ])
79
96
80
97
# Load statistics, normalize and NCHW
81
98
normalizers = {}
82
- for k in arch ['normalizer_files' ]:
83
- if (arch ['normalizer_files' ][k ]['max' ] is not None
84
- or arch ['normalizer_files' ][k ]['max' ] is not None ):
85
- normalizer = MinMaxScaler (
86
- xmax = np .fromfile (os .path .join (arch ['stat_dir' ], arch ['normalizer_files' ][k ]['max' ])),
87
- xmin = np .fromfile (os .path .join (arch ['stat_dir' ], arch ['normalizer_files' ][k ]['min' ])),
88
- )
89
- normalizers [k ] = normalizer
99
+ for k in arch ['normalizer' ]:
100
+ normalizers [k ] = {}
101
+ for norm_type in arch ['normalizer' ][k ]['type' ]:
102
+ if norm_type == 'minmax' :
103
+ normalizer = MinMaxScaler (
104
+ xmax = read_hdf5 (arch ['stats' ], '/max/' + k ),
105
+ xmin = read_hdf5 (arch ['stats' ], '/min/' + k ),
106
+ )
107
+ elif norm_type == 'meanvar' :
108
+ normalizer = StandardScaler (
109
+ mu = read_hdf5 (arch ['stats' ], '/mean/' + k ),
110
+ std = read_hdf5 (arch ['stats' ], '/scale/' + k ),
111
+ )
112
+
113
+ normalizers [k ][norm_type ] = normalizer
90
114
91
115
# Define placeholders
92
116
x_pl = tf .placeholder (tf .float32 , [None , input_feat_dim ])
93
- if input_feat in normalizers :
94
- x = normalizers [input_feat ].forward_process (x_pl )
95
- else :
96
- x = x_pl
97
- x = tf .expand_dims (tf .expand_dims (x , 1 ), - 1 )
117
+
98
118
yh_pl = tf .placeholder (dtype = tf .int64 , shape = [1 ,])
99
- yh = yh_pl * tf .ones (shape = [tf .shape (x )[0 ],], dtype = tf .int64 )
119
+ yh = yh_pl * tf .ones (shape = [tf .shape (x_pl )[0 ],], dtype = tf .int64 )
120
+ yh = tf .expand_dims (yh , 0 )
100
121
101
122
# Define model
102
- model = MODEL (arch )
103
- z = model .encode (x )
123
+ model = MODEL (arch , normalizers )
124
+ z , _ = model .encode (x_pl )
104
125
xh = model .decode (z , yh )
105
- xh = tf .squeeze (xh )
106
- if output_feat in normalizers :
107
- xh = normalizers [output_feat ].backward_process (xh )
108
126
109
127
# make directories for output
110
128
tf .gfile .MakeDirs (os .path .join (output_dir , 'latent' ))
@@ -123,7 +141,16 @@ def main():
123
141
_ , ckpt = os .path .split (args .checkpoint )
124
142
load (saver , sess , args .logdir , ckpt = ckpt )
125
143
126
- files = sorted (tf .gfile .Glob (arch ['conversion' ]['test_file_pattern' ].format (args .src )))
144
+ # get feature list, either validation set or test set
145
+ if args .type == 'test' :
146
+ files = tf .gfile .Glob (arch ['conversion' ]['test_file_pattern' ].format (args .src ))
147
+ elif args .type == 'valid' :
148
+ files = []
149
+ for p in arch ['training' ]['valid_file_pattern' ]:
150
+ files .extend (tf .gfile .Glob (p .replace ('*' , args .src )))
151
+ files = sorted (files )
152
+
153
+ # conversion
127
154
for f in files :
128
155
basename = os .path .split (f )[- 1 ]
129
156
path_to_latent = os .path .join (output_dir , 'latent' , '{}-{}-{}' .format (args .src , args .trg , basename ))
@@ -143,6 +170,26 @@ def main():
143
170
fp .write (latent .tostring ())
144
171
with open (path_to_cvt , 'wb' ) as fp :
145
172
fp .write (cvt .tostring ())
173
+
174
+ # optionally calculate MCD
175
+ if args .mcd :
176
+ cmd = "python ./mcd_calculate.py" + \
177
+ " --type " + args .type + \
178
+ " --logdir " + output_dir + \
179
+ " --input_feat " + input_feat + \
180
+ " --output_feat " + output_feat
181
+ print (cmd )
182
+ os .system (cmd )
183
+
184
+ # optionally synthesize waveform
185
+ if args .syn :
186
+ cmd = "python ./synthesize.py" + \
187
+ " --type " + args .type + \
188
+ " --logdir " + output_dir + \
189
+ " --input_feat " + input_feat + \
190
+ " --output_feat " + output_feat
191
+ print (cmd )
192
+ os .system (cmd )
146
193
147
194
if __name__ == '__main__' :
148
195
main ()
0 commit comments