def copy_scripts_and_config_files(work_dir, kaldi_root):
    misc.copy_file('data/src/speech/kaldi-run-lm.sh', '%s/run-lm.sh' % work_dir)
    # misc.copy_file ('data/src/speech/kaldi-run-am.sh', '%s/run-am.sh' % work_dir)
    # misc.copy_file ('data/src/speech/kaldi-run-nnet3.sh', '%s/run-nnet3.sh' % work_dir)
    misc.copy_file('data/src/speech/kaldi-run-chain.sh',
                   '%s/run-chain.sh' % work_dir)
    # misc.copy_file('data/src/speech/kaldi-run-chain-wrapper.sh',
    #                '%s/run-chain-wrapper.sh' % work_dir)
    # misc.copy_file('data/src/speech/kaldi-run-chain-cfg.sh',
    #                '%s/run-chain-cfg.sh' % work_dir)
    # misc.copy_file('data/src/speech/kaldi-run-chain-cpu.sh',
    #                '%s/run-chain-cpu.sh' % work_dir)
    # misc.copy_file('data/src/speech/kaldi-run-chain-cpu-wrapper.sh',
    #                '%s/run-chain-cpu-wrapper.sh' % work_dir)
    # misc.copy_file('data/src/speech/kaldi-run-chain-gpu.sh',
    #                '%s/run-chain-gpu.sh' % work_dir)
    # misc.copy_file('data/src/speech/kaldi-run-chain-gpu-wrapper.sh',
    #                '%s/run-chain-gpu-wrapper.sh' % work_dir)
    misc.copy_file('data/src/speech/kaldi-cmd.sh', '%s/cmd.sh' % work_dir)
    misc.render_template('data/src/speech/kaldi-path.sh.template',
                         '%s/path.sh' % work_dir, kaldi_root=kaldi_root)
    misc.mkdirs('%s/conf' % work_dir)
    misc.copy_file('data/src/speech/kaldi-mfcc.conf',
                   '%s/conf/mfcc.conf' % work_dir)
    misc.copy_file('data/src/speech/kaldi-mfcc-hires.conf',
                   '%s/conf/mfcc_hires.conf' % work_dir)
    misc.copy_file('data/src/speech/kaldi-online-cmvn.conf',
                   '%s/conf/online_cmvn.conf' % work_dir)
    misc.mkdirs('%s/local' % work_dir)
    misc.copy_file('data/src/speech/kaldi-score.sh',
                   '%s/local/score.sh' % work_dir)
    misc.mkdirs('%s/local/nnet3' % work_dir)
    misc.copy_file('data/src/speech/kaldi-run-ivector-common.sh',
                   '%s/local/nnet3/run_ivector_common.sh' % work_dir)
Exemple #2
0
    def test_render_template(self):
        # given
        template_text = """VAR1={{val1}}
        VAR2={{val2}}
        """

        val1 = "v1"
        val2 = "v2"

        expected_text = """VAR1=%s
        VAR2=%s
        """ % (val1, val2)

        src_path = os.path.join(str(self.test_dir), "src.txt")
        dst_path = os.path.join(str(self.test_dir), "dst.txt")

        with open(src_path, "wt") as f:
            f.write(template_text)

        # when
        misc.render_template(src_path, dst_path, val1=val1, val2=val2)

        # then
        with open(dst_path) as f:
            actual_text = f.read()

        self.assertEqual(expected_text, actual_text)
cmd = 'rm -rf %s' % WORK_DIR
logging.debug(cmd)
os.system(cmd)
misc.mkdirs('%s/test' % data_dir)

#
# scripts
#

misc.render_template('data/src/speech/w2l_run_auto_review.sh.template',
                     '%s/run_auto_review.sh' % WORK_DIR,
                     w2l_env_activate=w2l_env_activate,
                     w2l_decoder=w2l_decoder,
                     cuda_device=CUDA_DEVICE,
                     w2l_tokensdir='../../data/models/%s' % model_name,
                     w2l_tokens='tokens.txt',
                     w2l_lexicon='../../data/models/%s/lexicon.txt' %
                     model_name,
                     w2l_am='../../data/models/%s/model.bin' % model_name,
                     w2l_lm='data/lm6.bin')
#
# read lexicon
#

lexfn = 'data/models/%s/lexicon.txt' % model_name

logging.info('reading lexicon %s ...' % lexfn)

lex = {}
def export_sphinx_case(work_dir, sphinxtrain_cfg_fn):

    #
    # language model
    #

    misc.mkdirs('%s' % work_dir)

    fn = '%s/prompts.sent' % work_dir

    with codecs.open(fn, 'w', 'utf8') as outf:

        for cfn in ts_all:

            transcript = transcripts[cfn]['ts']

            outf.write('%s\n' % transcript)

    logging.info("%s written." % fn)

    fn = '%s/wlist.txt' % work_dir

    with codecs.open(fn, 'w', 'utf8') as outf:

        for word in lex:

            if ENABLE_NOISE_FILLER:
                if word == NOISE_WORD:
                    logging.debug('skipping noise word')
                    continue

            outf.write('%s\n' % word)

    logging.info("%s written." % fn)

    #
    # create work_dir structure
    #

    mfcc_dir = "%s/mfcc" % work_dir

    misc.mkdirs('%s/logs' % work_dir)
    misc.mkdirs('%s/etc' % work_dir)
    misc.mkdirs('%s' % mfcc_dir)

    # generate sphinx_train.cfg, featdir in there

    # inf = codecs.open ('data/src/speech/sphinx_train.cfg', 'r', 'utf8')
    # outf = codecs.open ('%s/etc/sphinx_train.cfg' % work_dir, 'w', 'utf8')
    # for line in inf:
    #     s = line.replace('%FEATDIR%', mfcc_dir).replace('%WORKDIR%', work_dir)
    #     outf.write (s)
    # inf.close()
    # outf.close()

    misc.copy_file(sphinxtrain_cfg_fn, '%s/etc/sphinx_train.cfg' % work_dir)
    if ENABLE_NOISE_FILLER:
        misc.copy_file('data/src/speech/sphinx-voxforge-noise.filler',
                       '%s/etc/voxforge.filler' % work_dir)
    else:
        misc.copy_file('data/src/speech/sphinx-voxforge.filler',
                       '%s/etc/voxforge.filler' % work_dir)
    misc.copy_file('data/src/speech/sphinx-feat.params',
                   '%s/etc/feat.params' % work_dir)

    #
    # prompts
    #

    train_fifn = '%s/etc/voxforge_train.fileids' % work_dir
    train_tsfn = '%s/etc/voxforge_train.transcription' % work_dir
    test_fifn = '%s/etc/voxforge_test.fileids' % work_dir
    test_tsfn = '%s/etc/voxforge_test.transcription' % work_dir
    runfeatfn = '%s/run-feat.sh' % work_dir

    lex_covered = set()

    SPHINXFE = "sphinx_fe -i '%s' -part 1 -npart 1 -ei wav -o '%s' -eo mfc -nist no -raw no -mswav yes -samprate 16000 -lowerf 130 -upperf 6800 -nfilt 25 -transform dct -lifter 22 >>logs/mfcc%02d.log 2>&1 &\n"
    with codecs.open(runfeatfn, 'w', 'utf8') as runfeatf:

        runfeatf.write('#!/bin/bash\n\n')

        cnt = 0
        for cfn in ts_all:

            w16filename = "%s/%s/%s.wav" % (wav16_dir, cfn2corpus[cfn], cfn)
            mfcfilename = "mfcc/%s.mfc" % cfn
            runfeatf.write(SPHINXFE % (w16filename, mfcfilename, cnt))
            cnt = (cnt + 1) % NJOBS

            if cnt == 0:
                runfeatf.write('wait\n')

    logging.info("%s written." % runfeatfn)

    with codecs.open (train_fifn, 'w', 'utf8') as train_fif, \
         codecs.open (train_tsfn, 'w', 'utf8') as train_tsf, \
         codecs.open (test_fifn,  'w', 'utf8') as test_fif,  \
         codecs.open (test_tsfn,  'w', 'utf8') as test_tsf:

        for cfn in ts_train:
            train_fif.write('%s\n' % cfn)
            tokens = tokenize(ts_train[cfn]['ts'],
                              lang=options.lang,
                              keep_punctuation=False)
            ts = u' '.join(tokens)
            train_tsf.write(u'<s> %s </s> (%s)\n' % (ts, cfn))

            for token in tokens:
                if not token in lex:
                    logging.error('word %s not covered by dict!')
                    sys.exit(1)
                lex_covered.add(token)

        for cfn in ts_test:
            test_fif.write('%s\n' % cfn)
            tokens = tokenize(ts_test[cfn]['ts'],
                              lang=options.lang,
                              keep_punctuation=False)
            ts = u' '.join(tokens)
            test_tsf.write(u'<s> %s </s> (%s)\n' % (ts, cfn))

            for token in tokens:
                if not token in lex:
                    logging.error('word %s not covered by dict!')
                    sys.exit(1)
                lex_covered.add(token)

    logging.info("%s written." % train_tsfn)
    logging.info("%s written." % train_fifn)
    logging.info("%s written." % test_tsfn)
    logging.info("%s written." % test_fifn)

    # generate dict

    phoneset = set()

    pdfn = '%s/etc/voxforge.dic' % work_dir
    with codecs.open(pdfn, 'w', 'utf8') as pdf:

        for word in lex:

            if ENABLE_NOISE_FILLER:
                if word == NOISE_WORD:
                    logging.debug('skipping noise word')
                    continue

            if not word in lex_covered:
                logging.debug(
                    'skipping word %s as it is not covered by transcripts' %
                    word)
                continue

            ipa = lex[word]['ipa']

            xs = ipa2xsampa(word, ipa)
            xa = xsampa2xarpabet(word, xs)

            pdf.write(u'%s %s\n' % (word, xa))

            phones = xa.split(' ')
            for phone in phones:

                if len(phone.strip()) == 0:
                    logging.error(
                        u"***ERROR: empty phone detected in lex entry %s %s" %
                        (word, ipa))

                phoneset.add(phone)

    logging.info("%s written." % pdfn)

    logging.info("Got %d phones." % len(phoneset))

    phfn = '%s/etc/voxforge.phone' % work_dir
    with codecs.open(phfn, 'w', 'utf8') as phf:

        for phone in phoneset:
            phf.write(u'%s\n' % phone)

        phf.write(u'SIL\n')
        if ENABLE_NOISE_FILLER:
            phf.write(u'NSPC\n')

    logging.info("%s written." % phfn)

    misc.render_template('data/src/speech/sphinx-run.sh.template',
                         '%s/sphinx-run.sh' % work_dir,
                         lm_name=lm_name)
# clean up leftovers from previous runs
#

cmd = 'rm -rf %s' % WORKDIR
logging.info(cmd)
os.system(cmd)
misc.mkdirs(WORKDIR)

#
# copy scripts
#

misc.copy_file ('data/src/speech/kaldi-run-segmentation.sh', '%s/run-segmentation.sh' % WORKDIR)

misc.copy_file ('data/src/speech/kaldi-cmd.sh', '%s/cmd.sh' % WORKDIR)
misc.render_template ('data/src/speech/kaldi-path.sh.template', '%s/path.sh' % WORKDIR, kaldi_root=kaldi_root)
misc.symlink ('%s/egs/wsj/s5/steps' % kaldi_root, '%s/steps' % WORKDIR)
misc.symlink ('%s/egs/wsj/s5/utils' % kaldi_root, '%s/utils' % WORKDIR)

#
# create skeleton dst model
#

misc.mkdirs ('%s/exp'  % WORKDIR)

cmd = "cp -r '%s/model' '%s/exp/tri2b_adapt'" % (modelfn, WORKDIR)
logging.info(cmd)
os.system(cmd)
cmd = "cp -r '%s/data'  '%s/data'" % (modelfn, WORKDIR)
logging.info(cmd)
os.system(cmd)
Exemple #6
0
def kaldi_adapt_lm(kaldi_root, src_model_dir, lm_fn, work_dir, dst_model_name):

    steps_path = '%s/egs/wsj/s5/steps' % kaldi_root
    if not os.path.exists (steps_path):
        raise Exception ('%s does not exist - is kaldi really installed in %s ?' % (steps_path, kaldi_root))

    tmpl_dir = os.path.dirname(os.path.abspath(__file__)) + '/templates'

    #
    # copy dictionary and phoneme sets from original model
    #

    logging.info("copying dictionary and phoneme sets from original model...")

    misc.mkdirs('%s/data/local/dict' % work_dir)
    misc.copy_file ('%s/data/local/dict/lexicon.txt' % src_model_dir,           '%s/data/local/dict/lexicon.txt' % work_dir)
    misc.copy_file ('%s/data/local/dict/nonsilence_phones.txt' % src_model_dir, '%s/data/local/dict/nonsilence_phones.txt' % work_dir)
    misc.copy_file ('%s/data/local/dict/silence_phones.txt' % src_model_dir,    '%s/data/local/dict/silence_phones.txt' % work_dir)
    misc.copy_file ('%s/data/local/dict/optional_silence.txt' % src_model_dir,  '%s/data/local/dict/optional_silence.txt' % work_dir)
    misc.copy_file ('%s/data/local/dict/extra_questions.txt' % src_model_dir,   '%s/data/local/dict/extra_questions.txt' % work_dir)

    #
    # language model 
    #

    misc.copy_file (lm_fn, '%s/lm.arpa' % work_dir)

    #
    # create skeleton dst model
    #

    logging.info("creating skeleton destination model...")

    misc.mkdirs ('%s/exp/adapt'  % work_dir)

    misc.copy_file ('%s/model/final.mdl' % src_model_dir, '%s/exp/adapt/final.mdl' % work_dir)
    misc.copy_file ('%s/model/cmvn_opts' % src_model_dir, '%s/exp/adapt/cmvn_opts' % work_dir)
    misc.copy_file ('%s/model/tree'      % src_model_dir, '%s/exp/adapt/tree'      % work_dir)

    for optional_file in [ 'final.mat', 'splice_opts', 'final.occs', 'full.mat' ] :
        if os.path.exists('%s/model/%s' % (src_model_dir, optional_file)):
            misc.copy_file ('%s/model/%s' % (src_model_dir, optional_file), '%s/exp/adapt/%s' % (work_dir, optional_file))

    if os.path.exists('%s/extractor' % src_model_dir):

        misc.mkdirs ('%s/exp/extractor' % work_dir)

        misc.copy_file ('%s/extractor/final.mat'         % src_model_dir, '%s/exp/extractor/final.mat'         % work_dir)
        misc.copy_file ('%s/extractor/global_cmvn.stats' % src_model_dir, '%s/exp/extractor/global_cmvn.stats' % work_dir)
        misc.copy_file ('%s/extractor/final.dubm'        % src_model_dir, '%s/exp/extractor/final.dubm'        % work_dir)
        misc.copy_file ('%s/extractor/final.ie'          % src_model_dir, '%s/exp/extractor/final.ie'          % work_dir)
        misc.copy_file ('%s/extractor/splice_opts'       % src_model_dir, '%s/exp/extractor/splice_opts'       % work_dir)

        misc.mkdirs ('%s/exp/ivectors_test_hires/conf' % work_dir)

        misc.copy_file ('%s/ivectors_test_hires/conf/splice.conf'       % src_model_dir, '%s/exp/ivectors_test_hires/conf'    % work_dir)

    misc.mkdirs ('%s/conf'  % work_dir)
    misc.copy_file ('%s/conf/mfcc.conf' % src_model_dir,        '%s/conf/mfcc.conf' % work_dir)
    misc.copy_file ('%s/conf/mfcc_hires.conf' % src_model_dir,  '%s/conf/mfcc_hires.conf' % work_dir)
    misc.copy_file ('%s/conf/online_cmvn.conf' % src_model_dir, '%s/conf/online_cmvn.conf' % work_dir)

    #
    # copy scripts and config files
    #
     
    misc.copy_file       ('%s/kaldi-run-adaptation.sh' % tmpl_dir, '%s/run-adaptation.sh' % work_dir)
    misc.copy_file       ('%s/kaldi-cmd.sh' % tmpl_dir,            '%s/cmd.sh' % work_dir)
    misc.render_template ('%s/kaldi-path.sh.template' % tmpl_dir,  '%s/path.sh' % work_dir, kaldi_root=kaldi_root)
    misc.copy_file       ('%s/kaldi-model-dist.sh' % tmpl_dir,     '%s/model-dist.sh' % work_dir)

    misc.symlink ('%s/egs/wsj/s5/steps' % kaldi_root, '%s/steps' % work_dir)
    misc.symlink ('%s/egs/wsj/s5/utils' % kaldi_root, '%s/utils' % work_dir)

    cmd = '/bin/bash -c "pushd %s && bash run-adaptation.sh && popd"' % work_dir
    logging.info (cmd)
    os.system (cmd)

    cmd = '/bin/bash -c "pushd %s && bash model-dist.sh "%s" && popd"' % (work_dir, dst_model_name)
    logging.info (cmd)
    os.system (cmd)
logging.info("loading lexicon...")
lex = Lexicon(file_name=dictionary)
logging.info("loading lexicon...done.")

#
# language model
#

misc.copy_file('%s/lm.arpa' % language_model_dir, '%s/lm.arpa' % data_dir)


#
# scripts
#

misc.render_template('data/src/speech/w2l_run_train.sh.template', '%s/run_train.sh' % work_dir, w2l_env_activate=w2l_env_activate, w2l_train=w2l_train)
misc.mkdirs('%s/config/conv_glu' % work_dir)
misc.render_template('data/src/speech/w2l_config_conv_glu_train.cfg.template', '%s/config/conv_glu/train.cfg' % work_dir, runname=model_name)
misc.copy_file('data/src/speech/w2l_config_conv_glu_network.arch', '%s/config/conv_glu/network.arch' % work_dir)

#
# export audio
#

def export_audio (train_val, tsdict):

    global data_dir, utt_num, options

    destdirfn = '%s/%s' % (data_dir, train_val)

    for utt_id in tsdict: