Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread
STM32F407
mnist
很奇怪的一个tensorflow lite软件包的问题
发布于 2023-03-21 19:43:58 浏览:556
订阅该版
我这份手写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
被浏览
556
关于作者
这瓜保熟吗
这家伙很懒,什么也没写!
提问
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
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
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
【RT-Thread】【ci】【scons】将ci.attachconfig.yml和scons结合使用
2
Rt-thread中OTA下载后,bootloader不搬程序
3
ulog 日志 LOG_HEX 输出时间改为本地日期时间
4
在RT-Thread Studio中构建前执行python命令
5
研究一了一段时间RTT,直接标准版上手太难,想用nano,但又舍不得组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
xusiwei1236
8
个答案
2
次被采纳
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
9
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
RTT_逍遥
3
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
4
次点赞
Ghost_Girls
1
篇文章
7
次点赞
xiaorui
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部