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