Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread
STM32F407
mnist
很奇怪的一个tensorflow lite软件包的问题
发布于 2023-03-21 19:43:58 浏览:391
订阅该版
我这份手写mnist数据集识别代码在linux运行是没问题的,https://github.com/tensorflow/tflite-micro.git ,直接使用的是官方的版本。 但是在RT-THread中,用RT-Thread Studio安装的tflite包,代码跑的结果不对。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230321/8db3b15367d72aec597140342ccb53ec.png) 连小数都没有 下面是linux上运行的结果,很正确。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230321/f0234b60f1927f1e55b2679c7add7b5d.png) 我的代码 ```c extern "C"{ #define _CRT_SECURE_NO_WARNINGS #include
#include
} #include "tensorflow/lite/c/common.h" #include "tensorflow/lite/micro/all_ops_resolver.h" #include "tensorflow/lite/micro/micro_interpreter.h" #include "tensorflow/lite/schema/schema_generated.h" extern "C" { #include "tjpgd.h" } typedef unsigned int UINT; typedef unsigned char BYTE; UINT in_func (JDEC* jd, BYTE* buff, UINT nbyte); int out_func (JDEC* jd, void* bitmap, JRECT* rect); typedef struct { FILE* fp; BYTE *fbuf; UINT wfbuf; } IODEV; struct RGB{ unsigned char r,g,b; }; class RTT_ERROR_REPORT:public tflite::ErrorReporter{ public: virtual int Report(const char* format, va_list args){ char buffer[256]; buffer[255]='\n'; vsnprintf(buffer,256,format,args); printf(buffer); return 0; } }; extern "C" int main() { // 读取模型 FILE* fd = fopen("/sdcard/mnist.tflite", "rb"); if(fd == 0){ perror("???"); printf("文件打开失败\n"); return 0; } char* fbuf = (char*)malloc(377200); if(!fbuf){ printf("内存分配失败\n"); return 0; } fread(fbuf,1,377200,fd); // 加载模型 const tflite::Model* model = tflite::GetModel(fbuf); printf("模型版本 : %d\n", model->version()); tflite::MicroMutableOpResolver<6> resolver; resolver.AddConv2D(); resolver.AddMaxPool2D(); resolver.AddReshape(); resolver.AddFullyConnected(); resolver.AddSoftmax(); //TFLITE_SCHEMA_VERSION /* 12 bytes lost due to alignment. To avoid this loss, please make sure the tensor_arena is 16 bytes aligned. Arena size is too small for activation buffers. Needed 108160 but only 8992 was available 下面这个大小要不断增大,直到不报错 108160=1024*105 不能超出可用RAM大小 */ constexpr int kTensorArenaSize = 205*1024; // https://github.com/espressif/tflite-micro-esp-examples/issues/3 // 用rt_malloc/栈空间不够 uint8_t* tensor_arena = (uint8_t*)malloc(kTensorArenaSize); if(!tensor_arena){ printf("内存不够\n"); return 0; } RTT_ERROR_REPORT error_reporter; tflite::MicroInterpreter interpreter(model, resolver, tensor_arena,kTensorArenaSize,&error_reporter); if(interpreter.initialization_status() != kTfLiteOk){ printf("解释器初始化失败\n"); return 0; } TfLiteStatus allocate_status; allocate_status = interpreter.AllocateTensors(); if(allocate_status != kTfLiteOk){ printf("AllocateTensors 失败\n"); return 0; } interpreter.input_tensor(0); interpreter.typed_input_tensor
(0); void* work = malloc(50000); JDEC jdec; IODEV devid; devid.fp = fopen("/sdcard/1.jpg", "rb"); if(!devid.fp){ printf("图像打开失败\n"); return 0; } typedef size_t (*fcp)(JDEC *, uint8_t *, size_t); JRESULT res = jd_prepare(&jdec, (fcp)in_func, work, 50000, &devid); if(res != JDR_OK){ printf("%d\n",res); printf("jpg 图像解码失败\n"); return 0; } printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, (unsigned int)(50000 - jdec.sz_pool)); devid.fbuf = (BYTE*)malloc(3 * jdec.width * jdec.height); devid.wfbuf = jdec.width; res = jd_decomp(&jdec, out_func, 0); if (res == JDR_OK) { printf("ok\n"); }else{ printf("not ok\n"); } // // 训练的图片是28*28的灰度的,需要将rgb888的图片转成灰度 TfLiteTensor* input_data_ptr = interpreter.input(0); float* gray = (float*)malloc(4*28*28); RGB* rgb = (RGB*)devid.fbuf; for(int i = 0;i<28;i++){ for(int j = 0;j<28;j++){ //printf("%u %u %u\n",rgb->r,rgb->g,rgb->b); *(gray+i*28+j) = rgb[i*28+j].r; } } for(int i = 0;i<28;i++){ printf("input[%d][]\n",i); for(int j = 0;j<28;j++){ // 输出灰度后的像素值 //printf("%f ",*(gray+j*28+i)); // 归一化 float d = 1.0-(float)(*(gray+i*28+j))/255.f; input_data_ptr->data.f[i*28+j] = d; printf("%f ",d); //input_data_ptr++; } printf("\n"); } // 运行模型 interpreter.Invoke(); printf("Invoke Done.\n"); TfLiteTensor* output = nullptr; output = interpreter.output(0); for (int i = 0; i < 10; i++) { //https://club.rt-thread.org/ask/question/04dc44403aedf571.html // printf正常打印浮点 printf("Result %d: %f\n", i, output->data.f[i]); } return 0; } unsigned int in_func (JDEC* jd, uint8_t* buff, unsigned int nbyte) { IODEV *dev = (IODEV*)jd->device; if (buff) { /* Read bytes from input stream */ return (uint16_t)fread(buff, 1, nbyte, dev->fp); } else { /* Remove bytes from input stream */ return fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte; } } int out_func (JDEC* jd, void* bitmap, JRECT* rect) { IODEV *dev = (IODEV*)jd->device; BYTE *src, *dst; UINT y, bws, bwd; if (rect->left == 0) { //printf("\r%lu%%", (rect->top << jd->scale) * 100UL / jd->height); } src = (BYTE*)bitmap; dst = dev->fbuf + 3 * (rect->top * dev->wfbuf + rect->left); bws = 3 * (rect->right - rect->left + 1); bwd = 3 * dev->wfbuf; for (y = rect->top; y <= rect->bottom; y++) { memcpy(dst, src, bws); src += bws; dst += bwd; } //RGB* src2 = (RGB*)dst; //for(int i= 0;i<28;i++){ //printf("(%d %d %d)\n",(src2+i)->r,(src2+i)->g,(src2+i)->b); //} return 1; } ``` 好奇怪,我感觉RT-Thread Studio的tflite安装包有问题
查看更多
1
个回答
默认排序
按发布时间排序
这瓜保熟吗
2023-03-21
这家伙很懒,什么也没写!
linux的代码,完整复制 ```c extern "C"{ #define _CRT_SECURE_NO_WARNINGS #include
#include
} #include "tensorflow/lite/c/common.h" #include "tensorflow/lite/micro/all_ops_resolver.h" #include "tensorflow/lite/micro/micro_interpreter.h" #include "tensorflow/lite/schema/schema_generated.h" extern "C" { #include "tjpgd.h" } typedef unsigned int UINT; typedef unsigned char BYTE; UINT in_func (JDEC* jd, BYTE* buff, UINT nbyte); int out_func (JDEC* jd, void* bitmap, JRECT* rect); typedef struct { FILE* fp; BYTE *fbuf; UINT wfbuf; } IODEV; struct RGB{ unsigned char r,g,b; }; class RTT_ERROR_REPORT:public tflite::ErrorReporter{ public: virtual int Report(const char* format, va_list args){ char buffer[256]; buffer[255]='\n'; vsnprintf(buffer,256,format,args); printf(buffer); return 0; } }; extern "C" int main() { // 读取模型 FILE* fd = fopen("/root/tflite-micro/tensorflow/lite/micro/examples/hello_world/mnist.tflite", "rb"); if(fd == 0){ perror("???"); printf("文件打开失败\n"); return 0; } char* fbuf = (char*)malloc(377200); if(!fbuf){ printf("内存分配失败\n"); return 0; } fread(fbuf,1,377200,fd); // 加载模型 const tflite::Model* model = tflite::GetModel(fbuf); printf("模型版本 : %d\n", model->version()); tflite::MicroMutableOpResolver<6> resolver; resolver.AddConv2D(); resolver.AddMaxPool2D(); resolver.AddReshape(); resolver.AddFullyConnected(); resolver.AddSoftmax(); /* 12 bytes lost due to alignment. To avoid this loss, please make sure the tensor_arena is 16 bytes aligned. Arena size is too small for activation buffers. Needed 108160 but only 8992 was available 下面这个大小要不断增大,直到不报错 108160=1024*105 不能超出可用RAM大小 */ constexpr int kTensorArenaSize = 205*1024; // https://github.com/espressif/tflite-micro-esp-examples/issues/3 // 用rt_malloc/栈空间不够 uint8_t* tensor_arena = (uint8_t*)malloc(kTensorArenaSize); if(!tensor_arena){ printf("内存不够\n"); return 0; } RTT_ERROR_REPORT error_reporter; tflite::MicroInterpreter interpreter(model, resolver, tensor_arena, kTensorArenaSize); if(interpreter.initialization_status() != kTfLiteOk){ printf("解释器初始化失败\n"); return 0; } TfLiteStatus allocate_status; allocate_status = interpreter.AllocateTensors(); if(allocate_status != kTfLiteOk){ printf("AllocateTensors 失败\n"); return 0; } interpreter.input_tensor(0); interpreter.typed_input_tensor
(0); void* work = malloc(50000); JDEC jdec; IODEV devid; devid.fp = fopen("/root/tflite-micro/tensorflow/lite/micro/examples/hello_world/1.jpg", "rb"); if(!devid.fp){ printf("图像打开失败\n"); return 0; } typedef size_t (*fcp)(JDEC *, uint8_t *, size_t); JRESULT res = jd_prepare(&jdec, (fcp)in_func, work, 50000, &devid); if(res != JDR_OK){ printf("%d\n",res); printf("jpg 图像解码失败\n"); return 0; } printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, (unsigned int)(50000 - jdec.sz_pool)); devid.fbuf = (BYTE*)malloc(3 * jdec.width * jdec.height); devid.wfbuf = jdec.width; res = jd_decomp(&jdec, out_func, 0); if (res == JDR_OK) { printf("ok\n"); }else{ printf("not ok\n"); } // // 训练的图片是28*28的灰度的,需要将rgb888的图片转成灰度 TfLiteTensor* input_data_ptr = interpreter.input(0); float* gray = (float*)malloc(4*28*28); RGB* rgb = (RGB*)devid.fbuf; for(int i = 0;i<28;i++){ for(int j = 0;j<28;j++){ //printf("%u %u %u\n",rgb->r,rgb->g,rgb->b); *(gray+i*28+j) = rgb[i*28+j].r; } } for(int i = 0;i<28;i++){ printf("input[%d][]\n",i); for(int j = 0;j<28;j++){ // 输出灰度后的像素值 //printf("%f ",*(gray+j*28+i)); // 归一化 float d = 1.0-(float)(*(gray+i*28+j))/255.f; input_data_ptr->data.f[i*28+j] = d; printf("%f ",d); //input_data_ptr++; } printf("\n"); } // 运行模型 interpreter.Invoke(); printf("Invoke Done.\n"); TfLiteTensor* output = nullptr; output = interpreter.output(0); for (int i = 0; i < 10; i++) { //https://club.rt-thread.org/ask/question/04dc44403aedf571.html // printf正常打印浮点 printf("Result %d: %f\n", i, output->data.f[i]); } return 0; } unsigned int in_func (JDEC* jd, uint8_t* buff, unsigned int nbyte) { IODEV *dev = (IODEV*)jd->device; if (buff) { /* Read bytes from input stream */ return (uint16_t)fread(buff, 1, nbyte, dev->fp); } else { /* Remove bytes from input stream */ return fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte; } } int out_func (JDEC* jd, void* bitmap, JRECT* rect) { IODEV *dev = (IODEV*)jd->device; BYTE *src, *dst; UINT y, bws, bwd; if (rect->left == 0) { //printf("\r%lu%%", (rect->top << jd->scale) * 100UL / jd->height); } src = (BYTE*)bitmap; dst = dev->fbuf + 3 * (rect->top * dev->wfbuf + rect->left); bws = 3 * (rect->right - rect->left + 1); bwd = 3 * dev->wfbuf; for (y = rect->top; y <= rect->bottom; y++) { memcpy(dst, src, bws); src += bws; dst += bwd; } //RGB* src2 = (RGB*)dst; //for(int i= 0;i<28;i++){ //printf("(%d %d %d)\n",(src2+i)->r,(src2+i)->g,(src2+i)->b); //} return 1; } ```
撰写答案
登录
注册新账号
关注者
1
被浏览
391
关于作者
这瓜保熟吗
这家伙很懒,什么也没写!
提问
1
回答
1
被采纳
0
关注TA
发私信
相关问题
1
RT-THREAD在STM32H747平台上移植lwip
2
正点原子miniSTM32开发板读写sdcard
3
反馈rtt串口驱动对低功耗串口lpuart1不兼容的问题
4
Keil MDK 移植 RT-Thread Nano
5
RT1061/1052 带 RTT + LWIP和LPSPI,有什么坑要注意吗?
6
RT thread HID 如何收发数据
7
求一份基于RTT系统封装好的STM32F1系列的FLASH操作程序
8
RT-Thread修改项目名称之后不能下载
9
rt-studio编译c++
10
有木有移植rt-thread(nano)到riscv 32位MCU上
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
机器人操作系统 (ROS2) 和 RT-Thread 通信
4
国产MCU移植系列教程汇总,欢迎查看!
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
【24嵌入式设计大赛】基于RT-Thread火星一号华为云平台储能监测管理系统
2
RVBacktrace RISC-V极简栈回溯组件
3
一文教你如何在VSCODE中搭建RT-Thread开发环境
4
分享第一次PR的经历
5
EtherCAT EOE移植及上手说明
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
ART-Pi
FinSH
USB
文件系统
DMA
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
ota在线升级
WIZnet_W5500
UART
flash
packages_软件包
cubemx
PWM
freemodbus
BSP
潘多拉开发板_Pandora
定时器
ADC
中断
flashDB
socket
Debug
GD32
编译报错
msh
keil_MDK
at_device
rt_mq_消息队列_msg_queue
ulog
C++_cpp
SFUD
本月问答贡献
出出啊
1512
个答案
341
次被采纳
小小李sunny
1437
个答案
289
次被采纳
张世争
774
个答案
167
次被采纳
crystal266
546
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部