~ubuntu-branches/ubuntu/vivid/sphinxtrain/vivid

« back to all changes in this revision

Viewing changes to scripts/sphinxtrain

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-01-02 04:10:21 UTC
  • Revision ID: package-import@ubuntu.com-20130102041021-ynsizmz33fx02hea
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import getopt, sys, os
 
4
 
 
5
 
 
6
# Find the location of the files
 
7
currentpath = os.path.dirname(os.path.abspath(__file__))
 
8
 
 
9
sphinxbinpath = os.path.abspath(currentpath + "/../libexec/sphinxtrain")
 
10
if os.path.exists(currentpath + "/../bin/Release"):
 
11
    sphinxbinpath = os.path.abspath(currentpath + "/../bin/Release")
 
12
 
 
13
sphinxpath = os.path.abspath(currentpath + "/../lib/sphinxtrain")
 
14
if os.path.exists(currentpath + "/../scripts/00.verify"):
 
15
    sphinxpath = os.path.abspath(currentpath + "/..")
 
16
 
 
17
 
 
18
# Perl script want forward slashes
 
19
training_basedir = os.getcwd().replace('\\', '/');
 
20
sphinxpath = sphinxpath.replace('\\','/')
 
21
sphinxbinpath = sphinxbinpath.replace('\\','/')
 
22
 
 
23
print "Sphinxtrain path:", sphinxpath
 
24
print "Sphinxtrain binaries path:", sphinxbinpath
 
25
 
 
26
#Proceed
 
27
def setup(task):
 
28
    if not os.path.exists("etc"):
 
29
        os.mkdir("etc")
 
30
 
 
31
    print "Setting up the database " + task
 
32
 
 
33
    out_cfg = open("etc/sphinx_train.cfg", "w")
 
34
    for line in open(sphinxpath + "/etc/sphinx_train.cfg", "r"):
 
35
        line = line.replace("___DB_NAME___", task)
 
36
        line = line.replace("___BASE_DIR___", training_basedir)
 
37
        line = line.replace("___SPHINXTRAIN_DIR___", sphinxpath)
 
38
        line = line.replace("___SPHINXTRAIN_BIN_DIR___", sphinxbinpath)
 
39
        out_cfg.write(line)
 
40
    out_cfg.close()
 
41
 
 
42
    out_cfg = open("etc/feat.params", "w")
 
43
    for line in open(sphinxpath + "/etc/feat.params", "r"):
 
44
        out_cfg.write(line)
 
45
    out_cfg.close()
 
46
 
 
47
steps = [
 
48
"000.comp_feat/slave_feat.pl",
 
49
"00.verify/verify_all.pl",
 
50
"0000.g2p_train/g2p_train.pl",
 
51
"01.lda_train/slave_lda.pl",
 
52
"02.mllt_train/slave_mllt.pl",
 
53
"05.vector_quantize/slave.VQ.pl",
 
54
"10.falign_ci_hmm/slave_convg.pl",
 
55
"11.force_align/slave_align.pl",
 
56
"12.vtln_align/slave_align.pl",
 
57
"20.ci_hmm/slave_convg.pl",
 
58
"30.cd_hmm_untied/slave_convg.pl",
 
59
"40.buildtrees/slave.treebuilder.pl",
 
60
"45.prunetree/slave.state-tying.pl",
 
61
"50.cd_hmm_tied/slave_convg.pl",
 
62
"60.lattice_generation/slave_genlat.pl",
 
63
"61.lattice_pruning/slave_prune.pl",
 
64
"62.lattice_conversion/slave_conv.pl",
 
65
"65.mmie_train/slave_convg.pl",
 
66
"90.deleted_interpolation/deleted_interpolation.pl",
 
67
"decode/slave.pl"
 
68
]
 
69
 
 
70
def run_stages(stages):
 
71
    for stage in stages.split(","):
 
72
        for step in steps:
 
73
                name = step.split("/")[0].split(".")[-1]
 
74
                if name == stage:
 
75
                    os.system(sphinxpath + "/scripts/" + step)
 
76
 
 
77
def run():
 
78
    print "Running the training"
 
79
    for step in steps:
 
80
        os.system(sphinxpath + "/scripts/" + step)
 
81
 
 
82
def usage():
 
83
    print ""
 
84
    print "Sphinxtrain processes the audio files and creates and acoustic model "
 
85
    print "for CMUSphinx toolkit. The data needs to have a certain layout "
 
86
    print "See the tutorial http://cmusphinx.sourceforge.net/wiki/tutorialam "
 
87
    print "for details"
 
88
    print ""
 
89
    print "Usage: sphinxtrain [options] <command>"
 
90
    print ""
 
91
    print "Commands:"
 
92
    print "     -t <task> setup - copy configuration into database"
 
93
    print "     [-s <stage1,stage2,stage3>] run - run the training or just selected stages"
 
94
 
 
95
def main():
 
96
 
 
97
    try:
 
98
        opts, args = getopt.getopt(sys.argv[1:], "ht:s:", ["help", "task", "stages"])
 
99
    except getopt.GetoptError, err:
 
100
        print str(err)
 
101
        usage()
 
102
        sys.exit(-1)
 
103
 
 
104
    task = None
 
105
    stages = None
 
106
 
 
107
    for o, a in opts:
 
108
        if o in ("-t", "--task"):
 
109
            task = a
 
110
        if o in ("-s", "--stages"):
 
111
            stages = a
 
112
        if o in ("-h", "--help"):
 
113
                usage()
 
114
 
 
115
    if len(args) == 0:
 
116
        usage()
 
117
        sys.exit(-1)
 
118
 
 
119
    command = args[0]
 
120
 
 
121
    if command == "setup":
 
122
        if task == None:
 
123
            print "No task name defined"
 
124
            sys.exit(-1)        
 
125
        setup(task)
 
126
    elif command == "run":
 
127
        if stages == None:
 
128
            run()
 
129
        else:
 
130
            run_stages(stages)
 
131
    else:
 
132
        run()
 
133
 
 
134
if __name__ == "__main__":
 
135
    main()