sirius-ai / MobileFaceNet_TF

Tensorflow implementation for MobileFaceNet
Apache License 2.0
432 stars 170 forks source link

Can it run on ESP-CAM with tensorflow lite? #67

Open manjrekarom opened 4 years ago

manjrekarom commented 4 years ago

This may be a bit off-topic not sure though. So I am trying to do face recognition on ESP-CAM with 4MB flash.

At the moment the size of weights file for this model is 8MB so I am not able to put it on the device's flash which is only 4MB. I am thinking about pruning/distilling the model further to reduce its size but I have not done it prior to this.

If possible can you share your ideas how can that be done and if it'll work? My last option is to make a custom network and training it myself. Welcoming other ideas.

Thanks!

mvhsin commented 4 years ago

Hi @manjrekarom ,

You can try with MobileFacenet-S, which model size will be around 1.4MB when quantized with TFlite with the following cmd:

tflite_convert --output_file tf-lite/MobileFacenet_uint8_128.tflite \ --graph_def_file tf-lite/MobileFacenet.pb \ --input_arrays "input" \ --input_shapes "1,112,112,3" \ --output_arrays output \ --output_format TFLITE \ --mean_values 128 \ --std_dev_values 127.5 \ --default_ranges_min 0 \ --default_ranges_max 255 \ --inference_type QUANTIZED_UINT8 \ --inference_input_type QUANTIZED_UINT8

But I met the other issue related to accuracy. The result seems not correct with the quantized model, please kindly share the information if you have the idea. Many thanks!

manjrekarom commented 4 years ago

Hi @mvhsin ! Thanks for your reply.

Can I get a link to pretrained MobileFacenet-S? The model that we had quantized was also about 1.5MB in size after quantization but after converting it back to .cc file it grew to about 6x - 8x (~8MB).

Sure. I will share if I find something relevant.

mvhsin commented 4 years ago

Hi @manjrekarom ,

The author of MobileFacenet mentioned in the paper.

remove the linear 1 × 1 convolution layer afterthe linear GDConv layer from MobileFaceNet, the resulting network of which is called MobileFaceNet-M. From MobileFaceNet-M, removing the 1 × 1 convolution layer before the linear GDConv layer produces the smallest network called MobileFaceNet-S. Sorry I am not sure about *.cc file. Is that done by TF-lite microcontroller?

manjrekarom commented 4 years ago

Oh thanks I'll take a look.

Yes it is for TF-Lite microcontrollers. The FlatBuffer obtained from an initial tf model, is converted to a C byte array.

This C file is taking up ~7x space (1.5 -> ~8MB). So we need an even smaller model. The options I feel I am left with are network pruning, knowledge distillation and training an even smaller custom architecture similar to MobileFaceNet.

mvhsin commented 4 years ago

Same here! Thanks for the info of TF-lite microcontroller.