加载训练有素的模型进行多处理。 (通过Python3)

时间:2018-06-06 06:27:47

标签: python tensorflow text-to-speech multiprocess

这是我的情况: 我有一个训练有素的语音合成模型。我将加快多处理速度的合成速度,在每个CPU中预先加载模型,然后继续输入文本到语音的句子。

这是我的尝试脚本:

####################################################################
#!/usr/bin/python3
####################################################################

from multiprocessing import Process, Pool, cpu_count
import os,time

####################################################################

from tacotron.demo_synthesizer import Synthesizer
from splitting_sent import splitting_para
import tensorflow as tf
from datasets import audio

####################################################################

from pypinyin import pinyin, Style

####################################################################

BASE_DIR = os.path.split(os.path.realpath(__file__))[0]
VOICE = BASE_DIR + "/tmp"
TXT = BASE_DIR + "/txt"
os.makedirs(VOICE, exist_ok=True)
os.makedirs(TXT, exist_ok=True)

####################################################################

def syn(py):
    synthesizer = Synthesizer()
    synthesizer.load("/path/to/the/model")
    wav_name = time.time()
    wav_path = VOICE + "/" + str(wav_name)
    wav = synthesizer.synthesize(py)
    audio.save_wav(wav, wav_path)

if __name__ =='__main__':

    with open(os.path.join(TXT, "content.txt"), "r") as f:
        lines = f.read().splitlines()
    lines = "".join(lines)

    sentences = splitting_para(lines)
    # splitting paragraph into individual sentences.

    py_list = []
    for sent in sentences:
        py_sent = pinyin(sent, style=Style.TONE3)
        py_sent = " ".join([i[0] for i in py_sent if i[0].isalnum()])
        py_list.append(py_sent)
    # as I am trying the Chinese TTS, it is the inevitable prerequisite step of translating Chinese character into Pinyin.

    print('Run the main process (%s).' % (os.getpid()))
    mainStart = time.time()
    p = Pool(cpu_count())
    for py in py_list:
        p.apply_async(syn,args=(py,))

    print('Waiting for all subprocesses done ...')
    p.close()
    p.join()
    print('All subprocesses done')
    mainEnd = time.time()
    print('All process ran %0.2f seconds.' % (mainEnd-mainStart))

我坚持这个问题:我只能将12个模型预加载到12个进程中以合成随机句子。但是,不可能继续将下12个句子输入到预加载模型中。在完成第一组12个句子之后终止了这些过程。我完全迷失在这里。 :(

如果有任何建议,我非常感激。 :)

1 个答案:

答案 0 :(得分:1)

我从朋友那里得到了答案,如下:

####################################################################

import multiprocessing
from multiprocessing import Process, Pool, cpu_count, Queue
import os, time, sys

####################################################################

from tacotron.demo_synthesizer import Synthesizer
from splitting_sent import splitting_para
import tensorflow as tf
from datasets import audio

####################################################################

from pypinyin import pinyin, Style

####################################################################

BASE_DIR = os.path.split(os.path.realpath(__file__))[0]
VOICE = BASE_DIR + "/tmp"
TXT = BASE_DIR + "/txt"
os.makedirs(VOICE, exist_ok=True)
os.makedirs(TXT, exist_ok=True)

####################################################################

num_cpu = cpu_count() - 1
q = []
q_re = []

for tmp in range(num_cpu):
    q.append(Queue())

def syn(id):
    synthesizer = Synthesizer()
    synthesizer.load("/home/chris/Pictures/tts-server/logs-Tacotron/model.ckpt-1255000")
    print("LOADED at {} CPU".format(id))
    while not q[id].empty():
        input_py = q[id].get(True)
        print("Starting decode:", id)
        wav_name = time.time()
        wav_path = VOICE + "/" + str(wav_name)
        wav = synthesizer.synthesize(input_py)
        audio.save_wav(wav, wav_path)
        print("Decoded:", id)

if __name__ =='__main__':

    with open(os.path.join(TXT, "content.txt"), "r") as f:
        lines = f.read().splitlines()
    lines = "".join(lines)

    sentences = splitting_para(lines)

    py_list = []
    for sent in sentences:
        py_sent = pinyin(sent, style=Style.TONE3)
        py_sent = " ".join([i[0] for i in py_sent if i[0].isalnum()])
        py_list.append(py_sent)

    for x in range(num_cpu):
        p = multiprocessing.Process(target=syn, args=(x,))
        p.start()

    # decoding
    for index, py in enumerate(py_list):
        q[index % num_cpu].put(py)