summaryrefslogtreecommitdiff
path: root/kaldi_io/tools/convert_from_kaldi_pretrain.sh
blob: dc8ec8e9c4deee2e1ee18b07d3d082f61e8302d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/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
false && {
###### 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
$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