Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
OlaWod opened this issue Nov 30, 2021 · 1 comment
Closed

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

OlaWod opened this issue Nov 30, 2021 · 1 comment

Comments

@OlaWod
Copy link

OlaWod commented Nov 30, 2021

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.

@OlaWod OlaWod closed this as completed Nov 30, 2021
@nikich340
Copy link

nikich340 commented Dec 7, 2021

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.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants