TensorFlow MTCNN模型可以转换为TensorFlow Lite格式吗?

时间:2019-03-25 18:11:10

标签: python tensorflow tensorflow-lite

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

我试图将输入形状设置为[None,None,3]并得到错误“仅在第一个尺寸上不支持” 。然后将其设置为[500,500,3]并得到其他错误“检查失败:批处理== 1(500对1)” 。然后,我将形状设置为[1,500,500,3]并得到“ ValueError:张量“输入”的形状不能从(?,?,3)更改为[1,500,500] ,3]。形状必须相等,但高度必须为3和4“

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

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

2 个答案:

答案 0 :(得分:0)

逐步介绍:

使用由caffe实施的官方zhang mtcnn dr回购: https://github.com/kpzhang93/MTCNN_face_detection_alignment

在将其转换为以下链接的张量流模型之后: https://github.com/ethereon/caffe-tensorflow

(即使您可以预先转换以下回购这样的模型:) https://github.com/AITTSMD/MTCNN-Tensorflow https://github.com/wangbm/MTCNN-Tensorflow (但我强烈建议您自己动手做

最后,您可以从Google文档将tf模型转换为tf lite: https://www.tensorflow.org/lite/convert

答案 1 :(得分:-2)

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)]