尝试将tflite权重作为c变量获取

2024-09-24 06:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用stm32进行人工智能,使用X Cube人工智能库,并试图通过UART将给定模型的网络权重发送到我的微控制器。 为此,我需要使用一个python脚本,它可以像在stm32上一样,以c变量的形式获取tflite模型的权重: How the model is stored on the stm32

enter image description here

我尝试过转换工具,如xxd -i命令或convert_bytes_to_c_source函数,但它总是转换整个tflite文件,而我只需要weights


Tags: the模型网络脚本modelis人工智能形式
2条回答

我找到了解决我问题的办法。X cube AI库使用IEEE 754标准将模型的权重从有符号浮点转换为十六进制。 我们可以通过使用这个webite:https://gregstoll.com/~gregstoll/floattohex/(他们甚至有一个python代码)

我们可以看到第一个权重和网络数据值之间的比较: 以下是模型的前两个重量,分别为float和hex(使用网站方法转换):

The first two weights of the model in float and hex

The weights as they are stored on the STM32

如果我们将其与存储在STM32上的权重值进行比较,它们实际上与前8个字节的值相同

简单地

  1. 编写一个从UART读取此数据的函数
  2. 叫它
ai_handle ai_network...get(void)
{
    static ai_u8 s_network_weights[6720];
    uart_read_data(s_network_weights, sizeof(s_network_weights));

    /*...*/
}

相关问题 更多 >