简体   繁体   English

减少TFLite模型的尺寸?

[英]Reducing TFLite model size?

I'm currently making a multi-label image classification model by following this guide (it uses inception as the base model): https://towardsdatascience.com/multi-label-image-classification-with-inception-net-cbb2ee538e30 我正在按照本指南(它使用初始作为基本模型)制作多标签图像分类模型: https//towardsdatascience.com/multi-label-image-classification-with-inception-net-cbb2ee538e30

After converting from .pb to .tflite the model is only approximately 0.3mb smaller. .pb转换为.tflite该模型仅减少约0.3mb。

Here is my conversion code: 这是我的转换代码:

toco \
  --graph_def_file=optimized_graph.pb \
  --output_file=output/optimized_graph.tflite \
  --output_format=TFLITE \
  --input_shape=1,299,299,3 \
  --input_array=Mul \
  --output_array=final_result \
  --inference_type=FLOAT \
  --inference_input_type=FLOAT

So, I have a couple of questions: 所以,我有几个问题:

  1. How much should I expect the size to reduce after converting a model to .tflite? 将模型转换为.tflite后,我应该期望减少多大的尺寸?
  2. Are there any ways of reducing the size while still being able to convert to a mobile friendly model? 有没有办法缩小尺寸,同时仍然能够转换为移动友好型号? If not, I'm guessing I'll need to convert the mobilenet to work with multi-label classification. 如果没有,我猜我需要将移动网络转换为多标签分类。

Okay, so I've found a way to do it. 好的,所以我找到了办法。 I use the optimized graph (unquantized) and run the following command: 我使用优化图(未量化)并运行以下命令:

tflite_convert --graph_def_file=optimized_graph.pb \
  --output_file=output/optimized_graph_quantized.tflite \
  --output_format=TFLITE \
  --input_shape=1,299,299,3 \
  --input_array=Mul \
  --output_array=final_result \
  --inference_type=QUANTIZED_UINT8 \
  --std_dev_values=128 --mean_values=128 \
  --default_ranges_min=-6 --default_ranges_max=6 \
  --quantize_weights=true

My main concern with the above is that when I don't specify min/max ranges I get the following message: "Array conv, which is an input to the Conv operator producing the output array conv_1, is lacking min/max data, which is necessary for quantization. Either target a non-quantized output format, or change the input graph to contain min/max information, or pass --default_ranges_min= and --default_ranges_max= if you do not care about the accuracy of results." 我主要关心的是,当我没有指定最小/最大范围时,我得到以下消息:“数组转换,它是产生输出数组conv_1的Conv运算符的输入,缺少最小/最大数据,量化是必要的。要么定位非量化的输出格式,要么改变输入图形以包含最小/最大信息,或者如果你不关心结果的准确性,则传递--default_ranges_min =和--default_ranges_max =。

I've changed the tf-for-poets android code to allow me to use the quantized tflite graph (basically the reverse of this - https://github.com/tensorflow/tensorflow/issues/14719 ) and I seem to be getting results that are as good as the original, unquantized graph. 我已经改变了tf-for-poets安卓代码,允许我使用量化的tflite图(基本上与此相反 - https://github.com/tensorflow/tensorflow/issues/14719 )我似乎得到了结果与原始的,未量化的图形一样好。

I solved the same problem using @ChristopherPaterson solution but removing --quantize_weights=true worked for me. 我使用@ChristopherPaterson解决方案解决了同样的问题,但删除了 --quantize_weights=true对我--quantize_weights=true The command is: 命令是:

tflite_convert --graph_def_file=optimized_graph.pb \
  --output_file=output/optimized_graph_quantized.tflite \
  --output_format=TFLITE \
  --input_shape=1,299,299,3 \
  --input_array=Mul \
  --output_array=final_result \
  --inference_type=QUANTIZED_UINT8 \
  --std_dev_values=128 --mean_values=128 \
  --default_ranges_min=-6 --default_ranges_max=6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM