Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:aalto-speech/AaltoASR into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
psmit committed Feb 4, 2014
2 parents 3704b9b + a66c3bf commit d9a4b40
Show file tree
Hide file tree
Showing 49 changed files with 3,187 additions and 1,717 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ ENDIF(UNIX AND NOT WIN32)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

ADD_DEFINITIONS(-std=gnu++0x)

include(ExternalProject)
ExternalProject_Add(lapackpp_ext
PREFIX ${CMAKE_BINARY_DIR}/vendor/lapackpp
Expand Down Expand Up @@ -50,3 +52,4 @@ add_subdirectory( aku )




2 changes: 1 addition & 1 deletion aku/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ ENDIF(FFTW_FOUND AND NOT KISS_FFT)
add_library( aku ${AKUSOURCES} )
add_dependencies(aku lapackpp_ext)

set(AKU_CMDS feadot feanorm phone_probs segfea vtln quanteq stats estimate align tie dur_est gconvert mllr logl gcluster lda optmodel cmpmodel combine_stats regtree clsstep clskld opt_ebw_d )
set(AKU_CMDS feacat feadot feanorm phone_probs segfea vtln quanteq stats estimate align tie dur_est gconvert mllr logl gcluster lda optmodel cmpmodel combine_stats regtree clsstep clskld opt_ebw_d )

foreach(AKU_CMD ${AKU_CMDS})
add_executable ( ${AKU_CMD} ${AKU_CMD}.cc )
Expand Down
2 changes: 1 addition & 1 deletion aku/Distributions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,7 @@ Mixture::estimate_parameters(EstimationMode mode)
// m_weights[i] /= norm;

// Heuristics: If outside permitted region, move halfway
if (!isnan(sol1))
if (!std::isnan(sol1))
{
if (sol1 <= 0)
m_weights[i] = m_weights[i]/2.0;
Expand Down
9 changes: 2 additions & 7 deletions decoder/rec_eng_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,14 @@ def rec(start, end):
# Recognize
#

t = Decoder.Toolbox()
sys.stderr.write("loading models\n")
t = Decoder.Toolbox(0, hmms, dur)

t.select_decoder(0)
t.set_optional_short_silence(1)
t.set_cross_word_triphones(1)
t.set_require_sentence_end(1)
t.set_silence_is_word(0)

sys.stderr.write("loading models\n")
t.hmm_read(hmms)
t.duration_read(dur)
#t.sr_read(sr_model)

t.set_verbose(1)
t.set_print_text_result(1)
t.set_print_word_start_frame(1)
Expand Down
7 changes: 2 additions & 5 deletions decoder/rec_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,15 @@ def rec(start, end):
# Recognize
#

t = Decoder.Toolbox()
sys.stderr.write("loading models\n")
t = Decoder.Toolbox(0, hmms, dur)

t.select_decoder(0)
t.set_optional_short_silence(1)

t.set_cross_word_triphones(1)

t.set_require_sentence_end(1)

sys.stderr.write("loading models\n")
t.hmm_read(hmms)
t.duration_read(dur)

t.set_verbose(1)
t.set_print_text_result(1)
Expand Down
9 changes: 2 additions & 7 deletions decoder/rec_hammaspuhe.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,8 @@ def logprobmul(x, y):
# Recognize
#

t = Decoder.Toolbox()

t.select_decoder(0)
print "Loading acoustic model."
t = Decoder.Toolbox(0, hmms, dur)

if morph_model:
t.set_silence_is_word(1)
Expand All @@ -229,10 +228,6 @@ def logprobmul(x, y):
t.set_cross_word_triphones(1)
t.set_require_sentence_end(1)

print "Loading acoustic model."
t.hmm_read(hmms)
t.duration_read(dur)

t.set_verbose(1)
t.set_print_text_result(0)

Expand Down
9 changes: 2 additions & 7 deletions decoder/rec_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,15 @@ def rec(start, end):
# Recognize
#

t = Decoder.Toolbox()
sys.stderr.write("loading models\n")
t = Decoder.Toolbox(0, hmms, dur)

t.select_decoder(0)
t.set_optional_short_silence(1)

t.set_cross_word_triphones(1)

t.set_require_sentence_end(1)

sys.stderr.write("loading models\n")
t.hmm_read(hmms)
t.duration_read(dur)
#t.sr_read(sr_model)

t.set_verbose(1)
t.set_print_text_result(1)
#t.set_print_state_segmentation(1)
Expand Down
9 changes: 2 additions & 7 deletions decoder/rec_recipe2.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,15 @@ def rec(start, end):
# Recognize
#

t = Decoder.Toolbox()
sys.stderr.write("loading models\n")
t = Decoder.Toolbox(0, hmms, dur)

t.select_decoder(0)
t.set_optional_short_silence(1)

t.set_cross_word_triphones(1)

t.set_require_sentence_end(1)

sys.stderr.write("loading models\n")
t.hmm_read(hmms)
t.duration_read(dur)
#t.sr_read(sr_model)

t.set_verbose(1)
t.set_print_text_result(1)
#t.set_print_state_segmentation(1)
Expand Down
12 changes: 10 additions & 2 deletions decoder/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ set(DECODERSOURCES
ArpaReader.cc
InterTreeGram.cc
WordClasses.cc
FstAcoustics.cc
Fst.cc
FstConfidence.cc
)

ADD_DEFINITIONS(-std=gnu++0x)
add_library( decoder ${DECODERSOURCES} )
add_executable ( arpa2bin arpa2bin.cc )
add_executable ( bin2arpa bin2arpa.cc )
target_link_libraries ( arpa2bin decoder fsalm misc )
target_link_libraries ( bin2arpa decoder fsalm misc )
add_executable ( hmm2fsm hmm2fsm.cc )
#add_executable ( fst_test fst_test.cc )
target_link_libraries ( arpa2bin decoder fsalm misc)
target_link_libraries ( bin2arpa decoder fsalm misc)
target_link_libraries ( hmm2fsm decoder )
#target_link_libraries ( fst_test decoder )

install(TARGETS arpa2bin bin2arpa DESTINATION bin)
install(FILES Toolbox.hh WordGraph.hh NowayHmmReader.hh Hmm.hh NowayLexiconReader.hh Lexicon.hh Vocabulary.hh NowayLexiconReader.hh TPNowayLexReader.hh TPLexPrefixTree.hh config.hh HashCache.hh SimpleHashCache.hh LMHistory.hh history.hh WordClasses.hh LnaReaderCircular.hh Acoustics.hh Expander.hh Search.hh TreeGram.hh NGram.hh TokenPassSearch.hh OneFrameAcoustics.hh DESTINATION include)
Expand Down
12 changes: 6 additions & 6 deletions decoder/src/Expander.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ struct TokenCompare {
};


Expander::Expander(const std::vector<Hmm> &hmms, Lexicon &lexicon,
Expander::Expander(const std::vector<Hmm> &hmms,
Acoustics &acoustics)
: m_hmms(hmms),
m_lexicon(lexicon),
m_lexicon(NULL),
m_acoustics(acoustics),

m_forced_end(false),
Expand Down Expand Up @@ -476,7 +476,7 @@ Expander::clear_tokens()
void
Expander::create_initial_tokens(int start_frame)
{
Lexicon::Node *node = m_lexicon.root();
Lexicon::Node *node = m_lexicon->root();
Lexicon::Token *token;

for (int next_id = 0; next_id < node->next.size(); next_id++) {
Expand Down Expand Up @@ -562,12 +562,12 @@ Expander::debug_print_tokens()
void
Expander::expand(int start_frame, int frames)
{
if (m_words.size() != m_lexicon.words()) {
if (m_words.size() != m_lexicon->words()) {
// Changing lexicon size is not supported at the moment
assert(m_words.size() == 0);
m_active_words.clear();
m_active_words.reserve(m_lexicon.words());
m_words.resize(m_lexicon.words());
m_active_words.reserve(m_lexicon->words());
m_words.resize(m_lexicon->words());
for (int i = 0; i < m_words.size(); i++)
m_words[i].word_id = i;
}
Expand Down
5 changes: 3 additions & 2 deletions decoder/src/Expander.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public:
};

// Actions
Expander(const std::vector<Hmm> &hmms, Lexicon &lexicon,
Expander(const std::vector<Hmm> &hmms,
Acoustics &m_acoustics);

~Expander();
Expand All @@ -66,6 +66,7 @@ public:

void set_post_durations(bool durations) { m_post_durations = durations; }
void set_rabiner_post_mode(int mode) { m_rabiner_post_mode = mode; }
void set_lexicon(Lexicon *l) {m_lexicon = l;}

// Info
inline std::vector<Lexicon::Token*> &tokens() { return m_tokens; }
Expand Down Expand Up @@ -109,7 +110,7 @@ private:
void release_token(Lexicon::Token *token);

const std::vector<Hmm> &m_hmms;
Lexicon &m_lexicon;
Lexicon *m_lexicon;
Acoustics &m_acoustics;

// Options
Expand Down
103 changes: 103 additions & 0 deletions decoder/src/Fst.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "Fst.hh"
#include "misc/str.hh"

#include <cstdlib>
#define strtof strtod

Fst::Fst(): initial_node_idx(-1) {
}

void Fst::read(std::string &fname) {
std::string line;

FILE *ifh = fopen(fname.c_str(), "r");
if (ifh==nullptr) {
perror("Error");
exit(-1); // FIXME: we should use exceptions
}

str::read_line(line, ifh, true);
if (line != "#FSTBasic MaxPlus") {
fprintf(stderr, "Unknown header '%s'.\n", line.c_str());
throw ReadError();
}
std::vector<std::string> fields;
while (str::read_line(line, ifh, true)) {
fields = str::split(line, " ", true);
if (fields.size()<2) {
fprintf(stderr, "Too few fields '%s'.\n", line.c_str());
throw ReadError();
}

// Resize nodes to the size of the first mentioned node
auto first_node_idx = atoi(fields[1].c_str());
if (nodes.size() <= first_node_idx) {
nodes.resize(first_node_idx+1);
}

if (fields[0]=="I") {
initial_node_idx = first_node_idx;
if (fields.size()>2) {
fprintf(stderr, "Too many fields for I: '%s'.\n", line.c_str());
throw ReadError();
}
continue;
}

if (fields[0]=="F") {
nodes[first_node_idx].end_node = true;
if (fields.size()>2) {
fprintf(stderr, "Too many fields for F: '%s'.\n", line.c_str());
throw ReadError();
}
continue;
}

if (fields[0]=="T") {
if (fields.size()<3 || fields.size()>6) {
fprintf(stderr, "Weird number of fields for T: '%s'.\n", line.c_str());
throw ReadError();
}

auto second_node_idx = atoi(fields[2].c_str());
if (nodes.size() <= second_node_idx) {
nodes.resize(second_node_idx+1);
}

auto aidx=arcs.size();
arcs.resize(aidx+1);
Arc &a = arcs[aidx];
a.source = first_node_idx;
a.target = second_node_idx;

if (fields.size()>=5) {
if (fields[4] != ",") {
a.emit_symbol = fields[4];
}
}

if (fields.size()>=6) {
a.transition_logprob = strtof(fields[5].c_str(), nullptr);
}
nodes[first_node_idx].arcidxs.push_back(aidx);

// Move emission pdf indices from arcs to nodes
auto emission_pdf_idx = atoi(fields[3].c_str());
if (nodes[second_node_idx].emission_pdf_idx==-1) {
nodes[second_node_idx].emission_pdf_idx = emission_pdf_idx;
} else if (nodes[second_node_idx].emission_pdf_idx != emission_pdf_idx) {
fprintf(stderr, "Conflicting emission_pdf_indices for node %d: %d != %d.\n",
second_node_idx, nodes[second_node_idx].emission_pdf_idx, emission_pdf_idx);
throw ReadError();
}


} else {
fprintf(stderr, "Weird type indicator: '%s'.\n", fields[0].c_str());
throw ReadError();
}

}
fclose(ifh);

}
55 changes: 55 additions & 0 deletions decoder/src/Fst.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef FST_HH
#define FST_HH
/*
Simple class to handle mitfst (http://people.csail.mit.edu/ilh/fst/) format networks.
AT&T fst toolkit and openfst have very similar formats, so this may work directly or
with small adjustments with thosenetworks.
*/

#include <vector>
#include <string>
#include <sstream>

class Fst {
public:
struct ReadError : public std::exception {
virtual const char *what() const throw() {
return "Fst: read error"; }
};

struct Arc {
int source;
int target;
float transition_logprob;
std::string emit_symbol;

inline std::string str() {
std::ostringstream os;
os << "Arc " << source << " -> " << target << " (" << transition_logprob << "): " << emit_symbol;
return os.str();
}
};

struct Node {
Node() : emission_pdf_idx(-1), end_node(false) {}
int emission_pdf_idx;
std::vector<int> arcidxs;
bool end_node;

inline std::string str() {
std::ostringstream os;
os << "Node " << emission_pdf_idx << " (" << arcidxs.size() << ")";
return os.str();
}
};

Fst();
void read(std::string &);
inline void read(const char *s) {std::string ss(s); read(ss);}
int initial_node_idx;
std::vector<Node> nodes;
std::vector<Arc> arcs;
};

#endif

Loading

0 comments on commit d9a4b40

Please sign in to comment.