如何解决运行时错误:在进行训练后量化时张量Cast的min/max为空(来自saved_model的完全量化的tflit



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

我安装在虚拟环境tf-nightly-2.50.dev20201123 tf夜间模型和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

我使用脚本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。

最新更新