记caffe的安装和open_nsfw项目的部署
open_nsfw_web 安装文档
caffe的python环境安装前准备
所需文件列表
~ require_softwear/
Anaconda2-4.3.1-Linux-x86_64.sh
boost_1_63_0.tar.gz
caffe-master.zip
gflags-2.1.2.tar.gz
glog-0.3.4.tar.gz
lmdb-LMDB_0.9.18.tar.gz
open_nsfw_web.zip
opencv.tar.gz
protobuf-cpp-3.1.0.tar.gz
Python-2.7.8.tar.xz
setuptools-1.4.2.tar.gz
查看系统版本
[[email protected] ~]# cat /proc/version
Linux version 2.6.32-504.12.2.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Mar 11 22:03:14 UTC 2015
版本要求
- OpenCV >= 2.4 including 3.0
- Python >= 2.7
- Boost >= 1.55
安装前准备
将require_softwear
目录下的所有文件放到/opt
目录下面
caffe的python环境安装
安装Python2.7
1 进入/opt
目录,解压Python-2.7.8.tar.xz
[[email protected] ~]# tar -xvf Python-2.7.8.tar.xz
如果出现下面信息:
[[email protected] opt]# tar -xvf Python-2.7.8.tar.xz
tar (child): xz:无法 exec: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
说明需要安装 xz
,安装 xz
:
[[email protected] opt]# yum install xz
然后执行以下命令安装Python-2.7.8
[[email protected] opt]# tar -xvf Python-2.7.8.tar.xz
[[email protected] opt]# cd Python-2.7.8
[[email protected] Python-2.7.8]# ./configure --prefix=/usr/local/Python2.7 --enable-shared cFLAGS=-fPIC
[[email protected] Python-2.7.8]# make
[[email protected] Python-2.7.8]# make install
安装完成后,查看当前的python
[[email protected] Python-2.7.8]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
\>>>
发现指现的依然是旧版本(Python2.6.6版本
),建立新版本的软链接:
[[email protected] ~]# ln -s /usr/local/Python2.7/bin/python2.7 /usr/bin/python
ln: 创建符号链接 "/usr/bin/python": 文件已存在
发现链接已存在,当然是旧版本的啦。这时mv
掉/usr/bin/python
:
[[email protected] ~]# mv /usr/bin/python /tmp
[[email protected] ~]# ln -s /usr/local/Python2.7/bin/python2.7 /usr/bin/python
此时再查看一下python
,发现:
[[email protected] ~]# python
python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
这是因为编译Python
时加了--enable-shared
,此时在/etc/ld.so.conf
中加入/usr/local/Python2.7/lib
[[email protected] ~]# vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/Python2.7/lib
然后执行以下命令:
[[email protected] ~]# /sbin/ldconfig
这时再查看python
,发现是新版本了:
[[email protected] ~]# python
Python 2.7.8 (default, Apr 18 2017, 17:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
\>>>
修复yum
这个时候运行yum
,会得到如下信息:
[[email protected] ~]# yum
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
No module named yum
Please install a package which provides this module, or
verify that the module is installed correctly.
It's possible that the above module doesn't match the
current version of Python, which is:
2.7.8 (default, Apr 18 2017, 17:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
这是因为yum
与python2.7
不兼容的缘故,此时需设置yum
继续使用python2.6
:
[[email protected] ~]# which yum
/usr/bin/yum
[[email protected] ~]# vim /usr/bin/yum
把第一行的
#!/usr/bin/python
修改为
#!/usr/bin/python2.6
安装pip
[[email protected] python]# curl https://bootstrap.pypa.io/get-pip.py \| python -
pip
安装后路径为/usr/local/Python2.7/bin/pip
把
export PATH="/usr/local/Python2.7/bin:$PATH"
添加到/etc/profile
文件的最后一行,然后使环境变量生效:
[[email protected] python]# source /etc/profile
安装Anaconda
[[email protected] ~]# cd /opt
[[email protected] ~]# bash Anaconda2-4.3.1-Linux-x86_64.sh
一路按回车默认安装,默认安装路径为/root/anaconda2
在/etc/ld.so.conf
中加入/root/anaconda2/lib
[[email protected] ~]# vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/Python2.7/lib
/root/anaconda2/lib
然后执行以下命令:
[[email protected] ~]# /sbin/ldconfig
安装ATLAS
[[email protected] opt]# yum install atlas-devel
安装glog
[[email protected] opt]# tar -xvf glog-0.3.4.tar.gz
[[email protected] opt]# cd glog-0.3.4
[[email protected] glog-0.3.4]# ./configure
[[email protected] glog-0.3.4]# make && make install
安装cmake
[[email protected] build]# yum install cmake
安装gflags
[[email protected] opt]# tar -xvf gflags-2.1.2.tar.gz
[[email protected] opt]# cd gflags-2.1.2
[[email protected] gflags-2.1.2]# mkdir build && cd build
[[email protected] build]# export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1
[[email protected] build]# make && make install
安装lmdb
[[email protected] opt]# tar -xvf lmdb-LMDB_0.9.18.tar.gz
[[email protected] opt]# cd lmdb-LMDB_0.9.18/libraries/liblmdb
[[email protected] liblmdb]# make && make install
安装opencv2
[[email protected] opt]# tar -xvf opencv.tar.gz
[[email protected] opt]# cd opencv
[[email protected] opencv]# mkdir release && cd release
[[email protected] release]# cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
[[email protected] release]# make
[[email protected] release]# make install
安装boost
[[email protected] opt]# tar -xvf boost_1_63_0.tar.gz
[[email protected] opt]# cd boost_1_63_0
[[email protected] boost_1_63_0]# ./bootstrap.sh --prefix=/usr/local
[[email protected] boost_1_63_0]# ./b2
[[email protected] boost_1_63_0]# ./b2 install
安装protobuf
[[email protected] opt]# tar -xvf protobuf-cpp-3.1.0.tar.gz
[[email protected] opt]# cd protobuf-3.1.0/
[[email protected] protobuf-3.1.0]# yum install autoconf automake libtool curl make g++ unzip
[[email protected] protobuf-3.1.0]# ./autogen.sh
[[email protected] protobuf-3.1.0]# ./configure
[[email protected] protobuf-3.1.0]# make
[[email protected] protobuf-3.1.0]# make check
[[email protected] protobuf-3.1.0]# make install
[[email protected] protobuf-3.1.0]# ldconfig
安装hdf5
[[email protected] caffe-master]# yum install hdf5-devel
安装epel-release
[[email protected] caffe-master]# yum install epel-release
安装leveldb-devel
[[email protected] caffe-master]# yum install leveldb-devel
安装snappy-devel
[[email protected] caffe-master]# yum install snappy-devel
安装caffe
[[email protected] opt]# unzip caffe-master.zip
如果没有unzip
,先安装unzip
:
[[email protected] opt]# yum install unzip
然后再
[[email protected] opt]# unzip caffe-master.zip
[[email protected] opt]# cd caffe-master
[[email protected] caffe-master]# cp Makefile.config.example Makefile.config
修改Makefile.config
配置文件
[[email protected] caffe-master]# vim Makefile.config
把
# CPU_ONLY := 1
注释去掉,修改后为
CPU_ONLY := 1
把
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas
注释去掉,并修改为
BLAS_INCLUDE := /usr/include
BLAS_LIB := /usr/lib64/atlas
把
# ANACONDA_HOME := $(HOME)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
# $(ANACONDA_HOME)/include/python2.7 \
# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
注释去掉,并把anaconda
改为anaconda2
,修改后为
ANACONDA_HOME := $(HOME)/anaconda2
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
开始编译:
[[email protected] caffe-master]# make all
[[email protected] caffe-master]# make test
[[email protected] caffe-master]# make runtest
此时提示:
[[email protected] caffe-master]# make runtest
.build_release/tools/caffe
.build_release/tools/caffe: error while loading shared libraries: libglog.so.0: cannot open shared object file: No such file or directory
make: *** [runtest] 错误 127
找到libglog.so.0
文件的位置
[[email protected] caffe-master]# find / -name libglog.so.0
/opt/glog-0.3.4/.libs/libglog.so.0
/usr/local/lib/libglog.so.0
把
/usr/local/lib
加入到/etc/ld.so.conf
文件中
文件/etc/ld.so.conf
修改前为:
include ld.so.conf.d/*.conf
/usr/local/Python2.7/lib
修改后为:
include ld.so.conf.d/*.conf
/usr/local/Python2.7/lib
/usr/local/lib
然后执行以下命令:
[[email protected] caffe-master]# /sbin/ldconfig
继续执行下面命令编译caffe
[[email protected] caffe-master]# make runtest
安装caffe
的pycaffe
接口模块
[[email protected] caffe-master]# cd python
[[email protected] python]# for req in $(cat requirements.txt); do pip install $req; done
如果出现以下信息
...
...
/usr/bin/ld: cannot find -lpython2.7
collect2: ld 返回 1
error: command 'g++' failed with exit status 1
...
...
则执行
[[email protected] python]# ln -s /usr/local/Python2.7/lib/python2.7/config/libpython2.7.a /usr/local/lib/
[[email protected] python]# for req in $(cat requirements.txt); do pip install $req; done
[[email protected] caffe-master]# make pycaffe
完成后测试caffe
模块
[[email protected] caffe-master]# python
Python 2.7.8 (default, Apr 18 2017, 17:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
\>>> import caffe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named caffe
\>>>
提示没有caffe
模块,是因为没有把caffe
模块加入到环境变量。
把caffe
模块的路径添加到当前用用的环境变量~/.bash_profile
文件里面
把
export PYTHONPATH=/opt/caffe-master/python:$PYTHONPATH
添加到~/.bash_profile
文件的最后一行。
使环境变量生效
[[email protected] caffe-master]# source ~/.bash_profile
再次测试caffe的python模块是否已安装成功
[[email protected] python]# python
Python 2.7.8 (default, Apr 18 2017, 17:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
\>>> import caffe
\>>>
信息提示已安装成功。
至此,caffe的python环境安装工作已结束。
open_nsfw
web程序的安装
[[email protected] opt]# cd /opt/
[[email protected] opt]# unzip open_nsfw_web.zip
[[email protected] open_nsfw_web]# pip install -r requirements.txt
启动web服务
[[email protected] open_nsfw_web]# python app.py &
测试web服务
[[email protected] ~]# curl http://127.0.0.1:5000/classify?imageurl=test_data/1.jpg
返回
{
"imageurl": "test_data/1.jpg",
"result": true,
"score": "0.788449168205",
"times": "0.368"
}
说明web服务已启动成功。
web服务接口说明
http://xx.xx.xx.xx:5000?classify?imageurl=图片路径
正确时返回格式为:
{
"imageurl": "test_data/1.jpg",
"result": true,
"score": "0.788449168205",
"times": "0.363"
}
错误时返回格式为:
{
"err": "check image path and only support png,bmp,jpg,jpe,jpeg,gif format.",
"imageurl": "test_data/21.jpg",
"result": false
}
2.
http://xx.xx.xx.xx:5000
参考链接
http://caffe.berkeleyvision.org/installation.html
http://caffe.berkeleyvision.org/install_yum.html