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

I use TFLiteConvert post_training_quantize=True but my model is still too big for being hosted in Firebase ML Kit's Custom servers

我写了一个 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 来帮助输出比我原来的 .h5 模型(159MB)更轻的 .tflite 模型。然而,生成的 .tflite 模型仍然是 159MB。

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

我怎么可能:

相关问题

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/". Here you have to load the model and check the random layers having weight.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=[min_value,max_value]
converter.quantized_input_stats={"name_of_the_input_layer_for_your_model":[平均值,标准]}

希望你处理的是图像。
min_value=0,max_value=255,mean=128(主观)和 std=128(主观)。
name_of_the_input_layer_for_your_model= 加载图表时的名字上面提到的模型link或者你可以通过代码"model.input"得到输入层的名字,就会得到输出"tf.Tensor 'input_1:0' shape=(?, 224, 224, 3) dtype=float32"。这里的 input_1 是输入层的名称(注意:模型必须包括图形配置和权重。)