Why size of file model_data.h is much larger than the model size?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why size of file model_data.h is much larger than the model size?

Jump to solution
1,114 Views
nnxxpp
Contributor IV

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.

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
98 Views
Pavel_Hernandez
NXP TechSupport
NXP TechSupport

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: 

Pavel_Hernandez_0-1734036118748.png

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: 

Pavel_Hernandez_1-1734036118660.png

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

View solution in original post

0 Kudos
Reply
7 Replies
99 Views
Pavel_Hernandez
NXP TechSupport
NXP TechSupport

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: 

Pavel_Hernandez_0-1734036118748.png

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: 

Pavel_Hernandez_1-1734036118660.png

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

0 Kudos
Reply
83 Views
nnxxpp
Contributor IV

Hi @Pavel_Hernandez 

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.

0 Kudos
Reply
120 Views
Pavel_Hernandez
NXP TechSupport
NXP TechSupport

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

539 Views
nnxxpp
Contributor IV

@Pavel_Hernandez 

Hi. Is there any update about this issue?

0 Kudos
Reply
1,069 Views
nnxxpp
Contributor IV
0 Kudos
Reply
1,084 Views
Pavel_Hernandez
NXP TechSupport
NXP TechSupport

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

0 Kudos
Reply
1,073 Views
nnxxpp
Contributor IV

@Pavel_Hernandez 

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.

0 Kudos
Reply