caffe+水印识别部署

 1.准备

cuda 8.0 注意环境配置,动态库及bin启动文件

cudnn 解压匹配的tgz包,拷贝到系统配置路径,并授权

cmake 安装3.12.3版本,适应软件编译版本要求

java + ant 配置jvm环境,便于部署及后续opencv执行jar的生成

opencv 2.4.11 gpu版本

openblas caffe准备

caffe 修改makefile.config 文件,gpu版本

jsoncpp 后续水印识别依赖包

logo_detect 校验环境配置

2.cmake安装

# 安装gcc等必备程序包
yum install -y gcc gcc-c++ make automake
# 获取安装包并解压
# 进入安装目录,此处为 cmake-3.12.3.tar.gz
./bootstrap
gmake
gmake install

3.cuda与cudnn的安装

两者的安装直接按照官网步骤即可,注意/etc/profile中的相关配置,如果指定cuda的bin路径与lib64在/usr/local下的软链接下,注意判断是否匹配

此处需特别注意,在安装时需指定版本 , 实操如下(此处为本机ubuntu16.04 安装命令):

sudo dpkg -i cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda-9-0 cuda-libraries-9-0

cudnn安装:

# 下载 cudnn的tgz压缩包,解压并执行如下命令
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

4.opencv安装

因此处的OpenCV不止是后续caffe的依赖项,也将用于Java web项目,故需要安装Java环境,以及ant构建OpenCV项目并生成执行jar。

java与ant的安装不再赘述,解压并配置环境变量即可,下为OpenCV安装:

# 解压opencv,创建并进入build目录
mkdir build && cd build
# 构建makefile编译依赖环境
cmake ..
make -j8
make install

注意,安装gpu版本有时会报以下错误:

nvcc fatal   : Unsupported gpu architecture 'compute_11'
CMake Error at cuda_compile_generated_matrix_operations.cu.o.cmake:206 (message):
  Error generating
/home/smie/Documents/opencv2.4.11/build/modules/core/CMakeFiles/cuda_compile.dir/__/dynamicuda/src/cuda/./cuda_compile_gene

rated_matrix_operations.cu.o

make[2]: ***
[modules/core/CMakeFiles/cuda_compile.dir/__/dynamicuda/src/cuda/./cuda_compile_generated_matrix_operations.cu.o] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs....

解决方案如下:

# 使用cmake重新构建编译环境
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..

因本机安装的OpenCV为2.4.11,cuda版本为8.0,编译报错,处理方案来源:https://www.cnblogs.com/jessezeng/p/7018267.html

/data/opencv-2.4.11/modules/gpu/src/graphcuts.cpp:120:54: error: ‘NppiGraphcutState’ has not been declared  
      typedef NppStatus (*init_func_t)(NppiSize oSize, NppiGraphcutState** ppStat  
                                                       ^  
 /data/opencv-2.4.11/modules/gpu/src/graphcuts.cpp:135:18: error: ‘NppiGraphcutState’ does not name a type  
          operator NppiGraphcutState*()  
                   ^  
 /data/opencv-2.4.11/modules/gpu/src/graphcuts.cpp:141:9: error: ‘NppiGraphcutState’ does not name a type  
          NppiGraphcutState* pState; 

cuda8.0较新,opencv-2.4.11较早,要编译通过需要修改源码:

修改modules/gpu/src/graphcuts.cpp

将  

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)   

改为  

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000) 

重新编译即可。

5.openblas安装

caffe依赖blas(BLAS(Basic Linear Algebra Subprograms)是一组线性代数计算中通用的基本运算操作函数集合),Linux本身已自带有atlas,但安装时会报错:

/usr/bin/ld: cannot find -lcblas 
/usr/bin/ld: cannot find -latlas 
collect2: error: ld returned 1 exit status 
make: * [.build_release/lib/libcaffe.so.1.0.0-rc3] Error 1

此处参考博客:https://blog.csdn.net/iotlpf/article/details/74669503,沿用其中的处理方式,安装openblas

git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
make -j8
make install

6.jsoncpp安装

安装步骤如下:

git clone https://github.com/open-source-parsers/jsoncpp.git
cd jsoncpp
mkdir -p build/debug
cd build/debug
cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../../
make 
make install

7.caffe安装

依赖项安装:

yum install epel-release -y
yum install protobuf-devel leveldb-devel snappy-devel opencv-devel boost-devel hdf5-devel -y
yum install gflags-devel glog-devel lmdb-devel -y
yum install atlas-devel -y

配置Makefile.config,因为使用了openblas以及cuda+cudnn,直接上本地配置:

## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
  USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers
  USE_OPENCV := 1
# USE_LEVELDB := 0
# USE_LMDB := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#    You should not set this flag if you will be reading LMDBs with any
#    possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3
# OPENCV_VERSION := 3

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH :=     -gencode arch=compute_30,code=sm_30 
        -gencode arch=compute_35,code=sm_35 
        -gencode arch=compute_50,code=sm_50 
        -gencode arch=compute_52,code=sm_52 
        -gencode arch=compute_60,code=sm_60 
        -gencode arch=compute_61,code=sm_61 
        -gencode arch=compute_61,code=compute_61

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /opt/OpenBLAS/include
BLAS_LIB := /opt/OpenBLAS/lib

# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib

# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 
        /usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include 
        # $(ANACONDA_HOME)/include/python2.7 
        # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m 
#                 /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib

# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib

# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib

# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1

# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1

# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0

# enable pretty build (comment to see full commands)
Q ?= @

直接编译,随后配置环境变量即可:

make distribute -j16

 8.去水印项目

首先需注意Makefile文件中的相关配置,尤其是新机器各种环境变量可能的变量需要特别注意。

我这里报错:device_alternate.hpp:34:23: fatal error: cublas_v2.h: No such file or direct,因为device_alternate.cpp是caffe的一个文件,一直怀疑是caffe安装有问题,查看无误后,断定是Makefile定义的环境变量有错,因为其中重写了cuda的路径,且该路径在当前机器有变动,导致找不到cuda的cublas_v2.h文件,重新配置路径即可。

原文地址:https://www.cnblogs.com/nyatom/p/11396031.html