def decode(): """Decoding the inputs using current model.""" tf.logging.info("Get TEST sets number.") num_batch = get_num_batch(FLAGS.test_list_file, infer=True) with tf.Graph().as_default(): with tf.device('/cpu:0'): with tf.name_scope('input'): data_list = read_list(FLAGS.test_list_file) test_utt_id, test_inputs, _ = get_batch( data_list, batch_size=1, input_size=FLAGS.input_dim, output_size=FLAGS.output_dim, left=FLAGS.left_context, right=FLAGS.right_context, num_enqueuing_threads=FLAGS.num_threads, num_epochs=1, infer=True) # test_inputs = tf.squeeze(test_inputs, axis=[0]) devices = [] for i in xrange(FLAGS.num_gpu): device_name = ("/gpu:%d" % i) print('Using device: ', device_name) devices.append(device_name) # Prevent exhausting all the gpu memories. config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.4 #config.gpu_options.allow_growth = True config.allow_soft_placement = True set_session(tf.Session(config=config)) # execute the session with tf.Session(config=config) as sess: # Create two models with tr_inputs and cv_inputs individually. with tf.name_scope('model'): model = DNNTrainer(sess, FLAGS, devices, test_inputs, labels=None, cross_validation=True) show_all_variables() init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) print("Initializing variables ...") sess.run(init) if model.load(model.save_dir, moving_average=False): print("[*] Load Moving Average model SUCCESS") else: print("[!] Load failed. Checkpoint not found. Exit now.") sys.exit(1) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) cmvn_filename = os.path.join(FLAGS.data_dir, "train_cmvn.npz") if os.path.isfile(cmvn_filename): cmvn = np.load(cmvn_filename) else: tf.logging.fatal("%s not exist, exit now." % cmvn_filename) sys.exit(1) out_dir_name = os.path.join('/Work18/2017/linan/SE/my_enh', FLAGS.save_dir, FLAGS.savetestdir) # out_dir_name = os.path.join(FLAGS.save_dir, 'test') if not os.path.exists(out_dir_name): os.makedirs(out_dir_name) write_scp_path = os.path.join(out_dir_name, 'feats.scp') write_ark_path = os.path.join(out_dir_name, 'feats.ark') writer = ArkWriter(write_scp_path) outputs = model.generator(test_inputs, None, reuse=True) outputs = tf.reshape(outputs, [-1, model.output_dim]) print('shape is', np.shape(outputs)) try: for batch in range(num_batch): if coord.should_stop(): break # outputs = model.generator(test_inputs, None, reuse=True) # outputs = tf.reshape(outputs, [-1, model.output_dim]) utt_id, activations = sess.run([test_utt_id, outputs]) # sequence = activations * cmvn['stddev_labels'] + \ # cmvn['mean_labels'] sequence = activations save_result = np.vstack(sequence) dir_load = FLAGS.savetestdir dir_load = dir_load.split('/')[-1] mode = FLAGS.mode if mode == 'use_org': inputs_path = os.path.join( 'workspace/features/spectrogram/test', dir_load, '%s.wav.p' % utt_id[0]) data = cPickle.load(open(inputs_path, 'rb')) [mixed_complx_x] = data #tf.logging.info("Write inferred %s to %s" %(utt_id[0], np.shape(save_result))) save_result = np.exp(save_result) n_window = cfg.n_window s = recover_wav(save_result, mixed_complx_x, cfg.n_overlap, np.hamming) s *= np.sqrt( (np.hamming(n_window)**2 ).sum()) # Scaler for compensate the amplitude # change after spectrogram and IFFT. print("start enhance wav file") # Write out enhanced wav. out_path = os.path.join("workspace", "enh_wavs", "test", dir_load, "%s.enh.wav" % utt_id[0]) print("have enhanced all the wav") pp_data.create_folder(os.path.dirname(out_path)) pp_data.write_audio(out_path, s, 16000) elif mode == 'g_l': inputs_path = os.path.join( 'workspace/features/spectrogram/test', dir_load, '%s.wav.p' % utt_id[0]) data = cPickle.load(open(inputs_path, 'rb')) [mixed_complx_x] = data save_result = np.exp(save_result) s = save_result s = audio_utilities.reconstruct_signal_griffin_lim( s, mixed_complx_x, 512, 256, 15) #s = recover_wav(save_result,mixed_complx_x,cfg.n_overlap, np.hamming) s *= np.sqrt((np.hamming(cfg.n_window)**2).sum()) #s = audio._griffin_lim(s) out_path = os.path.join("workspace", "enh_wavs", "test2", dir_load, "%s.enh.wav" % utt_id[0]) pp_data.create_folder(os.path.dirname(out_path)) pp_data.write_audio(out_path, s, 16000) tf.logging.info("Write inferred%s" % (np.shape(s))) #writer.write_next_utt(write_ark_path, utt_id[0], save_result) tf.logging.info("Write inferred %s to %s" % (utt_id[0], out_path)) except Exception, e: # Report exceptions to the coordinator. coord.request_stop(e) finally:
def decode(): """Decoding the inputs using current model.""" tf.logging.info("Get TEST sets number.") num_batch = get_num_batch(FLAGS.test_list_file, infer=True) with tf.Graph().as_default(): with tf.device('/cpu:0'): with tf.name_scope('input'): data_list = read_list(FLAGS.test_list_file) test_utt_id, test_inputs, _ = get_batch( data_list, batch_size=1, input_size=FLAGS.input_dim, output_size=FLAGS.output_dim, left=FLAGS.left_context, right=FLAGS.right_context, num_enqueuing_threads=FLAGS.num_threads, num_epochs=1, infer=True) devices = [] for i in xrange(FLAGS.num_gpu): device_name = ("/gpu:%d" % i) print('Using device: ', device_name) devices.append(device_name) # Prevent exhausting all the gpu memories. config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True # execute the session with tf.Session(config=config) as sess: # Create two models with tr_inputs and cv_inputs individually. with tf.name_scope('model'): model = GAN(sess, FLAGS, devices, test_inputs, labels=None, cross_validation=True) show_all_variables() init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) print("Initializing variables ...") sess.run(init) if model.load(model.save_dir, moving_average=True): print("[*] Load SUCCESS") else: print("[!] Load failed. Checkpoint not found. Exit now.") sys.exit(1) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) cmvn_filename = os.path.join(FLAGS.data_dir, "train_cmvn.npz") if os.path.isfile(cmvn_filename): cmvn = np.load(cmvn_filename) else: tf.logging.fatal("%s not exist, exit now." % cmvn_filename) sys.exit(1) out_dir_name = os.path.join(FLAGS.save_dir, 'test') if not os.path.exists(out_dir_name): os.makedirs(out_dir_name) write_scp_path = os.path.join(out_dir_name, 'feats.scp') write_ark_path = os.path.join(out_dir_name, 'feats.ark') writer = ArkWriter(write_scp_path) try: for batch in range(num_batch): if coord.should_stop(): break outputs = model.generator(test_inputs, None, reuse=True) outputs = tf.reshape(outputs, [-1, model.output_dim]) utt_id, activations = sess.run([test_utt_id, outputs]) sequence = activations * cmvn['stddev_labels'] + \ cmvn['mean_labels'] save_result = np.vstack(sequence) writer.write_next_utt(write_ark_path, utt_id[0], save_result) tf.logging.info("Write inferred %s to %s" % (utt_id[0], write_ark_path)) except Exception, e: # Report exceptions to the coordinator. coord.request_stop(e) finally:
def decode(): """Decoding the inputs using current model.""" tf.logging.info("Get TEST sets number.") num_batch = get_num_batch(FLAGS.test_list_file, infer=True) with tf.Graph().as_default(): with tf.device('/cpu:0'): with tf.name_scope('input'): data_list = read_list(FLAGS.test_list_file) test_utt_id, test_inputs, \ lengths = get_batch(data_list, batch_size=1, input_size=FLAGS.input_dim, output_size=FLAGS.output_dim, left_context=FLAGS.left_context, right_context=FLAGS.right_context, num_threads=FLAGS.num_threads, num_epochs=1, infer=True) devices = [] for i in range(FLAGS.num_gpu): device_name = ("/gpu:%d" % i) print('Using device: ', device_name) devices.append(device_name) # Prevent exhausting all the gpu memories. config = tf.ConfigProto() #config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.45 config.allow_soft_placement = True # execute the session with tf.Session(config=config) as sess: # Create two models with tr_inputs and cv_inputs individually. with tf.name_scope('model'): model = RNNTrainer(sess, FLAGS, devices, test_inputs, labels=None, lengths=lengths, cross_validation=True) show_all_variables() init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) print("Initializing variables ...") sess.run(init) if model.load(model.save_dir, moving_average=False): print("[*] Load model SUCCESS") else: print("[!] Load failed. Checkpoint not found. Exit now.") sys.exit(1) cmvn_filename = os.path.join(FLAGS.data_dir, "train_cmvn.npz") if os.path.isfile(cmvn_filename): cmvn = np.load(cmvn_filename) else: tf.logging.fatal("%s not exist, exit now." % cmvn_filename) sys.exit(1) out_dir_name = os.path.join(FLAGS.save_dir, 'test') if not os.path.exists(out_dir_name): os.makedirs(out_dir_name) write_scp_path = os.path.join(out_dir_name, 'feats.scp') write_ark_path = os.path.join(out_dir_name, 'feats.ark') writer = ArkWriter(write_scp_path) start = datetime.datetime.now() outputs = model.generator(test_inputs, None, lengths, reuse=True) outputs = tf.reshape(outputs, [-1, model.output_dim]) for batch in range(num_batch): try: # outputs = model.generator(test_inputs, None, lengths, reuse=True) # outputs = tf.reshape(outputs, [-1, model.output_dim]) utt_id, activations = sess.run([test_utt_id, outputs]) sequence = activations * cmvn['stddev_labels'] + \ cmvn['mean_labels'] save_result = np.vstack(sequence) writer.write_next_utt(write_ark_path, utt_id[0], save_result) print("[{}/{}] Write inferred {} to {}".format( batch + 1, num_batch, utt_id[0], write_ark_path)) sys.stdout.flush() except tf.errors.OutOfRangeError: tf.logging.error( "Out of range error occured when decoding.") break sess.close() end = datetime.datetime.now() duration = (end - start).total_seconds() print("Decoding time is {}s".format(duration)) sys.stdout.flush() tf.logging.info("Decoding Done.")