Skip to content

Commit 7b828d7

Browse files
author
Thong Phan
committed
samples: tflite-micro: add micro_speech sample with OpenAMP on i.MX8MP
This sample demonstrates TensorFlow Lite Micro speech recognition capabilities on Zephyr RTOS, specifically designed for i.MX8MP platforms with OpenAMP support for inter-core communication. The sample: - Processes audio input at 16 kHz, S16_LE, mono format. - Uses resource table for shared memory management between cores. Board tested: imx8mp_evk_mimx8ml8_adsp Known limitations: - If two commands are spoken within 1000 ms, the second command may not be detected. - If a command lasts longer than 1000 ms, it may be detected as two separate commands. Link to Linux application: https://github.com/thong-phn/gsoc2025-linux-app Source of micro_speech model: https://github.com/tensorflow/tflite-micro/tree/main/tensorflow/lite/micro/examples/micro_speech Signed-off-by: Thong Phan <[email protected]>
1 parent 208607d commit 7b828d7

15 files changed

+3098
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4+
project(micro_speech_openamp)
5+
6+
# These samples use local static initialization. Since Zephyr doesn't support the
7+
# C++ ABI for thread-safe initialization of local statics and the constructors don't
8+
# appear to require thread safety, we turn it off in the C++ compiler.
9+
set(NO_THREADSAFE_STATICS $<TARGET_PROPERTY:compiler-cpp,no_threadsafe_statics>)
10+
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${NO_THREADSAFE_STATICS}>)
11+
12+
# Define common paths
13+
set(OPTIONAL_TFLITE_DIR $ENV{ZEPHYR_BASE}/../optional/modules/lib/tflite-micro/signal)
14+
15+
file(
16+
GLOB
17+
app_sources
18+
src/*
19+
src/inference/*
20+
)
21+
22+
target_include_directories(
23+
app
24+
PRIVATE
25+
src
26+
${ZEPHYR_TFLITE_MICRO_MODULE_DIR}/signal
27+
${ZEPHYR_TFLITE_MICRO_MODULE_DIR}/signal/micro/kernels/
28+
${ZEPHYR_TFLITE_MICRO_MODULE_DIR}/signal/src
29+
${LIBMETAL_INCLUDE_DIR}
30+
${OPENAMP_INCLUDE_DIR}
31+
${PLATFORM_DIR}
32+
)
33+
34+
target_sources(
35+
app
36+
PRIVATE
37+
${app_sources}
38+
39+
# Signal processing
40+
${OPTIONAL_TFLITE_DIR}/src/fft_auto_scale.cc
41+
${OPTIONAL_TFLITE_DIR}/src/max_abs.cc
42+
${OPTIONAL_TFLITE_DIR}/src/msb_32.cc
43+
${OPTIONAL_TFLITE_DIR}/src/energy.cc
44+
${OPTIONAL_TFLITE_DIR}/src/filter_bank.cc
45+
${OPTIONAL_TFLITE_DIR}/src/pcan_argc_fixed.cc
46+
${OPTIONAL_TFLITE_DIR}/src/filter_bank_log.cc
47+
${OPTIONAL_TFLITE_DIR}/src/log.cc
48+
${OPTIONAL_TFLITE_DIR}/src/filter_bank_spectral_subtraction.cc
49+
50+
# Micro kernels
51+
${OPTIONAL_TFLITE_DIR}/micro/kernels/energy.cc
52+
${OPTIONAL_TFLITE_DIR}/micro/kernels/fft_auto_scale_common.cc
53+
${OPTIONAL_TFLITE_DIR}/micro/kernels/filter_bank_square_root_common.cc
54+
${OPTIONAL_TFLITE_DIR}/micro/kernels/filter_bank_spectral_subtraction.cc
55+
${OPTIONAL_TFLITE_DIR}/micro/kernels/filter_bank.cc
56+
${OPTIONAL_TFLITE_DIR}/micro/kernels/pcan.cc
57+
${OPTIONAL_TFLITE_DIR}/micro/kernels/filter_bank_log.cc
58+
)
59+
60+
if("${ARCH}" STREQUAL "xtensa")
61+
target_sources(
62+
app
63+
PRIVATE
64+
65+
# Xtensa kernels
66+
${OPTIONAL_TFLITE_DIR}/micro/kernels/xtensa/fft_auto_scale_kernel.cc
67+
${OPTIONAL_TFLITE_DIR}/micro/kernels/xtensa/filter_bank_square_root.cc
68+
${OPTIONAL_TFLITE_DIR}/micro/kernels/xtensa/xtensa_square_root.S
69+
)
70+
endif()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_LOG_PRINTK=n
2+
CONFIG_IPM_IMX_MAX_DATA_SIZE_16=n
3+
CONFIG_IPM_IMX_MAX_DATA_SIZE_4=y
4+
CONFIG_OPENAMP_WITH_DCACHE=y
5+
6+
#vendor-specific resource table
7+
CONFIG_OPENAMP_VENDOR_RSC_TABLE=y
8+
CONFIG_OPENAMP_VENDOR_RSC_TABLE_FILE="nxp_resource_table.c"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
chosen {
9+
/*
10+
* shared memory reserved for the inter-processor communication
11+
*/
12+
zephyr,ipc_shm = &dspsram3;
13+
zephyr,ipc = &mailbox0;
14+
};
15+
16+
dspsram3: memory@942f0000 {
17+
compatible = "mmio-sram";
18+
reg = <0x942f0000 0x110000>;
19+
};
20+
};
21+
22+
&mailbox0 {
23+
status = "okay";
24+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# tflite
2+
CONFIG_CPP=y
3+
CONFIG_STD_CPP17=y
4+
CONFIG_TENSORFLOW_LITE_MICRO=y
5+
# stack size needed to safely initialize
6+
# and run TensorFlow Lite Micro operations
7+
# in the main thread
8+
CONFIG_MAIN_STACK_SIZE=6144
9+
CONFIG_REQUIRES_FLOAT_PRINTF=y
10+
11+
# rpmsg
12+
CONFIG_KERNEL_BIN_NAME="micro_speech_openamp"
13+
CONFIG_PRINTK=n
14+
CONFIG_IPM=y
15+
# rpmsg buffers + OpenAMP overhead + safety margin
16+
CONFIG_HEAP_MEM_POOL_SIZE=5120
17+
CONFIG_OPENAMP=y
18+
CONFIG_OPENAMP_RSC_TABLE_NUM_RPMSG_BUFF=8
19+
CONFIG_OPENAMP_RSC_TABLE=y
20+
CONFIG_OPENAMP_MASTER=n
21+
CONFIG_SHELL=n
22+
23+
# logging
24+
CONFIG_LOG=y
25+
CONFIG_LOG_DEFAULT_LEVEL=3
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
sample:
2+
description: Micro Speech OpenAMP Sample
3+
name: micro speech openamp
4+
common:
5+
tags: tensorflow
6+
modules:
7+
- tflite-micro
8+
harness: console
9+
harness_config:
10+
type: multi_line
11+
ordered: false
12+
regex:
13+
- "Detected: (yes|no|silence|unknown)"
14+
tests:
15+
sample.tensorflow.micro_speech_openamp:
16+
platform_allow:
17+
- imx8mp_evk/mimx8ml8/adsp
18+
integration_platforms:
19+
- imx8mp_evk/mimx8ml8/adsp
20+
tags: tensorflow, openamp, ipc, audio

samples/modules/tflite-micro/micro_speech/src/inference/audio_preprocessor_int8_model.cpp

Lines changed: 755 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2025 The TensorFlow Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#ifndef MICRO_SPEECH_OPENAMP_AUDIO_PREPROCESSOR_INT8_MODEL_H_
20+
#define MICRO_SPEECH_OPENAMP_AUDIO_PREPROCESSOR_INT8_MODEL_H_
21+
22+
extern const unsigned char g_audio_preprocessor_int8_model[];
23+
extern const unsigned int g_audio_preprocessor_int8_model_len;
24+
25+
#endif /* MICRO_SPEECH_OPENAMP_AUDIO_PREPROCESSOR_INT8_MODEL_H_ */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2025 The TensorFlow Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#ifndef MICRO_SPEECH_OPENAMP_MODEL_SETTINGS_H_
20+
#define MICRO_SPEECH_OPENAMP_MODEL_SETTINGS_H_
21+
22+
/* The following values are derived from values used during model training.
23+
* If you change the way you preprocess the input, update all these constants.
24+
*/
25+
constexpr int kAudioSampleFrequency = 16000;
26+
constexpr int kFeatureSize = 40;
27+
constexpr int kFeatureCount = 49;
28+
constexpr int kFeatureElementCount = (kFeatureSize * kFeatureCount);
29+
constexpr int kFeatureStrideMs = 20;
30+
constexpr int kFeatureDurationMs = 30;
31+
32+
/* Variables for the model's output categories. */
33+
constexpr int kCategoryCount = 4;
34+
constexpr const char *kCategoryLabels[kCategoryCount] = {"silence", "unknown", "yes", "no"};
35+
36+
#endif /* MICRO_SPEECH_OPENAMP_MODEL_SETTINGS_H_ */

0 commit comments

Comments
 (0)