机器学习 / 语音识别 · 2023年8月1日

Kaldi解码器移植(Nano-pi M1) 二

下载KALDI源码,为了便于后续单独编译,故将其KALDI的源码精减一下,将解码器部单独分离出来,而用于训练的模型的KALDI源码则单独做另外一的份。

1.精减解码器源码

将KALDI源码下载后,删除到只保留src目录 与tools 目录,其它文件全清除

(base) zsf@kaldi-Super-Server:/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source$ ls
Readme.txt  src  tools

然后再将tools下的文件跟文件 夹全部清除,用于存放之前交叉编译好的文件 ,目录结构如下:

(base) zsf@kaldi-Super-Server:/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source/tools$ ls
alsa  openBLAS  openfst  portaudio  ReadMe.txt
(base) zsf@kaldi-Super-Server:/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source/tools$ 

在拷贝过程中要注意,将CLAPCK中生成的库全部放到openBlas/lib目录下,并且重命名

mv lapack_LINUX.a  libclapack.a
mv blas_LINUX.a  libblas.a

tools/openBLAS/lib目录下,结构如下:

(base) zsf@kaldi-Super-Server:/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source/tools/openBLAS$ ls
include  lib
(base) zsf@kaldi-Super-Server:/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source/tools/openBLAS$ tree lib/
lib/
├── cmake
│   └── openblas
│       ├── OpenBLASConfig.cmake
│       └── OpenBLASConfigVersion.cmake
├── libblas.a
├── libclapack.a
├── libf2c.a
├── libopenblas.a -> libopenblas_armv7p-r0.2.20.a
├── libopenblas_armv7p-r0.2.20.a
├── libopenblas_armv7p-r0.2.20.so
├── libopenblas.so -> libopenblas_armv7p-r0.2.20.so
├── libopenblas.so.0 -> libopenblas_armv7p-r0.2.20.so
└── pkgconfig
    └── openblas.pc

3 directories, 11 files

2.编译

进入到kaldi的src目录下,

CXX=arm-linux-gnueabi-g++ AR=arm-linux-gnueabi-ar AS=arm-lin
ux-gnueabi-as RANLIB=arm-linux-gnueabi-ranlib ./configure --static --use-cuda=no --openblas-root=/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source/tools/openBLAS  --host=arm-linux-gnueabi

CXX=arm-none-linux-gnueabihf-g++ AR=arm-none-linux-gnueabihf-ar AS=arm-n
one-linux-gnueabihf-as RANLIB=arm-none-linux-gnueabihf-ranlib ./configure --static --use-cuda=no --openblas-root=/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source/tools/openBLAS  --host=arm-none-linux-gnueabihf

执行后,会生成Makefile和kaldi*mk,然后对这kaldi.mk修改,把链接库与头文件 的路径全部改成相对路径,这样便于独立解码器
如果你之前使用openblas对其设成了soft, 那么kaldi.mk中也要将其改为soft,-mfloat-abi=soft,kaldi默认为hard,否则会出现类似的错误

Linking Error “Uses VFP register arguments, xxxxxxxxxxxxxxxxxxxxxx

然后修改kaldi*mk

CXXFLAGS = -std=c++14 -I.. -isystem $(OPENFSTINC) -O1 $(EXTRA_CXXFLAGS) \
           -Wall -Wno-sign-compare -Wno-unused-local-typedefs \
           -Wno-deprecated-declarations -Winit-self \
           -DKALDI_DOUBLEPRECISION=$(DOUBLE_PRECISION) \
           -DHAVE_EXECINFO_H=1 -DHAVE_CXXABI_H -DHAVE_OPENBLAS -I$(OPENBLASINC) \
           -ftree-vectorize -mfloat-abi=soft -mfpu=neon -pthread \
           -g

...............................

OPENFSTINC = ../../tools/openfst/include
OPENFSTLIBS = ../../tools/openfst/lib/libfst.a
OPENFSTLDFLAGS =

OPENBLASINC = ../../tools/openBLAS/include
OPENBLASLIBS = ../../tools/openBLAS/lib/libopenblas.a -lgfortran

OPENBLASLIBS += ../../tools/openBLAS/lib/libclapack.a
OPENBLASLIBS += ../../tools/openBLAS/lib/libblas.a
OPENBLASLIBS += ../../tools/openBLAS/lib/libf2c.a

接下来就是编译了

make depend -j x //x 代表线程数,硬件cpu性能越好,可以写的越大,速度越快
make -j x

如果出现下面的错误

arm-linux-gnueabi-g++   -rdynamic  matrix-lib-test.o kaldi-matrix.a ../base/kaldi-base.a   ../../tools/openfst/lib/libfst.a ../../tools/openBLAS/lib/libopenblas.a -lgfortra
n ../../tools/openBLAS/lib/libclapack.a ../../tools/openBLAS/lib/libblas.a ../../tools/openBLAS/lib/libf2c.a -lm -lpthread -ldl -o matrix-lib-testRunning matrix-lib-test ... 0s... FAIL matrix-lib-test
Running sparse-matrix-test ... 0s... FAIL sparse-matrix-test
Running numpy-array-test ... 0s... FAIL numpy-array-test
make[1]: *** [../makefiles/default_rules.mk:104: test] Error 1
make[1]: Leaving directory '/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/onli

解决:注释src/matrix/Makefile中的TESTFILES,即不进行测试。

3.进入解码器目录,编译解码器

进入online下进行,修改Makefile文件 ,主要修改的是portaudio的相对库与头文件 路径

EXTRA_CXXFLAGS += -Wno-sign-compare -I ../../tools/portaudio/include
EXTRA_LDLIBS =

include ../kaldi.mk

UNAME=$(shell uname)
ifeq ($(UNAME), Darwin)
    EXTRA_LDLIBS = ../../tools/portaudio/lib/libportaudio.a
    EXTRA_LDLIBS += -framework CoreAudio -framework AudioToolbox -framework AudioUnit -framework CoreServices
endif

ifeq ($(UNAME), Linux)
  ifneq "$(wildcard ../../tools/portaudio/install/lib/libportaudio.a)" ""
    EXTRA_LDLIBS = ../../tools/portaudio/lib/libportaudio.a
  else
    EXTRA_LDLIBS = ../../tools/portaudio/lib64/libportaudio.a
  endif
  ifneq ($(wildcard ../../tools/portaudio/install/include/pa_linux_alsa.h),)
    EXTRA_LDLIBS += -L../../tools/alsa -lasound -lrt
  else
    EXTRA_LDLIBS += -lrt
  endif
  ifneq ($(wildcard ../../tools/portaudio/install/include/pa_jack.h),)
    EXTRA_LDLIBS += -ljack
  endif
endif

修改完后,进行编译make,完成后,会生成kaldi-online.a静态库
最后进入onlinebin中进行gmm解码器的编译,同样修改Makefile,修改同online
最后会生成相应的解码器

(base) zsf@kaldi-Super-Server:/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source/src/onlinebin$ ls
java-online-audio-client  online-audio-client.cc                online-gmm-decode-faster     online-net-client.cc                online-wav-gmm-decode-faster
Makefile                  online-audio-server-decode-faster     online-gmm-decode-faster.cc  online-server-gmm-decode-faster     online-wav-gmm-decode-faster.cc
online-audio-client       online-audio-server-decode-faster.cc  online-net-client            online-server-gmm-decode-faster.cc
(base) zsf@kaldi-Super-Server:/data/zsf/Zsf_WorkSpace/Kaldi_WorkSpace/online-gmm-decoder/Online-Gmm-Source/src/onlinebin$ file online-gmm-decode-faster
online-gmm-decode-faster: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, interpreter /lib/ld-linux.so.3, BuildID[sha1]=1a20d5470788def0f53
c534550a883b892fea9fa, for GNU/Linux 3.2.0, with debug_info, not stripped

4上板测试

训练模型后就可以测试解码器了

打赏作者