数据科学Faiss博客写作素材

Faiss cpu 版本安装记

2017-06-15  本文已影响2772人  galois_xiong

由于工作需要,临时了解到一个Faiss,据说是一款较好的找相似图的工具,这里主要记录下我安装cpu版本的一个过程。主要参考了reference1。

开发环境介绍

centos 系统,64 位
faiss 官方也是在64位系统测试的,因此不知道32位系统是否兼容。

安装Anaconda

Anaconda是 Python 的科学计算工具包。根据对 Python2 和 Python3 的支持,分为 Anaconda2 和 Anaconda3。官网提供的是最新的版本,其他版本可以在清华大学开源软件镜像站下载。
由于本人习惯, 因此选用了python2

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda2-4.3.0-Linux-x86_64.sh
# 修改权限
chmod +x Anaconda2-4.3.0-Linux-x86_64.sh
# 执行默认安装,一路Enter键。
bash Anaconda2-4.3.0-Linux-x86_64.sh
# 检测1
conda list 
出现 N多Python依赖包
# 检测2
python --version
出现带Anaconda标记的Python,如下:
Python 2.7.13 :: Anaconda custom (64-bit)

安装openblas

事实上,mkl支持的FAISS是最高效的,然而,由于版权认证等问题,我们选择openblas。

# Anaconda2 安装 openblas。
conda install openblas
# root权限下创建软链。
ln -s $HOME/anaconda2/lib/libopenblas.so.0 /usr/lib64/libopenblas.so.0

安装FAISS

# 下载FAISS源码.
git clone https://github.com/facebookresearch/faiss.git
# 进入FAISS源码目录.
cd faiss
# 根据系统配置编译环境. [Linux 为例]
cp example_makefiles/makefile.inc.Linux ./makefile.inc
# 编译 &测试BLAS案例.
make tests/test_blas
./tests/test_blas

配置C++开发环境

# 编译安装.
make
# 5.1、简单测试.
# 运行测试案例.
./tests/demo_ivfpq_indexing
# 5.2、复杂测试.
# 下载数据集.
wget ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz
tar -xzvf sift.tar.gz
# 转移数据集。
mv sift sift1M
# 编译 &运行测试案例.
make tests/demo_sift1M
./tests/demo_sift1M

配置python开发环境

# 更改配置文件
vim makefile.inc
找到 PYTHONCFLAGS 选项,替换如下:
PYTHONCFLAGS=-I$HOME/anaconda2/include/python2.7/ -I$HOME/anaconda2/lib/python2.7/site-packages/numpy/core/include/
# 编译.
make py
# 检验 python-faiss.
python -c "import faiss"
ldd -r _swigfaiss.so
# 6.1、简单测试.
python -c "import faiss, numpy
faiss.Kmeans(10, 20).train(numpy.random.rand(1000, 10).astype('float32'))"
# 6.2、复杂测试.
export PYTHONPATH=.
mkdir tmp
python python/demo_auto_tune.py

trouble shooting

在上面的配置python开发环境时遇到了下面的错误:

#运行下面的命令
make py
#得到下面的报错
make: *** [python/_swigfaiss.so] Error 1

后来在faiss 官网上找到了如下解答:

cd faiss/python
# 官网解释是swig相关文件太旧了,需要更新
git checkout swigfaiss_gpu_wrap.cxx swigfaiss_gpu.py swigfaiss_wrap.cxx swigfaiss.py
# 再运行编译命令, 大功告成
make py

Reference

  1. http://blog.csdn.net/u010641294/article/details/72783372
  2. https://github.com/facebookresearch/faiss/wiki/Troubleshooting
上一篇下一篇

猜你喜欢

热点阅读