【问题标题】:Can the TensorFlow MTCNN model converted to the TensorFlow Lite format?TensorFlow MTCNN 模型可以转换成 TensorFlow Lite 格式吗?
【发布时间】:2019-03-25 18:11:10
【问题描述】:

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

我尝试将输入形状设置为 [None, None, 3] 并得到错误 “仅在第 1 维中不支持”。然后我将其设置为 [500, 500, 3] 并得到其他错误 “检查失败:批处理 == 1 (500 vs. 1)”。然后我将形状设置为 [1, 500, 500, 3] 并得到 "ValueError: The shape of tensor 'input' cannot be changed from (?, ?, 3) to [1, 500, 500 , 3]. 形状必须是等阶的,但是是 3 和 4".

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

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

【问题讨论】:

    标签: python tensorflow tensorflow-lite


    【解决方案1】:
    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)]            
    

    【讨论】:

    • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的回答添加解释并说明适用的限制和假设。
    • 这不起作用,因为输入是冻结图,而 API 用于saved_model
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多