Skip to content

A simple multi-process version of preprocess.py #37

Closed

Description

@OlaWod
import argparse
import text
from utils import load_filepaths_and_text
from multiprocessing import Pool, cpu_count
from tqdm import tqdm

def process(inputs):
  i, filepaths_and_text = inputs
  original_text = filepaths_and_text[args.text_index]
  cleaned_text = text._clean_text(original_text, args.text_cleaners)
  filepaths_and_text[args.text_index] = cleaned_text
  return i, filepaths_and_text

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument("--out_extension", default="cleaned")
  parser.add_argument("--text_index", default=1, type=int)
  parser.add_argument("--filelists", nargs="+", default=["filelists/ljs_audio_text_val_filelist.txt", "filelists/ljs_audio_text_test_filelist.txt"])
  parser.add_argument("--text_cleaners", nargs="+", default=["english_cleaners2"])

  args = parser.parse_args()
    
  for filelist in args.filelists:
    print("START:", filelist)
    filepaths_and_text = load_filepaths_and_text(filelist)
    inputs = [(i, filepaths_and_text[i]) for i in range(len(filepaths_and_text))]
    with Pool(processes=cpu_count()-1) as pool:
      with tqdm(total=len(inputs)) as pbar:
        for i, line in tqdm(pool.imap_unordered(process, inputs)):
          filepaths_and_text[i] = line
          pbar.update()

    new_filelist = filelist + "." + args.out_extension
    with open(new_filelist, "w", encoding="utf-8") as f:
      f.writelines(["|".join(x) + "\n" for x in filepaths_and_text])

It still takes hours to gen xx.txt.cleaned of 88k sentences though.

Activity

  1. nikich340 commented on Dec 7, 2021

    @nikich340

    Nice! Though process function doesn't know about args var and it raises error.
    Here's edited version:

    import argparse
    import text
    from utils import load_filepaths_and_text
    from multiprocessing import Pool, cpu_count
    from tqdm import tqdm
    
    def process(inputs):
      i, line = inputs
      cleaned_line = text._clean_text(line, ["my_cleaners2"])  # <- change this!
      return i, cleaned_line
    
    if __name__ == '__main__':
      parser = argparse.ArgumentParser()
      parser.add_argument("--out_extension", default="cleaned")
      parser.add_argument("--text_index", default=1, type=int)
      parser.add_argument("--filelists", nargs="+", default=["filelists/ljs_audio_text_val_filelist.txt", "filelists/ljs_audio_text_test_filelist.txt"])
      parser.add_argument("--text_cleaners", nargs="+", default=["my_cleaners2"])
    
      args = parser.parse_args()
    
      for filelist in args.filelists:
        print("!START: ", filelist)
        filepaths_and_text = load_filepaths_and_text(filelist)
        inputs = [(i, filepaths_and_text[i][args.text_index]) for i in range(len(filepaths_and_text))]
        print(f"!CPU count: {cpu_count()}")
        with Pool(processes=cpu_count()) as pool:
          with tqdm(total=len(inputs)) as pbar:
            for i, line in tqdm(pool.imap_unordered(process, inputs)):
              filepaths_and_text[i][args.text_index] = line
              # print(" >> cleaned: {}".format(line))
              pbar.update()
    
        new_filelist = filelist + "." + args.out_extension
        with open(new_filelist, "w", encoding="utf-8") as f:
          f.writelines(["|".join(x) + "\n" for x in filepaths_and_text])
    

    Took me ~25 mins for 19k lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions