Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/ExpressionClustering/flann/include/flann/nn/index_testing.h @ 15840

Last change on this file since 15840 was 15840, checked in by gkronber, 6 years ago

#2886 added utility console program for clustering of expressions

File size: 10.7 KB
Line 
1/***********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5 * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6 *
7 * THE BSD LICENSE
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *************************************************************************/
30
31#ifndef FLANN_INDEX_TESTING_H_
32#define FLANN_INDEX_TESTING_H_
33
34#include <cstring>
35#include <cassert>
36#include <cmath>
37
38#include "flann/util/matrix.h"
39#include "flann/algorithms/nn_index.h"
40#include "flann/util/result_set.h"
41#include "flann/util/logger.h"
42#include "flann/util/timer.h"
43
44
45namespace flann
46{
47
48inline int countCorrectMatches(int* neighbors, int* groundTruth, int n)
49{
50    int count = 0;
51    for (int i=0; i<n; ++i) {
52        for (int k=0; k<n; ++k) {
53            if (neighbors[i]==groundTruth[k]) {
54                count++;
55                break;
56            }
57        }
58    }
59    return count;
60}
61
62
63template <typename Distance>
64typename Distance::ResultType computeDistanceRaport(const Matrix<typename Distance::ElementType>& inputData, typename Distance::ElementType* target,
65                                                    int* neighbors, int* groundTruth, int veclen, int n, const Distance& distance)
66{
67    typedef typename Distance::ResultType DistanceType;
68
69    DistanceType ret = 0;
70    for (int i=0; i<n; ++i) {
71        DistanceType den = distance(inputData[groundTruth[i]], target, veclen);
72        DistanceType num = distance(inputData[neighbors[i]], target, veclen);
73
74        if ((den==0)&&(num==0)) {
75            ret += 1;
76        }
77        else {
78            ret += num/den;
79        }
80    }
81
82    return ret;
83}
84
85template <typename Distance>
86float search_with_ground_truth(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
87                               const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches, int nn, int checks,
88                               float& time, typename Distance::ResultType& dist, const Distance& distance, int skipMatches)
89{
90    typedef typename Distance::ResultType DistanceType;
91
92    if (matches.cols<size_t(nn)) {
93        Logger::info("matches.cols=%d, nn=%d\n",matches.cols,nn);
94
95        throw FLANNException("Ground truth is not computed for as many neighbors as requested");
96    }
97
98    KNNResultSet<DistanceType> resultSet(nn+skipMatches);
99    SearchParams searchParams(checks);
100
101    int* indices = new int[nn+skipMatches];
102    DistanceType* dists = new DistanceType[nn+skipMatches];
103    int* neighbors = indices + skipMatches;
104
105    int correct = 0;
106    DistanceType distR = 0;
107    StartStopTimer t;
108    int repeats = 0;
109    while (t.value<0.2) {
110        repeats++;
111        t.start();
112        correct = 0;
113        distR = 0;
114        for (size_t i = 0; i < testData.rows; i++) {
115            resultSet.clear();
116            index.findNeighbors(resultSet, testData[i], searchParams);
117            resultSet.copy(indices,dists,nn+skipMatches);
118
119            correct += countCorrectMatches(neighbors,matches[i], nn);
120            distR += computeDistanceRaport<Distance>(inputData, testData[i], neighbors, matches[i], testData.cols, nn, distance);
121        }
122        t.stop();
123    }
124    time = float(t.value/repeats);
125
126    delete[] indices;
127    delete[] dists;
128
129    float precicion = (float)correct/(nn*testData.rows);
130
131    dist = distR/(testData.rows*nn);
132
133    Logger::info("%8d %10.4g %10.5g %10.5g %10.5g\n",
134                 checks, precicion, time, 1000.0 * time / testData.rows, dist);
135
136    return precicion;
137}
138
139
140template <typename Distance>
141float test_index_checks(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
142                        const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
143                        int checks, float& precision, const Distance& distance, int nn = 1, int skipMatches = 0)
144{
145    typedef typename Distance::ResultType DistanceType;
146
147    Logger::info("  Nodes  Precision(%)   Time(s)   Time/vec(ms)  Mean dist\n");
148    Logger::info("---------------------------------------------------------\n");
149
150    float time = 0;
151    DistanceType dist = 0;
152    precision = search_with_ground_truth(index, inputData, testData, matches, nn, checks, time, dist, distance, skipMatches);
153
154    return time;
155}
156
157template <typename Distance>
158float test_index_precision(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
159                           const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
160                           float precision, int& checks, const Distance& distance, int nn = 1, int skipMatches = 0)
161{
162    typedef typename Distance::ResultType DistanceType;
163    const float SEARCH_EPS = 0.001f;
164
165    Logger::info("  Nodes  Precision(%)   Time(s)   Time/vec(ms)  Mean dist\n");
166    Logger::info("---------------------------------------------------------\n");
167
168    int c2 = 1;
169    float p2;
170    int c1 = 1;
171//     float p1;
172    float time;
173    DistanceType dist;
174
175    p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
176
177    if (p2>precision) {
178        Logger::info("Got as close as I can\n");
179        checks = c2;
180        return time;
181    }
182
183    while (p2<precision) {
184        c1 = c2;
185//         p1 = p2;
186        c2 *=2;
187        p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
188    }
189
190    int cx;
191    float realPrecision;
192    if (fabs(p2-precision)>SEARCH_EPS) {
193        Logger::info("Start linear estimation\n");
194        // after we got to values in the vecinity of the desired precision
195        // use linear approximation get a better estimation
196
197        cx = (c1+c2)/2;
198        realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
199        while (fabs(realPrecision-precision)>SEARCH_EPS) {
200
201            if (realPrecision<precision) {
202                c1 = cx;
203            }
204            else {
205                c2 = cx;
206            }
207            cx = (c1+c2)/2;
208            if (cx==c1) {
209                Logger::info("Got as close as I can\n");
210                break;
211            }
212            realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
213        }
214
215        c2 = cx;
216        p2 = realPrecision;
217
218    }
219    else {
220        Logger::info("No need for linear estimation\n");
221        cx = c2;
222        realPrecision = p2;
223    }
224
225    checks = cx;
226    return time;
227}
228
229
230template <typename Distance>
231void test_index_precisions(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
232                           const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
233                           float* precisions, int precisions_length, const Distance& distance, int nn = 1, int skipMatches = 0, float maxTime = 0)
234{
235    typedef typename Distance::ResultType DistanceType;
236
237    const float SEARCH_EPS = 0.001;
238
239    // make sure precisions array is sorted
240    std::sort(precisions, precisions+precisions_length);
241
242    int pindex = 0;
243    float precision = precisions[pindex];
244
245    Logger::info("  Nodes  Precision(%)   Time(s)   Time/vec(ms)  Mean dist\n");
246    Logger::info("---------------------------------------------------------\n");
247
248    int c2 = 1;
249    float p2;
250
251    int c1 = 1;
252    float p1;
253
254    float time;
255    DistanceType dist;
256
257    p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
258
259    // if precision for 1 run down the tree is already
260    // better then some of the requested precisions, then
261    // skip those
262    while (precisions[pindex]<p2 && pindex<precisions_length) {
263        pindex++;
264    }
265
266    if (pindex==precisions_length) {
267        Logger::info("Got as close as I can\n");
268        return;
269    }
270
271    for (int i=pindex; i<precisions_length; ++i) {
272
273        precision = precisions[i];
274        while (p2<precision) {
275            c1 = c2;
276            p1 = p2;
277            c2 *=2;
278            p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
279            if ((maxTime> 0)&&(time > maxTime)&&(p2<precision)) return;
280        }
281
282        int cx;
283        float realPrecision;
284        if (fabs(p2-precision)>SEARCH_EPS) {
285            Logger::info("Start linear estimation\n");
286            // after we got to values in the vecinity of the desired precision
287            // use linear approximation get a better estimation
288
289            cx = (c1+c2)/2;
290            realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
291            while (fabs(realPrecision-precision)>SEARCH_EPS) {
292
293                if (realPrecision<precision) {
294                    c1 = cx;
295                }
296                else {
297                    c2 = cx;
298                }
299                cx = (c1+c2)/2;
300                if (cx==c1) {
301                    Logger::info("Got as close as I can\n");
302                    break;
303                }
304                realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
305            }
306
307            c2 = cx;
308            p2 = realPrecision;
309
310        }
311        else {
312            Logger::info("No need for linear estimation\n");
313            cx = c2;
314            realPrecision = p2;
315        }
316
317    }
318}
319
320}
321
322#endif //FLANN_INDEX_TESTING_H_
Note: See TracBrowser for help on using the repository browser.