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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

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

跳至解决方案
1,112 次查看
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.

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
96 次查看
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 项奖励
回复
7 回复数
97 次查看
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 项奖励
回复
81 次查看
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 项奖励
回复
118 次查看
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

537 次查看
nnxxpp
Contributor IV

@Pavel_Hernandez 

Hi. Is there any update about this issue?

0 项奖励
回复
1,067 次查看
nnxxpp
Contributor IV
0 项奖励
回复
1,082 次查看
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 项奖励
回复
1,071 次查看
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 项奖励
回复