Faiss cpu 版本安装 -- CentOS7

来源:互联网 发布:7z ubuntu 解压 编辑:程序博客网 时间:2024/06/05 09:00

Faiss cpu 版本安装 – CentOS7

系统:CentOS7(64 位),Python 2.7.13 |Anaconda 4.4.0 (64-bit)

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

安装Anaconda

Anaconda是 Python 的科学计算工具包。根据对 Python2 和 Python3 的支持,分为 Anaconda2 和 Anaconda3。官网提供的是最新的版本,其他版本可以在清华大学开源软件镜像站下载。
由于安装Minimal版本的CentOS默认安装了Python2.7, 因此就直接选用了Python2.7,Anaconda2的安装,在上一篇博文中有介绍。
避免大家翻回去看麻烦(遇到的问题的解决方案):

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# 检测1conda list '''出现 N多Python依赖包'''# 检测2python --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.gztar -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 tmppython python/demo_auto_tune.py

trouble shooting

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

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

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

# 先清除之前编译的操作make clean# 进入 python文件夹cd faiss/python# 官网解释是swig相关文件太旧了,需要更新git checkout swigfaiss_gpu_wrap.cxx swigfaiss_gpu.py swigfaiss_wrap.cxx swigfaiss.py# 再运行编译命令make py

附: makefile.inc 中的:

PYTHONCFLAGS=-I$HOME/anaconda2/include/python2.7/ -I$HOME/anaconda2/lib/python2.7/site-packages/numpy/core/include/

一定要配置对,不然会出现和上述一样的错误。如下图:

这里写图片描述

Reference

  1. http://www.jianshu.com/p/657a0fe8cefb
  2. http://blog.csdn.net/u010641294/article/details/72783372