Update cmsisnn_demo.c

This commit is contained in:
kyt_2002 2023-10-05 19:15:51 +08:00
parent b8eb636bfd
commit a5ad91fc1d
1 changed files with 15 additions and 4 deletions

View File

@ -6,6 +6,7 @@
const char *cifar10_label[] = {"Plane", "Car", "Bird", "Cat", "Deer", "Dog", "Frog", "Horse", "Ship", "Truck"}; const char *cifar10_label[] = {"Plane", "Car", "Bird", "Cat", "Deer", "Dog", "Frog", "Horse", "Ship", "Truck"};
// 获取最高预测结果的索引
int get_top_prediction(q7_t *predictions) int get_top_prediction(q7_t *predictions)
{ {
int max_ind = 0; int max_ind = 0;
@ -21,6 +22,7 @@ int get_top_prediction(q7_t *predictions)
return max_ind; return max_ind;
} }
// 进行模型推理
int cmsisnn_inference(uint8_t *input_data) int cmsisnn_inference(uint8_t *input_data)
{ {
q7_t output_data[10]; q7_t output_data[10];
@ -38,6 +40,7 @@ typedef struct
uint16_t wfbuf; uint16_t wfbuf;
} IODEV; } IODEV;
// JPEG解码回调函数用于读取数据
unsigned int in_func_cmsisnn(JDEC *jd, uint8_t *buff, unsigned int nbyte) unsigned int in_func_cmsisnn(JDEC *jd, uint8_t *buff, unsigned int nbyte)
{ {
IODEV *dev = (IODEV *)jd->device; IODEV *dev = (IODEV *)jd->device;
@ -52,6 +55,7 @@ unsigned int in_func_cmsisnn(JDEC *jd, uint8_t *buff, unsigned int nbyte)
} }
} }
// JPEG解码回调函数用于输出解码后的数据
int out_func_cmsisnn(JDEC *jd, void *bitmap, JRECT *rect) int out_func_cmsisnn(JDEC *jd, void *bitmap, JRECT *rect)
{ {
IODEV *dev = (IODEV *)jd->device; IODEV *dev = (IODEV *)jd->device;
@ -77,12 +81,13 @@ int out_func_cmsisnn(JDEC *jd, void *bitmap, JRECT *rect)
return 1; return 1;
} }
// CIFAR-10图像解码和推理的示例代码
int cmsisnn_demo(int argc, char *argv[]) int cmsisnn_demo(int argc, char *argv[])
{ {
void *work; void *work;
JDEC jdec; JDEC jdec;
JRESULT res; JRESULT res;
IODEV devid; IODEV devid;
if (argc < 2) if (argc < 2)
{ {
@ -90,6 +95,7 @@ int cmsisnn_demo(int argc, char *argv[])
return -1; return -1;
} }
// 打开JPEG文件
devid.fp = fopen(argv[1], "r+"); devid.fp = fopen(argv[1], "r+");
if (!devid.fp) if (!devid.fp)
{ {
@ -97,6 +103,7 @@ int cmsisnn_demo(int argc, char *argv[])
return -1; return -1;
} }
// 分配工作内存
work = malloc(WORK_POOL_SIZE); work = malloc(WORK_POOL_SIZE);
if (work == NULL) if (work == NULL)
{ {
@ -105,11 +112,13 @@ int cmsisnn_demo(int argc, char *argv[])
goto __exit; goto __exit;
} }
// 准备JPEG解码器
res = jd_prepare(&jdec, in_func_cmsisnn, work, WORK_POOL_SIZE, &devid); res = jd_prepare(&jdec, in_func_cmsisnn, work, WORK_POOL_SIZE, &devid);
if (res == JDR_OK) if (res == JDR_OK)
{ {
printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, 3100 - jdec.sz_pool); printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, 3100 - jdec.sz_pool);
// 分配解码后的RGB像素数据缓存
devid.fbuf = malloc(3 * jdec.width * jdec.height); devid.fbuf = malloc(3 * jdec.width * jdec.height);
if (devid.fbuf == NULL) if (devid.fbuf == NULL)
{ {
@ -119,6 +128,7 @@ int cmsisnn_demo(int argc, char *argv[])
} }
devid.wfbuf = jdec.width; devid.wfbuf = jdec.width;
// 解码JPEG并输出到缓存
res = jd_decomp(&jdec, out_func_cmsisnn, 0); res = jd_decomp(&jdec, out_func_cmsisnn, 0);
if (res == JDR_OK) if (res == JDR_OK)
{ {
@ -129,6 +139,7 @@ int cmsisnn_demo(int argc, char *argv[])
printf("Failed to decompress: rc=%d\n", res); printf("Failed to decompress: rc=%d\n", res);
} }
// 进行图像推理
cmsisnn_inference(devid.fbuf); cmsisnn_inference(devid.fbuf);
if (devid.fbuf != NULL) if (devid.fbuf != NULL)
@ -154,4 +165,4 @@ __exit:
#ifdef __RT_THREAD_H__ #ifdef __RT_THREAD_H__
MSH_CMD_EXPORT(cmsisnn_demo, cifar10 demo and filename should be followed); MSH_CMD_EXPORT(cmsisnn_demo, cifar10 demo and filename should be followed);
#endif #endif