首页 > 解决方案 > TensorFlow MTCNN 模型能否转成 TensorFlow Lite 格式?

问题描述

我正在尝试将 MTCNN 模型(https://github.com/blaueck/tf-mtcnn/blob/master/mtcnn.pb)从 .pb 文件转换为 .tflite 并遇到输入和输出形状的问题。原始输入形状为 ?x?x3,输出形状为 Nx4,其中 N 是检测到的人脸数量。

我尝试将输入形状设置为 [None, None, 3] 并得到错误"None is only supported in the 1st dimension"。然后我将它设置为 [500, 500, 3] 并得到其他错误"Check failed: batch == 1 (500 vs. 1)"。然后我将形状设置为 [1, 500, 500, 3] 并得到“ValueError:张量'输入'的形状不能从 (?, ?, 3) 更改为 [1, 500, 500, 3] . 形状必须是等阶的,但为 3 和 4"

UPD:我已将原始 caffe 模型从输入形状 [None, None, 3] 转换为 [500, 500, 3] 但这并不能解决问题。

我想将此模型转换为 .tflite 格式。我真的可以这样做吗?

标签: pythontensorflowtensorflow-lite

解决方案


converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8  # or tf.int8
converter.inference_output_type = tf.uint8  # or tf.int8
tflite_model = converter.convert()


with open('8bit_quantized_model_in_.tflite', 'wb') as f:
  f.write(tflite_model)

def representative_dataset():
    for i in range(<>):
      testimage = cv2.imread('path')
      yield [testimage.astype(np.float32)]            

推荐阅读