summaryrefslogtreecommitdiff
path: root/kaldi_io
diff options
context:
space:
mode:
Diffstat (limited to 'kaldi_io')
-rw-r--r--kaldi_io/Makefile18
-rw-r--r--kaldi_io/init.c2
-rw-r--r--kaldi_io/init.lua25
-rw-r--r--kaldi_io/kaldi_io-scm-1.rockspec2
-rw-r--r--kaldi_io/src/cwrapper_kaldi.cpp52
-rw-r--r--kaldi_io/src/cwrapper_kaldi.h15
-rw-r--r--kaldi_io/src/init.c17
-rw-r--r--kaldi_io/src/test.c7
-rwxr-xr-xkaldi_io/tools/convert_from_kaldi_pretrain.sh64
-rwxr-xr-xkaldi_io/tools/kaldi_to_nervbin0 -> 18719 bytes
-rw-r--r--kaldi_io/tools/kaldi_to_nerv.cpp57
-rw-r--r--kaldi_io/tools/nerv_to_kaldi.lua4
12 files changed, 198 insertions, 65 deletions
diff --git a/kaldi_io/Makefile b/kaldi_io/Makefile
index 7b0c0bd..abfa8e6 100644
--- a/kaldi_io/Makefile
+++ b/kaldi_io/Makefile
@@ -1,6 +1,12 @@
-# Change KDIR to `kaldi-trunk' path (Kaldi must be compiled with --share)
-KDIR := /slfs6/users/ymz09/kaldi/
+ifndef LUA_BINDIR
+$(error Please build the package via luarocks: `luarocks make`)
+endif
+ifndef KALDI_BASE
+$(error KALDI_BASE is not set)
+endif
+
+KDIR := $(KALDI_BASE)
SHELL := /bin/bash
BUILD_DIR := $(CURDIR)/build
INC_PATH := $(LUA_BINDIR)/../include/
@@ -20,19 +26,19 @@ OBJ_SUBDIR := $(addprefix $(OBJ_DIR)/,$(SUBDIR))
LUA_SUBDIR := $(addprefix $(LUA_DIR)/,$(SUBDIR))
LUA_LIBS := $(addprefix $(LUA_DIR)/,$(LUA_LIBS))
LIB_PATH := $(LUA_BINDIR)/../lib
+LUALIB_PATH := $(LUA_BINDIR)/../lib/lua/5.1/
build: $(OBJ_DIR) $(OBJ_SUBDIR) $(OBJS) $(OBJ_DIR)/src/test
install: $(LUA_DIR) $(LUA_SUBDIR) $(LUA_LIBS) $(LIBS)
include $(KDIR)/src/kaldi.mk
-KL := $(KDIR)/src/feat/kaldi-feat.a $(KDIR)/src/matrix/kaldi-matrix.a $(KDIR)/src/base/kaldi-base.a $(KDIR)/src/util/kaldi-util.a $(KDIR)/src/hmm/kaldi-hmm.a $(KDIR)/src/tree/kaldi-tree.a -lcblas -llapack_atlas
-
+KL := $(KDIR)/src/feat/kaldi-feat.a $(KDIR)/src/matrix/kaldi-matrix.a $(KDIR)/src/base/kaldi-base.a $(KDIR)/src/util/kaldi-util.a $(KDIR)/src/hmm/kaldi-hmm.a $(KDIR)/src/tree/kaldi-tree.a $(BLAS_LDFLAGS)
$(OBJ_DIR) $(LUA_DIR) $(OBJ_SUBDIR) $(LUA_SUBDIR):
-mkdir -p $@
$(LUA_DIR)/%.lua: %.lua
cp $< $@
$(LIBS): $(OBJ_DIR)/src/cwrapper_kaldi.o $(OBJ_DIR)/init.o $(OBJ_DIR)/src/init.o
- gcc -shared -fPIC -o $@ $(OBJ_DIR)/src/cwrapper_kaldi.o $(OBJ_DIR)/init.o $(OBJ_DIR)/src/init.o -lstdc++ -Wl,-rpath=$(LIB_PATH) -L$(LIB_PATH) -lnervcore -lluaT $(KL)
+ gcc -shared -fPIC -o $@ $(OBJ_DIR)/src/cwrapper_kaldi.o $(OBJ_DIR)/init.o $(OBJ_DIR)/src/init.o -lstdc++ -Wl,-rpath=$(LIB_PATH) -L$(LIB_PATH) -lnervcore -Wl,-rpath=$(LUALIB_PATH) -L$(LUALIB_PATH) -lnerv -lluaT $(KL)
$(OBJ_DIR)/src/cwrapper_kaldi.o: src/cwrapper_kaldi.cpp
g++ -o $@ -c $< -DHAVE_ATLAS $(KALDIINCLUDE) -g -fPIC $(INCLUDE) -DKALDI_DOUBLEPRECISION=0 -msse2 -DHAVE_POSIX_MEMALIGN
$(OBJ_DIR)/src/test: $(OBJ_DIR)/src/cwrapper_kaldi.o $(OBJ_DIR)/src/test.o
@@ -40,5 +46,5 @@ $(OBJ_DIR)/src/test: $(OBJ_DIR)/src/cwrapper_kaldi.o $(OBJ_DIR)/src/test.o
$(OBJ_DIR)/%.o: %.c
gcc -o $@ -c $< -g $(INCLUDE) -fPIC
clean:
- -rm $(OBJ_DIR)/src/*.o
+ -rm -r $(OBJ_DIR)
diff --git a/kaldi_io/init.c b/kaldi_io/init.c
index fe2f967..87682d2 100644
--- a/kaldi_io/init.c
+++ b/kaldi_io/init.c
@@ -1,4 +1,4 @@
-#include "../nerv/common.h"
+#include "nerv/lib/common.h"
#include <stdio.h>
extern void kaldi_io_init(lua_State *L);
diff --git a/kaldi_io/init.lua b/kaldi_io/init.lua
index 9f3ad55..751247a 100644
--- a/kaldi_io/init.lua
+++ b/kaldi_io/init.lua
@@ -7,6 +7,11 @@ function KaldiReader:__init(global_conf, reader_conf)
self.frm_ext = reader_conf.frm_ext
self.need_key = reader_conf.need_key -- for sequence training
self.gconf = global_conf
+ if self.gconf.use_cpu then
+ self.mat_type = self.gconf.mmat_type
+ else
+ self.mat_type = self.gconf.cumat_type
+ end
self.debug = global_conf.debug
if self.debug == nil then
self.debug = false
@@ -42,12 +47,15 @@ function KaldiReader:get_data()
end
local res = {}
-- read Kaldi feature
- local raw = self.gconf.cumat_type.new_from_host(self.feat_repo:cur_utter(self.debug))
+ local raw = self.feat_repo:cur_utter(self.debug)
+ if not self.gconf.use_cpu then
+ raw = self.gconf.cumat_type.new_from_host(raw)
+ end
local rearranged
if self.frm_ext and self.frm_ext > 0 then
local step = self.frm_ext * 2 + 1
-- expand the feature
- local expanded = self.gconf.cumat_type(raw:nrow(), raw:ncol() * step)
+ local expanded = self.mat_type(raw:nrow(), raw:ncol() * step)
expanded:expand_frm(raw, self.frm_ext)
-- rearrange the feature (``transpose'' operation in TNet)
if self.gconf.rearrange then
@@ -64,8 +72,12 @@ function KaldiReader:get_data()
feat_utter = self.gconf.mmat_type(rearranged:nrow() - self.gconf.frm_trim * 2, rearranged:ncol())
rearranged:copy_toh(feat_utter, self.gconf.frm_trim, rearranged:nrow() - self.gconf.frm_trim)
else
- feat_utter = self.gconf.mmat_type(rearranged:nrow(), rearranged:ncol())
- rearranged:copy_toh(feat_utter)
+ if self.gconf.use_cpu then
+ feat_utter = rearranged
+ else
+ feat_utter = self.gconf.mmat_type(rearranged:nrow(), rearranged:ncol())
+ rearranged:copy_toh(feat_utter)
+ end
end
res[self.feat_id] = feat_utter
if self.need_key then
@@ -76,8 +88,7 @@ function KaldiReader:get_data()
local lab_utter = repo:get_utter(self.feat_repo,
feat_utter:nrow(),
self.debug)
- -- need provide 'gconf.decoding = true' while decoding
- if not self.gconf.decoding and lab_utter == nil then
+ if lab_utter == nil then
fail_to_read_alignment = true
end
res[id] = lab_utter
@@ -104,7 +115,7 @@ function KaldiReader:get_data()
self.feat_repo:next()
collectgarbage("collect")
if fail_to_read_alignment then
- nerv.info("[Kaldi IO] utterance %s alignment not found, skip it.", self.feat_repo:key())
+ nerv.info("[kaldi] utterance %s alignment not found, skip it.", self.feat_repo:key())
res = self:get_data()
end
return res
diff --git a/kaldi_io/kaldi_io-scm-1.rockspec b/kaldi_io/kaldi_io-scm-1.rockspec
index 7c9f8d8..5a97cff 100644
--- a/kaldi_io/kaldi_io-scm-1.rockspec
+++ b/kaldi_io/kaldi_io-scm-1.rockspec
@@ -4,7 +4,7 @@ source = {
url = "https://github.com/Nerv-SJTU/nerv-speech.git"
}
description = {
- summary = "Kaldi I/O support (Kaldi I/O wrapper) for Nerv",
+ summary = "Kaldi I/O support (Kaldi I/O wrapper) for NERV",
detailed = [[
]],
homepage = "https://github.com/Nerv-SJTU/nerv-speech",
diff --git a/kaldi_io/src/cwrapper_kaldi.cpp b/kaldi_io/src/cwrapper_kaldi.cpp
index 542f1d0..788128b 100644
--- a/kaldi_io/src/cwrapper_kaldi.cpp
+++ b/kaldi_io/src/cwrapper_kaldi.cpp
@@ -10,10 +10,11 @@ extern "C" {
#include "cwrapper_kaldi.h"
#include "string.h"
#include "assert.h"
-#include "nerv/common.h"
+#include "nerv/lib/common.h"
+#include "nerv/lib/matrix/mmatrix.h"
- extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, Status *status);
- extern Matrix *nerv_matrix_host_double_create(long nrow, long ncol, Status *status);
+ extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, MContext *context, Status *status);
+ extern Matrix *nerv_matrix_host_double_create(long nrow, long ncol, MContext *context, Status *status);
struct KaldiFeatureRepo {
kaldi::SequentialBaseFloatMatrixReader* feature_reader;
@@ -26,7 +27,8 @@ extern "C" {
return repo;
}
- Matrix *kaldi_feature_repo_read_utterance(KaldiFeatureRepo *repo, lua_State *L, int debug) {
+ Matrix *kaldi_feature_repo_read_utterance(KaldiFeatureRepo *repo, lua_State *L,
+ int debug, MContext *context) {
Matrix *mat; /* nerv implementation */
repo->utt = repo->feature_reader->Key();
@@ -37,9 +39,9 @@ extern "C" {
Status status;
assert(sizeof(BaseFloat) == sizeof(float));
if(sizeof(BaseFloat) == sizeof(float))
- mat = nerv_matrix_host_float_create(n, m, &status);
+ mat = nerv_matrix_host_float_create(n, m, context, &status);
else if(sizeof(BaseFloat) == sizeof(double))
- mat = nerv_matrix_host_double_create(n, m, &status);
+ mat = nerv_matrix_host_double_create(n, m, context, &status);
NERV_LUA_CHECK_STATUS(L, status);
size_t stride = mat->stride;
if (debug)
@@ -80,26 +82,33 @@ extern "C" {
KaldiLookupFeatureRepo *kaldi_lookup_feature_repo_new(const char *feature_rspecifier, const char *map_rspecifier) {
KaldiLookupFeatureRepo *repo = new KaldiLookupFeatureRepo();
- kaldi::SequentialBaseFloatMatrixReader feature_reader = kaldi::SequentialBaseFloatMatrixReader(string(feature_rspecifier));
- for (; !feature_reader.Done(); feature_reader.Next())
+ kaldi::SequentialBaseFloatMatrixReader *feature_reader = \
+ new kaldi::SequentialBaseFloatMatrixReader(string(feature_rspecifier));
+ for (; !feature_reader->Done(); feature_reader->Next())
{
- const std::string &key = feature_reader.Key();
- const kaldi::Matrix<BaseFloat> &feat = feature_reader.Value();
+ const std::string &key = feature_reader->Key();
+ const kaldi::Matrix<BaseFloat> &feat = feature_reader->Value();
if (repo->key2mat.find(key) != repo->key2mat.end())
fprintf(stderr, "[kaldi] warning: lookup feature for key %s already exists", key.c_str());
repo->key2mat[key] = feat;
}
- kaldi::SequentialTokenVectorReader map_reader = kaldi::SequentialTokenVectorReader(string(map_rspecifier));
- for (; !map_reader.Done(); map_reader.Next())
+ delete feature_reader;
+ kaldi::SequentialTokenVectorReader *map_reader = \
+ new kaldi::SequentialTokenVectorReader(string(map_rspecifier));
+ for (; !map_reader->Done(); map_reader->Next())
{
- const std::vector<std::string> target = map_reader.Value();
+ const std::vector<std::string> target = map_reader->Value();
assert(target.size() >= 1);
- repo->map[map_reader.Key()] = *target.begin();
+ repo->map[map_reader->Key()] = *target.begin();
}
+ delete map_reader;
return repo;
}
- Matrix *kaldi_lookup_feature_repo_read_utterance(KaldiLookupFeatureRepo *repo, KaldiFeatureRepo *frepo, int nframes, lua_State *L, int debug) {
+ Matrix *kaldi_lookup_feature_repo_read_utterance(KaldiLookupFeatureRepo *repo,
+ KaldiFeatureRepo *frepo,
+ int nframes, lua_State *L,
+ int debug, MContext *context) {
Matrix *mat; /* nerv implementation */
StringToString_t::iterator mit = repo->map.find(frepo->utt);
if (mit == repo->map.end())
@@ -115,9 +124,9 @@ extern "C" {
Status status;
assert(sizeof(BaseFloat) == sizeof(float));
if(sizeof(BaseFloat) == sizeof(float))
- mat = nerv_matrix_host_float_create(n, m, &status);
+ mat = nerv_matrix_host_float_create(n, m, context, &status);
else if(sizeof(BaseFloat) == sizeof(double))
- mat = nerv_matrix_host_double_create(n, m, &status);
+ mat = nerv_matrix_host_double_create(n, m, context, &status);
NERV_LUA_CHECK_STATUS(L, status);
size_t stride = mat->stride;
if (debug)
@@ -149,13 +158,14 @@ extern "C" {
Matrix *kaldi_label_repo_read_utterance(KaldiLabelRepo *repo, KaldiFeatureRepo *frepo, int nframes,
lua_State *L,
- int debug) {
+ int debug, MContext *context) {
Matrix *mat = NULL;
/* check if the alignment of the utterance exists, otherwise return NULL */
kaldi::Posterior targets;
if (repo->targets_reader->HasKey(frepo->utt))
targets = repo->targets_reader->Value(frepo->utt);
- else return mat;
+ else
+ return mat;
int n = targets.size() < nframes ? targets.size() : nframes;
int m = (int)targets[0].size();
@@ -163,9 +173,9 @@ extern "C" {
Status status;
assert(sizeof(BaseFloat) == sizeof(float));
if(sizeof(BaseFloat) == sizeof(float))
- mat = nerv_matrix_host_float_create(n, m, &status);
+ mat = nerv_matrix_host_float_create(n, m, context, &status);
else if(sizeof(BaseFloat) == sizeof(double))
- mat = nerv_matrix_host_double_create(n, m, &status);
+ mat = nerv_matrix_host_double_create(n, m, context, &status);
NERV_LUA_CHECK_STATUS(L, status);
size_t stride = mat->stride;
if (debug)
diff --git a/kaldi_io/src/cwrapper_kaldi.h b/kaldi_io/src/cwrapper_kaldi.h
index c8a7a25..db20087 100644
--- a/kaldi_io/src/cwrapper_kaldi.h
+++ b/kaldi_io/src/cwrapper_kaldi.h
@@ -1,7 +1,7 @@
#ifndef NERV_kaldi_KALDI_IO_CWRAPPER
#define NERV_kaldi_KALDI_IO_CWRAPPER
-#include "nerv/matrix/matrix.h"
-#include "nerv/common.h"
+#include "nerv/lib/matrix/mmatrix.h"
+#include "nerv/lib/common.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -9,7 +9,8 @@ extern "C" {
typedef struct KaldiFeatureRepo KaldiFeatureRepo;
KaldiFeatureRepo *kaldi_feature_repo_new(const char *);
- Matrix *kaldi_feature_repo_read_utterance(KaldiFeatureRepo *repo, lua_State *L, int debug);
+ Matrix *kaldi_feature_repo_read_utterance(KaldiFeatureRepo *repo, lua_State *L,
+ int debug, MContext *context);
void kaldi_feature_repo_next(KaldiFeatureRepo *repo);
int kaldi_feature_repo_is_end(KaldiFeatureRepo *repo);
const char *kaldi_feature_repo_key(KaldiFeatureRepo *repo);
@@ -21,14 +22,18 @@ extern "C" {
Matrix *kaldi_label_repo_read_utterance(KaldiLabelRepo *repo, KaldiFeatureRepo *, int,
lua_State *L,
- int debug);
+ int debug,
+ MContext *context);
void kaldi_label_repo_destroy(KaldiLabelRepo *repo);
typedef struct KaldiLookupFeatureRepo KaldiLookupFeatureRepo;
KaldiLookupFeatureRepo *kaldi_lookup_feature_repo_new(const char *, const char *);
- Matrix *kaldi_lookup_feature_repo_read_utterance(KaldiLookupFeatureRepo *repo, KaldiFeatureRepo *frepo, int nframes, lua_State *L, int debug);
+ Matrix *kaldi_lookup_feature_repo_read_utterance(KaldiLookupFeatureRepo *repo,
+ KaldiFeatureRepo *frepo,
+ int nframes, lua_State *L,
+ int debug, MContext *context);
void kaldi_lookup_feature_repo_destroy(KaldiLookupFeatureRepo *repo);
#ifdef __cplusplus
diff --git a/kaldi_io/src/init.c b/kaldi_io/src/init.c
index 529895b..e8b4ea6 100644
--- a/kaldi_io/src/init.c
+++ b/kaldi_io/src/init.c
@@ -1,4 +1,5 @@
-#include "nerv/common.h"
+#include "nerv/lib/common.h"
+#include "nerv/matrix/matrix.h"
#include "cwrapper_kaldi.h"
#include <stdio.h>
@@ -21,12 +22,14 @@ static int feat_repo_destroy(lua_State *L) {
}
static int feat_repo_current_utterance(lua_State *L) {
+ MContext *context;
+ MMATRIX_GET_CONTEXT(L, 3);
KaldiFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_feat_repo_tname);
int debug;
if (!lua_isboolean(L, 2))
nerv_error(L, "debug flag should be a boolean");
debug = lua_toboolean(L, 2);
- Matrix *utter = kaldi_feature_repo_read_utterance(repo, L, debug);
+ Matrix *utter = kaldi_feature_repo_read_utterance(repo, L, debug, context);
luaT_pushudata(L, utter, nerv_matrix_host_float_tname);
return 1;
}
@@ -72,6 +75,8 @@ static int lookup_feat_repo_destroy(lua_State *L) {
}
static int lookup_feat_repo_read_utterance(lua_State *L) {
+ MContext *context;
+ MMATRIX_GET_CONTEXT(L, 5);
KaldiLookupFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_lookup_feat_repo_tname);
KaldiFeatureRepo *feat_repo = luaT_checkudata(L, 2, nerv_kaldi_feat_repo_tname);
int nframes, debug;
@@ -81,7 +86,8 @@ static int lookup_feat_repo_read_utterance(lua_State *L) {
if (!lua_isboolean(L, 4))
nerv_error(L, "debug flag should be a boolean");
debug = lua_toboolean(L, 4);
- Matrix *utter = kaldi_lookup_feature_repo_read_utterance(repo, feat_repo, nframes, L, debug);
+ Matrix *utter = kaldi_lookup_feature_repo_read_utterance(repo, feat_repo,
+ nframes, L, debug, context);
luaT_pushudata(L, utter, nerv_matrix_host_float_tname);
return 1;
}
@@ -100,6 +106,8 @@ static int label_repo_new(lua_State *L) {
}
static int label_repo_read_utterance(lua_State *L) {
+ MContext *context;
+ MMATRIX_GET_CONTEXT(L, 5);
KaldiLabelRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_label_repo_tname);
KaldiFeatureRepo *feat_repo = luaT_checkudata(L, 2, nerv_kaldi_feat_repo_tname);
int nframes, debug;
@@ -109,7 +117,8 @@ static int label_repo_read_utterance(lua_State *L) {
if (!lua_isboolean(L, 4))
nerv_error(L, "debug flag should be a boolean");
debug = lua_toboolean(L, 4);
- Matrix *utter = kaldi_label_repo_read_utterance(repo, feat_repo, nframes, L, debug);
+ Matrix *utter = kaldi_label_repo_read_utterance(repo, feat_repo, nframes,
+ L, debug, context);
luaT_pushudata(L, utter, nerv_matrix_host_float_tname);
return 1;
}
diff --git a/kaldi_io/src/test.c b/kaldi_io/src/test.c
index e92b4c9..e3368df 100644
--- a/kaldi_io/src/test.c
+++ b/kaldi_io/src/test.c
@@ -7,8 +7,11 @@
**********************************************************************************/
#include "cwrapper_kaldi.h"
+#include "nerv/lib/matrix/mmatrix.h"
#include <stdio.h>
+MContext context;
+
char feature_rspecifier[] = {"ark:/slfs6/users/ymz09/kaldi/src/featbin/copy-feats scp:/slfs6/users/ymz09/swb_ivec/train_bp.scp ark:- |"};
void print_nerv_matrix(Matrix *mat) {
@@ -30,13 +33,13 @@ int main(int argc, char *argv[])
Matrix *mat;
KaldiFeatureRepo *repo = kaldi_feature_repo_new(feature_rspecifier);
- mat = kaldi_feature_repo_read_utterance(repo, NULL, 1);
+ mat = kaldi_feature_repo_read_utterance(repo, NULL, 1, &context);
printf("1st uttrance\n");
print_nerv_matrix(mat);
kaldi_feature_repo_next(repo);
- mat = kaldi_feature_repo_read_utterance(repo, NULL, 1);
+ mat = kaldi_feature_repo_read_utterance(repo, NULL, 1, &context);
printf("2nd uttrance\n");
print_nerv_matrix(mat);
diff --git a/kaldi_io/tools/convert_from_kaldi_pretrain.sh b/kaldi_io/tools/convert_from_kaldi_pretrain.sh
new file mode 100755
index 0000000..78f532f
--- /dev/null
+++ b/kaldi_io/tools/convert_from_kaldi_pretrain.sh
@@ -0,0 +1,64 @@
+#!/bin/bash
+
+hid_dim=1024
+hid_num=6
+pretrain_dir=exp/dnn4_pretrain-dbn
+nerv_kaldi=/speechlab/users/mfy43/nerv/speech/kaldi_io/
+
+[ -f path.sh ] && . ./path.sh
+. parse_options.sh || exit 1;
+
+data=$1
+data_cv=$2
+lang=$3
+alidir=$4
+alidir_cv=$5
+dir=$6
+
+[[ -z $data_fmllr ]] && data_fmllr=data-fmllr-tri3
+[[ -z $alidir ]] && alidir=exp/tri3_ali
+[[ -z $dir ]] && dir=exp/dnn4_nerv_prepare
+[[ -z $data ]] && data=$data_fmllr/train_tr90
+[[ -z $data_cv ]] && data_cv=$data_fmllr/train_cv10
+kaldi_to_nerv=$nerv_kaldi/tools/kaldi_to_nerv
+mkdir $dir -p
+mkdir $dir/log -p
+###### PREPARE DATASETS ######
+cp $data/feats.scp $dir/train_sorted.scp
+cp $data_cv/feats.scp $dir/cv.scp
+utils/shuffle_list.pl --srand ${seed:-777} <$dir/train_sorted.scp >$dir/train.scp
+
+feats_tr="ark:copy-feats scp:$dir/train.scp ark:- |"
+
+###### INITIALIZE OUTPUT LAYER ######
+[ -z $num_tgt ] && \
+ num_tgt=$(hmm-info --print-args=false $alidir/final.mdl | grep pdfs | awk '{ print $NF }')
+nnet_proto=$dir/nnet_output.proto
+echo "# genrating network prototype $nnet_proto"
+utils/nnet/make_nnet_proto.py \
+ $hid_dim $num_tgt 0 $hid_dim >$nnet_proto || exit 1
+nnet_init=$dir/nnet_output.init
+nnet-initialize --binary=false $nnet_proto $nnet_init
+
+###### MODEL PARAMETER CONVERSION ######
+$kaldi_to_nerv $nnet_init $dir/nnet_output.nerv $hid_num
+$kaldi_to_nerv <(nnet-copy --binary=false $pretrain_dir/${hid_num}.dbn -) $dir/nnet_init.nerv
+$kaldi_to_nerv <(nnet-copy --binary=false $pretrain_dir/final.feature_transform -) $dir/nnet_trans.nerv
+###### PREPARE FOR DECODING #####
+echo "Using PDF targets from dirs '$alidir' '$alidir_cv'"
+# training targets in posterior format,
+labels_tr="ark:ali-to-pdf $alidir/final.mdl \"ark:gunzip -c $alidir/ali.*.gz |\" ark:- | ali-to-post ark:- ark:- |"
+labels_cv="ark:ali-to-pdf $alidir/final.mdl \"ark:gunzip -c $alidir_cv/ali.*.gz |\" ark:- | ali-to-post ark:- ark:- |"
+# training targets for analyze-counts,
+labels_tr_pdf="ark:ali-to-pdf $alidir/final.mdl \"ark:gunzip -c $alidir/ali.*.gz |\" ark:- |"
+labels_tr_phn="ark:ali-to-phones --per-frame=true $alidir/final.mdl \"ark:gunzip -c $alidir/ali.*.gz |\" ark:- |"
+
+# get pdf-counts, used later for decoding/aligning,
+analyze-counts --verbose=1 --binary=false "$labels_tr_pdf" $dir/ali_train_pdf.counts 2>$dir/log/analyze_counts_pdf.log || exit 1
+# copy the old transition model, will be needed by decoder,
+copy-transition-model --binary=false $alidir/final.mdl $dir/final.mdl || exit 1
+# copy the tree
+cp $alidir/tree $dir/tree || exit 1
+
+# make phone counts for analysis,
+[ -e $lang/phones.txt ] && analyze-counts --verbose=1 --symbol-table=$lang/phones.txt "$labels_tr_phn" /dev/null 2>$dir/log/analyze_counts_phones.log || exit 1
diff --git a/kaldi_io/tools/kaldi_to_nerv b/kaldi_io/tools/kaldi_to_nerv
new file mode 100755
index 0000000..78469f8
--- /dev/null
+++ b/kaldi_io/tools/kaldi_to_nerv
Binary files differ
diff --git a/kaldi_io/tools/kaldi_to_nerv.cpp b/kaldi_io/tools/kaldi_to_nerv.cpp
index 1edb0f2..f16de44 100644
--- a/kaldi_io/tools/kaldi_to_nerv.cpp
+++ b/kaldi_io/tools/kaldi_to_nerv.cpp
@@ -3,31 +3,53 @@
#include <string>
#include <cstring>
#include <cassert>
+#include <cstdlib>
char token[1024];
char output[1024];
-double mat[4096][4096];
+
+double **new_matrix(int nrow, int ncol) {
+ double **mat = new double *[nrow];
+ int i;
+ for (i = 0; i < nrow; i++)
+ mat[i] = new double[ncol];
+ return mat;
+}
+
+void free_matrix(double **mat, int nrow, int ncol) {
+ int i;
+ for (i = 0; i < nrow; i++)
+ delete [] mat[i];
+ delete [] mat;
+}
+
int main(int argc, char **argv) {
+ FILE *fin;
std::ofstream fout;
- fout.open(argv[1]);
- int cnt = 0;
+ assert(argc >= 3);
+ fin = fopen(argv[1], "r");
+ fout.open(argv[2]);
+ assert(fin != NULL);
+ int cnt = argc > 3 ? atoi(argv[3]) : 0;
bool shift;
- while (scanf("%s", token) != EOF)
+ while (fscanf(fin, "%s", token) != EOF)
{
int nrow, ncol;
int i, j;
+ double **mat;
if (strcmp(token, "<AffineTransform>") == 0)
{
double lrate, blrate, mnorm;
- scanf("%d %d", &ncol, &nrow);
- scanf("%s %lf %s %lf %s %lf",
+ fscanf(fin, "%d %d", &ncol, &nrow);
+ fscanf(fin, "%s %lf %s %lf %s %lf",
token, &lrate, token, &blrate, token, &mnorm);
- scanf("%s", token);
+ fscanf(fin, "%s", token);
assert(*token == '[');
printf("%d %d\n", nrow, ncol);
+ mat = new_matrix(nrow, ncol);
for (j = 0; j < ncol; j++)
for (i = 0; i < nrow; i++)
- scanf("%lf", mat[i] + j);
+ fscanf(fin, "%lf", mat[i] + j);
long base = fout.tellp();
sprintf(output, "%16d", 0);
fout << output;
@@ -47,13 +69,13 @@ int main(int argc, char **argv) {
sprintf(output, "[%13lu]\n", length);
fout << output;
fout.seekp(0, std::ios_base::end);
- scanf("%s", token);
+ fscanf(fin, "%s", token);
assert(*token == ']');
- if (scanf("%s", token) == 1 && *token == '[')
+ if (fscanf(fin, "%s", token) == 1 && *token == '[')
{
base = fout.tellp();
for (j = 0; j < ncol; j++)
- scanf("%lf", mat[0] + j);
+ fscanf(fin, "%lf", mat[0] + j);
sprintf(output, "%16d", 0);
fout << output;
sprintf(output, "{type=\"nerv.BiasParam\",id=\"affine%d_bp\"}\n",
@@ -71,19 +93,21 @@ int main(int argc, char **argv) {
fout.seekp(0, std::ios_base::end);
cnt++;
}
+ free_matrix(mat, nrow, ncol);
}
else if ((shift = (strcmp(token, "<AddShift>") == 0)) ||
strcmp(token, "<Rescale>") == 0)
{
double lrate, blrate, mnorm;
- scanf("%d %d", &ncol, &ncol);
- scanf("%s %lf",
+ fscanf(fin, "%d %d", &ncol, &ncol);
+ mat = new_matrix(1, ncol);
+ fscanf(fin, "%s %lf",
token, &lrate);
- scanf("%s", token);
+ fscanf(fin, "%s", token);
assert(*token == '[');
printf("%d\n", ncol);
for (j = 0; j < ncol; j++)
- scanf("%lf", mat[0] + j);
+ fscanf(fin, "%lf", mat[0] + j);
long base = fout.tellp();
sprintf(output, "%16d", 0);
fout << output;
@@ -101,8 +125,9 @@ int main(int argc, char **argv) {
sprintf(output, "[%13lu]\n", length);
fout << output;
fout.seekp(0, std::ios_base::end);
- scanf("%s", token);
+ fscanf(fin, "%s", token);
assert(*token == ']');
+ free_matrix(mat, 1, ncol);
}
}
return 0;
diff --git a/kaldi_io/tools/nerv_to_kaldi.lua b/kaldi_io/tools/nerv_to_kaldi.lua
index 804f09b..fba6a6c 100644
--- a/kaldi_io/tools/nerv_to_kaldi.lua
+++ b/kaldi_io/tools/nerv_to_kaldi.lua
@@ -1,8 +1,8 @@
--- usage: nerv config_file nerv_param_input tnet_output
+-- usage: nerv nerv_to_kaldi.lua config_file nerv_param_input kaldi_param_output
dofile(arg[1])
param_repo = nerv.ParamRepo()
-param_repo:import({arg[2], gconf.initialized_param[2]}, nil, gconf)
+param_repo:import({arg[2]}, nil, gconf)
layer_repo = make_layer_repo(param_repo)
f = assert(io.open(arg[3], "w"))