tensorflow模型转ncnn模型

  ncnn本来是有tensorflow2ncnn的工具,但是在5月份时候被删除,原因是很多算子不支持,使用过程中很多bug,作者nihui直接将该功能删除。但是,tensorflow是目前最popular的深度学习框架,因此tensorflow转ncnn的需求还是必不可少的需求。下面提供一种将tensorflow转换为ncnn的一种解决方案。

感谢: https://github.com/Tencent/ncnn/issues/5

下面提供一个tf转ncnn的方法,在基于MobileNetV2修改的模型上测试通过,模型输出正确

tf2ncnn有很多op不支持,这里尝试tf2coreml2onnx2ncnn的方法进行解决

1、使用freeze_graph.py生成.pb模型;

2、使用tf-coreml将tf模型转换为coreml模型

例子:

import tfcoreml as tf_converter

tf_converter.convert(tf_model_path = './pfld.pb', mlmodel_path = 'my_model.mlmodel', output_feature_names = ['PFLDnet/fc8_1/BiasAdd:0'])

3、使用[WinMLTool]将coreml转换为onnx模型

https://docs.microsoft.com/zh-cn/windows/ai/windows-ml/convert-model-winmltools

from coremltools.models.utils import load_spec
# Load model file
model_coreml = load_spec('my_model.mlmodel')
from winmltools import convert_coreml
# Convert it!
# The automatic code generator (mlgen) uses the name parameter to generate class names.
model_onnx = convert_coreml(model_coreml, 7, name='ExampleModel')  

from winmltools.utils import save_model
# Save the produced ONNX model in binary format
save_model(model_onnx, 'example.onnx')

 

4、编译ncnn/toold/onnx,使用onnx2ncnn将onnx模型转换为ncnn

备注:ncnn/onnx2ncnn不支持onnx op Affine,而tf op Relu6 会被转换为Relu和Affine,使用Relu替换Relu6即可

 

posted @ 2019-06-17 15:55  qiuqiu1577469804  阅读(4947)  评论(4编辑  收藏  举报