Browse Source

three simple machine learning examples base on iris dataset

pull/8/head
chunyexixiaoyu 1 year ago
parent
commit
a3d753153c
  1. 1
      APP_Framework/Applications/knowing_app/Kconfig
  2. 57
      APP_Framework/Applications/knowing_app/iris_ml_demo/DecisionTreeClassifierModel.h
  3. 3
      APP_Framework/Applications/knowing_app/iris_ml_demo/Kconfig
  4. 41
      APP_Framework/Applications/knowing_app/iris_ml_demo/LogisticRegressionModel.h
  5. 9
      APP_Framework/Applications/knowing_app/iris_ml_demo/SConscript
  6. 78
      APP_Framework/Applications/knowing_app/iris_ml_demo/SVCModel.h
  7. 7
      APP_Framework/Applications/knowing_app/iris_ml_demo/iris.csv
  8. 98
      APP_Framework/Applications/knowing_app/iris_ml_demo/iris_ml_demo.c
  9. 6
      Ubiquitous/RT_Thread/bsp/stm32f407-atk-coreboard/.config
  10. 233
      Ubiquitous/RT_Thread/bsp/stm32f407-atk-coreboard/.gitignore
  11. 5
      Ubiquitous/RT_Thread/bsp/stm32f407-atk-coreboard/rtconfig.h
  12. 2
      Ubiquitous/RT_Thread/bsp/stm32f407-atk-coreboard/rtconfig.py

1
APP_Framework/Applications/knowing_app/Kconfig

@ -3,4 +3,5 @@ menu "knowing app"
source "$APP_DIR/Applications/knowing_app/face_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/iris_ml_demo/Kconfig"
endmenu

57
APP_Framework/Applications/knowing_app/iris_ml_demo/DecisionTreeClassifierModel.h

@ -0,0 +1,57 @@
#include <stdarg.h>
/**
* Predict class for features vector
*/
int predict(float *x)
{
if (x[2] <= 2.449999988079071) {
return 0;
}
else {
if (x[3] <= 1.75) {
if (x[2] <= 4.950000047683716) {
if (x[3] <= 1.6500000357627869) {
return 1;
}
else {
return 2;
}
}
else {
if (x[3] <= 1.550000011920929) {
return 2;
}
else {
if (x[2] <= 5.450000047683716) {
return 1;
}
else {
return 2;
}
}
}
}
else {
if (x[2] <= 4.8500001430511475) {
if (x[1] <= 3.100000023841858) {
return 2;
}
else {
return 1;
}
}
else {
return 2;
}
}
}
}

3
APP_Framework/Applications/knowing_app/iris_ml_demo/Kconfig

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

41
APP_Framework/Applications/knowing_app/iris_ml_demo/LogisticRegressionModel.h

@ -0,0 +1,41 @@
#include <stdarg.h>
/**
* Compute dot product
*/
float dot(float *x, ...)
{
va_list w;
va_start(w, 4);
float dot = 0.0;
for (int i = 0; i < 4; i++) {
const float wi = va_arg(w, double);
dot += x[i] * wi;
}
return dot;
}
/**
* Predict class for features vector
*/
int predict(float *x)
{
float votes[3] = {0.0f};
votes[0] = dot(x, -0.423405592418, 0.967388282125, -2.517050233286, -1.079182996654) + 9.84868307535428;
votes[1] = dot(x, 0.534517184386, -0.321908835083, -0.206465997471, -0.944448257908) + 2.238120068472271;
votes[2] = dot(x, -0.111111591968, -0.645479447042, 2.723516230758, 2.023631254562) + -12.086803143826813;
// return argmax of votes
int classIdx = 0;
float maxVotes = votes[0];
for (int i = 1; i < 3; i++) {
if (votes[i] > maxVotes) {
classIdx = i;
maxVotes = votes[i];
}
}
return classIdx;
}

9
APP_Framework/Applications/knowing_app/iris_ml_demo/SConscript

@ -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')

78
APP_Framework/Applications/knowing_app/iris_ml_demo/SVCModel.h

@ -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;
}

7
APP_Framework/Applications/knowing_app/iris_ml_demo/iris.csv

@ -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

98
APP_Framework/Applications/knowing_app/iris_ml_demo/iris_ml_demo.c

@ -0,0 +1,98 @@
#include <string.h>
#include <transform.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()
{
#include "SVCModel.h"
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
void iris_DecisonTree_predict()
{
#include "DecisionTreeClassifierModel.h"
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_DecisonTree_predict, iris predict by decison tree classifier);
#endif
void iris_LogisticRegression_predict()
{
#include "LogisticRegressionModel.h"
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_LogisticRegression_predict, iris predict by logistic regression);
#endif

6
Ubiquitous/RT_Thread/bsp/stm32f407-atk-coreboard/.config

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

233
Ubiquitous/RT_Thread/bsp/stm32f407-atk-coreboard/.gitignore

@ -1,30 +1,19 @@
*.pyc
# this
*.map
*.dblite
*.elf
*.bin
*.hex
*.axf
*.exe
*.pdb
*.idb
*.ilk
*.old
*~
*.o
*.obj
*.out
*.bak
*.dep
*.lib
*.i
*.d
.DS_Stor*
*.uimg
GPATH
GRTAGS
GTAGS
.vscode
JLinkLog.txt
JLinkSettings.ini
DebugConfig/
@ -32,3 +21,223 @@ RTE/
settings/
*.uvguix*
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

5
Ubiquitous/RT_Thread/bsp/stm32f407-atk-coreboard/rtconfig.h

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

2
Ubiquitous/RT_Thread/bsp/stm32f407-atk-coreboard/rtconfig.py

@ -17,7 +17,7 @@ if os.getenv('RTT_ROOT'):
# EXEC_PATH is the compiler execute path, for example, CodeSourcery, Keil MDK, IAR
if CROSS_TOOL == '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':
PLATFORM = 'armcc'
EXEC_PATH = r'C:/Keil_v5'

Loading…
Cancel
Save