feat(knowing app): stm32f4 svc iris demo

This commit is contained in:
yangtuo250 2021-08-10 18:02:16 +08:00
parent 336c970010
commit 9a1a407ea6
10 changed files with 388 additions and 15 deletions

View File

@ -3,4 +3,5 @@ menu "knowing app"
source "$APP_DIR/Applications/knowing_app/face_detect/Kconfig" source "$APP_DIR/Applications/knowing_app/face_detect/Kconfig"
source "$APP_DIR/Applications/knowing_app/instrusion_detect/Kconfig" source "$APP_DIR/Applications/knowing_app/instrusion_detect/Kconfig"
source "$APP_DIR/Applications/knowing_app/helmet_detect/Kconfig" source "$APP_DIR/Applications/knowing_app/helmet_detect/Kconfig"
source "$APP_DIR/Applications/knowing_app/iris_ml_demo/Kconfig"
endmenu endmenu

View File

@ -0,0 +1,3 @@
config IRIS_ML_DEMO
bool "enable apps/iris ml demo"
default n

View File

@ -0,0 +1,9 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('Applications', src, depend = ['IRIS_ML_DEMO'], LOCAL_CPPPATH = CPPPATH)
Return('group')

View File

@ -0,0 +1,78 @@
/**
* SVC model trained by iris dataset
*/
#include <stdarg.h>
/**
* Compute kernel between feature vector and support vector.
* Kernel type: linear
*/
float compute_kernel(float *x, ...)
{
va_list w;
va_start(w, 4);
float kernel = 0.0;
for (int i = 0; i < 4; i++) {
kernel += x[i] * va_arg(w, double);
}
return kernel;
}
/**
* Predict class for features vector
*/
int predict(float *x)
{
float kernels[27] = {0};
float decisions[3] = {0};
int votes[3] = {0};
kernels[0] = compute_kernel(x, 5.1, 3.3, 1.7, 0.5);
kernels[1] = compute_kernel(x, 4.8, 3.4, 1.9, 0.2);
kernels[2] = compute_kernel(x, 4.5, 2.3, 1.3, 0.3);
kernels[3] = compute_kernel(x, 6.9, 3.1, 4.9, 1.5);
kernels[4] = compute_kernel(x, 6.3, 3.3, 4.7, 1.6);
kernels[5] = compute_kernel(x, 6.1, 2.9, 4.7, 1.4);
kernels[6] = compute_kernel(x, 5.6, 3.0, 4.5, 1.5);
kernels[7] = compute_kernel(x, 6.2, 2.2, 4.5, 1.5);
kernels[8] = compute_kernel(x, 5.9, 3.2, 4.8, 1.8);
kernels[9] = compute_kernel(x, 6.3, 2.5, 4.9, 1.5);
kernels[10] = compute_kernel(x, 6.8, 2.8, 4.8, 1.4);
kernels[11] = compute_kernel(x, 6.7, 3.0, 5.0, 1.7);
kernels[12] = compute_kernel(x, 6.0, 2.7, 5.1, 1.6);
kernels[13] = compute_kernel(x, 5.4, 3.0, 4.5, 1.5);
kernels[14] = compute_kernel(x, 5.1, 2.5, 3.0, 1.1);
kernels[15] = compute_kernel(x, 4.9, 2.5, 4.5, 1.7);
kernels[16] = compute_kernel(x, 6.5, 3.2, 5.1, 2.0);
kernels[17] = compute_kernel(x, 6.0, 2.2, 5.0, 1.5);
kernels[18] = compute_kernel(x, 6.3, 2.7, 4.9, 1.8);
kernels[19] = compute_kernel(x, 6.2, 2.8, 4.8, 1.8);
kernels[20] = compute_kernel(x, 6.1, 3.0, 4.9, 1.8);
kernels[21] = compute_kernel(x, 7.2, 3.0, 5.8, 1.6);
kernels[22] = compute_kernel(x, 6.3, 2.8, 5.1, 1.5);
kernels[23] = compute_kernel(x, 6.0, 3.0, 4.8, 1.8);
kernels[24] = compute_kernel(x, 6.3, 2.5, 5.0, 1.9);
kernels[25] = compute_kernel(x, 6.5, 3.0, 5.2, 2.0);
kernels[26] = compute_kernel(x, 5.9, 3.0, 5.1, 1.8);
decisions[0] = 1.452844496978 + kernels[0] * 0.67075289031 + kernels[2] * 0.077097563476 + kernels[14] * -0.747850453786;
decisions[1] = 1.507713125178 + kernels[0] * 0.043820415076 + kernels[1] * 0.159872086718 + kernels[15] * -0.203692501794;
decisions[2] = 6.78097118511 + kernels[3] + kernels[4] + kernels[5] + kernels[6] + kernels[7] + kernels[8] + kernels[9] +
kernels[10] * 0.243261886421 + kernels[11] + kernels[12] + kernels[13] - kernels[15] - kernels[16] -
kernels[17] - kernels[18] - kernels[19] - kernels[20] + kernels[21] * -0.437859817863 - kernels[22] -
kernels[23] + kernels[24] * -0.645105347981 + kernels[25] * -0.160296720576 - kernels[26];
votes[decisions[0] > 0 ? 0 : 1] += 1;
votes[decisions[1] > 0 ? 0 : 2] += 1;
votes[decisions[2] > 0 ? 1 : 2] += 1;
int val = votes[0];
int idx = 0;
for (int i = 1; i < 3; i++) {
if (votes[i] > val) {
val = votes[i];
idx = i;
}
}
return idx;
}

View File

@ -0,0 +1,7 @@
5.1,3.5,1.4,0.2
6.4,3.2,4.5,1.5
5.8,2.7,5.1,1.9
7.7,3.8,6.7,2.2
5.5,2.6,4.4,1.2
5.1,3.8,1.9,0.4
5.8,2.7,3.9,1.2
1 5.1 3.5 1.4 0.2
2 6.4 3.2 4.5 1.5
3 5.8 2.7 5.1 1.9
4 7.7 3.8 6.7 2.2
5 5.5 2.6 4.4 1.2
6 5.1 3.8 1.9 0.4
7 5.8 2.7 3.9 1.2

View File

@ -0,0 +1,59 @@
#include <string.h>
#include <transform.h>
#include "SVCModel.h"
#define FEATURE_NUM 4
#define CSV_PATH "/csv/iris.csv"
#define CSV_BUFFER_SIZE (1 * 1024)
static float data[10][FEATURE_NUM] = {};
static int data_len = 0;
void simple_CSV_read()
{
int fin;
int col = 0;
char buffer[CSV_BUFFER_SIZE] = "";
char *tmp = "";
char *delim = ",\n ";
fin = open(CSV_PATH, O_RDONLY);
if (!fin) {
printf("Error open file %s", CSV_PATH);
exit(-1);
}
read(fin, buffer, sizeof(buffer));
close(fin);
data_len = 0;
for (tmp = strtok(buffer, delim); tmp && *tmp; col++, tmp = strtok(NULL, delim)) {
if (0 == col % FEATURE_NUM) {
// printf("\n");
data_len++;
col = 0;
}
data[data_len - 1][col] = atof(tmp);
// printf("%.4f ", data[data_len - 1][col]);
}
// printf("\n");
}
void iris_SVC_predict()
{
int result;
simple_CSV_read();
for (int i = 0; i < data_len; i++) {
result = predict(data[i]);
printf("data %d: ", i + 1);
for (int j = 0; j < FEATURE_NUM; j++) {
printf("%.4f ", data[i][j]);
}
printf("result: %d\n", result);
}
}
#ifdef __RT_THREAD_H__
MSH_CMD_EXPORT(iris_SVC_predict, iris predict by SVC);
#endif

View File

@ -356,6 +356,7 @@ CONFIG_MAIN_KTASK_STACK_SIZE=1024
# #
# knowing app # knowing app
# #
CONFIG_IRIS_ML_DEMO=y
# #
# sensor app # sensor app
@ -378,10 +379,13 @@ CONFIG_SUPPORT_SENSOR_FRAMEWORK=y
# CONFIG_SUPPORT_CONNECTION_FRAMEWORK is not set # CONFIG_SUPPORT_CONNECTION_FRAMEWORK is not set
CONFIG_SUPPORT_KNOWING_FRAMEWORK=y CONFIG_SUPPORT_KNOWING_FRAMEWORK=y
# CONFIG_USING_TENSORFLOWLITEMICRO is not set # CONFIG_USING_TENSORFLOWLITEMICRO is not set
CONFIG_USING_KPU_POSTPROCESSING=y
# CONFIG_USING_YOLOV2 is not set
# CONFIG_SUPPORT_CONTROL_FRAMEWORK is not set # CONFIG_SUPPORT_CONTROL_FRAMEWORK is not set
# #
# app lib # lib
# #
CONFIG_APP_SELECT_NEWLIB=y CONFIG_APP_SELECT_NEWLIB=y
# CONFIG_APP_SELECT_OTHER_LIB is not set # CONFIG_APP_SELECT_OTHER_LIB is not set
# CONFIG_LIB_USING_CJSON is not set

View File

@ -1,30 +1,19 @@
*.pyc # this
*.map *.map
*.dblite *.dblite
*.elf
*.bin *.bin
*.hex
*.axf *.axf
*.exe
*.pdb
*.idb
*.ilk
*.old *.old
*~ *~
*.o *.o
*.obj
*.out
*.bak *.bak
*.dep *.dep
*.lib
*.i *.i
*.d *.d
.DS_Stor*
*.uimg *.uimg
GPATH GPATH
GRTAGS GRTAGS
GTAGS GTAGS
.vscode
JLinkLog.txt JLinkLog.txt
JLinkSettings.ini JLinkSettings.ini
DebugConfig/ DebugConfig/
@ -32,3 +21,223 @@ RTE/
settings/ settings/
*.uvguix* *.uvguix*
cconfig.h cconfig.h
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.con

View File

@ -201,6 +201,8 @@
/* knowing app */ /* knowing app */
#define IRIS_ML_DEMO
/* sensor app */ /* sensor app */
@ -210,8 +212,9 @@
#define ADD_XIUOS_FETURES #define ADD_XIUOS_FETURES
#define SUPPORT_SENSOR_FRAMEWORK #define SUPPORT_SENSOR_FRAMEWORK
#define SUPPORT_KNOWING_FRAMEWORK #define SUPPORT_KNOWING_FRAMEWORK
#define USING_KPU_POSTPROCESSING
/* app lib */ /* lib */
#define APP_SELECT_NEWLIB #define APP_SELECT_NEWLIB

View File

@ -17,7 +17,7 @@ if os.getenv('RTT_ROOT'):
# EXEC_PATH is the compiler execute path, for example, CodeSourcery, Keil MDK, IAR # EXEC_PATH is the compiler execute path, for example, CodeSourcery, Keil MDK, IAR
if CROSS_TOOL == 'gcc': if CROSS_TOOL == 'gcc':
PLATFORM = 'gcc' PLATFORM = 'gcc'
EXEC_PATH = r'/usr/local/Cellar/gcc-arm-none-eabi/20180627/bin' EXEC_PATH = r'/opt/gcc-arm-none-eabi-7-2018-q2-update/bin'
elif CROSS_TOOL == 'keil': elif CROSS_TOOL == 'keil':
PLATFORM = 'armcc' PLATFORM = 'armcc'
EXEC_PATH = r'C:/Keil_v5' EXEC_PATH = r'C:/Keil_v5'