caffe、tf转ncnn

匿名 (未验证) 提交于 2019-12-03 00:32:02

一、安装protobuf

1gitclone https://github.com/google/protobuf

2、自动生成configure配置文件,运行:./autogen.sh

3、配置环境:./configure

4、编译源代码:make

5、安装:sudomake install

6、刷新动态库:sudoldconfig

二、安装ncnn

1mkdircode && cd code

2gitclone https://github.com/Tencent/ncnn

3cdncnn

4mkdirbuild && cd build

5cmake.. (只有安装成功protobuf才能成功)



6make-j


如果遇到这样的错误,geditCmakeLists.txt, 在第一行添加:add_definitions(-std=c++11).

编译成功:


如果要安装tensorflow2ncnn,则需要在code/ncnn/tools/下的CMakeLists.txt中加入add_subdirectory(tensorflow)


7makeinstall



8caffeתncnn

~/code/ncnn/build/tools$./caffe2ncnndeplpy.prototxt alexnet.caffemodel alexnet.param alexnet.bin

加密:

~/code/ncnn/build/tools$./ncnn2mem alexnet.param alexnet.bin alexnet.id.h alexnet.mem.h

成功会在当前文件夹生成.param.bin文件

加载未加密模型:

ncnn::Netnet;

net.load_param("alexnet.param");

net.load_model("alexnet.bin");

加载加密模型:

ncnn::Netnet;

net.load_param_bin("alexnet.param.bin");

net.load_model("alexnet.bin");

9tensorflowתncnn

tensorflow保存的模型分为meta,ckpt,pb等文件,我只试了转pb

先把meta转成pb

importtensorflow as tf

withtf.Session() as sess:

#初始化变量

sess.run(tf.global_variables_initializer())

#获取最新的checkpoint,其实就是解析了checkpoint文件

latest_ckpt= tf.train.latest_checkpoint("./checkpoint")

#加载图

restore_saver= tf.train.import_meta_graph('./checkpoint/Model.meta')

#恢复图,即将weights等参数加入图对应位置中

restore_saver.restore(sess,latest_ckpt)

#将图中的变量转为常量

output_graph_def= tf.graph_util.convert_variables_to_constants(

sess,sess.graph_def , ["node"])

#将新的图保存到"/pretrained/graph.pb"文件中

tf.train.write_graph(output_graph_def,'pretrained', "graph.pb", as_text=False)

最后运行:

~/code/ncnn/build/tools/tensorflow$./tensorflow2ncnn graph.pb

三、安装mxnet

参考:

http://mxnet.incubator.apache.org/install/


文章来源: caffe、tf转ncnn
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!