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即可

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值