Skip to content

Commit

Permalink
Update Code
Browse files Browse the repository at this point in the history
1. add new parameter C for NSG construction
2. modify test_nsg_index program
  • Loading branch information
DelightRun committed Oct 2, 2018
1 parent ba5f437 commit ce652f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/index_nsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ void IndexNSG::sync_prune(unsigned q,
boost::dynamic_bitset<>& flags,
SimpleNeighbor* cut_graph_) {
unsigned range = parameter.Get<unsigned>("R");
unsigned maxc = parameter.Get<unsigned>("C");
width = range;
unsigned start = 0;


for (unsigned nn = 0; nn < final_graph_[q].size(); nn++) {
unsigned id = final_graph_[q][nn];
if (flags[id])continue;
Expand All @@ -276,7 +276,7 @@ void IndexNSG::sync_prune(unsigned q,
if(pool[start].id == q)start++;
result.push_back(pool[start]);

while (result.size() < range && (++start) < pool.size() /* TODO: && start < 500 */) {
while (result.size() < range && (++start) < pool.size() && start < maxc) {
auto &p = pool[start];
bool occlude = false;
for (unsigned t = 0; t < result.size(); t++) {
Expand Down Expand Up @@ -399,7 +399,7 @@ void IndexNSG::Link(const Parameters &parameters, SimpleNeighbor* cut_graph_) {
cnt++;
if(cnt % step_size == 0){
LockGuard g(progress_lock);
std::cout<<progress++ <<"/"<< percent << " completed" << ", "<<pool.size() <<std::endl;
std::cout<<progress++ <<"/"<< percent << " completed" << std::endl;
}
}

Expand Down Expand Up @@ -444,7 +444,7 @@ void IndexNSG::Build(size_t n, const float *data, const Parameters &parameters)
}
}
avg /= 1.0 * nd_;
std::cout << max << ":" << avg << ":" << min << ":" << cnt << "\n";
//std::cout << max << ":" << avg << ":" << min << ":" << cnt << "\n";
//tree_grow(parameters);
//max = 0;
//for (unsigned i = 0; i < nd_; i++) {
Expand Down
43 changes: 27 additions & 16 deletions tests/test_nsg_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,60 @@
#include <efanna2e/index_nsg.h>
#include <efanna2e/util.h>


void load_data(char* filename, float*& data, unsigned& num,unsigned& dim){// load data with sift10K pattern
/// load data with sift10K pattern
void load_data(char* filename, float*& data, unsigned& num,
unsigned& dim) {
std::ifstream in(filename, std::ios::binary);
if(!in.is_open()){std::cout<<"open file error"<<std::endl;exit(-1);}
in.read((char*)&dim,4);
//std::cout<<"data dimension: "<<dim<<std::endl;
in.seekg(0,std::ios::end);
if (!in.is_open()) {
std::cout << "open file error" << std::endl;
exit(-1);
}
in.read((char*)&dim, 4);
in.seekg(0, std::ios::end);
std::ios::pos_type ss = in.tellg();
size_t fsize = (size_t)ss;
num = (unsigned)(fsize / (dim+1) / 4);
num = (unsigned)(fsize / (dim + 1) / 4);
data = new float[(size_t)num * (size_t)dim];

in.seekg(0,std::ios::beg);
for(size_t i = 0; i < num; i++){
in.seekg(4,std::ios::cur);
in.read((char*)(data+i*dim),dim*4);
in.seekg(0, std::ios::beg);
for (size_t i = 0; i < num; i++) {
in.seekg(4, std::ios::cur);
in.read((char*)(data + i * dim), dim * 4);
}
in.close();
}
int main(int argc, char** argv){
if(argc!=6){std::cout<< argv[0] <<" data_file nn_graph_path L R save_graph_file"<<std::endl; exit(-1);}
int main(int argc, char** argv) {
if (argc != 7) {
std::cout << argv[0] << " data_file nn_graph_path L R C save_graph_file"
<< std::endl;
exit(-1);
}
float* data_load = NULL;
unsigned points_num, dim;
load_data(argv[1], data_load, points_num, dim);

std::string nn_graph_path(argv[2]);
unsigned L = (unsigned)atoi(argv[3]);
unsigned R = (unsigned)atoi(argv[4]);
unsigned C = (unsigned)atoi(argv[5]);

//data_load = efanna2e::data_align(data_load, points_num, dim);//one must align the data before build
// data_load = efanna2e::data_align(data_load, points_num, dim);//one must
// align the data before build
efanna2e::IndexNSG index(dim, points_num, efanna2e::L2, nullptr);

auto s = std::chrono::high_resolution_clock::now();
efanna2e::Parameters paras;
paras.Set<unsigned>("L", L);
paras.Set<unsigned>("R", R);
paras.Set<unsigned>("C", C);
paras.Set<std::string>("nn_graph_path", nn_graph_path);

index.Build(points_num, data_load, paras);
auto e = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = e-s;
std::chrono::duration<double> diff = e - s;

std::cout << "indexing time: " << diff.count() << "\n";
index.Save(argv[5]);
index.Save(argv[6]);

return 0;
}

0 comments on commit ce652f6

Please sign in to comment.