caffe+水印識別部署

 1.準備

cuda 8.0 注意環境配置,動態庫及bin啓動文件html

cudnn 解壓匹配的tgz包,拷貝到系統配置路徑,並受權java

cmake 安裝3.12.3版本,適應軟件編譯版本要求python

java + ant 配置jvm環境,便於部署及後續opencv執行jar的生成c++

opencv 2.4.11 gpu版本git

openblas caffe準備github

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

jsoncpp 後續水印識別依賴包shell

logo_detect 校驗環境配置json

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下的軟連接下,注意判斷是否匹配bootstrap

此處需特別注意,在安裝時需指定版本 , 實操以下(此處爲本機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,編譯報錯,處理方案來源:http://www.noobyard.com/article/p-uhddulsy-gp.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文件,從新配置路徑便可。