由于工作需要,臨時了解到一個Faiss,據(jù)說是一款較好的找相似圖的工具,這里主要記錄下我安裝cpu版本的一個過程。主要參考了reference1。
開發(fā)環(huán)境介紹
centos 系統(tǒng),64 位
faiss 官方也是在64位系統(tǒng)測試的,因此不知道32位系統(tǒng)是否兼容。
安裝Anaconda
Anaconda是 Python 的科學(xué)計(jì)算工具包。根據(jù)對 Python2 和 Python3 的支持,分為 Anaconda2 和 Anaconda3。官網(wǎng)提供的是最新的版本,其他版本可以在清華大學(xué)開源軟件鏡像站下載。
由于本人習(xí)慣, 因此選用了python2
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda2-4.3.0-Linux-x86_64.sh
# 修改權(quán)限
chmod +x Anaconda2-4.3.0-Linux-x86_64.sh
# 執(zhí)行默認(rèn)安裝,一路Enter鍵。
bash Anaconda2-4.3.0-Linux-x86_64.sh
# 檢測1
conda list
出現(xiàn) N多Python依賴包
# 檢測2
python --version
出現(xiàn)帶Anaconda標(biāo)記的Python,如下:
Python 2.7.13 :: Anaconda custom (64-bit)
安裝openblas
事實(shí)上,mkl支持的FAISS是最高效的,然而,由于版權(quán)認(rèn)證等問題,我們選擇openblas。
# Anaconda2 安裝 openblas。
conda install openblas
# root權(quán)限下創(chuàng)建軟鏈。
ln -s $HOME/anaconda2/lib/libopenblas.so.0 /usr/lib64/libopenblas.so.0
安裝FAISS
# 下載FAISS源碼.
git clone https://github.com/facebookresearch/faiss.git
# 進(jìn)入FAISS源碼目錄.
cd faiss
# 根據(jù)系統(tǒng)配置編譯環(huán)境. [Linux 為例]
cp example_makefiles/makefile.inc.Linux ./makefile.inc
# 編譯 &測試BLAS案例.
make tests/test_blas
./tests/test_blas
配置C++開發(fā)環(huán)境
# 編譯安裝.
make
# 5.1、簡單測試.
# 運(yùn)行測試案例.
./tests/demo_ivfpq_indexing
# 5.2、復(fù)雜測試.
# 下載數(shù)據(jù)集.
wget ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz
tar -xzvf sift.tar.gz
# 轉(zhuǎn)移數(shù)據(jù)集。
mv sift sift1M
# 編譯 &運(yùn)行測試案例.
make tests/demo_sift1M
./tests/demo_sift1M
配置python開發(fā)環(huán)境
# 更改配置文件
vim makefile.inc
找到 PYTHONCFLAGS 選項(xiàng),替換如下:
PYTHONCFLAGS=-I$HOME/anaconda2/include/python2.7/ -I$HOME/anaconda2/lib/python2.7/site-packages/numpy/core/include/
# 編譯.
make py
# 檢驗(yàn) 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、復(fù)雜測試.
export PYTHONPATH=.
mkdir tmp
python python/demo_auto_tune.py
trouble shooting
在上面的配置python開發(fā)環(huán)境時遇到了下面的錯誤:
#運(yùn)行下面的命令
make py
#得到下面的報(bào)錯
make: *** [python/_swigfaiss.so] Error 1
后來在faiss 官網(wǎng)上找到了如下解答:
cd faiss/python
# 官網(wǎng)解釋是swig相關(guān)文件太舊了,需要更新
git checkout swigfaiss_gpu_wrap.cxx swigfaiss_gpu.py swigfaiss_wrap.cxx swigfaiss.py
# 再運(yùn)行編譯命令, 大功告成
make py