My question is same as the title.
I am surprised about this. The tflite model size is small at first. But after converting it to model_data.h to use on the board, the model_data.h size is much larger than the original model's size. I do not understand this point. Could you please explain this for me? Thank you.
Solved! Go to Solution.
Hello, the model data itself and the size of the model data for both the .tflite file and the .h header file are exactly the same. The only difference is how that data is represented, which is why the .h file is so much larger.
When a .tflite binary file is represented as ASCII characters in an array, then that means the ASCII header file needs at least 6 bytes of ASCII to represent each byte of binary of data in the .tflite file. For example, if 0x24 is a byte of data in the binary file, then to represent that in the ASCII header array would take a byte of data to the '0', a byte for the 'x', a byte for the '2' and a byte for the '4', a byte for the comma ',' and a byte for the space ' '. This means the .h file will be at least 6x larger than the binary file, plus there's also some extra ASCII text in the header file for comments and other data that aren't in the binary .tflite file because those are ignored by the compiler at compile time.
If you look at each file in a hex editor you can see this more clearly. Here's what the binary looks like to store 0x24 which takes up only one byte of space on the PC hard drive:
And here is that exact same data when represented by a ASCII header file where it now takes 6 bytes of ASCII data to represent it on a PC hard drive:
Keep in mind though that when the model_data.h file is brought into a project and compiled, then all those ASCII characters are translated back into binary data, so it essentially shrinks back down to the original binary size. Which is why the final total project size that gets flashed to the board will be the same regardless of if you import the binary .tflite file directly into the project or alternatively if you use the .h header file.
Best regards,
Pavel
Hello, the model data itself and the size of the model data for both the .tflite file and the .h header file are exactly the same. The only difference is how that data is represented, which is why the .h file is so much larger.
When a .tflite binary file is represented as ASCII characters in an array, then that means the ASCII header file needs at least 6 bytes of ASCII to represent each byte of binary of data in the .tflite file. For example, if 0x24 is a byte of data in the binary file, then to represent that in the ASCII header array would take a byte of data to the '0', a byte for the 'x', a byte for the '2' and a byte for the '4', a byte for the comma ',' and a byte for the space ' '. This means the .h file will be at least 6x larger than the binary file, plus there's also some extra ASCII text in the header file for comments and other data that aren't in the binary .tflite file because those are ignored by the compiler at compile time.
If you look at each file in a hex editor you can see this more clearly. Here's what the binary looks like to store 0x24 which takes up only one byte of space on the PC hard drive:
And here is that exact same data when represented by a ASCII header file where it now takes 6 bytes of ASCII data to represent it on a PC hard drive:
Keep in mind though that when the model_data.h file is brought into a project and compiled, then all those ASCII characters are translated back into binary data, so it essentially shrinks back down to the original binary size. Which is why the final total project size that gets flashed to the board will be the same regardless of if you import the binary .tflite file directly into the project or alternatively if you use the .h header file.
Best regards,
Pavel
Sure, thank you so much for detailed answer.
It's sound good that the binary size is come back to the original size when compilation and deploying on the board. My concern is gone out.
Hello, I apologize for the delayed response let me get more information from the internal team, when I have more information I will contact you.
Best regards,
Pavel
Hi. Is there any update about this issue?
Hello, my name is Pavel, and I will be supporting your case, could you let me know what tool are you using?
Best regards,
Pavel
I am using MCUXpresso IDE.
You can see in the tflm_cifar10 example for MCXN947. I attached 2 files for example. As you can see original tflite model size is 96 Kb. But after converting it model_data.h (because TFlite for microcontroller can no load file), the size is about 500 Kb.