caffez转ncnn,及环境配置

一、安装ncnn

1、安装protobuf

(a)、gitclone https://github.com/google/protobuf

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

(c)、配置环境:./configure

(d)、编译源代码:make

(e)、安装:sudomake install

(f)、刷新动态库:sudoldconfig

2、安装ncnn

(a)、mkdircode && cd code

(b)、gitclone https://github.com/Tencent/ncnn

(c)、cdncnn

(d)、mkdirbuild && cd build

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

(f)、make-j

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

编译成功:

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

 

(g)、makeinstall

二、安装opencv

1、安装Qt5

sudoapt-get install Qt5-default

2、Cmake版本3.8以上

(a)、移除久版本

sudo apt-get autoremove cmake

(b)、文件下载

wget https://cmake.org/files/v3.9/cmake-3.9.1-Linux-x86_64.tar.gz

(c)、创建软链接

mv cmake-3.9.1-Linux-x86_64 /opt/cmake-3.9.1
ln -sf /opt/cmake-3.9.1/bin/*  /usr/bin/ 

(d)、查看版本

cmake --version

3、安装opencv

(a)、安装依赖包

# 安装编译工具
sudo apt-get install build-essential
# 安装依赖包
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
# 安装可选包
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install build-essential
# 安装依赖包
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
# 安装可选包
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

(b)、编译安装

下载:https://opencv.org/releases.html

选择source

解压:

unzipopencv-2.4.13.zip

mvopencv-2.4.13 path

# 打开文件夹"opencv-2.4.13":
cd opencv-2.4.13
# 新建一个文件夹用于存放临时文件:
mkdir release
# 切换到该临时文件夹:
cd release
# 开始编译:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4    # 开启线程 按照自己的配置
sudo make install
cd opencv-2.4.13
# 新建一个文件夹用于存放临时文件:
mkdir release
# 切换到该临时文件夹:
cd release
# 开始编译:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4    # 开启线程 按照自己的配置
sudo make install

(c)、常见错误:

(1)在这个过程中,由于是cuda的64位和32位的问题,会出现nofound nppi...的问题(

CMakeError: Variables are set to NOTFOUND)。

解决方法为:https://stackoverflow.com/questions/46584000/cmake-error-variables-are-set-to-notfound

在opencv-2.4.13/cmake下找到FindCUDA.cmake文件,将:

find_cuda_helper_libs(nppi)

替换为:

 find_cuda_helper_libs(nppial)
  find_cuda_helper_libs(nppicc)
  find_cuda_helper_libs(nppicom)
  find_cuda_helper_libs(nppidei)
  find_cuda_helper_libs(nppif)
  find_cuda_helper_libs(nppig)
  find_cuda_helper_libs(nppim)
  find_cuda_helper_libs(nppist)
  find_cuda_helper_libs(nppisu)
  find_cuda_helper_libs(nppitc)
set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")

替换为:

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")
unset(CUDA_nppi_LIBRARY CACHE)

替换为:

unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)

同时,由于cuda9.0不支持2.0,所以,在该目录下找到OpenCVDetectCUDA.cmake文件:

  ...
  set(__cuda_arch_ptx "")
  if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "2.0")
  elseif(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")
  ...

去掉第一个if句,去掉elseif的else

set(__cuda_arch_bin "2.0 3.0 3.5 3.7 5.0 5.2 6.0 6.1") 

去掉2.0

(2)如果cmake遇到unsupport“compute_20”

nvccfatal : Unsupported gpu architecture 'compute_20'

用以下方式编译:

cmake-D CMAKE_BUILD_TYPE=RELEASE -D CUDA_GENERATION=Kepler ..

 

三、安装mxnet

参考:

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

 

四、tfcaffencnn

1、caffe转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");

 

 

2tensorflowncnn

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

 

 

五、配置java环境

1、安装java

(1)、在终端输入java-version:(查看java版本)

(2)、下载:jdk8

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

(3)、jdk-8u171-linux-x64.tar.gz

(4)、tar-zxvf jdk-8u171-linux-x64.tar.gz

(5)、sudomv jdk1.8.0_171/opt/

(6)、sudovi ~/.bashrc

(7)、在末尾添加:

#setJava environment

exportJAVA_HOME=/opt/jdk1.8.0_171

exportJRE_HOME=${JAVA_HOME}/jre

exportCLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib

exportPATH=${JAVA_HOME}/bin:$PATH

(8)、source~/.bashrc

 

 

2、安装andriodstudio

https://www.linuxidc.com/Linux/2017-08/146066.htm

plugingradle is too old

3、andriodstudio的简单使用

(1)创建支持c/c++和java混编环境

创建一个新项目-→选中includec++ support复选框-→next-→在向导customc++ support部分:a、c++standard:默认cmake设置,b、exceptionssupport:启用对c++异常处理的支持,c、Runtimetype information support:支持RTTI

run后,模拟器上显示“Hellofrom c++”

整个调用过程为:(重要文件:main下的cpp、java、jni,build.gradle,CmakeList.txt)

Gradle(设置ndk)调用CMakelists.txt(里面设置了cpp,jni库文件等),Cmake编译cpp,jni文件:class生成到incremental/calsses/com下,cpp生成so文件在cmake下。

MainActivity用于在模拟器上运行(相当于调用主函数)

 

(2)生成so文件的方法:

https://www.2cto.com/kf/201607/526887.html

(3)不使用mk,编译.a文件:

https://blog.csdn.net/yoyo_newbie/article/details/74427938 主要思路就是在cmakelist中文件add_libraries

(4)更改ndk版本的方法:

File-→Projectstructure-→Andriod Ndk location更改路径即可

(5)创建新的模拟器:

Run-→createnew virtual device-→选择一个机型-→next-→选择机型(otherimage)-->next

 

六、例子:mtcnn

1、新建工程androidstudio的c++混编环境,新建一个项目mtcnn_as

2、配置相关文件:

(a)https://github.com/Tencent/ncnn/releases上下载安卓端lib,

(b)将arm端的.a文件放到jniLibs目录下

(c)include的头文件放到cpp下

(d)mtcnn的c++的接口文件放在cpp下

(e)MTCNN的Java类放到java下

3、CMakeList.txt

cmake_minimum_required(VERSION 3.4.1)

#include头文件目录
include_directories(src/main/cpp/include
                    src/main/cpp/)

#source directory源文件目录
file(GLOB MTCNN_SRC src/main/cpp/*.h
                    src/main/cpp/*.cpp)
set(MTCNN_COMPILE_CODE ${MTCNN_SRC})

#添加ncnn
add_library(libncnn STATIC IMPORTED )
set_target_properties(libncnn
  PROPERTIES IMPORTED_LOCATION
  ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libncnn.a)

#编译为动态库
add_library(mtcnn SHARED ${MTCNN_COMPILE_CODE})

#添加工程所依赖的库
find_library(  log-lib log )
target_link_libraries(  mtcnn
                       libncnn
                       jnigraphics
                       z
                       ${log-lib} )

编译出现错误:Linker。。。或者Buildcommand failed.(1、改变NDK版本r15,2、在build.gradle下arguments"-DANDROID_TOOLCHAIN=clang")

4、编译成功后编辑MainActivity.jav

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值