예제 #1
0
def prepare_test_data(postProbDim):

  feat = exkaldi.load_feat( f"{args.expDir}/train_lstm/data/test/fmllr.ark" )

  if args.useCMVN:
    cmvn = exkaldi.load_cmvn( f"{args.expDir}/train_lstm/data/test/cmvn_of_fmllr.ark" )
    feat = exkaldi.use_cmvn(feat, cmvn, utt2spk=f"{args.expDir}/train_lstm/data/test/utt2spk")
    del cmvn

  if args.delta > 0:
    feat = feat.add_delta(args.delta)

  if args.splice > 0:
    feat = feat.splice(args.splice)

  feat = feat.to_numpy()
  if args.normalizeFeat:
    feat = feat.normalize(std=True)

  # Normalize acoustic model output
  if args.normalizeAMP:
    ali = exkaldi.load_ali(f"{args.expDir}/train_lstm/data/train/pdfID.npy", aliType="pdfID")
    normalizeBias = exkaldi.nn.compute_postprob_norm(ali,postProbDim)
  else:
    normalizeBias = 0
  
  # ref transcription
  trans = exkaldi.load_transcription(f"{args.expDir}/train_lstm/data/test/text")
  convertTable = exkaldi.load_list_table(f"{args.expDir}/dict/phones.48_to_39.map")
  trans = trans.convert(convertTable)

  return feat, normalizeBias, trans
예제 #2
0
def main():

    # ------------- Parse arguments from command line ----------------------
    # 1. Add a discription of this program
    args.discribe(
        "This program is used to make dictionaries and language model")
    # 2. Add options
    args.add(
        "--expDir",
        abbr="-e",
        dtype=str,
        default="exp",
        discription="The data resources and output path of current experiment."
    )
    args.add("--order",
             abbr="-o",
             dtype=int,
             default=6,
             minV=1,
             maxV=6,
             discription="The maximum order of N-grams language model.")
    # 3. Then start to parse arguments.
    args.parse()
    # 4. Take a backup of arguments
    args.print_args()  # print arguments to display
    argsLogFile = os.path.join(args.expDir, "conf", "make_dict_and_LM.args")
    args.save(argsLogFile)

    # ------- Make the word-pronumciation lexicon file ------
    textFile = os.path.join(args.expDir, "data", "train", "text")
    trainTrans = exkaldi.load_transcription(
        textFile)  # "trans" is an exkaldi Transcription object
    wordCount = trainTrans.count_word().sort(
    )  # accumulate all words and their frequency in the transcription

    word2pron = exkaldi.ListTable(
        dict((word, word)
             for word in wordCount.keys()))  # word to pronunciation

    pronFile = os.path.join(args.expDir, "dict", "pronunciation.txt")
    word2pron.save(pronFile)  # save it to file

    # -------  Make lexicons ------
    # 1. Generate the LexiconBank object from word-pronumciation file.
    # Depending on task, about 20 lexicons will be generated and managed by the LexiconBank.
    lexicons = exkaldi.decode.graph.lexicon_bank(pronFile,
                                                 silWords={"sil": "sil"},
                                                 unkSymbol={"sil": "sil"},
                                                 optionalSilPhone="sil",
                                                 extraQuestions=[],
                                                 positionDependent=False,
                                                 shareSilPdf=False,
                                                 extraDisambigPhoneNumbers=1,
                                                 extraDisambigWords=[])

    # 2. Add two extra questions.
    lexicons.add_extra_question(lexicons("silence_phones"))
    lexicons.add_extra_question(lexicons("nonsilence_phones"))

    # 3. Save this lexicon bank for future use.
    lexicons.save(os.path.join(args.expDir, "dict", "lexicons.lex"))
    print(f"Generate lexicon bank done.")

    # -------  Make Lexicon fst ------
    # 1. Generate the Lexicon fst
    exkaldi.decode.graph.make_L(lexicons,
                                outFile=os.path.join(args.expDir, "dict",
                                                     "L.fst"),
                                useSilprob=0.0,
                                useDisambigLexicon=False)
    print(f"Generate lexicon fst done.")
    # 1. Generate the disambig Lexicon fst
    exkaldi.decode.graph.make_L(lexicons,
                                outFile=os.path.join(args.expDir, "dict",
                                                     "L_disambig.fst"),
                                useSilprob=0.0,
                                useDisambigLexicon=True)
    print(f"Generate disambiguation lexicon fst done.")

    # -------  Make GMM-HMM topological structure for GMM-HMM ------
    exkaldi.hmm.make_topology(
        lexicons,
        outFile=os.path.join(args.expDir, "dict", "topo"),
        numNonsilStates=3,
        numSilStates=5,
    )
    print(f"Generate topo file done.")

    # -------  Train N-Grams language model ------
    # 1. Train a LM.
    # We have trained 2,3,4 grams model with both srilm and kenlm and chose the best one, which is 3-grams model back kenlm.
    # So we directly train this one.
    exkaldi.lm.train_ngrams_kenlm(
        lexicons,
        order=args.order,
        text=
        trainTrans,  # If "text" received an exkaldi Transcription object, the information of utterance IDs will be omitted automatically.
        outFile=os.path.join(args.expDir, "lm", f"{args.order}grams.arpa"),
        config={
            "--discount_fallback": True,
            "-S": "20%"
        },
    )
    print(f"Generate ARPA language model done.")

    # 2. Then test this model by compute the perplexity.
    exkaldi.lm.arpa_to_binary(
        arpaFile=os.path.join(args.expDir, "lm", f"{args.order}grams.arpa"),
        outFile=os.path.join(args.expDir, "lm", f"{args.order}grams.binary"),
    )
    model = exkaldi.load_ngrams(
        os.path.join(args.expDir, "lm", f"{args.order}grams.binary")
    )  # Actually, "load_ngrams" function also accepts ARPA format file.

    # 3. Prepare test transcription
    testTrans = exkaldi.load_transcription(
        os.path.join(args.expDir, "data", "test", "text"))

    # 4. score
    perScore = model.perplexity(testTrans)
    print(f"The weighted average perplexity of this model is: {perScore}.")
    del model
    del testTrans

    # ------- Make Grammar fst ------
    exkaldi.decode.graph.make_G(
        lexicons,
        arpaFile=os.path.join(args.expDir, "lm", f"{args.order}grams.arpa"),
        outFile=os.path.join(args.expDir, "lm", f"G.{args.order}.fst"),
        order=args.order)
    print(f"Make Grammar fst done.")

    # ------- Compose LG fst for futher use ------
    exkaldi.decode.graph.compose_LG(
        LFile=os.path.join(args.expDir, "dict", "L_disambig.fst"),
        GFile=os.path.join(args.expDir, "lm", f"G.{args.order}.fst"),
        outFile=os.path.join(args.expDir, "lm", f"LG.{args.order}.fst"),
    )
    print(f"Compose LG fst done.")
예제 #3
0
def main():

    # ------------- Parse arguments from command line ----------------------
    # 1. Add a discription of this program
    args.discribe("This program is used to train triphone GMM-HMM model")
    # 2. Add options
    args.add("--expDir",
             abbr="-e",
             dtype=str,
             default="exp",
             discription="The data and output path of current experiment.")
    args.add("--splice",
             abbr="-c",
             dtype=int,
             default=3,
             discription="How many left-right frames to splice.")
    args.add("--numIters",
             abbr="-n",
             dtype=int,
             default=35,
             discription="How many iterations to train.")
    args.add("--maxIterInc",
             abbr="-m",
             dtype=int,
             default=25,
             discription="The final iteration of increasing gaussians.")
    args.add("--realignIter",
             abbr="-r",
             dtype=int,
             default=[10, 20, 30],
             discription="The iteration to realign feature.")
    args.add("--fmllrIter",
             abbr="-f",
             dtype=int,
             default=[2, 4, 6, 12],
             discription="The iteration to estimate fmllr matrix.")
    args.add("--order",
             abbr="-o",
             dtype=int,
             default=6,
             discription="Which N-grams model to use.")
    args.add("--beam",
             abbr="-b",
             dtype=int,
             default=13,
             discription="Decode beam size.")
    args.add("--latBeam",
             abbr="-l",
             dtype=int,
             default=6,
             discription="Lattice beam size.")
    args.add("--acwt",
             abbr="-a",
             dtype=float,
             default=0.083333,
             discription="Acoustic model weight.")
    args.add(
        "--parallel",
        abbr="-p",
        dtype=int,
        default=4,
        minV=1,
        maxV=10,
        discription=
        "The number of parallel process to compute feature of train dataset.")
    args.add("--skipTrain",
             abbr="-s",
             dtype=bool,
             default=False,
             discription="If True, skip training. Do decoding only.")
    # 3. Then start to parse arguments.
    args.parse()
    # 4. Take a backup of arguments
    argsLogFile = os.path.join(args.expDir, "conf", "train_sat.args")
    args.save(argsLogFile)

    if not args.skipTrain:
        # ------------- Prepare feature and previous alignment for training ----------------------
        # 1. Load the feature for training
        print(f"Load MFCC+CMVN feature.")
        feat = exkaldi.load_index_table(
            os.path.join(args.expDir, "mfcc", "train", "mfcc_cmvn.ark"))
        print(f"Splice {args.splice} frames.")
        originalFeat = exkaldi.splice_feature(feat,
                                              left=args.splice,
                                              right=args.splice,
                                              outFile=os.path.join(
                                                  args.expDir, "train_delta",
                                                  "mfcc_cmvn_splice.ark"))
        print(f"Transform LDA feature")
        ldaFeat = exkaldi.transform_feat(
            feat=originalFeat,
            matFile=os.path.join(args.expDir, "train_lda_mllt", "trans.mat"),
            outFile=os.path.join(args.expDir, "train_sat", "lda_feat.ark"),
        )
        del originalFeat
        # 2. Load previous alignment and lexicons
        ali = exkaldi.load_index_table(os.path.join(args.expDir,
                                                    "train_lda_mllt",
                                                    "*final.ali"),
                                       useSuffix="ark")
        lexicons = exkaldi.load_lex(
            os.path.join(args.expDir, "dict", "lexicons.lex"))
        # 3. Estimate the primary fMLLR transform matrix
        print("Estiminate the primary fMLLR transform matrixs")
        fmllrTransMat = exkaldi.hmm.estimate_fMLLR_matrix(
            aliOrLat=ali,
            lexicons=lexicons,
            aliHmm=os.path.join(args.expDir, "train_lda_mllt", "final.mdl"),
            feat=ldaFeat,
            spk2utt=os.path.join(args.expDir, "data", "train", "spk2utt"),
            outFile=os.path.join(args.expDir, "train_sat", "trans.ark"),
        )
        print("Transform feature")
        fmllrFeat = exkaldi.use_fmllr(
            ldaFeat,
            fmllrTransMat,
            utt2spk=os.path.join("exp", "data", "train", "utt2spk"),
            outFile=os.path.join(args.expDir, "train_sat", "fmllr_feat.ark"),
        )

        # -------------- Build the decision tree ------------------------
        print("Start build a tree")
        tree = exkaldi.hmm.DecisionTree(lexicons=lexicons,
                                        contextWidth=3,
                                        centralPosition=1)
        tree.train(
            feat=fmllrFeat,
            hmm=os.path.join(args.expDir, "train_lda_mllt", "final.mdl"),
            ali=ali,
            topoFile=os.path.join(args.expDir, "dict", "topo"),
            numLeaves=2500,
            tempDir=os.path.join(args.expDir, "train_sat"),
        )
        tree.save(os.path.join(args.expDir, "train_sat", "tree"))
        print(f"Build tree done.")
        del fmllrFeat

        # ------------- Start training ----------------------
        # 1. Initialize a monophone HMM object
        print("Initialize a triphone HMM object")
        model = exkaldi.hmm.TriphoneHMM(lexicons=lexicons)
        model.initialize(
            tree=tree,
            topoFile=os.path.join(args.expDir, "dict", "topo"),
            treeStatsFile=os.path.join(args.expDir, "train_sat",
                                       "treeStats.acc"),
        )
        print(f"Initialized a monophone HMM-GMM model: {model.info}.")

        # 2. convert the previous alignment
        print(f"Transform the alignment")
        newAli = exkaldi.hmm.convert_alignment(
            ali=ali,
            originHmm=os.path.join(args.expDir, "train_lda_mllt", "final.mdl"),
            targetHmm=model,
            tree=tree,
            outFile=os.path.join(args.expDir, "train_sat", "initial.ali"),
        )

        # 2. Split data for parallel training
        transcription = exkaldi.load_transcription(
            os.path.join(args.expDir, "data", "train", "text"))
        transcription = transcription.sort()

        if args.parallel > 1:
            # split feature
            ldaFeat = ldaFeat.sort(by="utt").subset(chunks=args.parallel)
            # split transcription depending on utterance IDs of each feat
            tempTrans = []
            tempAli = []
            tempFmllrMat = []
            for f in ldaFeat:
                tempTrans.append(transcription.subset(keys=f.utts))
                tempAli.append(newAli.subset(keys=f.utts))
                spks = exkaldi.utt_to_spk(f.utts,
                                          utt2spk=os.path.join(
                                              args.expDir, "data", "train",
                                              "utt2spk"))
                tempFmllrMat.append(fmllrTransMat.subset(keys=spks))
            transcription = tempTrans
            newAli = tempAli
            fmllrTransMat = tempFmllrMat

        # 3. Train
        print("Train the triphone model")
        model.train(
            ldaFeat,
            transcription,
            os.path.join(args.expDir, "dict", "L.fst"),
            tree,
            tempDir=os.path.join(args.expDir, "train_sat"),
            initialAli=newAli,
            fmllrTransMat=fmllrTransMat,
            spk2utt=os.path.join(args.expDir, "data", "train", "spk2utt"),
            utt2spk=os.path.join(args.expDir, "data", "train", "utt2spk"),
            numIters=args.numIters,
            maxIterInc=args.maxIterInc,
            totgauss=15000,
            realignIter=args.realignIter,
            fmllrIter=args.fmllrIter,
            boostSilence=1.0,
            power=0.2,
            fmllrSilWt=0.0,
        )
        print(model.info)
        del ldaFeat
        del fmllrTransMat
        del newAli

    else:
        declare.is_file(os.path.join(args.expDir, "train_sat", "final.mdl"))
        declare.is_file(os.path.join(args.expDir, "train_sat", "tree"))
        model = exkaldi.load_hmm(
            os.path.join(args.expDir, "train_sat", "final.mdl"))
        tree = exkaldi.load_tree(os.path.join(args.expDir, "train_sat",
                                              "tree"))

    # ------------- Compile WFST training ----------------------
    # Make a WFST decoding graph
    make_WFST_graph(
        outDir=os.path.join(args.expDir, "train_sat", "graph"),
        hmm=model,
        tree=tree,
    )
    # Decode test data
    GMM_decode_fmllr_and_score(
        outDir=os.path.join(args.expDir, "train_sat",
                            f"decode_{args.order}grams"),
        hmm=model,
        HCLGfile=os.path.join(args.expDir, "train_sat", "graph",
                              f"HCLG.{args.order}.fst"),
        tansformMatFile=os.path.join(args.expDir, "train_lda_mllt",
                                     "trans.mat"),
    )
예제 #4
0
def main():

    # ------------- Parse arguments from command line ----------------------
    # 1. Add a discription of this program
    args.describe("This program is used to train monophone GMM-HMM model")
    # 2. Add options
    args.add("--expDir",
             abbr="-e",
             dtype=str,
             default="exp",
             discription="The data and output path of current experiment.")
    args.add("--delta",
             abbr="-d",
             dtype=int,
             default=2,
             discription="Add n-order to feature.")
    args.add("--numIters",
             abbr="-n",
             dtype=int,
             default=40,
             discription="How many iterations to train.")
    args.add("--maxIterInc",
             abbr="-m",
             dtype=int,
             default=30,
             discription="The final iteration of increasing gaussians.")
    args.add("--realignIter",
             abbr="-r",
             dtype=int,
             default=[
                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 23, 26, 29,
                 32, 35, 38
             ],
             discription="the iteration to realign feature.")
    args.add("--order",
             abbr="-o",
             dtype=int,
             default=6,
             minV=1,
             maxV=6,
             discription="Which N-grams model to use.")
    args.add("--beam",
             abbr="-b",
             dtype=int,
             default=13,
             discription="Decode beam size.")
    args.add("--latBeam",
             abbr="-l",
             dtype=int,
             default=6,
             discription="Lattice beam size.")
    args.add("--acwt",
             abbr="-a",
             dtype=float,
             default=0.083333,
             discription="Acoustic model weight.")
    args.add(
        "--parallel",
        abbr="-p",
        dtype=int,
        default=4,
        minV=1,
        maxV=10,
        discription=
        "The number of parallel process to compute feature of train dataset.")
    args.add("--skipTrain",
             abbr="-s",
             dtype=bool,
             default=False,
             discription="If True, skip training. Do decoding only.")
    # 3. Then start to parse arguments.
    args.parse()
    # 4. Take a backup of arguments
    args.print_args()  # print arguments to display
    argsLogFile = os.path.join(args.expDir, "conf", "train_mono.args")
    args.save(argsLogFile)

    if not args.skipTrain:
        # ------------- Prepare feature for training ----------------------
        # 1. Load the feature for training (We use the index table format)
        feat = exkaldi.load_index_table(
            os.path.join(args.expDir, "mfcc", "train", "mfcc_cmvn.ark"))
        print(f"Load MFCC+CMVN feature.")
        feat = exkaldi.add_delta(feat,
                                 order=args.delta,
                                 outFile=os.path.join(args.expDir,
                                                      "train_mono",
                                                      "mfcc_cmvn_delta.ark"))
        print(f"Add {args.delta}-order deltas.")
        # 2. Load lexicon bank
        lexicons = exkaldi.load_lex(
            os.path.join(args.expDir, "dict", "lexicons.lex"))
        print(f"Restorage lexicon bank.")

        # ------------- Start training ----------------------
        # 1. Initialize a monophone HMM object
        model = exkaldi.hmm.MonophoneHMM(lexicons=lexicons, name="mono")
        model.initialize(feat=feat,
                         topoFile=os.path.join(args.expDir, "dict", "topo"))
        print(f"Initialized a monophone HMM-GMM model: {model.info}.")

        # 2. Split data for parallel training
        transcription = exkaldi.load_transcription(
            os.path.join(args.expDir, "data", "train", "text"))
        transcription = transcription.sort()
        if args.parallel > 1:
            # split feature
            feat = feat.sort(by="utt").subset(chunks=args.parallel)
            # split transcription depending on utterance IDs of each feature
            temp = []
            for f in feat:
                temp.append(transcription.subset(keys=f.utts))
            transcription = temp

        # 3. Train
        model.train(
            feat,
            transcription,
            LFile=os.path.join(args.expDir, "dict", "L.fst"),
            tempDir=os.path.join(args.expDir, "train_mono"),
            numIters=args.numIters,
            maxIterInc=args.maxIterInc,
            totgauss=1000,
            realignIter=args.realignIter,
            boostSilence=1.0,
        )
        print(model.info)
        # Save the tree
        model.tree.save(os.path.join(args.expDir, "train_mono", "tree"))
        print(f"Tree has been saved.")

        # 4. Realign with boostSilence 1.25
        print("Realign the training feature (boost silence = 1.25)")
        trainGraphFiles = exkaldi.utils.list_files(
            os.path.join(args.expDir, "train_mono", "*train_graph"))
        model.align(
            feat,
            trainGraphFile=
            trainGraphFiles,  # train graphs have been generated in the train step.
            boostSilence=1.25,  #1.5
            outFile=os.path.join(args.expDir, "train_mono", "final.ali"))
        del feat
        print("Save the new alignment done.")
        tree = model.tree

    else:
        declare.is_file(os.path.join(args.expDir, "train_mono", "final.mdl"))
        declare.is_file(os.path.join(args.expDir, "train_mono", "tree"))
        model = exkaldi.load_hmm(
            os.path.join(args.expDir, "train_mono", "final.mdl"))
        tree = exkaldi.load_tree(
            os.path.join(args.expDir, "train_mono", "tree"))

    # ------------- Compile WFST training ----------------------
    # Make a WFST decoding graph
    make_WFST_graph(
        outDir=os.path.join(args.expDir, "train_mono", "graph"),
        hmm=model,
        tree=tree,
    )

    # Decode test data
    GMM_decode_mfcc_and_score(
        outDir=os.path.join(args.expDir, "train_mono",
                            f"decode_{args.order}grams"),
        hmm=model,
        HCLGfile=os.path.join(args.expDir, "train_mono", "graph",
                              f"HCLG.{args.order}.fst"),
    )
def main():

    # ------------- Parse arguments from command line ----------------------
    # 1. Add a discription of this program
    args.discribe("This program is used to train triphone GMM-HMM model")
    # 2. Add options
    args.add("--expDir",
             abbr="-e",
             dtype=str,
             default="exp",
             discription="The data and output path of current experiment.")
    args.add("--delta",
             abbr="-d",
             dtype=int,
             default=2,
             discription="Add n-order to feature.")
    args.add("--numIters",
             abbr="-n",
             dtype=int,
             default=35,
             discription="How many iterations to train.")
    args.add("--maxIterInc",
             abbr="-m",
             dtype=int,
             default=25,
             discription="The final iteration of increasing gaussians.")
    args.add("--realignIter",
             abbr="-r",
             dtype=int,
             default=[10, 20, 30],
             discription="the iteration to realign feature.")
    args.add("--order",
             abbr="-o",
             dtype=int,
             default=6,
             discription="Which N-grams model to use.")
    args.add("--beam",
             abbr="-b",
             dtype=int,
             default=13,
             discription="Decode beam size.")
    args.add("--latBeam",
             abbr="-l",
             dtype=int,
             default=6,
             discription="Lattice beam size.")
    args.add("--acwt",
             abbr="-a",
             dtype=float,
             default=0.083333,
             discription="Acoustic model weight.")
    args.add(
        "--parallel",
        abbr="-p",
        dtype=int,
        default=4,
        minV=1,
        maxV=10,
        discription=
        "The number of parallel process to compute feature of train dataset.")
    args.add("--skipTrain",
             abbr="-s",
             dtype=bool,
             default=False,
             discription="If True, skip training. Do decoding only.")
    # 3. Then start to parse arguments.
    args.parse()
    # 4. Take a backup of arguments
    argsLogFile = os.path.join(args.expDir, "conf", "train_delta.args")
    args.save(argsLogFile)

    if not args.skipTrain:
        # ------------- Prepare feature and previous alignment for training ----------------------
        # 1. Load the feature for training
        feat = exkaldi.load_index_table(
            os.path.join(args.expDir, "mfcc", "train", "mfcc_cmvn.ark"))
        print(f"Load MFCC+CMVN feature.")
        feat = exkaldi.add_delta(feat,
                                 order=args.delta,
                                 outFile=os.path.join(args.expDir,
                                                      "train_delta",
                                                      "mfcc_cmvn_delta.ark"))
        print(f"Add {args.delta}-order deltas.")
        # 2. Load lexicon bank
        lexicons = exkaldi.load_lex(
            os.path.join(args.expDir, "dict", "lexicons.lex"))
        print(f"Restorage lexicon bank.")
        # 3. Load previous alignment
        ali = exkaldi.load_index_table(os.path.join(args.expDir, "train_mono",
                                                    "*final.ali"),
                                       useSuffix="ark")

        # -------------- Build the decision tree ------------------------
        print("Start build a tree")
        tree = exkaldi.hmm.DecisionTree(lexicons=lexicons,
                                        contextWidth=3,
                                        centralPosition=1)
        tree.train(
            feat=feat,
            hmm=os.path.join(args.expDir, "train_mono", "final.mdl"),
            ali=ali,
            topoFile=os.path.join(args.expDir, "dict", "topo"),
            numLeaves=2500,
            tempDir=os.path.join(args.expDir, "train_delta"),
        )
        print(f"Build tree done.")

        # ------------- Start training ----------------------
        # 1. Initialize a monophone HMM object
        model = exkaldi.hmm.TriphoneHMM(lexicons=lexicons, name="mono")
        model.initialize(
            tree=tree,
            topoFile=os.path.join(args.expDir, "dict", "topo"),
            treeStatsFile=os.path.join(args.expDir, "train_delta",
                                       "treeStats.acc"),
        )
        print(f"Initialized a monophone HMM-GMM model: {model.info}.")

        # 2. convert the previous alignment
        print(f"Transform the alignment")
        newAli = exkaldi.hmm.convert_alignment(
            ali=ali,
            originHmm=os.path.join("exp", "train_mono", "final.mdl"),
            targetHmm=model,
            tree=tree,
            outFile=os.path.join(args.expDir, "train_delta", "initial.ali"),
        )

        # 2. Split data for parallel training
        transcription = exkaldi.load_transcription(
            os.path.join(args.expDir, "data", "train", "text"))
        transcription = transcription.sort()
        if args.parallel > 1:
            # split feature
            feat = feat.sort(by="utt").subset(chunks=args.parallel)
            # split transcription depending on utterance IDs of each feat
            tempTrans = []
            tempAli = []
            for f in feat:
                tempTrans.append(transcription.subset(keys=f.utts))
                tempAli.append(newAli.subset(keys=f.utts))
            transcription = tempTrans
            newAli = tempAli

        # 3. Train
        print("Train the triphone model")
        model.train(
            feat,
            transcription,
            os.path.join("exp", "dict", "L.fst"),
            tree,
            tempDir=os.path.join(args.expDir, "train_delta"),
            initialAli=newAli,
            numIters=args.numIters,
            maxIterInc=args.maxIterInc,
            totgauss=15000,
            realignIter=args.realignIter,
            boostSilence=1.0,
        )
        print(model.info)
        # Save the tree
        model.tree.save(os.path.join(args.expDir, "train_delta", "tree"))
        print(f"Tree has been saved.")
        del feat

    else:
        declare.is_file(os.path.join(args.expDir, "train_delta", "final.mdl"))
        declare.is_file(os.path.join(args.expDir, "train_delta", "tree"))
        model = exkaldi.load_hmm(
            os.path.join(args.expDir, "train_delta", "final.mdl"))
        tree = exkaldi.load_tree(
            os.path.join(args.expDir, "train_delta", "tree"))

    # ------------- Compile WFST training ----------------------
    # Make a WFST decoding graph
    make_WFST_graph(
        outDir=os.path.join(args.expDir, "train_delta", "graph"),
        hmm=model,
        tree=tree,
    )
    # Decode test data
    GMM_decode_mfcc_and_score(
        outDir=os.path.join(args.expDir, "train_delta",
                            f"decode_{args.order}grams"),
        hmm=model,
        HCLGfile=os.path.join(args.expDir, "train_delta", "graph",
                              f"HCLG.{args.order}.fst"),
    )
예제 #6
0
def main():

    # 1, Parse command line options.
    args.add("--order",
             abbr="-o",
             dtype=int,
             default=[2, 3, 4, 5, 6],
             minV=1,
             maxV=6,
             discription="The language model order.")
    args.add("--dataDir",
             abbr="-d",
             dtype=str,
             default="./exp",
             discription="The resource directory.")
    args.add("--exp",
             abbr="-e",
             dtype=str,
             default="./exp_lms",
             discription="Experiment output directory.")
    args.parse()

    # 2, Prepare text and lexicon.
    declare.is_file(
        f"./{args.dataDir}/data/train/text",
        debug=
        "There is not train text file avaliable. Please run '01_prepare_data.py' to generate it."
    )
    declare.is_file(
        f"./{args.dataDir}/data/test/text",
        debug=
        "There is not train text file avaliable. Please run '01_prepare_data.py' to generate it."
    )
    declare.is_file(
        f"./{args.dataDir}/dict/lexicons.lex",
        debug=
        "There is not lexicon file avaliable. Please run '02_make_dict_and_LM.py' to generate it."
    )

    trainTrans = exkaldi.load_transcription(
        f"./{args.dataDir}/data/train/text")
    lexicons = exkaldi.load_lex(f"./{args.dataDir}/dict/lexicons.lex")
    testTrans = exkaldi.load_transcription(f"./{args.dataDir}/data/test/text")

    # 3, Train LMs and compute perplexities.
    for o in args.order:

        for backend in ["sri", "ken"]:

            if backend == "sri":
                exkaldi.lm.train_ngrams_srilm(
                    lexicons,
                    order=o,
                    text=
                    trainTrans,  # If "text" received an exkaldi Transcription object, the information of utterance IDs will be omitted automatically.
                    outFile=os.path.join(args.exp, f"{backend}_{o}grams.arpa"),
                    config={"-wbdiscount": True},
                )

            else:
                exkaldi.lm.train_ngrams_kenlm(
                    lexicons,
                    order=o,
                    text=
                    trainTrans,  # If "text" received an exkaldi Transcription object, the information of utterance IDs will be omitted automatically.
                    outFile=os.path.join(args.exp, f"{backend}_{o}grams.arpa"),
                    config={
                        "--discount_fallback": True,
                        "-S": "20%"
                    },
                )

            exkaldi.lm.arpa_to_binary(
                arpaFile=os.path.join(args.exp, f"{backend}_{o}grams.arpa"),
                outFile=os.path.join(args.exp, f"{backend}_{o}grams.binary"),
            )

            model = exkaldi.load_ngrams(
                os.path.join(args.exp, f"{backend}_{o}grams.binary"))

            perScore = model.perplexity(testTrans)
            print(f"{o} {backend} score:", perScore)