python - 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 格式。我真的可以这样做吗?
解决方案
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)]
推荐阅读
- python - Python - 对不同大小的列表进行多次迭代
- javascript - 即使在 Expo CLI 中修复了问题后,React Native 中的黄色警告选项卡是否应该显示?
- javascript - 寻找一种在函数内操作 gatsby 中的 css 的方法
- azure-devops - 在 Azure DevOps 中执行测试用例
- algorithm - 扫描线填充优化可以扩展到 3D 吗?
- python - 有没有办法使用 geopandas/shapely/fiona 来获得多边形的最高点?
- python - PDB retrieve_pdb_file 方法的“所需结构不存在”
- javascript - 尝试将 EmailJS 与 React 一起使用时服务 ID 无效
- kubernetes-ingress - 入口服务器片段
- amazon-web-services - Terraform 格式错误的策略