TensorFlow Lite 官方倉庫:
https://github.com/iysheng/tflite-micro
我的ART-Pi-smart倉庫:
https://gitee.com/iysheng/ART-Pi-smart
經(jīng)過了多天的摸索學(xué)習(xí),今天將 TensorFlow Lite 的框架移植到了ART-Pi Smart上,但是還是存在一點問題,主要是 C++ 和 C 語言混合編譯的問題,根據(jù)目前我的總結(jié),具體體現(xiàn)在:
-
TensorFlow Lite 在函數(shù)中定義的靜態(tài)對象無法正常構(gòu)造,必須放在全局變量的位置
-
全局變量定義的對象只能使用默認(rèn)的構(gòu)造函數(shù),無發(fā)使用帶有參數(shù)的構(gòu)造函數(shù)初始對象
1diff--gita/kernel/bsp/imx6ull-artpi-smart/applications/tflm_tree/examples/hello_world/main_functions.ccb/kernel/bsp/imx6ull-artpi-smart/applications/tflm_tree/examples/hello_world/main_functions.cc
2indexa585ba7..a318c32100644
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@@constexprintkTensorArenaSize=2000;
6uint8_ttensor_arena[kTensorArenaSize];
7}//namespace
8
9+statictflite::MicroErrorReportermicro_error_reporter;
10+//Thispullsinalltheoperationimplementationsweneed.
11+//NOLINTNEXTLINE(runtime-global-variables)
12+statictflite::AllOpsResolverresolver;
13+statictflite::MicroInterpreter*static_interpreter;
14+
15//ThenameofthisfunctionisimportantforArduinocompatibility.
16voidsetup(){
17+
18+DebugLog("setupbegin.
");
19tflite::InitializeTarget();
20
21+DebugLog("initialok.
");
22+
23//Setuplogging.Googlestyleistoavoidglobalsorstaticsbecauseof
24//lifetimeuncertainty,butsincethishasatrivialdestructorit'sokay.
25//NOLINTNEXTLINE(runtime-global-variables)
26-statictflite::MicroErrorReportermicro_error_reporter;
27+/*這個地方有問題*/
28error_reporter=µ_error_reporter;
29
30//Mapthemodelintoausabledatastructure.Thisdoesn'tinvolveany
31//copyingorparsing,it'saverylightweightoperation.
32+//printf("value:%x%u
",g_hello_world_model_data[0],g_hello_world_model_data_size);
33+
34model=tflite::GetModel(g_hello_world_model_data);
35if(model->version()!=TFLITE_SCHEMA_VERSION){
36TF_LITE_REPORT_ERROR(error_reporter,
37@@-57,15+69,10@@voidsetup(){
38model->version(),TFLITE_SCHEMA_VERSION);
39return;
40}
41-
42-//Thispullsinalltheoperationimplementationsweneed.
43-//NOLINTNEXTLINE(runtime-global-variables)
44-statictflite::AllOpsResolverresolver;
45-
46//Buildaninterpretertorunthemodelwith.
47-statictflite::MicroInterpreterstatic_interpreter(
48+static_interpreter=newtflite::MicroInterpreter(
49model,resolver,tensor_arena,kTensorArenaSize,error_reporter);
50-interpreter=&static_interpreter;
51+interpreter=static_interpreter;
52
53//Allocatememoryfromthetensor_arenaforthemodel'stensors.
54TfLiteStatusallocate_status=interpreter->AllocateTensors();
55@@-102,6+109,7@@intloop(){
56if(invoke_status!=kTfLiteOk){
57TF_LITE_REPORT_ERROR(error_reporter,"Invokefailedonx:%f
",
58static_cast<double>(x));
59+printf("invokestatus:%x
",invoke_status);
60return-1;
61}
參考的資料主要有:
1、tinyML基TensorFlow Lite在Arduino
2、https://github.com/iysheng/tflite-micro/blob/main/tensorflow/lite/micro/docs/new_platform_support.md
特別關(guān)鍵的一點是提取基礎(chǔ)框架的文件:python3 tensorflow/lite/micro/tools/project_generation/create_tflm_tree.py -e hello_world /tmp/tflm-tree
根據(jù)官方的文檔,上述命令就可以將 hello_world 工程相關(guān)的文件提取到指定的 /tmp/tflm-tree 目錄,下一步做的就是將這部分內(nèi)容添加到 RT-Thread Smart 工程中,編譯出來。相關(guān)的代碼,我都上傳到了我的ART-Pi Smart的倉庫。如果有問題歡迎和我討論。展示下,運(yùn)行起來的效果,為了展示需要,刪除了部分空行打印:
1U-Boot2017.03-g002b758ac9-dirty(Jul082021-10:45:12+0000)
2CPU:Freescalei.MX6ULLrev1.1696MHz(runningat396MHz)
3CPU:Industrialtemperaturegrade(-40Cto105C)at44C
4Resetcause:POR
5Model:Freescalei.MX6ULL14x14EVKBoard
6Board:MX6ULL14x14EVK
7DRAM:512MiB
8MMC:FSL_SDHC:0,FSL_SDHC:1
9Display:TFT7016(1024x600)
10Video:1024x600x24
11In:serial
12Out:serial
13Err:serial
14switchtopartitions#0,OK
15mmc1(part0)iscurrentdevice
16Net:eth0:ethernet@02188000[PRIME]
17NormalBoot
18Hitanykeytostopautoboot:3210
19ethernet@02188000WaitingforPHYautonegotiationtocomplete....done
20Usingethernet@02188000device
21TFTPfromserver10.20.52.39;ourIPaddressis10.20.52.89
22Filename'rtthread.bin'.
23Loadaddress:0x80001000
24Loading:*#################################################################
25###############################################################
26159.2KiB/s
27done
28Bytestransferred=1866656(1c7ba0hex)
29##Startingapplicationat0x80001000...
30|/
31-RT-ThreadSmartOperatingSystem
32/|5.0.0buildApr302022
332006-2020Copyrightbyrt-threadteam
34docomponentsinitialization.
35initializerti_board_end:0done
36initializedfs_init:0done
37initializert_mmcsd_core_init:0done
38initializert_usbd_winusb_class_register:0done
39initializefutex_system_init:0done
40initializepmutex_system_init:0done
41initializelwip_system_initlwIP-2.1.2initialized!
42:0done
43initializeulog_console_backend_init:0done
44initializeulog_init:0done
45initializert_hw_adc_init:0done
46initializert_hw_csi_init:0done
47initializert_hw_dbg_mm_init:0done
48initializeimx6ul_eth_initredfirstva=f00here0000
49here1111mmu_l2=c107c000
50[31m[63]E/drv.enet:emacdeviceinitsuccess
51[0m:0done
52initializert_hw_i2c_init[32m[70]I/I2C:I2Cbus[i2c1]registered
53[0m[32m[75]I/I2C:I2Cbus[i2c3]registered
54[0m[32m[79]I/I2C:I2Cbus[i2c4]registered
55[0m:0done
56initializert_hw_elcd_init[86]D/drv.lcd:fbaddress=>0x811c0000
57:0done
58initializert_hw_pwm_init:0done
59initializert_hw_rtc_init:0done
60initializeimxrt_mci_init:0done
61initializert_hw_spi_init:0done
62initializegt911_init[31m[158]E/drv.enet:PHYLinkdown,pleasecheckthecableconnectionandlinkpartnersetting.
63[0m[32m[192]I/SDIO:emmccardcapacity3817472KB.
64[0mfoundpart[0],begin:2098176,size:500.0MB
65foundpart[1],begin:526386176,size:1.0GB
66foundpart[2],begin:1600128000,size:10.0MB
67foundpart[3],begin:1610613760,size:200.0MB
68[32m[328]I/touch:rt_touchinitsuccess
69[0m[32m[332]I/gt911:touchdevicegt911initsuccess
70[0m:0done
71initializert_usbd_init:0done
72initializert_hw_wdt_init:0done
73initializenull_device_init:0done
74initializerandom_device_init:0done
75initializeurandom_device_init:0done
76initializezero_device_init:0done
77initializert_work_sys_workqueue_init:0done
78initializeptmx_register:0done
79initializecritical_init:0done
80initializert_hw_ov5640_init:0done
81initializecplusplus_system_init:0done
82initializeelm_init:0done
83initializedfs_romfs_init:0done
84initializert_i2c_core_init:0done
85initializelibc_system_init:0done
86initializeclock_time_system_init:0done
87initializesal_init[32m[390]I/sal.skt:SocketAbstractionLayerinitializesuccess.
88[0m:0done
89initializemnt_initemmcfilesysteminitializationdone!
90:0done
91initializeTF_lite_init[5309]D/drv.enet:enet1linkup
92:0done
93initializeem_initopenemfailed!
94:-1done
95initializeset_adc_init:0done
96initializeset_pwm_default:0done
97initializert_sysinfo_init:0done
98initializefinsh_system_init:0done
99Redsay:hellort-smart.
100openlcdokwatchdogstarting.....
101testtf.
102setupbegin.
103gohere
104initialok.
105getmodebegin.
106gethere?
107gethere000?
108constructtheclass.
109initok.
110x_value:1.0*2^-127,y_value:1.0297613*2^-7
111x_value:1.0053092*2^-5,y_value:1.8020826*2^-5
112x_value:1.0053092*2^-4,y_value:1.1584818*2^-4
113x_value:1.507964*2^-4,y_value:1.9308027*2^-4
114x_value:1.0053092*2^-3,y_value:1.0941217*2^-3
115x_value:1.2566366*2^-3,y_value:1.4802819*2^-3
116x_value:1.507964*2^-3,y_value:1.6733624*2^-3
117x_value:1.7592913*2^-3,y_value:1.8664425*2^-3
118x_value:1.0053092*2^-2,y_value:1.0619412*2^-2
119x_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^-2
10x_value:1.130973*2^-2,y_value:1.1584818*2^-2
11...
上述部分是模型估算的正弦函數(shù)的數(shù)據(jù),我繪制成圖表的效果如下:效果還是不錯的,這使用的是官方例程 16 * 16 * 1 的三層模型。
原文標(biāo)題:在 ART-Pi Smart 上運(yùn)行 TensorFlow Lite
文章出處:【微信公眾號:RTThread物聯(lián)網(wǎng)操作系統(tǒng)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
-
移植
+關(guān)注
關(guān)注
1文章
379瀏覽量
28124 -
SMART
+關(guān)注
關(guān)注
3文章
224瀏覽量
44680 -
tensorflow
+關(guān)注
關(guān)注
13文章
329瀏覽量
60527 -
TensorFlow Lite
+關(guān)注
關(guān)注
0文章
26瀏覽量
617
原文標(biāo)題:在 ART-Pi Smart 上運(yùn)行 TensorFlow Lite
文章出處:【微信號:RTThread,微信公眾號:RTThread物聯(lián)網(wǎng)操作系統(tǒng)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論