forked from moodoki/tfnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
159 lines (134 loc) · 6.56 KB
/
train.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
"""Trains the AudioUNet Model"""
import tensorflow as tf
from tfnet import TFNetEstimator
from tfnet import nets
from tfnet import summaries
import argshelper
import datahelper.dataset as ds
FLAGS = argshelper.FLAGS
def main(argv):
if FLAGS.debug:
print("Unprocessed flags:", argv)
tf.logging.set_verbosity(tf.logging.DEBUG)
tf.logging.debug('-------------------------------------------')
tf.logging.debug('DEBUG MODE')
tf.logging.debug('-------------------------------------------')
tf.logging.debug('Time params:' + str(argshelper.get_time_params()))
tf.logging.debug('Freq params:' + str(argshelper.get_freq_params()))
tf.logging.set_verbosity(tf.logging.INFO)
degrade_fn = lambda x: ds.downsample_by(x, FLAGS.downsample_rate)
dset = ds.get_dataset(FLAGS.trainset,
path=FLAGS.datapath,
degrade_fn=degrade_fn,
epochs=FLAGS.epochs,
batchsize=FLAGS.batchsize,
segs_per_sample=FLAGS.batchsize//4,
)
#train_input_fn = lambda: dset().make_one_shot_iterator().get_next()
train_input_fn = dset
if FLAGS.testset:
eval_dset = ds.get_dataset(FLAGS.testset,
path=FLAGS.datapath,
epochs=1,
degrade_fn=degrade_fn,
batchsize=FLAGS.batchsize,
shuffle=False
)
#eval_input_fn = lambda: eval_dset().make_one_shot_iterator().get_next()
eval_input_fn = eval_dset
if FLAGS.multigpu:
config = argshelper.distribute.multi_gpu_config(
log_step_count_steps=FLAGS.log_step_count_steps)
else:
config = tf.estimator.RunConfig(
log_step_count_steps=FLAGS.log_step_count_steps)
if FLAGS.save_checkpoints_steps:
config = config.replace(save_checkpoints_steps=FLAGS.save_checkpoints_steps)
if FLAGS.save_checkpoints_secs:
config = config.replace(save_checkpoints_secs=FLAGS.save_checkpoints_secs)
if FLAGS.usexla:
sess_config = tf.ConfigProto()
sess_config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1
config = config.replace(session_config=sess_config)
_summaries = []
if FLAGS.audio_sample_rate > 0:
_summaries.append(summaries.audio_sample_summary(FLAGS.audio_sample_rate))
elif FLAGS.audio_sample_rate == 0:
raise NotImplementedError('Automatic sample rate determination is not'
'yet implemented')
_summaries.append(summaries.audio_spectrogram_summary())
if FLAGS.spectral_copies:
net_config = nets.build_net(FLAGS.objective, FLAGS.downsample_rate,
time_params=argshelper.get_time_params(),
freq_params=argshelper.get_freq_params(),
window_length=FLAGS.window_length,
transform=FLAGS.transform,
fusion_op=FLAGS.fusion_op,
)
else:
net_config = nets.build_net(FLAGS.objective,
time_params=argshelper.get_time_params(),
freq_params=argshelper.get_freq_params(),
window_length=FLAGS.window_length,
transform=FLAGS.transform,
fusion_op=FLAGS.fusion_op,
)
if FLAGS.learning_rate_decay:
learning_rate = lambda: tf.train.polynomial_decay(FLAGS.learning_rate,
end_learning_rate=1e-6,
global_step=tf.train.get_global_step(),
decay_steps=500000,
power=0.5)
else:
learning_rate = lambda: FLAGS.learning_rate
optimizers = {'adam': lambda: tf.train.AdamOptimizer(learning_rate=learning_rate()),
'sgd': lambda: tf.train.GradientDescentOptimizer(learning_rate=learning_rate()),
}
print('------------------------------------------')
print('Save steps: {}, ({}s)'.format(config.save_checkpoints_steps,
config.save_checkpoints_secs))
print('------------------------------------------')
tfnet_est = TFNetEstimator(**net_config,
model_dir=FLAGS.model_dir,
add_summaries=_summaries,
optimizer=optimizers[FLAGS.optimizer],
weight_decay=FLAGS.weight_decay,
config=config)
hooks = []
if FLAGS.profile:
hooks += [tf.train.ProfilerHook(output_dir=FLAGS.model_dir,
save_steps=500,
show_memory=False),
]
if FLAGS.enable_tracer:
try:
from tftracer import TracingServer
tracing_server = TracingServer(server_port=8888)
hooks += [tracing_server.hook]
except ImportError:
tf.logging.warn("tensorflow-tracer not available. Will not be "
"enabled")
if FLAGS.testset:
#eval_summary_hook = tf.train.SummarySaverHook(
# save_steps=1,
# summary_op=tf.summary.merge_all('audio_samples'))
train_spec = tf.estimator.TrainSpec(train_input_fn, hooks=hooks)
eval_spec = tf.estimator.EvalSpec(eval_input_fn,
steps=None,
#hooks=[eval_summary_hook]
)
while True:
try:
tf.estimator.train_and_evaluate(estimator=tfnet_est,
train_spec=train_spec,
eval_spec=eval_spec)
except tf.estimator.NanLossDuringTrainingError:
tf.logging.warn("NaN loss encountered. Attempting to continue")
continue
break
else:
tfnet_est.train(input_fn=train_input_fn,
hooks=hooks
)
if __name__ == '__main__':
tf.app.run(main)