减小 TFLite 模型大小?
Reducing TFLite model size?
我目前正在按照本指南制作多标签图像分类模型(它使用 inception 作为基础模型):https://towardsdatascience.com/multi-label-image-classification-with-inception-net-cbb2ee538e30
从 .pb
转换为 .tflite
后,模型仅缩小了大约 0.3mb。
这是我的转换代码:
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
所以,我有几个问题:
- 在将模型转换为 .tflite 后,我预计尺寸会减少多少?
- 是否有任何方法可以在减小尺寸的同时仍然能够转换为适合移动设备的模型?如果没有,我猜我需要将 mobilenet 转换为使用多标签分类。
好的,所以我找到了一种方法。我使用优化图(未量化)和 运行 以下命令:
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
我对上述内容的主要担心是,当我未指定 min/max 范围时,我收到以下消息:"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."
我已经更改了 tf-for-poets android 代码以允许我使用量化的 tflite 图(基本上与此相反 - https://github.com/tensorflow/tensorflow/issues/14719)并且我似乎得到了结果与原始的未量化图表一样好。
我使用@ChristopherPaterson 解决方案解决了同样的问题,但 删除 --quantize_weights=true
对我有用。命令是:
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
我目前正在按照本指南制作多标签图像分类模型(它使用 inception 作为基础模型):https://towardsdatascience.com/multi-label-image-classification-with-inception-net-cbb2ee538e30
从 .pb
转换为 .tflite
后,模型仅缩小了大约 0.3mb。
这是我的转换代码:
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
所以,我有几个问题:
- 在将模型转换为 .tflite 后,我预计尺寸会减少多少?
- 是否有任何方法可以在减小尺寸的同时仍然能够转换为适合移动设备的模型?如果没有,我猜我需要将 mobilenet 转换为使用多标签分类。
好的,所以我找到了一种方法。我使用优化图(未量化)和 运行 以下命令:
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
我对上述内容的主要担心是,当我未指定 min/max 范围时,我收到以下消息:"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."
我已经更改了 tf-for-poets android 代码以允许我使用量化的 tflite 图(基本上与此相反 - https://github.com/tensorflow/tensorflow/issues/14719)并且我似乎得到了结果与原始的未量化图表一样好。
我使用@ChristopherPaterson 解决方案解决了同样的问题,但 删除 --quantize_weights=true
对我有用。命令是:
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