def main(unused_argv=None): os.environ["CUDA_VISIBLE_DEVICES"] = str(FLAGS.gpu_number) source_path = utils.shell_path(FLAGS.source_path) checkpoint_path = utils.shell_path(FLAGS.checkpoint_path) save_path = utils.shell_path(FLAGS.save_path) if not save_path: raise RuntimeError("Must specify a save_path.") tf.logging.set_verbosity(FLAGS.log) # Generate from wav files if tf.gfile.IsDirectory(source_path): files = tf.gfile.ListDirectory(source_path) exts = [os.path.splitext(f)[1] for f in files] if ".wav" in exts: postfix = ".wav" elif ".npy" in exts: postfix = ".npy" else: raise RuntimeError("Folder must contain .wav or .npy files.") postfix = ".npy" if FLAGS.npy_only else postfix files = sorted([ os.path.join(source_path, fname) for fname in files if fname.lower().endswith(postfix) ]) elif source_path.lower().endswith((".wav", ".npy")): files = [source_path] else: files = [] # Now synthesize from files one batch at a time batch_size = FLAGS.batch_size sample_length = FLAGS.sample_length n = len(files) for start in range(0, n, batch_size): end = start + batch_size batch_files = files[start:end] save_names = [ os.path.join( save_path, "gen_" + os.path.splitext(os.path.basename(f))[0] + ".wav") for f in batch_files ] print('loading batch..') batch_data = fastgen.load_batch(batch_files, sample_length=sample_length) # Encode waveforms encodings = batch_data if postfix == ".npy" else fastgen.encode( batch_data, checkpoint_path, sample_length=sample_length) if FLAGS.gpu_number != 0: with tf.device("/device:GPU:%d" % FLAGS.gpu_number): fastgen.synthesize(encodings, save_names, checkpoint_path=checkpoint_path) else: fastgen.synthesize(encodings, save_names, checkpoint_path=checkpoint_path)
def main(unused_argv=None): os.environ["CUDA_VISIBLE_DEVICES"] = str(FLAGS.gpu_number) source_path = utils.shell_path(FLAGS.source_path) checkpoint_path = utils.shell_path(FLAGS.checkpoint_path) save_path = utils.shell_path(FLAGS.save_path) if not save_path: raise RuntimeError("Must specify a save_path.") tf.logging.set_verbosity(FLAGS.log) # Generate from wav files if tf.gfile.IsDirectory(source_path): files = tf.gfile.ListDirectory(source_path) exts = [os.path.splitext(f)[1] for f in files] if ".wav" in exts: postfix = ".wav" elif ".npy" in exts: postfix = ".npy" else: raise RuntimeError("Folder must contain .wav or .npy files.") postfix = ".npy" if FLAGS.npy_only else postfix files = sorted([ os.path.join(source_path, fname) for fname in files if fname.lower().endswith(postfix) ]) elif source_path.lower().endswith((".wav", ".npy")): files = [source_path] else: files = [] # Now synthesize from files one batch at a time batch_size = FLAGS.batch_size sample_length = FLAGS.sample_length n = len(files) for start in range(0, n, batch_size): end = start + batch_size batch_files = files[start:end] save_names = [ os.path.join(save_path, "gen_" + os.path.splitext(os.path.basename(f))[0] + ".wav") for f in batch_files ] batch_data = fastgen.load_batch(batch_files, sample_length=sample_length) # Encode waveforms encodings = batch_data if postfix == ".npy" else fastgen.encode( batch_data, checkpoint_path, sample_length=sample_length) if FLAGS.gpu_number != 0: with tf.device("/device:GPU:%d" % FLAGS.gpu_number): fastgen.synthesize( encodings, save_names, checkpoint_path=checkpoint_path) else: fastgen.synthesize(encodings, save_names, checkpoint_path=checkpoint_path)
def main(unused_argv=None): source_path = utils.shell_path(FLAGS.source_path) checkpoint_path = utils.shell_path(FLAGS.checkpoint_path) save_path = utils.shell_path(FLAGS.save_path) if not save_path: raise RuntimeError("Must specify a save_path.") tf.logging.set_verbosity(FLAGS.log) # Generate from wav files if tf.gfile.IsDirectory(source_path): files = tf.gfile.ListDirectory(source_path) exts = [os.path.splitext(f)[1] for f in files] if ".wav" in exts: postfix = ".wav" elif ".npy" in exts: postfix = ".npy" else: raise RuntimeError("Folder must contain .wav or .npy files.") postfix = ".npy" if FLAGS.encodings else postfix files = sorted([ os.path.join(source_path, fname) for fname in files if fname.lower().endswith(postfix) ]) elif source_path.lower().endswith(postfix): files = [source_path] else: files = [] for f in files: out_file = os.path.join( save_path, "gen_" + os.path.splitext(os.path.basename(f))[0] + ".wav") tf.logging.info("OUTFILE %s" % out_file) synthesize(source_file=f, checkpoint_path=checkpoint_path, out_file=out_file, sample_length=FLAGS.sample_length)
def main(unused_argv=None): os.environ["CUDA_VISIBLE_DEVICES"] = str(FLAGS.gpu_number) source_path = utils.shell_path(FLAGS.source_path) checkpoint_path = utils.shell_path(FLAGS.checkpoint_path) save_path = utils.shell_path(FLAGS.save_path) if not save_path: raise ValueError("Must specify a save_path.") tf.logging.set_verbosity(FLAGS.log) # Use directory of files if tf.gfile.IsDirectory(source_path): files = tf.gfile.ListDirectory(source_path) file_extensions = [os.path.splitext(f)[1] for f in files] if ".wav" in file_extensions: file_extension = ".wav" elif ".npy" in file_extensions: file_extension = ".npy" else: raise RuntimeError("Folder must contain .wav or .npy files.") file_extension = ".npy" if FLAGS.npy_only else file_extension files = sorted([ os.path.join(source_path, fname) for fname in files if fname.lower().endswith(file_extension) ]) # Use a single file elif source_path.lower().endswith((".wav", ".npy")): file_extension = os.path.splitext(source_path.lower())[1] files = [source_path] else: raise ValueError( "source_path {} must be a folder or file.".format(source_path)) # Now synthesize from files one batch at a time batch_size = FLAGS.batch_size sample_length = FLAGS.sample_length n = len(files) for start in range(0, n, batch_size): end = start + batch_size batch_files = files[start:end] save_names = [ os.path.join( save_path, "gen_" + os.path.splitext(os.path.basename(f))[0] + ".wav") for f in batch_files ] # Encode waveforms if file_extension == ".wav": batch_data = fastgen.load_batch_audio(batch_files, sample_length=sample_length) encodings = fastgen.encode(batch_data, checkpoint_path, sample_length=sample_length) # Or load encodings else: encodings = fastgen.load_batch_encodings( batch_files, sample_length=sample_length) # Synthesize multi-gpu if FLAGS.gpu_number != 0: with tf.device("/device:GPU:%d" % FLAGS.gpu_number): fastgen.synthesize(encodings, save_names, checkpoint_path=checkpoint_path) # Single gpu else: fastgen.synthesize(encodings, save_names, checkpoint_path=checkpoint_path)
def main(unused_argv=None): tf.logging.set_verbosity(FLAGS.log) if FLAGS.checkpoint_path: checkpoint_path = utils.shell_path(FLAGS.checkpoint_path) else: expdir = utils.shell_path(FLAGS.expdir) tf.logging.info("Will load latest checkpoint from %s.", expdir) while not tf.gfile.Exists(expdir): tf.logging.fatal("\tExperiment save dir '%s' does not exist!", expdir) sys.exit(1) try: checkpoint_path = tf.train.latest_checkpoint(expdir) except tf.errors.NotFoundError: tf.logging.fatal("There was a problem determining the latest checkpoint.") sys.exit(1) if not tf.train.checkpoint_exists(checkpoint_path): tf.logging.fatal("Invalid checkpoint path: %s", checkpoint_path) sys.exit(1) tf.logging.info("Will restore from checkpoint: %s", checkpoint_path) source_path = utils.shell_path(FLAGS.source_path) tf.logging.info("Will load Wavs from %s." % source_path) save_path = utils.shell_path(FLAGS.save_path) tf.logging.info("Will save embeddings to %s." % save_path) if not tf.gfile.Exists(save_path): tf.logging.info("Creating save directory...") tf.gfile.MakeDirs(save_path) sample_length = FLAGS.sample_length batch_size = FLAGS.batch_size def is_wav(f): return f.lower().endswith(".wav") wavfiles = sorted([ os.path.join(source_path, fname) for fname in tf.gfile.ListDirectory(source_path) if is_wav(fname) ]) for start_file in xrange(0, len(wavfiles), batch_size): batch_number = (start_file / batch_size) + 1 tf.logging.info("On file number %s (batch %d).", start_file, batch_number) end_file = start_file + batch_size wavefiles_batch = wavfiles[start_file:end_file] # Ensure that files has batch_size elements. batch_filler = batch_size - len(wavefiles_batch) wavefiles_batch.extend(batch_filler * [wavefiles_batch[-1]]) wav_data = np.array( [utils.load_audio(f, sample_length) for f in wavefiles_batch]) try: tf.reset_default_graph() # Load up the model for encoding and find the encoding encoding = encode(wav_data, checkpoint_path, sample_length=sample_length) if encoding.ndim == 2: encoding = np.expand_dims(encoding, 0) tf.logging.info("Encoding:") tf.logging.info(encoding.shape) tf.logging.info("Sample length: %d" % sample_length) for num, (wavfile, enc) in enumerate(zip(wavefiles_batch, encoding)): filename = "%s_embeddings.npy" % wavfile.split("/")[-1].strip(".wav") with tf.gfile.Open(os.path.join(save_path, filename), "w") as f: np.save(f, enc) if num + batch_filler + 1 == batch_size: break except Exception as e: tf.logging.info("Unexpected error happened: %s.", e) raise
def main(unused_argv=None): os.environ["CUDA_VISIBLE_DEVICES"] = str(FLAGS.gpu_number) source_path = utils.shell_path(FLAGS.source_path) checkpoint_path = utils.shell_path(FLAGS.checkpoint_path) save_path = utils.shell_path(FLAGS.save_path) if not save_path: raise ValueError("Must specify a save_path.") tf.logging.set_verbosity(FLAGS.log) # Use directory of files if tf.gfile.IsDirectory(source_path): files = tf.gfile.ListDirectory(source_path) file_extensions = [os.path.splitext(f)[1] for f in files] if ".wav" in file_extensions: file_extension = ".wav" elif ".npy" in file_extensions: file_extension = ".npy" else: raise RuntimeError("Folder must contain .wav or .npy files.") file_extension = ".npy" if FLAGS.npy_only else file_extension files = sorted([ os.path.join(source_path, fname) for fname in files if fname.lower().endswith(file_extension) ]) # Use a single file elif source_path.lower().endswith((".wav", ".npy")): file_extension = os.path.splitext(source_path.lower())[1] files = [source_path] else: raise ValueError( "source_path {} must be a folder or file.".format(source_path)) # Now synthesize from files one batch at a time batch_size = FLAGS.batch_size sample_length = FLAGS.sample_length n = len(files) for start in range(0, n, batch_size): end = start + batch_size batch_files = files[start:end] save_names = [ os.path.join(save_path, "gen_" + os.path.splitext(os.path.basename(f))[0] + ".wav") for f in batch_files ] # Encode waveforms if file_extension == ".wav": batch_data = fastgen.load_batch_audio( batch_files, sample_length=sample_length) encodings = fastgen.encode( batch_data, checkpoint_path, sample_length=sample_length) # Or load encodings else: encodings = fastgen.load_batch_encodings( batch_files, sample_length=sample_length) # Synthesize multi-gpu if FLAGS.gpu_number != 0: with tf.device("/device:GPU:%d" % FLAGS.gpu_number): fastgen.synthesize( encodings, save_names, checkpoint_path=checkpoint_path) # Single gpu else: fastgen.synthesize( encodings, save_names, checkpoint_path=checkpoint_path)