python - 我使用 TFLiteConvert post_training_quantize=True 但我的模型仍然太大,无法托管在 Firebase ML Kit 的自定义服务器中

标签 python firebase tensorflow keras tensorflow-lite

我写了一个 TensorFlow/Keras Super-Resolution GAN。我已将经过训练的 .h5 模型转换为 .tflite 模型,使用以下代码,在 Google Colab 中执行:

import tensorflow as tf
model = tf.keras.models.load_model('/content/drive/My Drive/srgan/output/srgan.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.post_training_quantize=True
tflite_model = converter.convert()
open("/content/drive/My Drive/srgan/output/converted_model_quantized.tflite", "wb").write(tflite_model)

如您所见,我使用了 converter.post_training_quantize=True,它经过审查以帮助输出比我的原始 大小更轻的 .tflite 模型。 h5 模型,159MB。然而,生成的 .tflite 模型仍然是 159MB。

它太大了,我无法在 Google Firebase 控制台中将它上传到 Google Firebase 机器学习套件的服务器。

我怎么可能:

  • 减小当前 .tflite 模型的大小为 159MB(例如使用工具),

  • 或者在删除当前 159MB 的 .tflite 模型之后,将 .h5 模型转换为更轻的 .tflite模型(例如使用工具)?

相关问题

How to decrease size of .tflite which I converted from keras :没有答案,但有一条评论告诉我们使用 converter.post_training_quantize=True。但是,正如我所解释的,此解决方案似乎不适用于我的情况。

最佳答案

一般来说,量化意味着从 dtype float32 转换为 uint8。所以理论上我们的模型应该减少 4 的大小。这将在更大的文件中清晰可见。

使用工具“https://lutzroeder.github.io/netron/”检查您的模型是否已量化。在这里你必须加载模型并检查具有权重的随机层。 The quantized graph contains the weights value in uint8 format In unquantized graph the weights value will be in float32 format.

仅设置“converter.post_training_quantize=True”不足以量化您的模型。其他设置包括:
converter.inference_type=tf.uint8
converter.default_ranges_stats=[最小值,最大值]
converter.quantized_input_stats={"name_of_the_input_layer_for_your_model":[mean,std]}

希望你正在处理图像。
min_value=0, max_value=255, mean=128(subjective) and std=128(subjective).
name_of_the_input_layer_for_your_model= 在上述链接中加载模型时图表的名字,或者您可以获得通过代码“model.input”的输入层将给出输出“tf.Tensor 'input_1:0' shape=(?, 224, 224, 3) dtype=float32”。这里的input_1是输入层的名称(注意:model必须包含graph configuration和weight。)

关于python - 我使用 TFLiteConvert post_training_quantize=True 但我的模型仍然太大,无法托管在 Firebase ML Kit 的自定义服务器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57631313/

相关文章:

python 3 Tkinter : NameError with Entry widget: name 'Entry' is not defined

python - 使用换行符查找字符串的其余部分 Python

python - 如何在 Python 中提取 XML 属性的值?

android - Firebase 仅在某些设备上进行身份验证?

python - Tensorflow:使用预训练的初始模型

python - 替换未知的先验组数 - 正则表达式 python

java - 服务器到 Firebase HTTP POST 结果为响应消息 200

firebase - 如何区分使用 Firebase 进行 flutter 身份验证的异常?

python - 有选择地迭代张量

python - TensorFlow - 一次读取 TFRecords 中的所有示例?