2021年10月27日 星期三

Linux driver: How to enable dynamic debug at booting time for built-in driver.

 Dynamic debug is useful for debug driver, and can be enable by:

1. Mount debug fs

#>mount -t debugfs none /sys/kernel/debug

2. Enable dynamic debug for specifiy funcition

 echo 'file virtio_pci_modern.c +p' > /sys/kernel/debug/dynamic_debug/control

However somtimes we need to debug built-in driver and need enable dynamic debug at boot time. then we can enable by passing dyndbg to Kernel command line like

dyndbg="'file virtio_pci_modern.c +p"

with QEMU we can use -append to attach dyndbg

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -append "dyndbg="file virtio_pci_modern.c +p""

Ref:
  Kernel document/dynamic-debug-howto
 https://www.kernel.org/doc/html/v4.11/admin-guide/dynamic-debug-howto.html

2021年4月21日 星期三

Edge AI - Convert training speech data to micro feature.

In micro speech project we can use training data and convert it to micro feature so that we can test model accuracy in MCU without bothering MIC gain or signal chain issue.

Tensorflow provide a python script to generate feature from a input data.

Steps:

1. Download datasets.

2. Using tensorflow/tensorflow/examples/speech_commands/wav_to_features.py to convert input data. (!!Notice: Must use --preprocess=micro for tensorflow micro model comparability.

Here is my colab note book to generate 10 random feature with specific keyword.


Ref.

 1. https://stackoverflow.com/questions/701402/best-way-to-choose-a-random-file-from-a-directory

 2.https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/micro/examples/micro_speech/train/README.md



2021年4月20日 星期二

Hexdump: Covert file content to hex array.

 Hexdump can dump file content with hex format,  with parameter -e can format displaying data and it's useful in writing shell.

Example usage:

#> hexdump -v FILE -e '1/1 "0x%02X "'

Test result:






Ref:
  Hexdump man page
  https://man7.org/linux/man-pages/man1/hexdump.1.html

2021年4月12日 星期一

Edge AI - Train micro_speech model and deploy to NXP FRDM-K66F.

Tensorflow lite micro_speech example provide training method for speech recognize model to different key word detect.

In this article we use Google Colaboratory to train micro_speech model.

1. Open note book and change WANTED_WORDS
    I choose ON/OFF for my project.
# A comma-delimited list of the words you want to train for.
# The options are: yes,no,up,down,left,right,on,off,stop,go
# All the other words will be used to train an "unknown" label and silent
# audio data with no spoken words will be used to train a "silence" label.
WANTED_WORDS = "on,off"

2. Follow instruction and you can see training  progress(Model accuracy for training set and validation set) in tensorboard section.

3. Convert to TF-lite model and test TensorFlow Lite model's accuracy

5. Modify micro_speech/micro_features/micro_model_settings.cc kCategoryLabels to on/off

6. Replace micro_speech/micro_features/model.cc  to new trained model.

7. Build and flash 

8. Test result 





I use K66F onboard mic to test and the accuracy is lower than I expect. But the trained model has 80% more accuracy, so maybe need to use line mic to get better accuracy speech recognize result.

2021年4月11日 星期日

Edge AI - Modify micro speech example MIC gain on NXP FRDM-K66F for better accuracy.

 The example code recognize accuracy is lower than I expect. So I try to increase microphone gain from 0 dB to +6 dB and the result is better now.

1. Change DA7212 configuration in tensorflow/lite/micro/examples/micro_speech/nxp_k66f/audio_provider.cc

volatile uint8_t g_da7212_register_config[da7212ConfigurationSize][2] = {

...

   {0x34, 0x07},                              // Change MIXIN to 6 dB

    {0x35, 0x07},                         // Change MIXIN to 6 dB

....




2. Build and flash new binary to NXP_K66F board and test.



Ref 

DA7212 datasheet 

Edge AI - Run tensorflow lite micro speech expample on NXP FRDM-K66F.

 Step to build tf-lite micro speach example for nxp_k66f

1. Clone tensorflow code

  $>git clone https://github.com/tensorflow/tensorflow.git

  

2. Install mbed-cli 

     $>sudo apt install python3 python3-pip git mercurial

     $>python3 -m pip install mbed-cli


3. Install ARM cross compile toolchain.(In this project I use GNU Arm Embedded Toolchain)

https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads


4. Configure mbed cli

  $>mbed config -G  GCC_ARM_PATH=/YOUR_COMPILER_INSTALL_PATH/gcc-arm-none-eabi-9-2020-q2-update/bin


5. Generate mbed project for nxp_k66f

  $>cd tensorflow

  $>make -f tensorflow/lite/micro/tools/make/Makefile TARGET=mbed TAGS="nxp_k66f" generate_micro_speech_mbed_project


6.  Create mbed project 

  $>cd tensorflow/lite/micro/tools/make/gen/mbed_cortex-m4/prj/micro_speech/mbed

  $>mbed new  .


7. Configure porject usin C++ 11

python -c 'import fileinput, glob;

for filename in glob.glob("mbed-os/tools/profiles/*.json"):

  for line in fileinput.input(filename, inplace=True):

    print line.replace("\"-std=gnu++14\"","\"-std=c++11\", \"-fpermissive\"")'

 

8 Connect the USB cable to nxp_k66f



9. Compile and flash project, after flash complete reset the board.

mbed compile --target K66F --toolchain GCC_ARM --profile release --flash


10. Connect to nxp_k66f serial port with baudrate 14400.


11. Saying Yes and NO and will print repsonse word in serial port, since nxp_k66f has onboard microphone you can direct speak to it and see the result in terminal.


12. Test result 



I want using this sample code to build a IOT switch that can control with speach, So  in next article I will try to retrain model with 'ON' and 'OFF' key word and try to deploy to nxp_k66f


Ref

Mbed

https://os.mbed.com/docs/mbed-os/v6.9/build-tools/install-and-set-up.html

Tensorflow lite

https://github.com/tensorflow/tensorflow/tree/114b8ef31ac66155ec9b0590bc7115125f7fe61e/tensorflow/lite/micro/examples/micro_speech#deploy-to-nxp-frdm-k66f

NXP_K66F user guide

https://www.nxp.com/docs/en/user-guide/FRDMK66FUG.pdf


Linux driver: How to enable dynamic debug at booting time for built-in driver.

 Dynamic debug is useful for debug driver, and can be enable by: 1. Mount debug fs #>mount -t debugfs none /sys/kernel/debug 2. Enable dy...