If you want to use this in python3
then you can follow the following steps
Requirements
make, g++, boost-python3.5
Installation of Boost for Python3
Make sure you have the libboost-python-dev libraries installed: sudo apt-get install libboost-python-dev
#####Install python 3.5 :
Make sure you have the libboost-python-dev libraries installed:
#####Download the 1.58.0 不要最新的会有问题!! of Boost from http://www.boost.org/
#####Run bootstrap with correct flags :
#####Compile Boost in directory :
#####Update the Makefile to take the new boost locations (Already did)
Boost.python的编译安装
- 说明:因为caffe需要boost的支持,在卸载以后所有boost文件都需要通过安装(对于非boost.python来说,安装就是将头文件拷贝到PREFIX路径[默认为/usr/local]),不只boost.python(boost.python不是headers-only文件,需要单独build)
step1:源码文件的下载和解压
首先,按照boost官网下载指引描述,到所示网站下载boost源码的压缩文件,当前时间的文件是boost_1_66_0.tar.bz2
,放在$HOME目录下。
~$ tar --bzip2 -xvf boost_1_66_0.tar.bz2
~$ cd boost_1_66_0
~/boost_1_66_0$ ./bootstrap.sh
#返回信息如下
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2
Detecting Python version... 2.7
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... not found.
Backing up existing Boost.Build configuration in project-config.jam.1
Generating Boost.Build configuration in project-config.jam... #配置文件名为project-config.jam
Bootstrapping is done. To build, run:
./b2 #通常的下一步操作
To adjust configuration, edit 'project-config.jam'.
Further information:
- Command line help:
./b2 --help #如果需要更详细的说明,运行左边的命令后再根据需要修改自己的命令
- Getting started guide:
http://www.boost.org/more/getting_started/unix-variants.html
- Boost.Build documentation:
http://www.boost.org/build/doc/html/index.html
我们应关注上述返回信息,包括配置文件名和一些使用提示。
step2:重新编译boost
这一步是为了保险起见,将所有boost相关文件都重新编译,运行一下命令:
~/boost_1_66_0$ ./b2 -a
关注shell返回信息中,建议将路径1加入#include path,将路径2加入#compiler path,以下根据指引设置环境变量。
step3:设置环境变量
~$ sudo gedit ~/.profile
#根据上一步shell中返回的信息,在打开的文件中加入如下两行信息
export BOOST_INCLUDE=$HOME/boost_1_66_0
export BOOST_LIB=$HOME/boost_1_66_0/stage/lib
#保存后关闭文件
~$source ~/.profile
step4:验证安装boost成功
对于我来说,因为caffe是需要boost支持的,验证方式是在caffe文件夹下重新编译Makefile.config,依次make all
,make test
,make runtest
直至最后测试完全通过。
当然简单的方式,应该是调用库,运行一小段代码呗。
~$ touch sample.cpp
~$ sudo gedit sample.cpp
向打开的文件中填入以下代码
#include <string>
#include <iostream>
#include <boost/version.hpp>
#include <boost/timer.hpp>
using namespace std;
int main()
{
boost::timer t;
cout << "max timespan: " << t.elapsed_max() / 3600 << "h" << endl;
cout << "min timespan: " << t.elapsed_min() << "s" << endl;
cout << "now time elapsed: " << t.elapsed() << "s" << endl;
cout << "boost version" << BOOST_VERSION <<endl;
cout << "boost lib version" << BOOST_LIB_VERSION <<endl;
return 0;
}
~$ g++ sample.cpp -o sample.out #编译生成sample.out文件
~$ ./sample.out #查看输出
得到以下输出
max timespan: 2.56205e+09h
min timespan: 1e-06s
now time elapsed: 0.000135s
boost version105800#好吧,真的是没有卸载干净....
boost lib version1_58
附上makefile
附上makefile
# update the path variables
CC = g++
CFLAGS = -W -Wall -O2 -DNDEBUG
#CFLAGS = -W -Wall -g
LIB_ROOT = denseinference/lib/
all:
make -C clean
make -C python
clean:
rm -f *.a
rm -f *.o
rm -f *.so
libDenseCRF.a: libDenseCRF/bipartitedensecrf.cpp libDenseCRF/densecrf.cpp libDenseCRF/filter.cpp libDenseCRF/permutohedral.cpp libDenseCRF/util.cpp libDenseCRF/densecrf.h libDenseCRF/fastmath.h libDenseCRF/permutohedral.h libDenseCRF/sse_defs.h libDenseCRF/util.h
$(CC) -fPIC libDenseCRF/bipartitedensecrf.cpp libDenseCRF/densecrf.cpp libDenseCRF/filter.cpp libDenseCRF/permutohedral.cpp libDenseCRF/util.cpp -c $(CFLAGS) -DNDEBUG
ar rcs libDenseCRF.a bipartitedensecrf.o densecrf.o filter.o permutohedral.o util.o
dense_inference.so: dense_inference.o libDenseCRF.a
$(CC) -shared -I/usr/include/python2.7 -Wl,-no-undefined -o dense_inference.so dense_inference.o -lpython2.7 -lboost_python -L. -lDenseCRF $(CFLAGS)
dense_inference.o: refine_3d/dense_inference.cpp refine_3d/dense_inference.h libDenseCRF.a
$(CC) -c -fPIC -I/usr/include/python2.7 refine_3d/dense_inference.cpp -o dense_inference.o $(CFLAGS) -lDenseCRF $(CFLAGS)
# For python3 use the following \
dense_inference.so: dense_inference.o libDenseCRF.a \
$(CC) -shared -I/usr/include/python3.5m -Wl,-no-undefined -o dense_inference.so dense_inference.o -lboost_python-py35 -lpython3.5m -L. -lDenseCRF $(CFLAGS) \
dense_inference.o: refine_3d/dense_inference.cpp refine_3d/dense_inference.h libDenseCRF.a \
$(CC) -c -fPIC -I/usr/include/python3.5m refine_3d/dense_inference.cpp -o dense_inference.o $(CFLAGS) -lDenseCRF $(CFLAGS)
python: dense_inference.o dense_inference.so libDenseCRF.a
.PHONY: default clean
中间还遇到过一个问题,就是numpy.hpp不存在,这个问题是因为虽然pip 安装了numpy,但是我是anaconda的python,所以系统里没有numpy,需要用apt-get install python3-numpy 安装