def main(_): print('Parsed arguments: ', FLAGS.__flags) # make save path if it is required if not os.path.exists(FLAGS.save_path): os.makedirs(FLAGS.save_path) if not os.path.exists(FLAGS.synthesis_path): os.makedirs(FLAGS.synthesis_path) np.random.seed(FLAGS.seed) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True udevices = [] for device in devices: if len(devices) > 1 and 'CPU' in device.name: # Use cpu only when we dont have gpus # udevices.append(device.name) continue print('Using device: ', device.name) udevices.append(device.name) # execute the session with tf.Session(config=config) as sess: if FLAGS.model == 'gan': print('Creating GAN model') se_model = SEGAN(sess, FLAGS, udevices) elif FLAGS.model == 'ae': print('Creating AE model') se_model = SEAE(sess, FLAGS, udevices) else: raise ValueError('{} model type not understood!'.format( FLAGS.model)) if FLAGS.test_wav is None: se_model.train(FLAGS, udevices) else: if FLAGS.weights is None: raise ValueError('weights must be specified!') print('Loading model weights...') se_model.load(FLAGS.save_path, FLAGS.weights) fm, wav_data = wavfile.read(FLAGS.test_wav) print(fm) wavname = FLAGS.test_wav.split('/')[-1] if fm != 16000: raise ValueError('16kHz required! Test file is different') wave = (2. / 65535.) * (wav_data.astype(np.float32) - 32767) + 1. if FLAGS.preemph > 0: print('preemph test wave with {}'.format(FLAGS.preemph)) x_pholder, preemph_op = pre_emph_test(FLAGS.preemph, wave.shape[0]) wave = sess.run(preemph_op, feed_dict={x_pholder: wave}) print('test wave shape: ', wave.shape) print('test wave min:{} max:{}'.format(np.min(wave), np.max(wave))) c_wave = se_model.clean(wave) print('c wave min:{} max:{}'.format(np.min(c_wave), np.max(c_wave))) wavfile.write(os.path.join(FLAGS.save_clean_path, wavname), 16000, c_wave) print('Done cleaning {} and saved ' 'to {}'.format(FLAGS.test_wav, os.path.join(FLAGS.save_clean_path, wavname)))
def main(_): print('Parsed arguments: ', FLAGS.__flags) # make save path if it is required if not os.path.exists(FLAGS.save_path): os.makedirs(FLAGS.save_path) if not os.path.exists(FLAGS.synthesis_path): os.makedirs(FLAGS.synthesis_path) np.random.seed(FLAGS.seed) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement=True udevices = [] for device in devices: if len(devices) > 1 and 'cpu' in device.name: # Use cpu only when we dont have gpus continue print('Using device: ', device.name) udevices.append(device.name) # execute the session with tf.Session(config=config) as sess: if FLAGS.model == 'gan': print('Creating GAN model') se_model = SEGAN(sess, FLAGS, udevices) elif FLAGS.model == 'ae': print('Creating AE model') se_model = SEAE(sess, FLAGS, udevices) else: raise ValueError('{} model type not understood!'.format(FLAGS.model)) if FLAGS.test_wavs is None: se_model.train(FLAGS, udevices) else: if FLAGS.weights is None: raise ValueError('weights must be specified!') print('Loading model weights...') se_model.load(FLAGS.save_path, FLAGS.weights) FLAGS.test_wavs = FLAGS.test_wavs.split(',') for i in range(len(FLAGS.test_wavs)): wav_data, fm = librosa.load(FLAGS.test_wavs[i], sr=16000) wavname = FLAGS.test_wavs[i].split('/')[-1].split('.')[0] +'_segan.wav' wave = (2./65535.) * (wav_data.astype(np.float32) - 32767) + 1. if FLAGS.preemph > 0: print('preemph test wave with {}'.format(FLAGS.preemph)) x_pholder, preemph_op = pre_emph_test(FLAGS.preemph, wav_data.shape[0]) wave = sess.run(preemph_op, feed_dict={x_pholder: wav_data}) print("【*】Clean start time ...", time.asctime(time.localtime(time.time()))) c_wave = se_model.clean(wave) print("【*】Clean end time ...", time.asctime(time.localtime(time.time()))) print('c wave min:{} max:{}'.format(np.min(c_wave), np.max(c_wave))) print(c_wave) librosa.output.write_wav(os.path.join(FLAGS.save_clean_path, wavname), c_wave, sr=16000) print('Done cleaning {} and saved ' 'to {}'.format(FLAGS.test_wavs[i], os.path.join(FLAGS.save_clean_path, wavname)))
def main(_): print('Parsed arguments: ', FLAGS.__flags) # make save path if it is required if not os.path.exists(FLAGS.save_path): os.makedirs(FLAGS.save_path) if not os.path.exists(FLAGS.synthesis_path): os.makedirs(FLAGS.synthesis_path) np.random.seed(FLAGS.seed) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement=True udevices = [] for device in devices: if len(devices) > 1 and 'cpu' in device.name: # Use cpu only when we dont have gpus continue print('Using device: ', device.name) udevices.append(device.name) # execute the session with tf.Session(config=config) as sess: if FLAGS.model == 'gan': print('Creating GAN model') se_model = SEGAN(sess, FLAGS, udevices) elif FLAGS.model == 'ae': print('Creating AE model') se_model = SEAE(sess, FLAGS, udevices) else: raise ValueError('{} model type not understood!'.format(FLAGS.model)) if FLAGS.test_wav is None: se_model.train(FLAGS, udevices) else: if FLAGS.weights is None: raise ValueError('weights must be specified!') print('Loading model weights...') se_model.load(FLAGS.save_path, FLAGS.weights) fm, wav_data = wavfile.read(FLAGS.test_wav) wavname = FLAGS.test_wav.split('/')[-1] if fm != 16000: raise ValueError('16kHz required! Test file is different') wave = (2./65535.) * (wav_data.astype(np.float32) - 32767) + 1. if FLAGS.preemph > 0: print('preemph test wave with {}'.format(FLAGS.preemph)) x_pholder, preemph_op = pre_emph_test(FLAGS.preemph, wave.shape[0]) wave = sess.run(preemph_op, feed_dict={x_pholder:wave}) print('test wave shape: ', wave.shape) print('test wave min:{} max:{}'.format(np.min(wave), np.max(wave))) c_wave = se_model.clean(wave) print('c wave min:{} max:{}'.format(np.min(c_wave), np.max(c_wave))) wavfile.write(os.path.join(FLAGS.save_clean_path, wavname), 16e3, c_wave) print('Done cleaning {} and saved ' 'to {}'.format(FLAGS.test_wav, os.path.join(FLAGS.save_clean_path, wavname)))
def main(_): print('Parsed arguments: ', FLAGS.__flags) # make save path if it is required if not os.path.exists(FLAGS.save_path): os.makedirs(FLAGS.save_path) if not os.path.exists(FLAGS.synthesis_path): os.makedirs(FLAGS.synthesis_path) np.random.seed(FLAGS.seed) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True udevices = [] for device in devices: if len(devices) > 1 and 'CPU' in device.name: # Use cpu only when we dont have gpus continue print('Using device: ', device.name) udevices.append(device.name) print("!!!!!!", udevices) # execute the session with tf.Session(config=config) as sess: if FLAGS.squeeze_generator: print('Creating Classifier') se_model = CLASSIFY(sess, FLAGS, udevices) elif FLAGS.model == 'gan': print('Creating GAN model') se_model = SEGAN(sess, FLAGS, udevices) elif FLAGS.model == 'ae': print('Creating AE model') se_model = SEAE(sess, FLAGS, udevices) else: raise ValueError('{} model type not understood!'.format( FLAGS.model)) if FLAGS.test_wav is None: se_model.train(FLAGS, udevices) else: if FLAGS.weights is None: raise ValueError('weights must be specified!') print('Loading model weights...') se_model.load(FLAGS.save_path, FLAGS.weights) if FLAGS.ref_wav is None: wave = read_wave(sess, FLAGS.test_wav) c_wave = se_model.clean(wave) print('c wave min:{} max:{}'.format(np.min(c_wave), np.max(c_wave))) wavfile.write(os.path.join(FLAGS.save_clean_path, wavname), 16000, c_wave) print('Done cleaning {} and saved ' 'to {}'.format( FLAGS.test_wav, os.path.join(FLAGS.save_clean_path, wavname))) else: files = [] if os.path.isdir(FLAGS.test_wav): for filename in os.listdir(FLAGS.test_wav): if filename.endswith(".wav"): files.append(os.path.join(FLAGS.test_wav, filename)) else: files.append(FLAGS.test_wav) for f in files: refs = [] if os.path.isdir(FLAGS.ref_wav): m = s3re.search(f) if m: vid = m.group(1) for filename in os.listdir(FLAGS.ref_wav): if filename.endswith(".wav") and filename.find( "s3_" + vid) != -1: refs.append( os.path.join(FLAGS.ref_wav, filename)) else: refs.append(FLAGS.ref_wav) score = [] wav_signals = read_signals(sess, f, FLAGS.canvas_size) if wav_signals is not None: for r in refs: ref_signals = read_signals(sess, r, FLAGS.canvas_size) if ref_signals is not None: logit = se_model.classify( wav_signals, ref_signals) score.append(str(logit)) print("RES\t", f, "\t".join(score))
def main(_): print('Parsed arguments: ', FLAGS.__flags) # make save path if it is required if not os.path.exists(FLAGS.save_path): os.makedirs(FLAGS.save_path) if not os.path.exists(FLAGS.synthesis_path): os.makedirs(FLAGS.synthesis_path) np.random.seed(FLAGS.seed) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True udevices = [] for device in devices: if len(devices) > 1 and 'cpu' in device.name: # Use cpu only when we dont have gpus continue print('Using device: ', device.name) udevices.append(device.name) # execute the session with tf.Session(config=config) as sess: if FLAGS.model == 'gan': print('Creating GAN model') se_model = SEGAN(sess, FLAGS, udevices) elif FLAGS.model == 'ae': print('Creating AE model') se_model = SEAE(sess, FLAGS, udevices) else: raise ValueError('{} model type not understood!'.format( FLAGS.model)) folders = [] for depth in range(FLAGS.maxdepth): path_str = '*' + os.sep path_str = os.path.join(FLAGS.in_dir, path_str * depth) folders += glob(path_str) root_folder = os.path.basename( os.path.dirname(os.path.join(FLAGS.in_dir, ''))) if FLAGS.weights is None: raise ValueError('weights must be specified!') print('Loading model weights...') se_model.load(FLAGS.save_path, FLAGS.weights) for fld in tqdm(folders): files = sorted(glob(os.path.join(fld, '*.wav'))) for fle in files: fm, wav_data = wavfile.read(fle) wavname = fle.split('/')[-1] if fm != 16000: #raise ValueError('16kHz required! Test file is different') number_of_samples = int( round(len(wav_data) * float(16000) / fm)) wav_data = sps.resample(wav_data, number_of_samples) wave = (2. / 65535.) * (wav_data.astype(np.float32) - 32767) + 1. if FLAGS.preemph > 0: print('preemph test wave with {}'.format(FLAGS.preemph)) x_pholder, preemph_op = pre_emph_test( FLAGS.preemph, wave.shape[0]) try: wave = sess.run(preemph_op, feed_dict={x_pholder: wave}) except: print('Error processing %s' % wavname) continue print('test wave shape: ', wave.shape) print('test wave min:{} max:{}'.format( np.min(wave), np.max(wave))) c_wave = se_model.clean(wave) print('c wave min:{} max:{}'.format(np.min(c_wave), np.max(c_wave))) save_path = os.path.join( FLAGS.save_clean_path, fle[fle.find(root_folder) + len(root_folder) + 1:]) save_dir = os.path.dirname(save_path) if not os.path.exists(save_dir): os.makedirs(save_dir) wavfile.write(save_path, 16e3, c_wave) print('Done cleaning {} and saved ' 'to {}'.format(wavname, save_path))
def main(_): if FLAGS.feature_type == 'wavform': from model import SEGAN, SEAE elif FLAGS.feature_type == 'logspec': from spec_model import SEGAN, SEAE print('Parsed arguments: ', FLAGS.__flags) # make save path if it is required if not os.path.exists(FLAGS.save_path): os.makedirs(FLAGS.save_path) if not os.path.exists(FLAGS.synthesis_path): os.makedirs(FLAGS.synthesis_path) np.random.seed(FLAGS.seed) #gpu_options = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.gpu_options.allocator_type = 'BFC' udevices = [] for device in devices: print("Device lists:{}".format(devices)) if len(devices) > 1 and 'cpu' in device.name: # Use cpu only when we dont have gpus continue print('Using device: ', device.name) udevices.append(device.name) print("device:{}".format(udevices)) # execute the session with tf.Session(config=config) as sess: if FLAGS.model == 'gan': print('Creating GAN model') se_model = SEGAN(sess, FLAGS, udevices) elif FLAGS.model == 'ae': print('Creating AE model') se_model = SEAE(sess, FLAGS, udevices) else: raise ValueError('{} model type not understood!'.format( FLAGS.model)) if FLAGS.test_wav is None: mode = 'stage2' se_model.train(FLAGS, udevices, mode) else: if FLAGS.weights is None: raise ValueError('weights must be specified!') print('Loading model weights...') se_model.load(FLAGS.save_path, FLAGS.weights) noisy_test_filelist = [] for (dirpath, dirnames, filenames) in os.walk(FLAGS.test_wav): # print('dirpath = ' + dirpath) for filename in filenames: file_path = os.path.join(dirpath, filename) noisy_test_filelist.append(file_path) nlist = noisy_test_filelist for name in nlist: t1 = time.time() fm, wav_data = wavfile.read(name) wavname = name.split('/')[-1] if fm != 16000: raise ValueError('16kHz required! Test file is different') #import librosa #print('16kHz is required: test file is {}kHz, have to resample to the required samplerate') #wav_data = librosa.resample(wav_data, fm, 16000) if FLAGS.feature_type == 'wavform': wave = (2. / 65535.) * (wav_data.astype(np.float32) - 32767) + 1. if FLAGS.preemph > 0: print('preemph test wave with {}'.format( FLAGS.preemph)) x_pholder, preemph_op = pre_emph_test( FLAGS.preemph, wave.shape[0]) wave = sess.run(preemph_op, feed_dict={x_pholder: wave}) print('test wave shape: ', wave.shape) print('test wave min:{} max:{}'.format( np.min(wave), np.max(wave))) c_wave = se_model.clean(wave) print('c wave min:{} max:{}'.format( np.min(c_wave), np.max(c_wave))) wavfile.write( os.path.join(FLAGS.save_clean_path, wavname), 16e3, np.int16(c_wave * 32767)) #(0.9*c_wave/max(abs(c_wave))) t2 = time.time( ) #np.int16((1.0*c_wave/max(abs(c_wave)))*32767) print('Done cleaning {}/{}s and saved ' 'to {}'.format( name, t2 - t1, os.path.join(FLAGS.save_clean_path, wavname))) if FLAGS.feature_type == 'logspec': if wav_data.dtype != 'float32': wave = np.float32(wav_data / 32767.) #wave = (2./65535.) * (wav_data.astype(np.float32) - 32767) + 1. if FLAGS.preemph > 0: print('preemph test wave with {}'.format( FLAGS.preemph)) x_pholder, preemph_op = pre_emph_test( FLAGS.preemph, wave.shape[0]) wave = sess.run(preemph_op, feed_dict={x_pholder: wave}) print('test wave shape: ', wave.shape) print('test wave min:{} max:{}'.format( np.min(wave), np.max(wave))) c_wave = se_model.clean(wave) print('c wave min:{} max:{}'.format( np.min(c_wave), np.max(c_wave))) wavfile.write( os.path.join(FLAGS.save_clean_path, wavname), 16e3, np.int16(c_wave * 32767)) #(0.9*c_wave/max(abs(c_wave))) t2 = time.time() print('Done cleaning {}/{}s and saved ' 'to {}'.format( name, t2 - t1, os.path.join(FLAGS.save_clean_path, wavname))) '''
def model_fn(features, labels, mode, params): print('Creating GAN model') print(features["wav_and_noisy"]) wavbatch = labels input_var = None # if mode == tf.estimator.ModeKeys.PREDICT: # # var = tf.compat.v1.get_variable("wav_and_noisy", [FLAGS.batch_size, FLAGS.canvas_size], tf.float32, initializer=tf.zeros_initializer()) # # var_update_rows = tf.compat.v1.scatter_update(var, [0], features["wav_and_noisy"]) # var_update_rows = features["wav_and_noisy"] # var_update_rows.set_shape([FLAGS.batch_size, FLAGS.canvas_size]) # input_var = {"wav_and_noisy":var_update_rows} # else: # input_var = features input_var = tf.reshape(features["wav_and_noisy"], shape=[-1, FLAGS.canvas_size]) se_model = SEGAN(None, params, ['/gpu:0'], input_var, wavbatch) if mode == tf.estimator.ModeKeys.PREDICT: # var_update_rows = tf.compat.v1.scatter_update(var, [0], t[0]) G, _ = se_model.generator(input_var, is_ref=False, spk=None, do_prelu=False) # wavbatch = labels # fake_feature = {'input_noise',tf.placeholder(dtype=tf.float32, shape=[None,], name='input_noise')} # se_model = SEGAN(None, params, ['/GPU:0'], features, wavbatch) # wav_data = features["input_noise"] # wave = (2./65535.) * (wav_data.astype(np.float32) - 32767) + 1. # if params.preemph > 0: # print('preemph test wave with {}'.format(params.preemph)) # # x_pholder, preemph_op = pre_emph_test(FLAGS.preemph, wave.shape[0]) # # wave = sess.run(preemph_op, feed_dict={x_pholder: wave}) # wave = pre_emph(wave, params.preemph) # print('test wave shape: ', wave.shape) # print('test wave min:{} max:{}'.format(np.min(wave), np.max(wave))) # c_wave = se_model.clean(wave) # print('c wave min:{} max:{}'.format(np.min(c_wave), np.max(c_wave))) # se_model.Gs[0](features['input_noise'],) predictions = {"clean_wav": G} export_outputs = { tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: tf.estimator.export.PredictOutput(predictions)} return tf.estimator.EstimatorSpec( mode=mode, predictions=predictions, export_outputs=export_outputs ) elif mode == tf.estimator.ModeKeys.TRAIN: train_op = None train_op = tf.group(se_model.d_opt, se_model.g_opt) g_loss = se_model.g_losses[-1] d_loss = se_model.d_losses[-1] tf.summary.scalar('g_loss', g_loss) tf.summary.scalar('d_loss', d_loss) predictions = {"clean_wav": se_model.Gs[-1]} return tf.estimator.EstimatorSpec( mode=mode, predictions=predictions, loss=g_loss+d_loss, train_op=train_op ) else: print("wrong!! invalid mode: {}".format(mode))