lapjv.cpp 9.5 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 56 57 58 59 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 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 229 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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 339 340 341 342 343 344 345 346 347 348 349 350 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
//   Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "include/lapjv.h"

namespace PaddleDetection {

/** Column-reduction and reduction transfer for a dense cost matrix.
 */
int _ccrrt_dense(const int n, float *cost[],
                     int *free_rows, int *x, int *y, float *v)
{
    int n_free_rows;
    bool *unique;

    for (int i = 0; i < n; i++) {
        x[i] = -1;
        v[i] = LARGE;
        y[i] = 0;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            const float c = cost[i][j];
            if (c < v[j]) {
                v[j] = c;
                y[j] = i;
            }
        }
    }
    NEW(unique, bool, n);
    memset(unique, TRUE, n);
    {
        int j = n;
        do {
            j--;
            const int i = y[j];
            if (x[i] < 0) {
                x[i] = j;
            } else {
                unique[i] = FALSE;
                y[j] = -1;
            }
        } while (j > 0);
    }
    n_free_rows = 0;
    for (int i = 0; i < n; i++) {
        if (x[i] < 0) {
            free_rows[n_free_rows++] = i;
        } else if (unique[i]) {
            const int j = x[i];
            float min = LARGE;
            for (int j2 = 0; j2 < n; j2++) {
                if (j2 == (int)j) {
                    continue;
                }
                const float c = cost[i][j2] - v[j2];
                if (c < min) {
                    min = c;
                }
            }
            v[j] -= min;
        }
    }
    FREE(unique);
    return n_free_rows;
}


/** Augmenting row reduction for a dense cost matrix.
 */
int _carr_dense(
    const int n, float *cost[],
    const int n_free_rows,
    int *free_rows, int *x, int *y, float *v)
{
    int current = 0;
    int new_free_rows = 0;
    int rr_cnt = 0;
    while (current < n_free_rows) {
        int i0;
        int j1, j2;
        float v1, v2, v1_new;
        bool v1_lowers;

        rr_cnt++;
        const int free_i = free_rows[current++];
        j1 = 0;
        v1 = cost[free_i][0] - v[0];
        j2 = -1;
        v2 = LARGE;
        for (int j = 1; j < n; j++) {
            const float c = cost[free_i][j] - v[j];
            if (c < v2) {
                if (c >= v1) {
                    v2 = c;
                    j2 = j;
                } else {
                    v2 = v1;
                    v1 = c;
                    j2 = j1;
                    j1 = j;
                }
            }
        }
        i0 = y[j1];
        v1_new = v[j1] - (v2 - v1);
        v1_lowers = v1_new < v[j1];
        if (rr_cnt < current * n) {
            if (v1_lowers) {
                v[j1] = v1_new;
            } else if (i0 >= 0 && j2 >= 0) {
                j1 = j2;
                i0 = y[j2];
            }
            if (i0 >= 0) {
                if (v1_lowers) {
                    free_rows[--current] = i0;
                } else {
                    free_rows[new_free_rows++] = i0;
                }
            }
        } else {
            if (i0 >= 0) {
                free_rows[new_free_rows++] = i0;
            }
        }
        x[free_i] = j1;
        y[j1] = free_i;
    }
    return new_free_rows;
}


/** Find columns with minimum d[j] and put them on the SCAN list.
 */
int _find_dense(const int n, int lo, float *d, int *cols, int *y)
{
    int hi = lo + 1;
    float mind = d[cols[lo]];
    for (int k = hi; k < n; k++) {
        int j = cols[k];
        if (d[j] <= mind) {
            if (d[j] < mind) {
                hi = lo;
                mind = d[j];
            }
            cols[k] = cols[hi];
            cols[hi++] = j;
        }
    }
    return hi;
}


// Scan all columns in TODO starting from arbitrary column in SCAN
// and try to decrease d of the TODO columns using the SCAN column.
int _scan_dense(const int n, float *cost[],
                    int *plo, int*phi,
                    float *d, int *cols, int *pred,
                    int *y, float *v)
{
    int lo = *plo;
    int hi = *phi;
    float h, cred_ij;

    while (lo != hi) {
        int j = cols[lo++];
        const int i = y[j];
        const float mind = d[j];
        h = cost[i][j] - v[j] - mind;
        // For all columns in TODO
        for (int k = hi; k < n; k++) {
            j = cols[k];
            cred_ij = cost[i][j] - v[j] - h;
            if (cred_ij < d[j]) {
                d[j] = cred_ij;
                pred[j] = i;
                if (cred_ij == mind) {
                    if (y[j] < 0) {
                        return j;
                    }
                    cols[k] = cols[hi];
                    cols[hi++] = j;
                }
            }
        }
    }
    *plo = lo;
    *phi = hi;
    return -1;
}


/** Single iteration of modified Dijkstra shortest path algorithm as explained in the JV paper.
 *
 * This is a dense matrix version.
 *
 * \return The closest free column index.
 */
int find_path_dense(
    const int n, float *cost[],
    const int start_i,
    int *y, float *v,
    int *pred)
{
    int lo = 0, hi = 0;
    int final_j = -1;
    int n_ready = 0;
    int *cols;
    float *d;

    NEW(cols, int, n);
    NEW(d, float, n);

    for (int i = 0; i < n; i++) {
        cols[i] = i;
        pred[i] = start_i;
        d[i] = cost[start_i][i] - v[i];
    }
    while (final_j == -1) {
        // No columns left on the SCAN list.
        if (lo == hi) {
            n_ready = lo;
            hi = _find_dense(n, lo, d, cols, y);
            for (int k = lo; k < hi; k++) {
                const int j = cols[k];
                if (y[j] < 0) {
                    final_j = j;
                }
            }
        }
        if (final_j == -1) {
            final_j = _scan_dense(
                    n, cost, &lo, &hi, d, cols, pred, y, v);
        }
    }

    {
        const float mind = d[cols[lo]];
        for (int k = 0; k < n_ready; k++) {
            const int j = cols[k];
            v[j] += d[j] - mind;
        }
    }

    FREE(cols);
    FREE(d);

    return final_j;
}


/** Augment for a dense cost matrix.
 */
int _ca_dense(
    const int n, float *cost[],
    const int n_free_rows,
    int *free_rows, int *x, int *y, float *v)
{
    int *pred;

    NEW(pred, int, n);

    for (int *pfree_i = free_rows; pfree_i < free_rows + n_free_rows; pfree_i++) {
        int i = -1, j;
        int k = 0;

        j = find_path_dense(n, cost, *pfree_i, y, v, pred);
        while (i != *pfree_i) {
            i = pred[j];
            y[j] = i;
            SWAP_INDICES(j, x[i]);
            k++;
        }
    }
    FREE(pred);
    return 0;
}


/** Solve dense sparse LAP.
 */
int lapjv_internal(
    const cv::Mat &cost, const bool extend_cost, const float cost_limit,
    int *x, int *y ) {
    int n_rows = cost.rows;
    int n_cols = cost.cols;
    int n;
    if (n_rows == n_cols) {
      n = n_rows;
    } else if (!extend_cost) {
      throw std::invalid_argument("Square cost array expected. If cost is intentionally non-square, pass extend_cost=True.");
    }

    // Get extend cost
    if (extend_cost || cost_limit < LARGE) {
      n = n_rows + n_cols;
    }
    cv::Mat cost_expand(n, n, CV_32F);
    float expand_value;
    if (cost_limit < LARGE) {
      expand_value = cost_limit / 2;
    } else {
      double max_v;
      minMaxLoc(cost, nullptr, &max_v);
      expand_value = (float)max_v + 1;
    }

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cost_expand.at<float>(i, j) = expand_value;
        if (i >= n_rows && j >= n_cols) {
          cost_expand.at<float>(i, j) = 0;
        } else if (i < n_rows && j < n_cols) {
          cost_expand.at<float>(i, j) = cost.at<float>(i, j);
        }
      }
    } 

    // Convert Mat to pointer array
    float **cost_ptr;
    NEW(cost_ptr, float *, n);
    for (int i = 0; i < n; ++i) {
      NEW(cost_ptr[i], float, n);
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cost_ptr[i][j] = cost_expand.at<float>(i, j);
      }
    }

    int ret;
    int *free_rows;
    float *v;
    int *x_c;
    int *y_c;

    NEW(free_rows, int, n);
    NEW(v, float, n);
    NEW(x_c, int, n);
    NEW(y_c, int, n);

    ret = _ccrrt_dense(n, cost_ptr, free_rows, x_c, y_c, v);
    int i = 0;
    while (ret > 0 && i < 2) {
      ret = _carr_dense(n, cost_ptr, ret, free_rows, x_c, y_c, v);
      i++;
    }
    if (ret > 0) {
      ret = _ca_dense(n, cost_ptr, ret, free_rows, x_c, y_c, v);
    }
    FREE(v);
    FREE(free_rows);
    for (int i = 0; i < n; ++i) {
      FREE(cost_ptr[i]);
    }
    FREE(cost_ptr);
    if (ret != 0) {
      if (ret == -1){
        throw "Out of memory.";
      }
      throw "Unknown error (lapjv_internal)";
    }
    // Get output of x, y, opt
    for (int i = 0; i < n; ++i) {
      if (i < n_rows) {
        x[i] = x_c[i];
        if (x[i] >= n_cols) {
          x[i] = -1;
        }
      }      
      if (i < n_cols) {
        y[i] = y_c[i];
        if (y[i] >= n_rows) {
          y[i] = -1;
        }
      } 
    }
    
    FREE(x_c);
    FREE(y_c);
    return ret;
}

} // namespace PaddleDetection