Last active
May 17, 2019 03:14
-
-
Save mpkuse/15325501406e1feb4ab3287a941164eb to your computer and use it in GitHub Desktop.
Linking faiss in your Cmake projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2015-present, Facebook, Inc. | |
* All rights reserved. | |
* | |
* This source code is licensed under the BSD+Patents license found in the | |
* LICENSE file in the root directory of this source tree. | |
*/ | |
#include <cstdio> | |
#include <cstdlib> | |
#include <faiss/IndexFlat.h> | |
int main() { | |
int d = 64; // dimension | |
int nb = 100000; // database size | |
int nq = 10000; // nb of queries | |
float *xb = new float[d * nb]; | |
float *xq = new float[d * nq]; | |
for(int i = 0; i < nb; i++) { | |
for(int j = 0; j < d; j++) | |
xb[d * i + j] = drand48(); | |
xb[d * i] += i / 1000.; | |
} | |
for(int i = 0; i < nq; i++) { | |
for(int j = 0; j < d; j++) | |
xq[d * i + j] = drand48(); | |
xq[d * i] += i / 1000.; | |
} | |
faiss::IndexFlatL2 index(d); // call constructor | |
printf("is_trained = %s\n", index.is_trained ? "true" : "false"); | |
index.add(nb, xb); // add vectors to the index | |
printf("ntotal = %ld\n", index.ntotal); | |
int k = 4; | |
{ // sanity check: search 5 first vectors of xb | |
long *I = new long[k * 5]; | |
float *D = new float[k * 5]; | |
index.search(5, xb, k, D, I); | |
// print results | |
printf("I=\n"); | |
for(int i = 0; i < 5; i++) { | |
for(int j = 0; j < k; j++) | |
printf("%5ld ", I[i * k + j]); | |
printf("\n"); | |
} | |
printf("D=\n"); | |
for(int i = 0; i < 5; i++) { | |
for(int j = 0; j < k; j++) | |
printf("%7g ", D[i * k + j]); | |
printf("\n"); | |
} | |
delete [] I; | |
delete [] D; | |
} | |
{ // search xq | |
long *I = new long[k * nq]; | |
float *D = new float[k * nq]; | |
index.search(nq, xq, k, D, I); | |
// print results | |
printf("I (5 first results)=\n"); | |
for(int i = 0; i < 5; i++) { | |
for(int j = 0; j < k; j++) | |
printf("%5ld ", I[i * k + j]); | |
printf("\n"); | |
} | |
printf("I (5 last results)=\n"); | |
for(int i = nq - 5; i < nq; i++) { | |
for(int j = 0; j < k; j++) | |
printf("%5ld ", I[i * k + j]); | |
printf("\n"); | |
} | |
delete [] I; | |
delete [] D; | |
} | |
delete [] xb; | |
delete [] xq; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required (VERSION 2.8) | |
project (faiss_demo_app) | |
#find_package( FAISS REQUIRED ) #no cmake file from faiss team yet. may be future they make one. | |
# Faiss-github: | |
# This is equivalent to doing: | |
# g++ 1-Flat.cpp -L /usr/local/lib/ -lfaiss | |
add_executable( faissDemo 1-Flat.cpp ) | |
link_directories("/usr/local/lib/") | |
target_link_libraries( faissDemo faiss ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script loads the whole image descriptors (computed by `place_recog_analysis_tool`). | |
# Then try using faiss into it. Use my docker image: mpkuse/kusevisionkit:ros-kinetic-vins-tf-faiss | |
# | |
# Author : Manohar Kuse <[email protected]> | |
# 29th Apr, 209 | |
# | |
import numpy as np | |
from TerminalColors import bcolors | |
tcol = bcolors() | |
import faiss | |
import time | |
#-------------------------------------------------- | |
if False: | |
# load `pinhole_1loop_in_lab` to train an index | |
BASE = '/Bulk_Data/_tmp_saved_seq/mynt_pinhole_1loop_in_lab/' | |
fname = BASE+'/gray_conv6_K16__centeredinput.npz' | |
DESCRIPTOR_STR = 'from '+fname | |
print tcol.OKGREEN, 'Load ', fname, tcol.ENDC | |
loaded = np.load(fname) | |
netvlad_desc = loaded['netvlad_desc'].astype('float32') | |
netvlad_at_i = loaded['netvlad_at_i'] | |
print 'netvlad_desc.shape=', netvlad_desc.shape , '\tnetvlad_at_i.shape', netvlad_at_i.shape | |
#-------------------------------------------------- | |
BASE = '/Bulk_Data/_tmp/' | |
BASE = '/Bulk_Data/_tmp_saved_seq/mynt_mall0/' | |
fname = BASE+'/gray_conv6_K16__centeredinput.npz' | |
DESCRIPTOR_STR = 'from '+fname | |
print tcol.OKGREEN, 'Load ', fname, tcol.ENDC | |
loaded = np.load(fname) | |
netvlad_desc = loaded['netvlad_desc'].astype('float32') | |
netvlad_at_i = loaded['netvlad_at_i'] | |
print 'netvlad_desc.shape=', netvlad_desc.shape , '\tnetvlad_at_i.shape', netvlad_at_i.shape | |
index = faiss.IndexFlatIP( 4096 ) | |
# index.add( netvlad_desc[0:-200] ) | |
for i in range(0, len(netvlad_desc) ): | |
# for i in range(0, 500 ): | |
print tcol.HEADER, '---', i , tcol.ENDC | |
if i-150 >= 0: | |
start_t = time.time() | |
print 'add netvlad_desc[', i-150, ']', '\t done in %4.4fms' %(1000.*(time.time() - start_t)) | |
index.add( np.expand_dims( netvlad_desc[i-150], 0 ) ) | |
if i<200: | |
print 'i less than 200. not seen enough' | |
continue | |
start_t = time.time() | |
print 'search(', i, ')' | |
# D, I = index.search( np.expand_dims(netvlad_desc[i],0), 4 ) | |
D, I = index.search( netvlad_desc[i-3:i], 4 ) | |
print 'Elapsed time: %4.4fms' %(1000.* (time.time() - start_t) ) | |
print 'i=', i, '::::>\n', I | |
print D |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment