Model Size is too large

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

Model Size is too large

Jump to solution
5,402 Views
wklee
Contributor II

Hi everyone,

I came across a strange problem. I manually trained a small object detection CNN and successfully created the .tflite file. Then I use the eIQ toolkit to convert it into NPU-supported version (shown below). 

wklee_0-1731381228281.png

We can see that the input and output are in int8, which is great. The total size of this model is just 68KB.

However, when I loaded it to the MCXN947 board, I found that it was bloated up to 512KB!!!

wklee_1-1731381409249.png

 

I was using the face detection example (dm-multiple-face-detection-on-mcxn947) from Application Code Hub, with small modifications on the MODEL_GetOpsResolver so that it loads the necessary operations.

Anyone sees this problem before? Did I miss out anything else?

*When I tried with other CNN models, it does not have such a problem.

 

Thanks!!!

 

Labels (2)
Tags (1)
0 Kudos
Reply
1 Solution
4,888 Views
Harry_Zhang
NXP Employee
NXP Employee

Hi @wklee 

I want to train a model trough your dataset. I downloaded your dataset and train.py.

How do you process the data?

BR

Harry

View solution in original post

0 Kudos
Reply
11 Replies
5,359 Views
Harry_Zhang
NXP Employee
NXP Employee

Hi @wklee 

When you export the model, do you select the "Export Quantized Model"?

image (5).png

BR

Hang

0 Kudos
Reply
5,354 Views
wklee
Contributor II

Dear Hang,

Yes, you can check both the attached file.

helmet_quantized.tflite --> Trained and quantized to 8-bit using python code on my host PC. I did not use the eIQ toolkit to train it.

helmet_quantized_converted.tflite --> This is the converted file using eIQv1.13. I use the eIQ Model Tool to open the helmet_quantized.tflite, then convert to NPU file.

wklee_0-1731488416427.png

wklee_1-1731488444594.png

I am using FRDM-MCXN947 board.

No quantization was selected because it was already quantized. You can see that the converted version also have 8-bit input and output. It is only ~67KB in size.

However, when I download the latest face detection demo and replace the original model with my helmet_quantized_converted.tflite , the size is much larger than expected.

*You may try converting my helmet_quantized.tflite, see if you get the same problem.

Thanks.

 

 

0 Kudos
Reply
5,338 Views
Harry_Zhang
NXP Employee
NXP Employee

Hi @wklee 

This is the compilation size of the face_detect.tflite model.

Harry_Zhang_2-1731556305096.png

 

This is the compilation size of the helmet_quantized_converted1.tflite model.

Harry_Zhang_1-1731556239852.png

BR

Hang

 

 

0 Kudos
Reply
5,329 Views
wklee
Contributor II

Dear Hang,

The file size for helmet_quantized_converted is 68KB:

wklee_1-1731563124363.png

But the compile size becomes 406.5KB, what could be the problem?

There are only ~70K parameters in this model.

wklee_0-1731563083532.png

Thanks.

 

 

0 Kudos
Reply
5,297 Views
Harry_Zhang
NXP Employee
NXP Employee

Hi @wklee 

Harry_Zhang_0-1731662233218.png

You can check the liker script.

It includes the *(.ezh_code) and *(.model_input_buffer).

So the compile size becomes 406.5KB.

BR

Harry

0 Kudos
Reply
5,134 Views
wklee
Contributor II

I don't think that's the reason.

The ezh_code is very small, only 512*4=2KB.

wklee_1-1732780150006.png

I verify this point by checking the original compilation:

wklee_0-1732780129871.png

Then modify the ezh_code to only ezh_code[2], which is 8 bytes.

wklee_2-1732780197711.png

You can see that the SRAMX region is only changed a little.

I think that the problem should be some problems during the conversion from a trained model to the NPU version. If the trained model is in int8 and contains only 70K parameters, there is no reason for the size to grow up to so big (400KB+).

Thanks.

 

0 Kudos
Reply
5,113 Views
wklee
Contributor II

Sorry for not making the problems clear.

In my first post, I mentioned that the model that I trained only has ~67 K parameters, so I expect that after int8 quantization the size should be around 70KB. However, when it is loaded onto the MCU (FRDM-MCXN947), I see the size grows to ~512KB, which is not what I expected, see the serial port information below.

helmet_model.png

0 Kudos
Reply
5,079 Views
Harry_Zhang
NXP Employee
NXP Employee

Hi @wklee 

I tried to convert this model in two ways and ran it on mcxn947, both of which encountered the same error. I think it's a problem with the model itself.

If your model includes operations unsupported by the NPU, increasing the size.

How did you train this model?

BR

Harry

0 Kudos
Reply
5,031 Views
wklee
Contributor II

The code can be found in the attachment. It is a customized CNN that performs object detection (helmet).

The training dataset is here:

https://drive.google.com/file/d/1xXwR6-OhXmePOGtyy9Yxn0bdhv3gmu4y/view?usp=sharing

 

Thanks.

Tags (1)
0 Kudos
Reply
4,889 Views
Harry_Zhang
NXP Employee
NXP Employee

Hi @wklee 

I want to train a model trough your dataset. I downloaded your dataset and train.py.

How do you process the data?

BR

Harry

0 Kudos
Reply
4,643 Views
wklee
Contributor II

Hi Harry Zhang,

I choose to compress the model into very small so that it can fit into the MCXN947 MCU. Although this does not solve the issue, it works for my project.

 

Thanks for your time and effort to support.

Tags (1)
0 Kudos
Reply