【问题标题】:How to solve Runtime Error: Empty min/max for tensor Cast while doing post-training quantization (fully quantized tflite model from saved_model)?如何解决运行时错误:在进行训练后量化时张量 Cast 的最小值/最大值为空(来自 saved_model 的完全量化的 tflite 模型)?
【发布时间】:2021-03-06 03:48:03
【问题描述】:

我尝试创建完全量化的 tflite 模型,以便能够在珊瑚上运行它。我从https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md下载了SSD MobileNet V2 FPNLite 640x640

我在虚拟环境中安装了 tf-nightly-2.5.0.dev20201123 tf-nightly-models 和 tensorflow/object_detection_0.1

我运行此代码来进行训练后量化

import tensorflow as tf
import cv2
import numpy as np
converter = tf.lite.TFLiteConverter.from_saved_model('./0-ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/saved_model/',signature_keys=['serving_default']) # path to the SavedModel directory

VIDEO_PATH = '/home/andrej/Videos/outvideo3.h264'
def rep_data_gen():
    REP_DATA_SIZE = 10#00
    a = []
    video = cv2.VideoCapture(VIDEO_PATH)
    i=0
    while(video.isOpened()): 
        ret, img = video.read()
        i=i+1
        if not ret or i > REP_DATA_SIZE:
          print('Reached the end of the video!')
          break
        img = cv2.resize(img, (640, 640))#todo parametrize based on network size
        img = img.astype(np.uint8)
        #img = (img /127.5) -1 #
        #img = img.astype(np.float32)#causing types mismatch error
        a.append(img)
    a = np.array(a)
    print(a.shape) # a is np array of 160 3D images
    for i in tf.data.Dataset.from_tensor_slices(a).batch(1).take(REP_DATA_SIZE):
        yield [i]

#tf2 models
converter.allow_custom_ops=True
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = rep_data_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8, tf.lite.OpsSet.SELECT_TF_OPS]
#converter.quantized_input_stats = {'inputs': (0, 255)} #does not help
converter.inference_input_type = tf.uint8  # or tf.uint8
converter.inference_output_type = tf.uint8  # or tf.uint8
quantized_model = converter.convert()

# Save the model.
with open('quantized_model.tflite', 'wb') as f:
  f.write(quantized_model)

我明白了

RuntimeError: Max and min for dynamic tensors should be recorded during calibration: Failed for tensor Cast
Empty min/max for tensor Cast

【问题讨论】:

  • 你能分享 FP lite 模型吗?
  • 不幸的是存档中没有 tflite 模型。
  • 是的,我正在尝试从 saved_model(.pb 扩展名)获取 tflite 模型
  • 能否请您将模型转换为没有量化的 tflite 并分享它,以便我们看看是什么操作导致了问题?还评论converter.allow_custom_ops=True我不确定你是否在运行时实现了自定义操作。

标签: python tensorflow tensorflow-lite google-coral edge-tpu


【解决方案1】:

我使用脚本model_main_tf2.py 训练了相同的模型SSD MobileNet V2 FPNLite 640x640,然后使用脚本exporter_main_v2.py 将检查点导出到saved_model。在尝试转换为“.tflite”以在 Edge TPU 上使用时,我遇到了同样的问题。

我的解决方案是使用脚本export_tflite_graph_tf2.py 而不是exporter_main_v2.py 导出训练模型以生成saved_model.pb。然后转换进行得很顺利。

也许尝试使用export_tflite_graph_tf2.py 生成一个saved_model。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2017-09-09
    • 2021-09-30
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多