lsh.cpp 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2009, Xavier Delacour, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

// 2009-01-12, Xavier Delacour <xavier.delacour@gmail.com>


// * hash perf could be improved
// * in particular, implement integer only (converted fixed from float input)

// * number of hash functions could be reduced (andoni paper)

// * redundant distance computations could be suppressed

// * rework CvLSHOperations interface-- move some of the loops into it to
// * allow efficient async storage


56 57 58 59
// Datar, M., Immorlica, N., Indyk, P., and Mirrokni, V. S. 2004. Locality-sensitive hashing
// scheme based on p-stable distributions. In Proceedings of the Twentieth Annual Symposium on
// Computational Geometry (Brooklyn, New York, USA, June 08 - 11, 2004). SCG '04. ACM, New York,
// NY, 253-262. DOI= http://doi.acm.org/10.1145/997817.997857
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

#include "precomp.hpp"
#include <math.h>
#include <vector>
#include <algorithm>
#include <limits>

template <class T>
class memory_hash_ops : public CvLSHOperations {
  int d;
  std::vector<T> data;
  std::vector<int> free_data;
  struct node {
    int i, h2, next;
  };
  std::vector<node> nodes;
  std::vector<int> free_nodes;
  std::vector<int> bins;

public:
  memory_hash_ops(int _d, int n) : d(_d) {
    bins.resize(n, -1);
  }

  virtual int vector_add(const void* _p) {
    const T* p = (const T*)_p;
    int i;
    if (free_data.empty()) {
      i = (int)data.size();
      data.insert(data.end(), d, 0);
    } else {
      i = free_data.end()[-1];
      free_data.pop_back();
    }
    std::copy(p, p + d, data.begin() + i);
    return i / d;
  }
  virtual void vector_remove(int i) {
    free_data.push_back(i * d);
  }
  virtual const void* vector_lookup(int i) {
    return &data[i * d];
  }
  virtual void vector_reserve(int n) {
    data.reserve(n * d);
  }
  virtual unsigned int vector_count() {
    return (unsigned)(data.size() / d - free_data.size());
  }

  virtual void hash_insert(lsh_hash h, int /*l*/, int i) {
    int ii;
    if (free_nodes.empty()) {
      ii = (int)nodes.size();
      nodes.push_back(node());
    } else {
      ii = free_nodes.end()[-1];
      free_nodes.pop_back();
    }
    node& n = nodes[ii];
120
    int h1 = (int)(h.h1 % bins.size());
121 122 123 124 125 126
    n.i = i;
    n.h2 = h.h2;
    n.next = bins[h1];
    bins[h1] = ii;
  }
  virtual void hash_remove(lsh_hash h, int /*l*/, int i) {
127
    int h1 = (int)(h.h1 % bins.size());
128 129 130
    for (int ii = bins[h1], iin, iip = -1; ii != -1; iip = ii, ii = iin) {
      iin = nodes[ii].next;
      if (nodes[ii].h2 == h.h2 && nodes[ii].i == i) {
131 132 133 134 135
    free_nodes.push_back(ii);
    if (iip == -1)
      bins[h1] = iin;
    else
      nodes[iip].next = iin;
136 137 138 139
      }
    }
  }
  virtual int hash_lookup(lsh_hash h, int /*l*/, int* ret_i, int ret_i_max) {
140
    int h1 = (int)(h.h1 % bins.size());
141 142 143
    int k = 0;
    for (int ii = bins[h1]; ii != -1 && k < ret_i_max; ii = nodes[ii].next)
      if (nodes[ii].h2 == h.h2)
144
    ret_i[k++] = nodes[ii].i;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    return k;
  }
};

template <class T,int cvtype>
class pstable_l2_func {
  CvMat *a, *b, *r1, *r2;
  int d, k;
  double r;
  pstable_l2_func(const pstable_l2_func& x);
  pstable_l2_func& operator= (const pstable_l2_func& rhs);
public:
  typedef T scalar_type;
  typedef T accum_type;
  pstable_l2_func(int _d, int _k, double _r, CvRNG& rng)
    : d(_d), k(_k), r(_r) {
    assert(sizeof(T) == CV_ELEM_SIZE1(cvtype));
    a = cvCreateMat(k, d, cvtype);
    b = cvCreateMat(k, 1, cvtype);
    r1 = cvCreateMat(k, 1, CV_32SC1);
    r2 = cvCreateMat(k, 1, CV_32SC1);
    cvRandArr(&rng, a, CV_RAND_NORMAL, cvScalar(0), cvScalar(1));
    cvRandArr(&rng, b, CV_RAND_UNI, cvScalar(0), cvScalar(r));
    cvRandArr(&rng, r1, CV_RAND_UNI,
169 170
          cvScalar(std::numeric_limits<int>::min()),
          cvScalar(std::numeric_limits<int>::max()));
171
    cvRandArr(&rng, r2, CV_RAND_UNI,
172 173
          cvScalar(std::numeric_limits<int>::min()),
          cvScalar(std::numeric_limits<int>::max()));
174 175 176 177 178 179 180 181
  }
  ~pstable_l2_func() {
    cvReleaseMat(&a);
    cvReleaseMat(&b);
    cvReleaseMat(&r1);
    cvReleaseMat(&r2);
  }

182 183
  // * factor all L functions into this (reduces number of matrices to 4 total;
  // * simpler syntax in lsh_table). give parameter l here that tells us which
184 185 186 187
  // * row to use etc.

  lsh_hash operator() (const T* x) const {
    const T* aj = (const T*)a->data.ptr;
188
    const T* bj = (const T*)b->data.ptr;
189 190 191 192 193 194

    lsh_hash h;
    h.h1 = h.h2 = 0;
    for (int j = 0; j < k; ++j) {
      accum_type s = 0;
      for (int jj = 0; jj < d; ++jj)
195
    s += aj[jj] * x[jj];
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      s += *bj;
      s = accum_type(s/r);
      int si = int(s);
      h.h1 += r1->data.i[j] * si;
      h.h2 += r2->data.i[j] * si;

      aj += d;
      bj++;
    }
    return h;
  }
  accum_type distance(const T* p, const T* q) const {
    accum_type s = 0;
    for (int j = 0; j < d; ++j) {
      accum_type d1 = p[j] - q[j];
      s += d1 * d1;
    }
    return s;
  }
};

template <class H>
class lsh_table {
public:
  typedef typename H::scalar_type scalar_type;
  typedef typename H::accum_type accum_type;
private:
  std::vector<H*> g;
  CvLSHOperations* ops;
  int d, L, k;
  double r;

  static accum_type comp_dist(const std::pair<int,accum_type>& x,
229
                  const std::pair<int,accum_type>& y) {
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    return x.second < y.second;
  }

  lsh_table(const lsh_table& x);
  lsh_table& operator= (const lsh_table& rhs);
public:
  lsh_table(CvLSHOperations* _ops, int _d, int Lval, int _k, double _r, CvRNG& rng)
    : ops(_ops), d(_d), L(Lval), k(_k), r(_r) {
    g.resize(L);
    for (int j = 0; j < L; ++j)
      g[j] = new H(d, k, r, rng);
  }
  ~lsh_table() {
    for (int j = 0; j < L; ++j)
      delete g[j];
    delete ops;
  }

  int dims() const {
    return d;
  }
  unsigned int size() const {
    return ops->vector_count();
  }

  void add(const scalar_type* data, int n, int* ret_indices = 0) {
    for (int j=0;j<n;++j) {
      const scalar_type* x = data+j*d;
      int i = ops->vector_add(x);
      if (ret_indices)
260
    ret_indices[j] = i;
261 262

      for (int l = 0; l < L; ++l) {
263 264
    lsh_hash h = (*g[l])(x);
    ops->hash_insert(h, l, i);
265 266 267 268 269 270 271 272 273
      }
    }
  }
  void remove(const int* indices, int n) {
    for (int j = 0; j < n; ++j) {
      int i = indices[n];
      const scalar_type* x = (const scalar_type*)ops->vector_lookup(i);

      for (int l = 0; l < L; ++l) {
274 275
    lsh_hash h = (*g[l])(x);
    ops->hash_remove(h, l, i);
276 277 278 279 280
      }
      ops->vector_remove(i);
    }
  }
  void query(const scalar_type* q, int k0, int emax, double* dist, int* results) {
281
    cv::AutoBuffer<int> tmp(emax);
282
    typedef std::pair<int, accum_type> dr_type; // * swap int and accum_type here, for naming consistency
283
    cv::AutoBuffer<dr_type> dr(k0);
284 285 286 287 288 289 290 291
    int k1 = 0;

    // * handle k0 >= emax, in which case don't track max distance

    for (int l = 0; l < L && emax > 0; ++l) {
      lsh_hash h = (*g[l])(q);
      int m = ops->hash_lookup(h, l, tmp, emax);
      for (int j = 0; j < m && emax > 0; ++j, --emax) {
292 293 294 295 296 297 298 299 300 301 302
    int i = tmp[j];
    const scalar_type* p = (const scalar_type*)ops->vector_lookup(i);
    accum_type pd = (*g[l]).distance(p, q);
    if (k1 < k0) {
      dr[k1++] = std::make_pair(i, pd);
      std::push_heap(&dr[0], &dr[k1], comp_dist);
    } else if (pd < dr[0].second) {
      std::pop_heap(&dr[0], &dr[k0], comp_dist);
      dr[k0 - 1] = std::make_pair(i, pd);
      std::push_heap(&dr[0], &dr[k0], comp_dist);
    }
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
      }
    }

    for (int j = 0; j < k1; ++j)
      dist[j] = dr[j].second, results[j] = dr[j].first;
    std::fill(dist + k1, dist + k0, 0);
    std::fill(results + k1, results + k0, -1);
  }
  void query(const scalar_type* data, int n, int k0, int emax, double* dist, int* results) {
    for (int j = 0; j < n; ++j) {
      query(data, k0, emax, dist, results);
      data += d; // * this may not agree with step for some scalar_types
      dist += k0;
      results += k0;
    }
  }
};

typedef lsh_table<pstable_l2_func<float, CV_32FC1> > lsh_pstable_l2_32f;
typedef lsh_table<pstable_l2_func<double, CV_64FC1> > lsh_pstable_l2_64f;

struct CvLSH {
  int type;
  union {
    lsh_pstable_l2_32f* lsh_32f;
    lsh_pstable_l2_64f* lsh_64f;
  } u;
};

CvLSH* cvCreateLSH(CvLSHOperations* ops, int d, int L, int k, int type, double r, int64 seed) {
  CvLSH* lsh = 0;
  CvRNG rng = cvRNG(seed);

  if (type != CV_32FC1 && type != CV_64FC1)
    CV_Error(CV_StsUnsupportedFormat, "vectors must be either CV_32FC1 or CV_64FC1");
  lsh = new CvLSH;
339 340 341 342 343 344 345 346 347 348 349 350
  try
  {
      lsh->type = type;
      switch (type) {
      case CV_32FC1: lsh->u.lsh_32f = new lsh_pstable_l2_32f(ops, d, L, k, r, rng); break;
      case CV_64FC1: lsh->u.lsh_64f = new lsh_pstable_l2_64f(ops, d, L, k, r, rng); break;
      }
  }
  catch(...)
  {
      delete lsh;
      throw;
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  }

  return lsh;
}

CvLSH* cvCreateMemoryLSH(int d, int n, int L, int k, int type, double r, int64 seed) {
  CvLSHOperations* ops = 0;

  switch (type) {
  case CV_32FC1: ops = new memory_hash_ops<float>(d,n); break;
  case CV_64FC1: ops = new memory_hash_ops<double>(d,n); break;
  }
  return cvCreateLSH(ops, d, L, k, type, r, seed);
}

void cvReleaseLSH(CvLSH** lsh) {
  switch ((*lsh)->type) {
  case CV_32FC1: delete (*lsh)->u.lsh_32f; break;
  case CV_64FC1: delete (*lsh)->u.lsh_64f; break;
  default: assert(0);
  }
  delete *lsh;
  *lsh = 0;
}

unsigned int LSHSize(CvLSH* lsh) {
  switch (lsh->type) {
  case CV_32FC1: return lsh->u.lsh_32f->size(); break;
  case CV_64FC1: return lsh->u.lsh_64f->size(); break;
  default: assert(0);
  }
  return 0;
}


void cvLSHAdd(CvLSH* lsh, const CvMat* data, CvMat* indices) {
  int dims, n;
  int* ret_indices = 0;

  switch (lsh->type) {
  case CV_32FC1: dims = lsh->u.lsh_32f->dims(); break;
  case CV_64FC1: dims = lsh->u.lsh_64f->dims(); break;
  default: assert(0); return;
  }

  n = data->rows;

  if (dims != data->cols)
    CV_Error(CV_StsBadSize, "data must be n x d, where d is what was used to construct LSH");

  if (CV_MAT_TYPE(data->type) != lsh->type)
    CV_Error(CV_StsUnsupportedFormat, "type of data and constructed LSH must agree");
  if (indices) {
    if (CV_MAT_TYPE(indices->type) != CV_32SC1)
      CV_Error(CV_StsUnsupportedFormat, "indices must be CV_32SC1");
    if (indices->rows * indices->cols != n)
      CV_Error(CV_StsBadSize, "indices must be n x 1 or 1 x n for n x d data");
    ret_indices = indices->data.i;
  }

  switch (lsh->type) {
  case CV_32FC1: lsh->u.lsh_32f->add(data->data.fl, n, ret_indices); break;
  case CV_64FC1: lsh->u.lsh_64f->add(data->data.db, n, ret_indices); break;
  default: assert(0); return;
  }
}

void cvLSHRemove(CvLSH* lsh, const CvMat* indices) {
  int n;

  if (CV_MAT_TYPE(indices->type) != CV_32SC1)
    CV_Error(CV_StsUnsupportedFormat, "indices must be CV_32SC1");
  n = indices->rows * indices->cols;
  switch (lsh->type) {
  case CV_32FC1: lsh->u.lsh_32f->remove(indices->data.i, n); break;
  case CV_64FC1: lsh->u.lsh_64f->remove(indices->data.i, n); break;
  default: assert(0); return;
  }
}

void cvLSHQuery(CvLSH* lsh, const CvMat* data, CvMat* indices, CvMat* dist, int k, int emax) {
  int dims;

  switch (lsh->type) {
  case CV_32FC1: dims = lsh->u.lsh_32f->dims(); break;
  case CV_64FC1: dims = lsh->u.lsh_64f->dims(); break;
  default: assert(0); return;
  }

  if (k<1)
    CV_Error(CV_StsOutOfRange, "k must be positive");
  if (CV_MAT_TYPE(data->type) != lsh->type)
    CV_Error(CV_StsUnsupportedFormat, "type of data and constructed LSH must agree");
  if (dims != data->cols)
    CV_Error(CV_StsBadSize, "data must be n x d, where d is what was used to construct LSH");
  if (dist->rows != data->rows || dist->cols != k)
    CV_Error(CV_StsBadSize, "dist must be n x k for n x d data");
  if (dist->rows != indices->rows || dist->cols != indices->cols)
    CV_Error(CV_StsBadSize, "dist and indices must be same size");
  if (CV_MAT_TYPE(dist->type) != CV_64FC1)
    CV_Error(CV_StsUnsupportedFormat, "dist must be CV_64FC1");
  if (CV_MAT_TYPE(indices->type) != CV_32SC1)
    CV_Error(CV_StsUnsupportedFormat, "indices must be CV_32SC1");

  switch (lsh->type) {
  case CV_32FC1: lsh->u.lsh_32f->query(data->data.fl, data->rows,
457
                       k, emax, dist->data.db, indices->data.i); break;
458
  case CV_64FC1: lsh->u.lsh_64f->query(data->data.db, data->rows,
459
                       k, emax, dist->data.db, indices->data.i); break;
460 461 462
  default: assert(0); return;
  }
}