TensorFlow Lite实现移植到ART-Pi Smart

tensorflow lite 官方仓库:
https://github.com/iysheng/tflite-micro
我的art-pi-smart仓库:
https://gitee.com/iysheng/art-pi-smart
经过了多天的摸索学习,今天将 tensorflow lite 的框架移植到了 art-pi smart 上,但是还是存在一点问题,主要是 c++ 和 c 语言混合编译的问题,根据目前我的总结,具体体现在: tensorflow lite 在函数中定义的静态对象无法正常构造,必须放在全局变量的位置
全局变量定义的对象只能使用默认的构造函数,无发使用带有参数的构造函数初始对象
 针对上述两个现象,我在运行测试 hello world 例程时,对 tensorflow lite 做出来的主要改动是: 1diff --git a/kernel/bsp/imx6ull-artpi-smart/applications/tflm_tree/examples/hello_world/main_functions.cc b/kernel/bsp/imx6ull-artpi-smart/applications/tflm_tree/examples/hello_world/main_functions.cc 2index a585ba7..a318c32 100644 3--- a/kernel/bsp/imx6ull-artpi-smart/applications/tflm_tree/examples/hello_world/main_functions.cc 4+++ b/kernel/bsp/imx6ull-artpi-smart/applications/tflm_tree/examples/hello_world/main_functions.cc 5@@ -37,18 +37,30 @@ constexpr int ktensorarenasize = 2000; 6 uint8_t tensor_arena[ktensorarenasize]; 7 }  // namespace 8 9+static tflite::microerrorreporter micro_error_reporter;10+  // this pulls in all the operation implementations we need.11+  // nolintnextline(runtime-global-variables)12+  static tflite::allopsresolver resolver;13+  static tflite::microinterpreter *static_interpreter;14+15 // the name of this function is important for arduino compatibility.16 void setup() {17+18+  debuglog(setup begin.);19   tflite::initializetarget();2021+  debuglog(initial ok.);22+23   // set up logging. google style is to avoid globals or statics because of24   // lifetime uncertainty, but since this has a trivial destructor it's okay.25   // nolintnextline(runtime-global-variables)26-  static tflite::microerrorreporter micro_error_reporter;27+  /* 这个地方有问题 */28   error_reporter = µ_error_reporter;2930   // map the model into a usable data structure. this doesn't involve any31   // copying or parsing, it's a very lightweight operation.32+  // printf(value:%x %u, g_hello_world_model_data[0], g_hello_world_model_data_size);33+34   model = tflite::getmodel(g_hello_world_model_data);35   if (model->version() != tflite_schema_version) {36     tf_lite_report_error(error_reporter,37@@ -57,15 +69,10 @@ void setup() {38                          model->version(), tflite_schema_version);39     return;40   }41-42-  // this pulls in all the operation implementations we need.43-  // nolintnextline(runtime-global-variables)44-  static tflite::allopsresolver resolver;45-46   // build an interpreter to run the model with.47-  static tflite::microinterpreter static_interpreter(48+  static_interpreter = new tflite::microinterpreter(49       model, resolver, tensor_arena, ktensorarenasize, error_reporter);50-  interpreter = &static_interpreter;51+  interpreter = static_interpreter;5253   // allocate memory from the tensor_arena for the model's tensors.54   tflitestatus allocate_status = interpreter->allocatetensors();55@@ -102,6 +109,7 @@ int loop() {56   if (invoke_status != ktfliteok) {57     tf_lite_report_error(error_reporter, invoke failed on x: %f,58                          static_cast(x));59+    printf(invoke status:%x, invoke_status);60     return -1;61   }   参考的资料主要有:
1、tinyml基tensorflow lite在arduino
2、https://github.com/iysheng/tflite-micro/blob/main/tensorflow/lite/micro/docs/new_platform_support.md
 特别关键的一点是提取基础框架的文件:python3 tensorflow/lite/micro/tools/project_generation/create_tflm_tree.py -e hello_world /tmp/tflm-tree
根据官方的文档,上述命令就可以将 hello_world 工程相关的文件提取到指定的 /tmp/tflm-tree 目录,下一步做的就是将这部分内容添加到 rt-thread smart 工程中,编译出来。相关的代码,我都上传到了我的 art-pi smart 的仓库。如果有问题欢迎和我讨论。展示下,运行起来的效果,为了展示需要,删除了部分空行打印: 1u-boot 2017.03-g002b758ac9-dirty (jul 08 2021 - 10:45:12 +0000) 2cpu:   freescale i.mx6ull rev1.1 696 mhz (running at 396 mhz) 3cpu:   industrial temperature grade (-40c to 105c) at 44c 4reset cause: por 5model: freescale i.mx6 ull 14x14 evk board 6board: mx6ull 14x14 evk 7dram:  512 mib 8mmc:   fsl_sdhc: 0, fsl_sdhc: 1 9display: tft7016 (1024x600) 10video: 1024x600x24 11in:    serial 12out:   serial 13err:   serial 14switch to partitions #0, ok 15mmc1(part 0) is current device 16net:   eth0: ethernet@02188000 [prime] 17normal boot 18hit any key to stop autoboot:  3  2  1  0  19ethernet@02188000 waiting for phy auto negotiation to complete.... done 20using ethernet@02188000 device 21tftp from server 10.20.52.39; our ip address is 10.20.52.89 22filename 'rtthread.bin'. 23load address: 0x80001000 24loading: *################################################################# 25     ############################################################### 26     159.2 kib/s 27done 28bytes transferred = 1866656 (1c7ba0 hex) 29## starting application at 0x80001000 ... 30  | / 31- rt -     thread smart operating system 32 / |      5.0.0 build apr 30 2022 33 2006 - 2020 copyright by rt-thread team 34do components initialization. 35initialize rti_board_end:0 done 36initialize dfs_init:0 done 37initialize rt_mmcsd_core_init:0 done 38initialize rt_usbd_winusb_class_register:0 done 39initialize futex_system_init:0 done 40initialize pmutex_system_init:0 done 41initialize lwip_system_initlwip-2.1.2 initialized! 42:0 done 43initialize ulog_console_backend_init:0 done 44initialize ulog_init:0 done 45initialize rt_hw_adc_init:0 done 46initialize rt_hw_csi_init:0 done 47initialize rt_hw_dbg_mm_init:0 done 48initialize imx6ul_eth_initred first va=f00here 0000 49here 1111 mmu_l2=c107c000 50[31m[63] e/drv.enet: emac device init success 51[0m:0 done 52initialize rt_hw_i2c_init[32m[70] i/i2c: i2c bus [i2c1] registered 53[0m[32m[75] i/i2c: i2c bus [i2c3] registered 54[0m[32m[79] i/i2c: i2c bus [i2c4] registered 55[0m:0 done 56initialize rt_hw_elcd_init[86] d/drv.lcd: fb address => 0x811c0000 57:0 done 58initialize rt_hw_pwm_init:0 done 59initialize rt_hw_rtc_init:0 done 60initialize imxrt_mci_init:0 done 61initialize rt_hw_spi_init:0 done 62initialize gt911_init[31m[158] e/drv.enet: phy link down, please check the cable connection and link partner setting. 63[0m[32m[192] i/sdio: emmc card capacity 3817472 kb. 64[0mfound part[0], begin: 2098176, size: 500.0mb 65found part[1], begin: 526386176, size: 1.0gb 66found part[2], begin: 1600128000, size: 10.0mb 67found part[3], begin: 1610613760, size: 200.0mb 68[32m[328] i/touch: rt_touch init success 69[0m[32m[332] i/gt911: touch device gt911 init success 70[0m:0 done 71initialize rt_usbd_init:0 done 72initialize rt_hw_wdt_init:0 done 73initialize null_device_init:0 done 74initialize random_device_init:0 done 75initialize urandom_device_init:0 done 76initialize zero_device_init:0 done 77initialize rt_work_sys_workqueue_init:0 done 78initialize ptmx_register:0 done 79initialize critical_init:0 done 80initialize rt_hw_ov5640_init:0 done 81initialize cplusplus_system_init:0 done 82initialize elm_init:0 done 83initialize dfs_romfs_init:0 done 84initialize rt_i2c_core_init:0 done 85initialize libc_system_init:0 done 86initialize clock_time_system_init:0 done 87initialize sal_init[32m[390] i/sal.skt: socket abstraction layer initialize success. 88[0m:0 done 89initialize mnt_initemmc file system initialization done! 90:0 done 91initialize tf_lite_init[5309] d/drv.enet: enet1 link up 92:0 done 93initialize em_initopen em failed! 94:-1 done 95initialize set_adc_init:0 done 96initialize set_pwm_default:0 done 97initialize rt_sysinfo_init:0 done 98initialize finsh_system_init:0 done 99red say: hello rt-smart. 100open lcd okwatchdog start ing.....101test tf.102setup begin.103go here104initial ok.105get mode begin.106get here ?107get here 000?108construct the class.109init ok.110x_value: 1.0*2^-127, y_value: 1.0297613*2^-7111x_value: 1.0053092*2^-5, y_value: 1.8020826*2^-5112x_value: 1.0053092*2^-4, y_value: 1.1584818*2^-4113x_value: 1.507964*2^-4, y_value: 1.9308027*2^-4114x_value: 1.0053092*2^-3, y_value: 1.0941217*2^-3115x_value: 1.2566366*2^-3, y_value: 1.4802819*2^-3116x_value: 1.507964*2^-3, y_value: 1.6733624*2^-3117x_value: 1.7592913*2^-3, y_value: 1.8664425*2^-3118x_value: 1.0053092*2^-2, y_value: 1.0619412*2^-2119x_value: 1.130973*2^-2, y_value: 1.1584818*2^-2  特别地: 1x_value: 1.0*2^-127, y_value: 1.0297613*2^-7 2x_value: 1.0053092*2^-5, y_value: 1.8020826*2^-5 3x_value: 1.0053092*2^-4, y_value: 1.1584818*2^-4 4x_value: 1.507964*2^-4, y_value: 1.9308027*2^-4 5x_value: 1.0053092*2^-3, y_value: 1.0941217*2^-3 6x_value: 1.2566366*2^-3, y_value: 1.4802819*2^-3 7x_value: 1.507964*2^-3, y_value: 1.6733624*2^-3 8x_value: 1.7592913*2^-3, y_value: 1.8664425*2^-3 9x_value: 1.0053092*2^-2, y_value: 1.0619412*2^-210x_value: 1.130973*2^-2, y_value: 1.1584818*2^-211...  上述部分是模型估算的正弦函数的数据,我绘制成图表的效果如下:
效果还是不错的,这使用的是官方例程 16 * 16 * 1 的三层模型。  
原文标题:在 art-pi smart 上运行 tensorflow lite
文章出处:【微信公众号:rtthread物联网操作系统】欢迎添加关注!文章转载请注明出处。


年产10万吨正极材料项目落地!
我国已经制造出无人太阳能飞机 无需加油
光学知识水体COD光谱特性分析
正业科技三驾马车增长延续实现2018年的良好开局
全球住宅备用电源市场增长动力将加速 至2024年期间将以超过4%的复合年增长率增长
TensorFlow Lite实现移植到ART-Pi Smart
通用汽车为自动驾驶加码,手握自动驾驶和电气化两张大牌
麒麟A1芯片以及发布了比苹果H1还高
移动下调移动资费_原因竟是它_中国移动5G即将来临
区块链项目TON和TG与GRAM的区别介绍
数字化的5G,未来的新布局
利用ESP32 PWM信号控制LED的亮度
微机电系统(MEMS)惯性组件应用起飞
verilog移位操作和C语言的移位操作的异同点有哪些?
导线截面的计算公式
区块链还有哪些局限性
因应物联网时代来临 传统独立货运业务公司恐将面临被淘汰的命运
戴尔Q2财季营收下降13%至229亿美元,个人电脑销售好于预期
如何制作数字+模拟时钟
新型无电容DRAM能否迎来爆发?