-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfl.py
107 lines (85 loc) · 3.29 KB
/
dfl.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
# -*- coding: utf-8 -*-
import sys
sys.path.insert(1,'/path/to/SpeechDenoisingWithDeepFeatureLosses/')
from model import *
from data_import import *
import getopt
from glob import glob
import argparse
def run_senet():
valfolder = args.in_dir
modfolder = args.model_dir
# SPEECH ENHANCEMENT NETWORK
SE_LAYERS = 13 # NUMBER OF INTERNAL LAYERS
SE_CHANNELS = 64 # NUMBER OF FEATURE CHANNELS PER LAYER
SE_LOSS_LAYERS = 6 # NUMBER OF FEATURE LOSS LAYERS
SE_NORM = "NM" # TYPE OF LAYER NORMALIZATION (NM, SBN or None)
fs = 16000
# SET LOSS FUNCTIONS AND PLACEHOLDERS
with tf.variable_scope(tf.get_variable_scope()):
input=tf.placeholder(tf.float32,shape=[None,1,None,1])
clean=tf.placeholder(tf.float32,shape=[None,1,None,1])
enhanced=senet(input, n_layers=SE_LAYERS, norm_type=SE_NORM, n_channels=SE_CHANNELS)
# LOAD DATA
folders = []
for depth in range(args.maxdepth):
path_str = '*' + os.sep
path_str = os.path.join(args.in_dir, path_str*depth)
folders += glob(path_str)
valset = {}
valset['innames'] = []
valset['shortnames'] = []
for fld in folders:
tmp = load_noisy_data_list(valfolder = fld)
valset['innames'].extend(tmp['innames'])
valset['shortnames'].extend(tmp['shortnames'])
valset = load_noisy_data(valset)
# BEGIN SCRIPT #########################################################################################################
# INITIALIZE GPU CONFIG
config=tf.ConfigProto()
config.gpu_options.allow_growth=True
sess=tf.Session(config=config)
print "Config ready"
sess.run(tf.global_variables_initializer())
print "Session initialized"
saver = tf.train.Saver([var for var in tf.trainable_variables() if var.name.startswith("se_")])
saver.restore(sess, "./%s/se_model.ckpt" % modfolder)
#####################################################################################
for id in tqdm(range(0, len(valset["innames"]))):
i = id # NON-RANDOMIZED ITERATION INDEX
inputData = valset["inaudio"][i] # LOAD DEGRADED INPUT
# VALIDATION ITERATION
output = sess.run([enhanced],
feed_dict={input: inputData})
output = np.reshape(output, -1)
if not os.path.exists(args.out_dir):
os.makedirs(args.out_dir)
wavfile.write("%s/%s" % (args.out_dir,valset["shortnames"][i]), fs, output)
if __name__ == '__main__':
ap = argparse.ArgumentParser(
description = '''Ta skripta izvede odstranitev šuma s postopkom
DFL (ang. Deep Feature Losses).''')
ap._action_groups.pop()
required = ap.add_argument_group('Obvezni argumenti')
optional = ap.add_argument_group('Opcijski argumenti')
required.add_argument('-i','--in_dir',
type = str,
required = True,
metavar = 'INPUT_DIR',
help = 'Direktorij z vhodnimi datotekami WAV .')
required.add_argument('-o','--out_dir',
type = str,
required = True,
metavar = 'OUTPUT_DIR',
help = 'Direktorij kamor se bodo shranjevale razšumljene datoteke WAV.')
required.add_argument('-m','--model_dir',
type = str,
required = True,
metavar = 'MODEL_DIR',
help = 'Direktorij s SEnet modelom.')
optional.add_argument('-x','--maxdepth',
type = int,
default = 1,
help = 'Maksimalna globina poddirektorijev v katerih iščemo datoteke WAV.')
args = ap.parse_args()
run_senet()