Home > Back-end >  Trying to get tflite weights as a c variable
Trying to get tflite weights as a c variable

Time:09-22

I am using stm32 for AI, using the X Cube AI library, and i am trying to send the network weights of a given model to my microcontroller via UART. For this purpose i need to use a python script that could get the weights of my tflite model as a c variable like it is on the stm32: enter image description here

I have tried conversion tools such as the xxd -i command or the convert_bytes_to_c_source function, but it always converts the whole tflite file while i only need the weigths.

CodePudding user response:

Simply

  1. Write a function which will read this data from the UART.
  2. Call it
ai_handle ai_network...get(void)
{
    static ai_u8 s_network_weights[6720];
    uart_read_data(s_network_weights, sizeof(s_network_weights));

    /*...*/
}

CodePudding user response:

I found the solution to my problem. The X cube AI library just transforms the weights of the model from signed float to hex using the IEEE 754 standard. We can do this by usig this webite: https://gregstoll.com/~gregstoll/floattohex/ (they even have a python code for it)

We can see the comparison between the first weights and the values of the network data: here are the the first two weigths of the model in float and in hex (converted using the website method):

The first two weights of the model in float and hex

The weights as they are stored on the STM32

If we compare this to the value of the weights as they are stored on the STM32 they are indeed the same values as the first 8 bytes of value.

  • Related