[r36635]: trunk / contrib / brl / bseg / boxm2 / volm / boxm2_volm_matcher_p1.cxx Maximize Restore History

Download this file

boxm2_volm_matcher_p1.cxx    1899 lines (1774 with data), 89.8 kB

   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
 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
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
// This is brl/bseg/boxm2/volm/boxm2_volm_matcher_p1.cxx
#include "boxm2_volm_matcher_p1.h"
#include <vul/vul_timer.h>
#include <vcl_where_root_dir.h>
#include <vcl_algorithm.h>
#include <vcl_iomanip.h>
#define GBYTE 1073741824
boxm2_volm_matcher_p1::boxm2_volm_matcher_p1(volm_camera_space_sptr const& cam_space,
volm_query_sptr const& query,
vcl_vector<volm_geo_index_node_sptr> const& leaves,
float const& buffer_capacity,
vcl_string const& geo_index_folder,
unsigned const& tile_id,
vcl_vector<float> const& depth_interval,
vgl_polygon<double> const& cand_poly,
bocl_device_sptr gpu,
bool const& is_candidate,
bool const& is_last_pass,
vcl_string const& out_folder,
float const& threshold,
unsigned const& max_cam_per_loc,
vcl_vector<volm_weight> weights) :
cam_space_(cam_space), query_(query), leaves_(leaves), ind_buffer_(buffer_capacity),
layer_size_buff_(0), is_candidate_(is_candidate), cand_poly_(cand_poly), fallback_size_buff_(0),
is_last_pass_(is_last_pass), out_folder_(out_folder), depth_interval_(depth_interval),
gpu_(gpu), is_grd_reg_(true), is_sky_reg_(true), is_obj_reg_(true), n_cam_(0), n_obj_(0),
grd_id_buff_(0), grd_dist_buff_(0), grd_land_buff_(0), grd_land_wgt_buff_(0),
grd_id_offset_buff_(0), grd_weight_buff_(0), grd_wgt_attri_buff_(0),
sky_id_buff_(0), sky_id_offset_buff_(0), sky_weight_buff_(0),
obj_id_buff_(0), obj_id_offset_buff_(0), obj_min_dist_buff_(0), obj_order_buff_(0),
obj_weight_buff_(0), obj_wgt_attri_buff_(0),
obj_orient_buff_(0), depth_interval_buff_(0), obj_land_buff_(0), obj_land_wgt_buff_(0),
depth_length_buff_(0),
threshold_(threshold), max_cam_per_loc_(max_cam_per_loc), weights_(weights)
{
valid_cam_indices_ = cam_space_->valid_indices();
layer_size_ = query_->get_query_size();
volm_fallback_label::size(fallback_size_);
ind_ = new boxm2_volm_wr3db_index(layer_size_, ind_buffer_);
ind_orient_ = new boxm2_volm_wr3db_index(layer_size_, ind_buffer_);
ind_land_ = new boxm2_volm_wr3db_index(layer_size_, ind_buffer_);
file_name_pre_ << geo_index_folder << "geo_index_tile_" << tile_id;
if (!query_->depth_scene()->ground_plane().size())
is_grd_reg_ = false;
if (!query_->depth_scene()->sky().size())
is_sky_reg_ = false;
if (!query_->depth_regions().size())
is_obj_reg_ = false;
}
boxm2_volm_matcher_p1::~boxm2_volm_matcher_p1()
{
if (n_cam_) delete n_cam_;
if (n_obj_) delete n_obj_;
if (fallback_size_buff_) delete fallback_size_buff_;
if (layer_size_buff_) delete layer_size_buff_;
if (depth_length_buff_) delete depth_length_buff_;
if (grd_id_buff_) delete [] grd_id_buff_;
if (grd_dist_buff_) delete [] grd_dist_buff_;
if (grd_land_buff_) delete [] grd_land_buff_;
if (grd_land_wgt_buff_) delete [] grd_land_wgt_buff_;
if (grd_id_offset_buff_) delete [] grd_id_offset_buff_;
if (grd_weight_buff_) delete grd_weight_buff_;
if (grd_wgt_attri_buff_) delete [] grd_wgt_attri_buff_;
if (sky_id_buff_) delete [] sky_id_buff_;
if (sky_id_offset_buff_) delete [] sky_id_offset_buff_;
if (sky_weight_buff_) delete sky_weight_buff_;
if (obj_id_buff_) delete [] obj_id_buff_;
if (obj_id_offset_buff_) delete [] obj_id_offset_buff_;
if (obj_min_dist_buff_) delete [] obj_min_dist_buff_;
if (obj_orient_buff_) delete [] obj_orient_buff_;
if (obj_order_buff_) delete [] obj_order_buff_;
if (obj_land_buff_) delete [] obj_land_buff_;
if (obj_land_wgt_buff_) delete [] obj_land_wgt_buff_;
if (obj_weight_buff_) delete [] obj_weight_buff_;
if (obj_wgt_attri_buff_) delete [] obj_wgt_attri_buff_;
if (depth_interval_buff_) delete [] depth_interval_buff_;
}
bool boxm2_volm_matcher_p1::volm_matcher_p1()
{
n_cam_ = new unsigned;
*n_cam_ = (unsigned)query_->get_cam_num();
n_cam_cl_mem_ = new bocl_mem(gpu_->context(), n_cam_, sizeof(unsigned), " n_cam " );
if (!n_cam_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for N_CAM\n";
delete n_cam_cl_mem_;
return false;
}
n_obj_ = new unsigned;
*n_obj_ = (unsigned)(query_->depth_regions()).size();
n_obj_cl_mem_ = new bocl_mem(gpu_->context(), n_obj_, sizeof(unsigned), " n_obj " );
if (!n_obj_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for N_OBJ\n";
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
return false;
}
unsigned nc = *n_cam_;
unsigned no = *n_obj_;
// transfer all queries information to 1D array structure
query_global_mem_ = 0;
query_local_mem_ = 0;
vul_timer trans_query_time;
if (!this->transfer_query()) {
vcl_cerr << "\n ERROR: transfering query to 1D structure failed.\n";
return false;
}
vcl_cout << "\t 4.1.1 Setting up query in pass 1 matcher for GPU ------> \t" << trans_query_time.all()/1000.0 << " seconds." << vcl_endl;
// create queue
if (!this->create_queue()) {
vcl_cerr << "\n ERROR: creating pass 1 matcher queue failed.\n";
return false;
}
// create depth_interval
depth_interval_buff_ = new float[depth_interval_.size()];
for (unsigned i = 0; i < depth_interval_.size(); i++)
depth_interval_buff_[i] = depth_interval_[i];
depth_interval_cl_mem_ = new bocl_mem(gpu_->context(), depth_interval_buff_, sizeof(float)*((unsigned)depth_interval_.size()), " depth_interval ");
if (!depth_interval_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for DEPTH_INTERVAL\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_;
return false;
}
depth_length_buff_ = new unsigned;
*depth_length_buff_ = (unsigned)depth_interval_.size();
depth_length_cl_mem_ = new bocl_mem(gpu_->context(), depth_length_buff_, sizeof(unsigned), " depth_length ");
if (!depth_length_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for DEPTH_LENGTH\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
return false;
}
// create cl_mem for layer_size
layer_size_buff_ = new unsigned;
*layer_size_buff_ = layer_size_;
layer_size_cl_mem_ = new bocl_mem(gpu_->context(), layer_size_buff_, sizeof(unsigned), " layer_size ");
if (!layer_size_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for LAYER_SIZE\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
return false;
}
// create cl_mem for fallback_size
fallback_size_buff_ = new unsigned char;
*fallback_size_buff_ = fallback_size_;
fallback_size_cl_mem_ = new bocl_mem(gpu_->context(), fallback_size_buff_, sizeof(unsigned char), " fallback size ");
if (!fallback_size_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for FALLBACK SIZE\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
return false;
}
// compile the kernel
vcl_string identifier = gpu_->device_identifier();
if (kernels_.find(identifier) == kernels_.end()) {
vcl_cout << "\t 4.1.2 Comipling kernels for device " << identifier << vcl_endl;
vcl_vector<bocl_kernel*> ks;
if (!this->compile_kernel(ks)) {
vcl_cerr << "\n ERROR: compiling matcher kernel failed.\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
return false;
}
kernels_[identifier] = ks;
}
// calculate available memory space for indices , not query_global_mem_ includes everything previously defined
cl_ulong avail_global_mem = device_global_mem_ - query_global_mem_ ;
cl_ulong extra_global_mem = (cl_ulong)(1.5*GBYTE); // leave extra 1.5 GB space for kernel to run
if (avail_global_mem < extra_global_mem) {
vcl_cerr << "\n ERROR: available memory is smaller than pre-defined extra memory, reduce the extra memory space (current value = "
<< extra_global_mem / GBYTE << ")\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
return false;
}
cl_ulong index_global_mem = avail_global_mem - extra_global_mem; // in byte
// note that for each index, we require space for
// a float score array with length n_cam
// a float mean value array with length n_cam*n_obj
// an uchar index array with length layer_size
cl_ulong per_index_mem = nc*no*sizeof(float) + nc*sizeof(float) + sizeof(unsigned char)*layer_size_;
if (index_global_mem < per_index_mem) {
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
vcl_cerr << "\n ERROR: available memory can not take a single index, reduce the extra memory space (current value = "
<< extra_global_mem / GBYTE << ")\n";
return false;
}
unsigned ni = index_global_mem / per_index_mem;
// hack here to specify the number of indice per lunching, instead of use calculated value
// because of CL_INVALID_COMMAND error for large number of indices given large number of cameras...
if(layer_size_ < 20000) {
if (*n_cam_ < 10000) ni = 64;
else if (*n_cam_ < 15000) ni = 32;
else if (*n_cam_ < 20000) ni = 32;
else ni = 16;
}
else {
if (*n_cam_ < 10000) ni = 8;
else if (*n_cam_ < 15000) ni = 4;
else ni = 2;
}
vcl_cout << "\t 4.1.3: device have total " << device_global_mem_ << " Byte (" << (float)device_global_mem_/(float)GBYTE << " GB) memory space\n"
<< "\t query requires " << query_global_mem_ << " Byte (" << (float)query_global_mem_/(float)GBYTE << " GB)\n"
<< "\t leave " << extra_global_mem << " Byte (" << (float)extra_global_mem/(float)GBYTE << " GB) extra empty space on device\n"
<< "\t a single index require " << per_index_mem << " Byte given " << nc << " cameras and " << no << " objects\n"
<< "\t ---> kernel can calcualte " << ni << " indices per lunching" << vcl_endl;
// define the work group size and NDRange dimension
work_dim_ = 2;
local_threads_[0] = 8;
local_threads_[1] = 8;
// note the global work goupe size is defined inside the loop since we may have different number of
// indices passed into buffer
// -----------------------------------------------------------
// start loop over all indices
unsigned leaf_id = 0;
unsigned round_cnt = 0;
float gpu_matcher_time = 0.0f;
vul_timer total_matcher_time;
cl_int status;
cl_ulong total_index_num = 0;
vcl_string index_file = leaves_[leaf_id]->get_index_name(file_name_pre_.str());
vcl_string index_orient_file = leaves_[leaf_id]->get_label_index_name(file_name_pre_.str(), "orientation");
vcl_string index_land_file = leaves_[leaf_id]->get_label_index_name(file_name_pre_.str(), "");
ind_->initialize_read(index_file);
ind_orient_->initialize_read(index_orient_file);
ind_land_->initialize_read(index_land_file);
while (leaf_id < leaves_.size())
{
unsigned char* index_buff_ = new unsigned char[ni*layer_size_];
unsigned char* index_orient_buff_ = new unsigned char[ni*layer_size_];
unsigned char* index_land_buff_ = new unsigned char[ni*layer_size_];
// fill the index buffer
vcl_vector<unsigned> l_id; // leaf_id for indices filled into buffer
vcl_vector<unsigned> h_id; // hypo_id in this leaf_id for indices filled into buffer
unsigned actual_n_ind = ni; // handling the last round where the number of loaded indices is smaller than pre-computed ni
if (!this->fill_index(ni, layer_size_, leaf_id, index_buff_, index_orient_buff_, index_land_buff_, l_id, h_id, actual_n_ind) ) {
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
delete [] index_buff_;
delete [] index_orient_buff_;
delete [] index_land_buff_;
return false;
}
// resize the index buffer if actual loaded index size is smaller than pre-defined
if (actual_n_ind != ni)
ni = actual_n_ind;
if (ni == 0) {
continue;
}
total_index_num += ni;
float* score_buff_ = new float[ni*nc];
float* mu_buff_ = new float[ni*no*nc];
unsigned* n_ind_ = new unsigned;
*n_ind_ = ni;
bocl_mem* n_ind_cl_mem_ = new bocl_mem(gpu_->context(), n_ind_, sizeof(unsigned), " n_ind ");
if (!n_ind_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for N_IND\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
delete n_ind_cl_mem_; delete [] n_ind_;
delete [] index_buff_; delete [] score_buff_; delete [] mu_buff_;
delete [] index_orient_buff_;
return false;
}
// define work group size based on number of cameras, nc, and number of indices, ni;
cl_ulong cl_ni = (cl_ulong)RoundUp(*n_ind_, (int)local_threads_[0]); // row is index
cl_ulong cl_nj = (cl_ulong)RoundUp(*n_cam_, (int)local_threads_[1]); // col is camera
global_threads_[0] = cl_ni;
global_threads_[1] = cl_nj;
vcl_cout << " -------- in round " << round_cnt++ << " ------\n"
<< " Giving " << nc << " camera hypos per location and " << ni << " locations pre lunching\n"
<< " NDRange stucture:\n"
<< " \t dimension = " << work_dim_ << '\n'
<< " \t work group size = (" << local_threads_[0] << ", " << local_threads_[1] << ")\n"
<< " \t number of work item = (" << global_threads_[0] << ", " << global_threads_[1] << ")\n"
<< " \t number of work group = (" << global_threads_[0]/local_threads_[0]
<< ", " << global_threads_[1]/local_threads_[1] << ')' << vcl_endl;
#if 0
// check loaded indices and associated ids
vcl_cout << " -------> leaf_id_updated = " << leaf_id << vcl_endl;
for (unsigned i = 0; i < ni; i++) {
vcl_cout << " i = " << i << ", leaf_id = " << l_id[i] << " hypo_id = " << h_id[i] << "\nindex_depth\t";
unsigned start = i*layer_size_;
unsigned end = (i+1)*layer_size_;
for (unsigned j = start; j < end; j++)
vcl_cout << ' ' << (int)index_buff_[j];
vcl_cout << "\n\nindex_orient\t";
for (unsigned j = start; j < end; j++)
vcl_cout << ' ' << (int)index_orient_buff_[j];
vcl_cout << '\n';
}
#endif
// create cl_mem_ for index, score and mu
// Note: here the data passed into device may be smaller than the pre-assigned index_buff size (since actual_n_ind < pre-calculated ni)
bocl_mem* index_cl_mem_ = new bocl_mem(gpu_->context(), index_buff_, sizeof(unsigned char)*ni*layer_size_, " index ");
if (!index_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for INDEX\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
delete n_ind_cl_mem_; delete [] n_ind_;
delete index_cl_mem_;
delete [] index_buff_; delete [] score_buff_; delete [] mu_buff_;
delete [] index_orient_buff_;
return false;
}
// create index orientation
bocl_mem* index_orient_cl_mem_ = new bocl_mem(gpu_->context(), index_orient_buff_, sizeof(unsigned char)*ni*layer_size_, " index_orient ");
if (!index_orient_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for INDEX_ORIENT\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
delete n_ind_cl_mem_; delete [] n_ind_;
delete index_cl_mem_;
delete [] index_buff_; delete [] score_buff_; delete [] mu_buff_;
delete [] index_orient_buff_; delete index_orient_cl_mem_;
return false;
}
// create index land cl_mem
bocl_mem* index_land_cl_mem_ = new bocl_mem(gpu_->context(), index_land_buff_, sizeof(unsigned char)*ni*layer_size_, " index_land ");
if (!index_land_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for INDEX_LAND\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
delete n_ind_cl_mem_; delete [] n_ind_;
delete index_cl_mem_; delete [] index_buff_;
delete index_orient_cl_mem_; delete [] index_orient_buff_;
delete index_land_cl_mem_; delete [] index_land_buff_;
delete [] score_buff_; delete [] mu_buff_;
return false;
}
// create score cl_mem
bocl_mem* score_cl_mem_ = new bocl_mem(gpu_->context(), score_buff_, sizeof(float)*ni*nc, " score ");
if (!score_cl_mem_->create_buffer( CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for SCORE\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
delete n_ind_cl_mem_; delete [] n_ind_;
delete index_cl_mem_; delete [] index_buff_;
delete index_orient_cl_mem_; delete [] index_orient_buff_;
delete index_land_cl_mem_; delete [] index_land_buff_;
delete score_cl_mem_; delete [] score_buff_;
delete [] mu_buff_;
return false;
}
// create mu cl_mem
bocl_mem* mu_cl_mem_ = new bocl_mem(gpu_->context(), mu_buff_, sizeof(float)*ni*nc*no, " mu ");
if (!mu_cl_mem_->create_buffer( CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for MU\n";
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
delete n_ind_cl_mem_; delete [] n_ind_;
delete index_cl_mem_; delete [] index_buff_;
delete index_orient_cl_mem_; delete [] index_orient_buff_;
delete index_land_cl_mem_; delete [] index_land_buff_;
delete score_cl_mem_; delete [] score_buff_;
delete mu_cl_mem_; delete [] mu_buff_;
return false;
}
// start the obj_based kernel matcher
vcl_string identifier = gpu_->device_identifier();
if (!this->execute_matcher_kernel_orient(gpu_, queue_, kernels_[identifier], n_ind_cl_mem_,
index_cl_mem_,
index_orient_cl_mem_,
index_land_cl_mem_,
score_cl_mem_,
mu_cl_mem_)) {
vcl_cerr << "\n ERROR: executing pass 1 kernel(with orientation) failed on device " << identifier << '\n';
this->clean_query_cl_mem();
delete depth_interval_cl_mem_; delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
delete n_ind_cl_mem_; delete [] n_ind_;
delete index_cl_mem_; delete [] index_buff_;
delete index_orient_cl_mem_; delete [] index_orient_buff_;
delete index_land_cl_mem_; delete [] index_land_buff_;
delete score_cl_mem_; delete [] score_buff_;
delete mu_cl_mem_; delete [] mu_buff_;
return false;
}
// block everything to ensure the reading score
status = clFinish(queue_);
// read score
score_cl_mem_->read_to_buffer(queue_);
//mu_cl_mem_->read_to_buffer(queue_);
status = clFinish(queue_);
// count time
if (is_grd_reg_ && is_sky_reg_)
gpu_matcher_time += kernels_[identifier][0]->exec_time();
else if (!is_grd_reg_ && is_sky_reg_)
gpu_matcher_time += kernels_[identifier][1]->exec_time();
else if (is_grd_reg_ && !is_sky_reg_)
gpu_matcher_time += kernels_[identifier][2]->exec_time();
else
gpu_matcher_time += kernels_[identifier][3]->exec_time();
// post-processing data
for (unsigned ind_id = 0; ind_id < ni; ind_id++)
{
// for each location, find maximum score, top cameras and best camera giving max_score
float max_score = 0.0f;
unsigned max_cam_id = 0;
float min_score_in_list = 0.0f;
vcl_vector<unsigned> cam_ids;
vcl_vector<float> cam_scores;
for (unsigned cam_id = 0; cam_id < nc; cam_id++) {
unsigned id = cam_id + ind_id*nc;
float s = score_buff_[id];
// find the max_score and max_cam_id
if (s > max_score) {
max_score = s; max_cam_id = valid_cam_indices_[cam_id];
}
// decide to keep the camera or not
if (s > threshold_) {
if (cam_ids.size() < max_cam_per_loc_) {
cam_ids.push_back(valid_cam_indices_[cam_id]);
cam_scores.push_back(s);
}
else if (s > min_score_in_list) {
// check the list and replace the camera with min_score in the list
min_score_in_list = cam_scores[0];
unsigned min_score_id = 0;
for (unsigned jj = 0; jj < cam_ids.size(); jj++) {
if (min_score_in_list > cam_scores[jj]) {
min_score_in_list = cam_scores[jj];
min_score_id = jj;
}
}
// replace the id that have min_score among the camera id list
cam_scores[min_score_id] = s;
cam_ids[min_score_id] = valid_cam_indices_[cam_id];
}
}
}
// put score date of location ind_id to score_all
score_all_.push_back(new volm_score(l_id[ind_id], h_id[ind_id], max_score, max_cam_id, cam_ids));
score_cam_.push_back(boxm2_volm_score_out(l_id[ind_id], h_id[ind_id], cam_ids, cam_scores));
}
// clean cl_mem before next round matcher
delete n_ind_cl_mem_;
status = clFinish(queue_);
check_val(status, MEM_FAILURE, " release N_INDEX failed on device " + gpu_->device_identifier() + error_to_string(status));
delete index_cl_mem_;
status = clFinish(queue_);
check_val(status, MEM_FAILURE, " release INDEX failed on device " + gpu_->device_identifier() + error_to_string(status));
delete score_cl_mem_;
status = clFinish(queue_);
check_val(status, MEM_FAILURE, " release SCORE failed on device " + gpu_->device_identifier() + error_to_string(status));
delete mu_cl_mem_;
status = clFinish(queue_);
check_val(status, MEM_FAILURE, " release MU(average depth value) failed on device " + gpu_->device_identifier() + error_to_string(status));
delete index_orient_cl_mem_;
status = clFinish(queue_);
check_val(status, MEM_FAILURE, " release INDEX_ORIENT failed on device " + gpu_->device_identifier() + error_to_string(status));
delete index_land_cl_mem_;
status = clFinish(queue_);
check_val(status, MEM_FAILURE, " release INDEX_LAND failed on device " + gpu_->device_identifier() + error_to_string(status));
// do the test
#if 0
if (ni < 16)
this->volm_matcher_p1_test_ori(ni, index_buff_, index_orient_buff_, index_land_buff_, score_buff_, mu_buff_);
#endif
// finish current round, clean host memory
delete n_ind_;
delete [] index_buff_; delete [] score_buff_; delete [] mu_buff_;
delete [] index_orient_buff_;
} // end of loop over all leaves
// time evaluation
float total_time = (float)total_matcher_time.all();
vcl_cout << "\t\t total time for " << total_index_num << " indices and " << *n_cam_ << " cameras -------> " << total_time/1000.0 << " seconds (" << total_time << " ms)\n"
<< "\t\t GPU kernel execution ------------------> " << gpu_matcher_time/1000.0 << " seconds (" << gpu_matcher_time << " ms)\n"
<< "\t\t CPU host execution --------------------> " << (total_time - gpu_matcher_time)/1000.0 << " seconds (" << total_time - gpu_matcher_time << " ms)" << vcl_endl;
clReleaseCommandQueue(queue_);
// clear query_cl_mem
this->clean_query_cl_mem();
delete depth_interval_cl_mem_;
delete depth_length_cl_mem_;
delete layer_size_cl_mem_;
delete fallback_size_cl_mem_;
// finalize the index
ind_->finalize();
ind_orient_->finalize();
return true;
}
bool boxm2_volm_matcher_p1::fill_index(unsigned const& n_ind,
unsigned const& layer_size,
unsigned& leaf_id,
unsigned char* index_buff,
unsigned char* index_orient_buff,
unsigned char* index_land_buff,
vcl_vector<unsigned>& l_id,
vcl_vector<unsigned>& h_id,
unsigned& actual_n_ind)
{
if (is_last_pass_) {
vcl_cerr << " pass 1 check whether we have last_pass is NOT implemented yet ...\n";
return false;
}
else
{
// for no previous output case
unsigned cnt = 0;
unsigned li;
for (li = leaf_id; li < leaves_.size(); li++) {
if (!leaves_[li]->hyps_ )
continue;
vgl_point_3d<double> h_pt;
while (cnt < n_ind && leaves_[li]->hyps_->get_next(0,1,h_pt) ) {
if (is_candidate_) {
if (cand_poly_.contains(h_pt.x(), h_pt.y())) { // having candiate list and current hypo is inside it --> accept
unsigned char* values = index_buff + cnt * layer_size;
unsigned char* values_orient = index_orient_buff + cnt * layer_size;
unsigned char* values_land = index_land_buff + cnt * layer_size;
ind_->get_next(values, layer_size);
ind_orient_->get_next(values_orient, layer_size);
ind_land_->get_next(values_land, layer_size);
cnt++;
//vcl_cout << " leaf_id = " << li << " hypo_id = " << leaves_[li]->hyps_->current_-1
// << " h_pt = " << h_pt << vcl_endl;
l_id.push_back(li); h_id.push_back(leaves_[li]->hyps_->current_-1);
}
else { // having candidate list but current hypo is outside candidate list --> ignore
vcl_vector<unsigned char> values(layer_size);
ind_->get_next(values);
vcl_vector<unsigned char> values_ori(layer_size);
ind_orient_->get_next(values_ori);
vcl_vector<unsigned char> values_lnd(layer_size);
ind_land_->get_next(values_lnd);
}
}
else { // no candidate list, put all indices into buffer
unsigned char* values = index_buff + cnt * layer_size;
unsigned char* values_orient = index_orient_buff + cnt * layer_size;
unsigned char* values_land = index_land_buff + cnt * layer_size;
ind_->get_next(values, layer_size);
ind_orient_->get_next(values_orient, layer_size);
ind_land_->get_next(values_land, layer_size);
cnt++;
l_id.push_back(li); h_id.push_back(leaves_[li]->hyps_->current_-1);
}
}
if (cnt == n_ind) {
leaf_id = li;
break;
}
else {
if (is_leaf_finish(li)) {
ind_->finalize();
ind_orient_->finalize();
if (li < leaves_.size()-1) {
ind_->initialize_read(leaves_[li+1]->get_index_name(file_name_pre_.str()));
ind_orient_->initialize_read(leaves_[li+1]->get_label_index_name(file_name_pre_.str(), "orientation"));
ind_land_->initialize_read(leaves_[li+1]->get_label_index_name(file_name_pre_.str(), ""));
}
}
}
} // loop over all leaves
if (li == leaves_.size())
leaf_id = li;
if (cnt != n_ind)
actual_n_ind = cnt;
return true;
}
}
// check if the hypothesis inside given leaf has been loaded
bool boxm2_volm_matcher_p1::is_leaf_finish(unsigned const& leaf_id)
{
return leaves_[leaf_id]->hyps_->current_ == leaves_[leaf_id]->hyps_->size();
}
// execute kernel with orientation considered
bool boxm2_volm_matcher_p1::execute_matcher_kernel_orient(bocl_device_sptr device,
cl_command_queue& queue,
vcl_vector<bocl_kernel*> kern_vec,
bocl_mem* n_ind_cl_mem_,
bocl_mem* index_cl_mem_,
bocl_mem* index_orient_cl_mem_,
bocl_mem* index_land_cl_mem_,
bocl_mem* score_cl_mem_,
bocl_mem* mu_cl_mem_)
{
// create a debug buffer
cl_int status;
unsigned debug_size = 100;
float* debug_buff_ = new float[debug_size];
vcl_fill(debug_buff_, debug_buff_+debug_size, (float)12.31);
bocl_mem* debug_cl_mem_ = new bocl_mem(gpu_->context(), debug_buff_, sizeof(float)*debug_size, " debug ");
if (!debug_cl_mem_->create_buffer( CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for DEBUG\n";
delete debug_cl_mem_; delete [] debug_buff_;
return false;
}
bocl_kernel* kern;
// choose whether kernel to use
if (is_grd_reg_ && is_sky_reg_) { // both sky and ground
vcl_cout << "\t using both sky and grd kernel with orientatl attribute" << vcl_endl;
kern = kern_vec[0];
// set up argument list
kern->set_arg(n_cam_cl_mem_);
kern->set_arg(n_obj_cl_mem_);
kern->set_arg(grd_id_cl_mem_); kern->set_arg(grd_id_offset_cl_mem_);
kern->set_arg(grd_dist_cl_mem_);
kern->set_arg(grd_land_cl_mem_); kern->set_arg(grd_land_wgt_cl_mem_);
kern->set_arg(grd_weight_cl_mem_); kern->set_arg(grd_wgt_attri_cl_mem_);
kern->set_arg(sky_id_cl_mem_); kern->set_arg(sky_id_offset_cl_mem_); kern->set_arg(sky_weight_cl_mem_);
}
else if ( !is_grd_reg_ && is_sky_reg_) { // no ground but sky
vcl_cout << "\t using NO grd kernel with orientation attribute" << vcl_endl;
kern = kern_vec[1];
// set up argument list
kern->set_arg(n_cam_cl_mem_);
kern->set_arg(n_obj_cl_mem_);
kern->set_arg(sky_id_cl_mem_); kern->set_arg(sky_id_offset_cl_mem_); kern->set_arg(sky_weight_cl_mem_);
#if 0
// check the global memory
vcl_cerr << " ------------ INSIDE execute_kernel -------------------------------\n"
<< " n_cam_cl_mem = " << n_cam_cl_mem_->num_bytes() << '\n'
<< " n_obj_cl_mem = " << n_obj_cl_mem_->num_bytes() << '\n'
<< " sky_id_cl_mem_ = " << sky_id_cl_mem_->num_bytes() << '\n'
<< " sky_offset_cl_mem_ = " << sky_id_offset_cl_mem_->num_bytes() << '\n'
<< " sky_weight_cl_mem_ = " << sky_weight_cl_mem_->num_bytes() << '\n';
#endif
}
else if ( is_grd_reg_ && !is_sky_reg_) { // no sky but ground
vcl_cout << "\t using NO sky kernel with orientation attribute" << vcl_endl;
kern = kern_vec[2];
// set up argument list
kern->set_arg(n_cam_cl_mem_);
kern->set_arg(n_obj_cl_mem_);
kern->set_arg(grd_id_cl_mem_); kern->set_arg(grd_id_offset_cl_mem_);
kern->set_arg(grd_dist_cl_mem_);
kern->set_arg(grd_land_cl_mem_); kern->set_arg(grd_land_wgt_cl_mem_);
kern->set_arg(grd_weight_cl_mem_); kern->set_arg(grd_wgt_attri_cl_mem_);
}
else { // neither sky nor ground
vcl_cout << "\t using NO grd NOR sky kernel" << vcl_endl;
kern = kern_vec[3];
kern->set_arg(n_cam_cl_mem_);
kern->set_arg(n_obj_cl_mem_);
}
// set up argument list
kern->set_arg(obj_id_cl_mem_);
kern->set_arg(obj_id_offset_cl_mem_);
kern->set_arg(obj_min_dist_cl_mem_);
kern->set_arg(obj_orient_cl_mem_);
kern->set_arg(obj_land_cl_mem_);
kern->set_arg(obj_land_wgt_cl_mem_);
kern->set_arg(obj_weight_cl_mem_);
kern->set_arg(obj_wgt_attri_cl_mem_);
kern->set_arg(n_ind_cl_mem_);
kern->set_arg(layer_size_cl_mem_);
kern->set_arg(fallback_size_cl_mem_);
kern->set_arg(index_cl_mem_);
kern->set_arg(index_orient_cl_mem_);
kern->set_arg(index_land_cl_mem_);
kern->set_arg(score_cl_mem_);
kern->set_arg(mu_cl_mem_);
kern->set_arg(depth_interval_cl_mem_);
kern->set_arg(depth_length_cl_mem_);
kern->set_arg(debug_cl_mem_);
// set up local memory argument
kern->set_local_arg((*n_obj_)*sizeof(unsigned char)); // local memory for obj_min_dist
kern->set_local_arg((*n_obj_)*sizeof(unsigned char)); // local memory for orientation
kern->set_local_arg((*n_obj_)*sizeof(unsigned char)*4); // local memory for land fallback category
kern->set_local_arg((*n_obj_)*sizeof(float)*4);
kern->set_local_arg((*n_obj_)*sizeof(float)); // local memory for obj_weight
kern->set_local_arg((*n_obj_)*sizeof(float)*4); // local memory for obj_wgt_attri
if (is_grd_reg_) {
kern->set_local_arg(3*(*n_obj_)*sizeof(float)); // local memory for grd_wgt_attri
}
kern->set_local_arg((*depth_length_buff_)*sizeof(float)); // local memory for depth_interval table
#if 0
vcl_cerr << " obj_id = " << obj_id_cl_mem_->num_bytes() << '\n'
<< " obj_offset = " << obj_id_offset_cl_mem_->num_bytes() << '\n'
<< " obj_min_dist = " << obj_min_dist_cl_mem_->num_bytes() << '\n'
<< " obj_weight = " << obj_weight_cl_mem_->num_bytes() << '\n'
<< " n_ind = " << n_ind_cl_mem_->num_bytes() << '\n'
<< " lyaer_size = " << layer_size_cl_mem_->num_bytes() << '\n'
<< " index_cl_mem_ = " << index_cl_mem_->num_bytes() << '\n'
<< " score_cl_mem = " << score_cl_mem_->num_bytes() << '\n'
<< " mu_cl_mem_ = " << mu_cl_mem_->num_bytes() << '\n'
<< " depth_interval = " << depth_interval_cl_mem_->num_bytes() << '\n'
<< " depth_length = " << depth_length_cl_mem_->num_bytes() << '\n'
<< " debug_cl_mem_ = " << debug_cl_mem_->num_bytes() << '\n'
<< " local for min_dist = " << (*n_obj_)*sizeof(unsigned char) << '\n'
<< " local for weight = " << (*n_obj_)*sizeof(float) << '\n'
<< " local for depth_interval = " << (*depth_length_buff_)*sizeof(float) << '\n';
#endif
// execute kernel
if (!kern->execute(queue, work_dim_, local_threads_, global_threads_)) {
vcl_cerr << "\n ERROR: kernel execuation failed\n";
delete debug_cl_mem_;
delete debug_buff_;
return false;
}
status = clFinish(queue); // block to ensure kernel finishes
if (status != 0) {
vcl_cerr << "\n ERROR: " << status << " kernel matcher failed: " + error_to_string(status) << '\n';
return false;
}
// clear bocl_kernel argument list
kern->clear_args();
#if 0
// read debug data from device
debug_cl_mem_->read_to_buffer(queue);
unsigned i = 0;
while ((debug_buff_[i]-12.31)*(debug_buff_[i]-12.31)>0.0001 && i < 1000) {
vcl_cout << " debug[" << i << "] = " << debug_buff_[i] << vcl_endl;
i++;
}
#endif
// clear debug buffer
delete debug_cl_mem_;
status = clFinish(queue);
check_val(status, MEM_FAILURE, " release DEBUG failed on device " + device->device_identifier() + error_to_string(status));
delete [] debug_buff_;
return true;
}
// clear all query cl_mem pointer
bool boxm2_volm_matcher_p1::clean_query_cl_mem()
{
delete n_cam_cl_mem_;
delete n_obj_cl_mem_;
if ( is_grd_reg_ ) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
delete grd_id_offset_cl_mem_;
}
if ( is_sky_reg_ ) { delete sky_id_cl_mem_; delete sky_id_offset_cl_mem_; }
if ( is_obj_reg_ ) {
delete obj_id_cl_mem_; delete obj_id_offset_cl_mem_;
delete obj_orient_cl_mem_; delete obj_min_dist_cl_mem_;
delete obj_land_cl_mem_; delete obj_land_wgt_cl_mem_;
delete obj_order_cl_mem_;
}
if (this->clean_weight_cl_mem())
return true;
else
return true;
}
// clear all query weight cl_mem pointer
bool boxm2_volm_matcher_p1::clean_weight_cl_mem()
{
if ( is_grd_reg_ ) { delete grd_weight_cl_mem_; delete grd_wgt_attri_cl_mem_; }
if ( is_sky_reg_ ) { delete sky_weight_cl_mem_; }
if ( is_obj_reg_ ) { delete obj_weight_cl_mem_; delete obj_wgt_attri_cl_mem_; }
return true;
}
// compile kernel
bool boxm2_volm_matcher_p1::compile_kernel(vcl_vector<bocl_kernel*>& vec_kernels)
{
// declare the kernel source
vcl_vector<vcl_string> src_paths;
vcl_string volm_cl_source_dir = vcl_string(VCL_SOURCE_ROOT_DIR) + "/contrib/brl/bseg/boxm2/volm/cl/";
// add orientation kernels in
src_paths.push_back(volm_cl_source_dir + "generalized_volm_obj_based_matching_with_orient.cl");
src_paths.push_back(volm_cl_source_dir + "generalized_volm_obj_based_matching_no_grd_with_orient.cl");
src_paths.push_back(volm_cl_source_dir + "generalized_volm_obj_based_matching_no_sky_with_orient.cl");
src_paths.push_back(volm_cl_source_dir + "generalized_volm_obj_based_matching_no_grd_no_sky_with_orient.cl");
// create the matching orientation kernel
bocl_kernel* kern_matcher_with_orient = new bocl_kernel();
if (!kern_matcher_with_orient->create_kernel(&gpu_->context(), gpu_->device_id(), src_paths,
"generalized_volm_obj_based_matching_with_orient",
"",
"generalized_volm_obj_based_matching_with_orient") )
return false;
vec_kernels.push_back(kern_matcher_with_orient);
// create with_orientation kernel for queries without ground
bocl_kernel* kern_matcher_no_grd_with_orient = new bocl_kernel();
if (!kern_matcher_no_grd_with_orient->create_kernel(&gpu_->context(), gpu_->device_id(), src_paths,
"generalized_volm_obj_based_matching_no_grd_with_orient",
"",
"generalized_volm_obj_based_matching_no_grd_with_orient") )
return false;
vec_kernels.push_back(kern_matcher_no_grd_with_orient);
// create with_orientation kernel for queries without sky
bocl_kernel* kern_matcher_no_sky_with_orient = new bocl_kernel();
if (!kern_matcher_no_sky_with_orient->create_kernel(&gpu_->context(), gpu_->device_id(), src_paths,
"generalized_volm_obj_based_matching_no_sky_with_orient",
"",
"generalized_volm_obj_based_matching_no_sky_with_orient") )
return false;
vec_kernels.push_back(kern_matcher_no_sky_with_orient);
// create with_orientation kernel for queries without sky nor ground
bocl_kernel* kern_matcher_no_grd_no_sky_with_orient = new bocl_kernel();
if (!kern_matcher_no_grd_no_sky_with_orient->create_kernel(&gpu_->context(), gpu_->device_id(), src_paths,
"generalized_volm_obj_based_matching_no_grd_no_sky_with_orient",
"",
"generalized_volm_obj_based_matching_no_grd_no_sky_with_orient") )
return false;
vec_kernels.push_back(kern_matcher_no_grd_no_sky_with_orient);
return true;
}
bool boxm2_volm_matcher_p1::create_queue()
{
cl_int status = SDK_FAILURE;
queue_ = clCreateCommandQueue(gpu_->context(),
*(gpu_->device_id()),
CL_QUEUE_PROFILING_ENABLE,
&status);
if ( !check_val(status, CL_SUCCESS, error_to_string(status)) )
return false;
return true;
}
bool boxm2_volm_matcher_p1::write_matcher_result(vcl_string const& tile_fname_bin, vcl_string const& tile_fname_txt)
{
// write the output store binary as tile
// write the scores out as binary
volm_score::write_scores(score_all_, tile_fname_bin);
// for testing purpose, generate a temparay txt file for output
if (vul_file::exists(tile_fname_txt))
vul_file::delete_file_glob(tile_fname_txt);
unsigned all_loc = (unsigned)score_all_.size();
for (unsigned i = 0; i < all_loc; i++) {
volm_score_sptr score = score_all_[i];
vcl_vector<unsigned> cam_ids = score->cam_id_;
// write the txt output
vcl_ofstream txt_ofs(tile_fname_txt.c_str(), vcl_ios_app);
txt_ofs << vcl_setprecision(6) << score->leaf_id_ << ' ' << score->hypo_id_ << ' ' << score->max_score_ << ' ' << score->max_cam_id_ << '\n';
for (unsigned jj = 0; jj < cam_ids.size(); jj++)
txt_ofs << ' ' << cam_ids[jj];
txt_ofs << '\n';
txt_ofs.close();
}
return true;
}
bool boxm2_volm_matcher_p1::write_matcher_result(vcl_string const& tile_fname_bin)
{
volm_score::write_scores(score_all_, tile_fname_bin);
return true;
}
// write the score for all cameras whose score higher than threshold
bool boxm2_volm_matcher_p1::write_gt_cam_score(unsigned const& leaf_id, unsigned const& hypo_id, vcl_string const& out_fname)
{
vcl_ofstream ofs(out_fname.c_str());
for (unsigned i = 0; i < score_cam_.size(); i++) {
if (score_cam_[i].l_id_ == leaf_id && score_cam_[i].h_id_ == hypo_id) {
ofs << score_cam_[i].l_id_ << ' ' << score_cam_[i].h_id_ << '\n';
vcl_vector<unsigned> cam_ids = score_cam_[i].cam_id_;
vcl_vector<float> cam_scores = score_cam_[i].cam_score_;
for (unsigned jj = 0; jj < cam_ids.size(); jj++) {
ofs << vcl_setprecision(6) << cam_ids[jj] << ' ' << cam_scores[jj] << ' ' << query_->get_cam_string(cam_ids[jj]) << '\n';
}
return true;
}
}
ofs.close();
return false;
}
bool boxm2_volm_matcher_p1::transfer_query()
{
if ( !is_grd_reg_ && !is_sky_reg_ && !is_obj_reg_ ) {
vcl_cerr << "\n ERROR: no depth_map_region defined in query image, check the labelme.xml\n";
return false;
}
if (!is_obj_reg_) {
vcl_cerr << "\n ERROR: current pass 01 matcher is not able to match query without any non_grd, non_sky object, add at least one in the labelme.xml\n";
return false;
}
query_global_mem_ = 3 * sizeof(unsigned); // n_cam + n_obj + n_ind
query_local_mem_ = 3 * sizeof(unsigned); // n_cam + n_obj + n_ind
// transfer weight parameter from volm_weights
if (!this->transfer_weight()) {
vcl_cerr << "\n ERROR: failed when transferring volm_weight parameters to 1D weight buffer\n";
return false;
}
// construct the ground_id, ground_dist, ground_offset 1D array
if (is_grd_reg_)
{
unsigned grd_vox_size = query_->get_ground_id_size();
grd_id_buff_ = new unsigned[grd_vox_size];
grd_dist_buff_ = new unsigned char[grd_vox_size];
grd_id_offset_buff_ = new unsigned[*n_cam_+1];
unsigned grd_count = 0;
vcl_vector<vcl_vector<unsigned> >& grd_id = query_->ground_id();
vcl_vector<vcl_vector<unsigned char> >& grd_dist = query_->ground_dist();
for (unsigned cam_id = 0; cam_id < *n_cam_; cam_id++) {
grd_id_offset_buff_[cam_id] = grd_count;
for (unsigned vox_id = 0; vox_id < grd_id[cam_id].size(); vox_id++) {
unsigned i = grd_count + vox_id;
grd_id_buff_[i] = grd_id[cam_id][vox_id];
grd_dist_buff_[i] = grd_dist[cam_id][vox_id];
}
grd_count += (unsigned)grd_id[cam_id].size();
}
grd_id_offset_buff_[*n_cam_] = grd_count;
// construct grd_land_buff and grd_land_wgt_buff
grd_land_buff_ = new unsigned char[grd_vox_size*fallback_size_];
grd_land_wgt_buff_ = new float[grd_vox_size*fallback_size_];
vcl_vector<vcl_vector<vcl_vector<unsigned char> > > grd_land = query_->ground_land_id();
vcl_vector<vcl_vector<vcl_vector<float> > > grd_land_wgt = query_->ground_land_wgt();
unsigned cnt = 0;
for (unsigned cam_id = 0; cam_id < *n_cam_; cam_id++)
for (unsigned vox_id = 0; vox_id < grd_land[cam_id].size(); vox_id++)
for (unsigned lnd_id = 0; lnd_id < fallback_size_; lnd_id++) {
grd_land_buff_[cnt] = grd_land[cam_id][vox_id][lnd_id];
grd_land_wgt_buff_[cnt++] = grd_land_wgt[cam_id][vox_id][lnd_id];
}
#if 0
// check the grd_land_buff and grd_land_wgt_buff_
vcl_cout << " ----------------- date in query --------------- " << vcl_endl;
for (unsigned cam_id = 0; cam_id < *n_cam_; cam_id++) {
for (unsigned vox_id = 0; vox_id < grd_land[cam_id].size(); vox_id++) {
vcl_cout << " cam_id = " << cam_id << ", vox_id = " << vox_id << " --> lnd_id = [";
for (vcl_vector<unsigned char>::iterator vit = grd_land[cam_id][vox_id].begin(); vit != grd_land[cam_id][vox_id].end(); ++vit)
vcl_cout << (int)(*vit) << ' ';
vcl_cout << "] ---> lnd_wgt = [";
for (vcl_vector<float>::iterator vit = grd_land_wgt[cam_id][vox_id].begin(); vit != grd_land_wgt[cam_id][vox_id].end(); ++vit)
vcl_cout << *vit << ' ';
vcl_cout << ']' << vcl_endl;
}
}
vcl_cout << " ++++++++++++++++++ date in 1D buffer ++++++++++++++ " << vcl_endl;
for (unsigned cam_id = 0; cam_id < *n_cam_; cam_id++) {
/*unsigned land_start = grd_id_offset_buff_[cam_id] * 4;
unsigned land_end = grd_id_offset_buff_[cam_id+1] * 4;*/
unsigned vox_start = grd_id_offset_buff_[cam_id];
unsigned vox_end = grd_id_offset_buff_[cam_id+1];
for(unsigned vox_id = vox_start; vox_id < vox_end; vox_id++) {
vcl_cout << " in buffer, cam_id = " << cam_id << ", vox_global_id = " << vox_id << ", vox_id = " << vox_id - vox_start << " ---> lnd_id = [";
for (unsigned ii = 0; ii < fallback_size_; ii++) {
unsigned lnd_id = vox_id * 4 + ii ;
vcl_cout << (int)grd_land_buff_[lnd_id] << ' ';
}
vcl_cout << "] ---> lnd_wgt = [";
for (unsigned ii = 0; ii < fallback_size_; ii++) {
unsigned lnd_id = vox_id * 4 + ii;
vcl_cout << grd_land_wgt_buff_[lnd_id] << ' ';
}
vcl_cout << ']' << vcl_endl;
}
}
#endif
grd_id_cl_mem_ = new bocl_mem(gpu_->context(), grd_id_buff_, sizeof(unsigned)*grd_vox_size, " grd_id " );
if (!grd_id_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for GROUND_ID\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
delete grd_id_cl_mem_;
return false;
}
grd_dist_cl_mem_ = new bocl_mem(gpu_->context(), grd_dist_buff_, sizeof(unsigned char)*grd_vox_size, " grd_dist " );
if (!grd_dist_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for GROUND_DIST\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
delete grd_id_cl_mem_; delete grd_dist_cl_mem_;
return false;
}
grd_id_offset_cl_mem_ = new bocl_mem(gpu_->context(), grd_id_offset_buff_, sizeof(unsigned)*(*n_cam_+1), " grd_offset " );
if (!grd_id_offset_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for GROUND_OFFSET\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
return false;
}
grd_land_cl_mem_ = new bocl_mem(gpu_->context(), grd_land_buff_, sizeof(unsigned char)*grd_vox_size*fallback_size_, " grd_lnd ");
if (!grd_land_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for GROUND_LAND\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_;
return false;
}
grd_land_wgt_cl_mem_ = new bocl_mem(gpu_->context(), grd_land_wgt_buff_, sizeof(float)*grd_vox_size*fallback_size_, " grd_lnd_wgt ");
if (!grd_land_wgt_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for GROUND_LAND_WGT\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
return false;
}
query_global_mem_ += (sizeof(unsigned char)+sizeof(unsigned))*grd_vox_size; // ground id and dist
query_global_mem_ += sizeof(unsigned)*(query_->ground_offset().size()); // ground offset array
query_global_mem_ += (sizeof(unsigned char) + sizeof(float))*grd_vox_size*fallback_size_; // ground fallback land category
}
// construct sky_id buff and cl_mem
if (is_sky_reg_) {
unsigned sky_vox_size = query_->get_sky_id_size();
sky_id_buff_ = new unsigned[sky_vox_size];
sky_id_offset_buff_ = new unsigned[*n_cam_+1];
unsigned sky_count = 0;
vcl_vector<vcl_vector<unsigned> >& sky_id = query_->sky_id();
for (unsigned cam_id = 0; cam_id < *n_cam_; cam_id++) {
sky_id_offset_buff_[cam_id] = sky_count;
for (unsigned vox_id = 0; vox_id < sky_id[cam_id].size(); vox_id++) {
unsigned i = sky_count + vox_id;
sky_id_buff_[i] = sky_id[cam_id][vox_id];
}
sky_count += (unsigned)sky_id[cam_id].size();
}
sky_id_offset_buff_[*n_cam_] = sky_count;
sky_id_cl_mem_ = new bocl_mem(gpu_->context(), sky_id_buff_, sizeof(unsigned)*sky_vox_size, " sky_id " );
if (!sky_id_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for SKY_ID\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
if (is_grd_reg_) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
}
delete sky_id_cl_mem_;
return false;
}
sky_id_offset_cl_mem_ = new bocl_mem(gpu_->context(), sky_id_offset_buff_, sizeof(unsigned)*(*n_cam_+1), " sky_offset " );
if (!sky_id_offset_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for SKY_OFFSET\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
if (is_grd_reg_) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
}
delete sky_id_cl_mem_; delete sky_id_offset_cl_mem_;
return false;
}
query_global_mem_ += sizeof(unsigned)*sky_vox_size; // sky id
query_global_mem_ += sizeof(unsigned)*query_->sky_offset().size(); // sky offset
}
// construct obj_id, obj_offset 1D array
if (is_obj_reg_)
{
unsigned obj_vox_size = query_->get_dist_id_size();
unsigned obj_offset_size = (*n_cam_) * (*n_obj_);
obj_id_buff_ = new unsigned[obj_vox_size];
obj_id_offset_buff_ = new unsigned[obj_offset_size+1];
vcl_vector<vcl_vector<vcl_vector<unsigned> > >& dist_id = query_->dist_id();
unsigned obj_count = 0;
for (unsigned cam_id = 0; cam_id < *n_cam_; cam_id++) {
for (unsigned obj_id = 0; obj_id < *n_obj_; obj_id++) {
unsigned offset_id = obj_id + cam_id * (*n_obj_);
obj_id_offset_buff_[offset_id] = obj_count;
for (unsigned vox_id = 0; vox_id < dist_id[cam_id][obj_id].size(); vox_id++) {
unsigned i = obj_count + vox_id;
obj_id_buff_[i] = dist_id[cam_id][obj_id][vox_id];
}
obj_count += (unsigned)dist_id[cam_id][obj_id].size();
}
}
obj_id_offset_buff_[obj_offset_size] = obj_count;
// construct min_dist, max_dist and order_obj for non-ground, non-sky object
obj_min_dist_buff_ = new unsigned char[*n_obj_];
obj_order_buff_ = new unsigned char[*n_obj_];
for (unsigned obj_id = 0; obj_id < *n_obj_; obj_id++) {
obj_min_dist_buff_[obj_id] = query_->min_obj_dist()[obj_id];
obj_order_buff_[obj_id] = query_->order_obj()[obj_id];
}
// construct land and land_wgt fallback category buffer
obj_land_buff_ = new unsigned char[*n_obj_*fallback_size_];
obj_land_wgt_buff_ = new float[*n_obj_*fallback_size_];
unsigned cnt = 0;
for (unsigned obj_id = 0; obj_id < *n_obj_; obj_id++) {
for (unsigned ii = 0; ii < fallback_size_; ii++) {
obj_land_buff_[cnt] = query_->obj_land_id()[obj_id][ii];
obj_land_wgt_buff_[cnt++] = query_->obj_land_wgt()[obj_id][ii];
}
}
#if 0
// check the constucted 1D obj_land_array
vcl_cout << " ---------------- in the volm_query ----------------- " << vcl_endl;
for (unsigned obj_id = 0; obj_id < *n_obj_; obj_id++) {
vcl_cout << " obj_id = " << obj_id << " ---> lnd_id = [";
for (vcl_vector<unsigned char>::iterator vit = query_->obj_land_id()[obj_id].begin(); vit != query_->obj_land_id()[obj_id].end(); ++vit)
vcl_cout << (int)(*vit) << ' ';
vcl_cout << "] ---> lnd_wgt = [";
for (vcl_vector<float>::iterator vit = query_->obj_land_wgt()[obj_id].begin(); vit != query_->obj_land_wgt()[obj_id].end(); ++vit)
vcl_cout << *vit << ' ';
vcl_cout << ']' << vcl_endl;
}
// I am back
vcl_cout << " ++++++++++++++++ in the 1D array ++++++++++++++++++++++ " << vcl_endl;
for (unsigned k = 0; k < *n_obj_; k++) {
vcl_cout << " obj_id = " << k << " ---> lnd_id_buff = [";
unsigned land_start = k*fallback_size_;
unsigned land_end = (k+1)*fallback_size_;
for (unsigned ii = land_start; ii < land_end; ii++)
vcl_cout << (int)obj_land_buff_[ii] << ' ';
vcl_cout << "] ---> lnd_wgt_buff = [";
for (unsigned ii = land_start; ii < land_end; ii++)
vcl_cout << obj_land_wgt_buff_[ii] << ' ';
vcl_cout << ']' << vcl_endl;
}
#endif
// create corresponding cl_mem
obj_id_cl_mem_ = new bocl_mem(gpu_->context(), obj_id_buff_, sizeof(unsigned)*obj_vox_size, " obj_id " );
if (!obj_id_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for OBJ_ID\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
if (is_grd_reg_) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
}
if (is_obj_reg_) { delete sky_id_cl_mem_; delete sky_id_offset_cl_mem_; }
delete obj_id_cl_mem_;
return false;
}
obj_id_offset_cl_mem_ = new bocl_mem(gpu_->context(), obj_id_offset_buff_, sizeof(unsigned)*(obj_offset_size+1), " obj_offset " );
if (!obj_id_offset_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for OBJ_OFFSET\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
if (is_grd_reg_) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
}
if (is_obj_reg_) { delete sky_id_cl_mem_; delete sky_id_offset_cl_mem_; }
delete obj_id_cl_mem_; delete obj_id_offset_cl_mem_;
return false;
}
obj_min_dist_cl_mem_ = new bocl_mem(gpu_->context(), obj_min_dist_buff_, sizeof(unsigned char)*(*n_obj_), " obj_min_dist " );
if (!obj_min_dist_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for OBJ_MIN_DIST\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
if (is_grd_reg_) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
}
if (is_obj_reg_) { delete sky_id_cl_mem_; delete sky_id_offset_cl_mem_; delete sky_weight_cl_mem_; }
delete obj_id_cl_mem_; delete obj_id_offset_cl_mem_;
delete obj_min_dist_cl_mem_;
return false;
}
obj_order_cl_mem_ = new bocl_mem(gpu_->context(), obj_order_buff_, sizeof(unsigned char)*(*n_obj_), " obj_order " );
if (!obj_order_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for OBJ_ORDER\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
if (is_grd_reg_) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
}
if (is_obj_reg_) { delete sky_id_cl_mem_; delete sky_id_offset_cl_mem_; }
delete obj_id_cl_mem_; delete obj_id_offset_cl_mem_;
delete obj_min_dist_cl_mem_; delete obj_order_cl_mem_;
return false;
}
obj_land_cl_mem_ = new bocl_mem(gpu_->context(), obj_land_buff_, sizeof(unsigned char)*(*n_obj_)*fallback_size_, " obj_land " );
if (!obj_land_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for OBJ_LAND\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
if (is_grd_reg_) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
}
if (is_obj_reg_) { delete sky_id_cl_mem_; delete sky_id_offset_cl_mem_; }
delete obj_id_cl_mem_; delete obj_id_offset_cl_mem_;
delete obj_min_dist_cl_mem_; delete obj_order_cl_mem_;
delete obj_land_cl_mem_;
return false;
}
obj_land_wgt_cl_mem_ = new bocl_mem(gpu_->context(), obj_land_wgt_buff_, sizeof(float)*(*n_obj_)*fallback_size_, " obj_land_wgt ");
if (!obj_land_wgt_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for OBJ_LAND_WGT\n";
this->clean_weight_cl_mem();
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
if (is_grd_reg_) {
delete grd_id_cl_mem_; delete grd_dist_cl_mem_; delete grd_id_offset_cl_mem_;
delete grd_land_cl_mem_; delete grd_land_wgt_cl_mem_;
}
if (is_obj_reg_) { delete sky_id_cl_mem_; delete sky_id_offset_cl_mem_; }
delete obj_id_cl_mem_; delete obj_id_offset_cl_mem_;
delete obj_min_dist_cl_mem_; delete obj_order_cl_mem_;
delete obj_land_cl_mem_; delete obj_land_wgt_cl_mem_;
return false;
}
query_global_mem_ += sizeof(unsigned)*obj_vox_size; // object id
query_global_mem_ += sizeof(unsigned)*query_->dist_offset().size(); // object offset
query_global_mem_ += sizeof(unsigned char)*(*n_obj_); // object dist
query_global_mem_ += (sizeof(unsigned char) + sizeof(float))*fallback_size_*(*n_obj_); // object land fallback category and fallback weight
query_local_mem_ += sizeof(unsigned char)*(*n_obj_); // object dist on local memory
query_local_mem_ += (sizeof(unsigned char) + sizeof(float))*fallback_size_*(*n_obj_); // object land category on local memory
}
// implement orientation transfer function
if (!this->transfer_orient()) {
vcl_cerr << " ERROR: transfering query orientation failed\n";
return false;
}
// count the memory for other stuff
query_global_mem_ += sizeof(unsigned); // depth_interval length
query_global_mem_ += sizeof(float)*depth_interval_.size(); // depth_interval buffer
query_global_mem_ += sizeof(unsigned)*2; // layer_size and fallback size
query_local_mem_ += sizeof(unsigned); // depth_interval length
query_local_mem_ += sizeof(float)*depth_interval_.size(); // depth_interval buffer
query_local_mem_ += sizeof(unsigned)*2; // layer_size and fallback size
// check the device memory
// check whether query size has exceeded the available global memory and local_memory
device_global_mem_ = (gpu_->info()).total_global_memory_;
device_local_mem_ = (gpu_->info()).total_local_memory_;
if (query_global_mem_ > device_global_mem_) {
vcl_cerr << " ERROR: the required global memory for query " << query_global_mem_/1073741824.0
<< " GByte is larger than available global memory " << device_global_mem_/1073741824.0
<< " GB\n";
this->clean_query_cl_mem();
return false;
}
if (query_local_mem_ > device_local_mem_) {
vcl_cerr << " ERROR: the required local memoery for query " << query_local_mem_/1024.0
<< " KByte is larger than available local memory " << device_local_mem_/1024.0
<< " KB\n";
this->clean_query_cl_mem();
return false;
}
return true;
}
bool boxm2_volm_matcher_p1::transfer_orient()
{
// temporary only consider horizontal and front_parallel
vcl_vector<unsigned char>& obj_orient = query_->obj_orient();
obj_orient_buff_ = new unsigned char[*n_obj_];
for (unsigned i = 0; i < *n_obj_; i++) {
if (obj_orient[i] == depth_map_region::HORIZONTAL)
obj_orient_buff_[i] = (unsigned char)1; // horizontal
else
obj_orient_buff_[i] = (unsigned char)2; // vertical(front parallel)
}
#if 0
// check the transferred orientation
for (unsigned i = 0; i < *n_obj_; i++) {
vcl_cout << " i = " << i << ", query_orient = " << (int)obj_orient[i]
<< ", orient_buff = " << (int)obj_orient_buff_[i] << vcl_endl;
}
#endif
// declare cl_mem for orient
obj_orient_cl_mem_ = new bocl_mem(gpu_->context(), obj_orient_buff_, sizeof(unsigned char)*(*n_obj_), " obj_orient ");
if (!obj_orient_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for OBJ_ORIENT\n";
this->clean_query_cl_mem();
return false;
}
// count the memory usage
query_global_mem_ += sizeof(unsigned char)*(*n_obj_); // obj orientation on global memory
query_local_mem_ += sizeof(unsigned char)*(*n_obj_); // obj orientation on local memory
return true;
}
bool boxm2_volm_matcher_p1::transfer_weight()
{
// construct all the weight buffer and cl_mem
// Note that the order of the weight parameters in the weight_param.txt has been ordered accordingly
if (is_sky_reg_ && is_grd_reg_) {
if (this->weights_.size() != (2+(*n_obj_)) ) {
vcl_cerr << "\n ERROR: inconsistancy between volm_query and volm_weight\n";
return false;
}
// sky is line 1, grd is line 2
sky_weight_buff_ = new float; *sky_weight_buff_ = this->weights_[0].w_obj_;
grd_weight_buff_ = new float; *grd_weight_buff_ = this->weights_[1].w_obj_;
grd_wgt_attri_buff_ = new float[3];
grd_wgt_attri_buff_[0] = this->weights_[1].w_ori_;
grd_wgt_attri_buff_[1] = this->weights_[1].w_lnd_;
grd_wgt_attri_buff_[2] = this->weights_[1].w_dst_;
obj_weight_buff_ = new float[*n_obj_];
obj_wgt_attri_buff_ = new float[4*(*n_obj_)];
unsigned cnt = 0;
for (unsigned i = 2; i < this->weights_.size(); i++) {
obj_wgt_attri_buff_[cnt*4] = this->weights_[i].w_ori_;
obj_wgt_attri_buff_[cnt*4+1] = this->weights_[i].w_lnd_;
obj_wgt_attri_buff_[cnt*4+2] = this->weights_[i].w_dst_;
obj_wgt_attri_buff_[cnt*4+3] = this->weights_[i].w_ord_;
obj_weight_buff_[cnt++] = this->weights_[i].w_obj_;
}
// create cl_mem for sky and ground
sky_weight_cl_mem_ = new bocl_mem(gpu_->context(), sky_weight_buff_, sizeof(float), " sky_weight " );
if (!sky_weight_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for SKY_WEIGHT\n";
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
delete sky_weight_cl_mem_;
return false;
}
grd_weight_cl_mem_ = new bocl_mem(gpu_->context(), grd_weight_buff_, sizeof(float), " grd_weight " );
if (!grd_weight_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: create bocl_mem failed for GRD_WEIGHT\n";
delete sky_weight_cl_mem_;
delete grd_weight_cl_mem_;
return false;
}
grd_wgt_attri_cl_mem_ = new bocl_mem(gpu_->context(), grd_wgt_attri_buff_, 3*sizeof(float), " grd_wgt_attri " );
if (!grd_wgt_attri_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: create bocl_mem failed for GRD_WGT_ATTRI\n";
delete sky_weight_cl_mem_;
delete grd_weight_cl_mem_; delete grd_wgt_attri_cl_mem_;
return false;
}
// count the memoery usage
query_global_mem_ += sizeof(float); // sky weight
query_global_mem_ += sizeof(float)*4; // grd weight and grd weight for attributes
query_global_mem_ += sizeof(float)*5*(*n_obj_); // obj weight and obj weight for attributes
query_local_mem_ += sizeof(float); // sky weight
query_local_mem_ += sizeof(float)*4; // grd weight and grd weight for attributes
query_local_mem_ += sizeof(float)*4*(*n_obj_); // obj weight and obj weight for attributes
}
else if (is_sky_reg_) {
for (vcl_vector<volm_weight>::iterator vit = this->weights_.begin(); vit != this->weights_.end(); ++vit)
vcl_cerr << ' ' << vit->w_typ_ << ' ' << vit->w_ori_ << ' ' << vit->w_lnd_ << ' ' << vit->w_dst_ << ' ' << vit->w_ord_ << ' ' << vit->w_obj_ << vcl_endl;
if (this->weights_.size() != (1+(*n_obj_)) ) {
vcl_cerr << "\n ERROR: inconsistancy between volm_query and volm_weight\n";
return false;
}
// sky is line 1, all other objects starts from line 2
sky_weight_buff_ = new float; *sky_weight_buff_ = this->weights_[0].w_obj_;
obj_weight_buff_ = new float[*n_obj_];
obj_wgt_attri_buff_ = new float[4*(*n_obj_)];
unsigned cnt = 0;
for (unsigned i = 1; i < this->weights_.size(); i++) {
obj_wgt_attri_buff_[cnt*4] = this->weights_[i].w_ori_;
obj_wgt_attri_buff_[cnt*4+1] = this->weights_[i].w_lnd_;
obj_wgt_attri_buff_[cnt*4+2] = this->weights_[i].w_dst_;
obj_wgt_attri_buff_[cnt*4+3] = this->weights_[i].w_ord_;
obj_weight_buff_[cnt++] = this->weights_[i].w_obj_;
}
// create cl_mem for sky
sky_weight_cl_mem_ = new bocl_mem(gpu_->context(), sky_weight_buff_, sizeof(float), " sky_weight " );
if (!sky_weight_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: creating bocl_mem failed for SKY_WEIGHT\n";
delete n_cam_cl_mem_; delete n_obj_cl_mem_;
delete sky_weight_cl_mem_;
return false;
}
// count the memoery usage
query_global_mem_ += sizeof(float); // sky weight
query_global_mem_ += sizeof(float)*5*(*n_obj_); // obj weight and obj weight for attributes
query_local_mem_ += sizeof(float); // sky weight
query_local_mem_ += sizeof(float)*4*(*n_obj_); // obj weight and obj weight for attributes
}
else if (is_grd_reg_) {
if (this->weights_.size() != (1+(*n_obj_)) ) {
vcl_cerr << "\n ERROR: inconsistancy between volm_query and volm_weight\n";
return false;
}
// grd is line 1, objects starts from line 2
grd_weight_buff_ = new float; *grd_weight_buff_ = this->weights_[0].w_obj_;
grd_wgt_attri_buff_ = new float[3];
grd_wgt_attri_buff_[0] = this->weights_[0].w_ori_;
grd_wgt_attri_buff_[1] = this->weights_[0].w_lnd_;
grd_wgt_attri_buff_[2] = this->weights_[0].w_dst_;
obj_weight_buff_ = new float[*n_obj_];
obj_wgt_attri_buff_ = new float[4*(*n_obj_)];
unsigned cnt = 0;
for (unsigned i = 1; i < this->weights_.size(); i++) {
obj_wgt_attri_buff_[cnt*4] = this->weights_[i].w_ori_;
obj_wgt_attri_buff_[cnt*4+1] = this->weights_[i].w_lnd_;
obj_wgt_attri_buff_[cnt*4+2] = this->weights_[i].w_dst_;
obj_wgt_attri_buff_[cnt*4+3] = this->weights_[i].w_ord_;
obj_weight_buff_[cnt++] = this->weights_[i].w_obj_;
}
// create cl_mem for ground
grd_weight_cl_mem_ = new bocl_mem(gpu_->context(), grd_weight_buff_, sizeof(float), " grd_weight " );
if (!grd_weight_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: create bocl_mem failed for GRD_WEIGHT\n";
delete grd_weight_cl_mem_;
return false;
}
grd_wgt_attri_cl_mem_ = new bocl_mem(gpu_->context(), grd_wgt_attri_buff_, 3*sizeof(float), " grd_wgt_attri " );
if (!grd_wgt_attri_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: create bocl_mem failed for GRD_WGT_ATTRI\n";
delete grd_weight_cl_mem_; delete grd_wgt_attri_cl_mem_;
return false;
}
// count the memoery usage
query_global_mem_ += sizeof(float)*4; // grd weight and grd weight for attributes
query_global_mem_ += sizeof(float)*5*(*n_obj_); // obj weight and obj weight for attributes
query_local_mem_ += sizeof(float)*4; // grd weight and grd weight for attributes
query_local_mem_ += sizeof(float)*4*(*n_obj_); // obj weight and obj weight for attributes
}
else {
if (this->weights_.size() != (*n_obj_) ) {
vcl_cerr << "\n ERROR: inconsistancy between volm_query and volm_weight\n";
return false;
}
// objects starts from line1
obj_weight_buff_ = new float[*n_obj_];
obj_wgt_attri_buff_ = new float[4*(*n_obj_)];
for (unsigned i = 0; i < this->weights_.size(); i++) {
obj_wgt_attri_buff_[i*4] = this->weights_[i].w_ori_;
obj_wgt_attri_buff_[i*4+1] = this->weights_[i].w_lnd_;
obj_wgt_attri_buff_[i*4+2] = this->weights_[i].w_dst_;
obj_wgt_attri_buff_[i*4+3] = this->weights_[i].w_ord_;
obj_weight_buff_[i] = this->weights_[i].w_obj_;
}
// count the memoery usage
query_global_mem_ += sizeof(float)*5*(*n_obj_); // obj weight and obj weight for attributes
query_local_mem_ += sizeof(float)*4*(*n_obj_); // obj weight and obj weight for attributes
}
// create cl_mem for non sky/grd objects
obj_weight_cl_mem_ = new bocl_mem(gpu_->context(), obj_weight_buff_, (*n_obj_)*sizeof(float), " obj_weight ");
if (!obj_weight_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: create bocl_mem failed for OBJ_WEIGHT\n";
delete sky_weight_cl_mem_;
delete grd_weight_cl_mem_; delete grd_wgt_attri_cl_mem_;
delete obj_weight_cl_mem_;
return false;
}
obj_wgt_attri_cl_mem_ = new bocl_mem(gpu_->context(), obj_wgt_attri_buff_, 4*(*n_obj_)*sizeof(float), " obj_wgt_attri " );
if (!obj_wgt_attri_cl_mem_->create_buffer( CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR )) {
vcl_cerr << "\n ERROR: create bocl_mem fialed for OBJ_WGT_ATTRI\n";
delete sky_weight_cl_mem_;
delete grd_weight_cl_mem_; delete grd_wgt_attri_cl_mem_;
delete obj_weight_cl_mem_; delete obj_wgt_attri_cl_mem_;
return false;
}
#if 0
// screen output
vcl_cout << " we have weight parameters in p1_matcher as follows " << vcl_endl;
for (vcl_vector<volm_weight>::iterator vit = this->weights_.begin(); vit != this->weights_.end(); ++vit)
vcl_cout << ' ' << vit->w_typ_ << ' ' << vit->w_ori_ << ' ' << vit->w_lnd_ << ' ' << vit->w_dst_ << ' ' << vit->w_ord_ << ' ' << vit->w_obj_ << vcl_endl;
vcl_cout << " ---------------- WEIGHT BUFFER ------------------ " << vcl_endl;
if (is_sky_reg_ && is_grd_reg_) {
vcl_cout << ' ' << "sky" << " 0.0 0.0 1.0 0.0 " << *sky_weight_buff_ << vcl_endl;
vcl_cout << ' ' << "ground " << grd_wgt_attri_buff_[0] << ' ' << grd_wgt_attri_buff_[1] << ' ' << grd_wgt_attri_buff_[2] << " 0.0 " << *grd_weight_buff_ << vcl_endl;
}
else if (is_sky_reg_) {
vcl_cout << ' ' << "sky" << " 0.0 0.0 1.0 0.0 " << *sky_weight_buff_ << vcl_endl;
}
else if (is_grd_reg_) {
vcl_cout << ' ' << "ground " << grd_wgt_attri_buff_[0] << ' ' << grd_wgt_attri_buff_[1] << ' ' << grd_wgt_attri_buff_[2] << " 0.0 " << *grd_weight_buff_ << vcl_endl;
}
for (unsigned i = 0; i < (*n_obj_); i++) {
vcl_cout << ' ' << "object " << obj_wgt_attri_buff_[i*4] << ' '
<< obj_wgt_attri_buff_[i*4+1] << ' '
<< obj_wgt_attri_buff_[i*4+2] << ' '
<< obj_wgt_attri_buff_[i*4+3] << ' '
<< obj_weight_buff_[i] << vcl_endl;
}
#endif
return true;
}
bool boxm2_volm_matcher_p1::volm_matcher_p1_test_ori(unsigned n_ind,
unsigned char* index,
unsigned char* index_orient,
unsigned char* index_land,
float* score_buff,
float* mu_buff)
{
vcl_vector<float> mu;
unsigned nc = *n_cam_;
unsigned no = *n_obj_;
if (is_obj_reg_) {
// calcualte mean depth value
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++) {
unsigned start_ind = ind_id * (layer_size_);
for (unsigned cam_id = 0; cam_id < nc; cam_id++) {
for (unsigned k = 0; k < no; k++) {
unsigned offset_id = k + no * cam_id;
unsigned start_obj = obj_id_offset_buff_[offset_id];
unsigned end_obj = obj_id_offset_buff_[offset_id+1];
float mu_obj = 0;
unsigned cnt = 0;
for (unsigned i = start_obj; i < end_obj; i++) {
unsigned id = start_ind + obj_id_buff_[i];
if ( (unsigned)index[id] < 253 && (unsigned)index[id] < depth_interval_.size() ) {
mu_obj += depth_interval_[index[id]];
cnt += 1;
}
}
mu_obj = (cnt > 0) ? mu_obj/cnt : 0;
mu.push_back(mu_obj);
}
}
}
}
// calculate sky score
vcl_vector<float> score_sky_all;
if (is_sky_reg_) {
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++) {
unsigned start_ind = ind_id * (layer_size_);
for (unsigned cam_id = 0; cam_id < nc; cam_id++) {
unsigned start = sky_id_offset_buff_[cam_id];
unsigned end = sky_id_offset_buff_[cam_id+1];
unsigned cnt = 0;
for (unsigned k = start; k < end; k++) {
unsigned id = start_ind + sky_id_buff_[k];
if (index[id] == 254) cnt++;
}
float score_sky = (end != start) ? (float)cnt/(end-start) : 0.0f;
score_sky *= (*sky_weight_buff_);
score_sky_all.push_back(score_sky);
}
}
}
// calculate the ground score
// define the altitude ratio, suppose the altitude in index could ba up to 3 meter
// assuming the read-in alt values in query is normally ~1m, the altiutide ratio would be (2-1)/1 ~2
// the altitude ratio defined the tolerance for ground distance d as delta_d = alt_ratio * d
// this delte_d coule be in depth_interval unit ?
unsigned char alt_ratio = 2;
vcl_vector<float> score_grd_all;
vcl_vector<float> score_grd_dist_all;
vcl_vector<float> score_grd_ori_all;
vcl_vector<float> score_grd_lnd_all;
if (is_grd_reg_) {
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++) {
unsigned start_ind = ind_id * layer_size_;
for (unsigned cam_id = 0; cam_id < nc; cam_id++) {
unsigned start = grd_id_offset_buff_[cam_id];
unsigned end = grd_id_offset_buff_[cam_id+1];
unsigned cnt = 0;
unsigned ori_cnt = 0;
float score_lnd = 0.0f;
for (unsigned k = start; k < end; k++) {
unsigned id = start_ind + grd_id_buff_[k];
unsigned char ind_d = index[id];
unsigned char ind_lnd = index_land[id];
unsigned char grd_d = grd_dist_buff_[k];
// use phyical distance to compute the grd_dst score
if(ind_id < depth_interval_.size() && grd_d < depth_interval_.size()) {
float ind_dst = depth_interval_[ind_d];
float grd_dst = depth_interval_[grd_d];
float delta_d = alt_ratio * grd_dst;
if ( ind_dst >= (grd_dst-delta_d) && ind_dst <= (grd_dst+delta_d) ) cnt++;
}
// calculate ground orientation score, ground plane has to be horizontal
if (index_orient[id] == 1) ori_cnt++;
// calcualte the ground land type score
//vcl_cout << (int)ind_lnd << vcl_endl;
if( ind_lnd != 0 && ind_lnd != 254) {
for (unsigned ii = 0; ii < *fallback_size_buff_; ii++) {
unsigned lnd_id = k*(*fallback_size_buff_)+ii;
if (ind_lnd == grd_land_buff_[lnd_id]) {
score_lnd += grd_land_wgt_buff_[lnd_id];
break;
}
}
}
}
float score_grd_dst = (end!=start) ? (float)cnt/(end-start) : 0.0f;
float score_grd_ori = (end!=start) ? (float)ori_cnt/(end-start) : 0.0f;
float score_grd_lnd = (end!=start) ? score_lnd/(end-start) : 0.0f;
score_grd_dst *= (*grd_weight_buff_);
score_grd_ori *= (*grd_weight_buff_);
score_grd_lnd *= (*grd_weight_buff_);
score_grd_lnd_all.push_back(score_grd_lnd);
score_grd_all.push_back(this->grd_wgt_attri_buff_[0] * score_grd_ori +
this->grd_wgt_attri_buff_[1] * score_grd_lnd +
this->grd_wgt_attri_buff_[2] * score_grd_dst);
}
}
}
// calculate the object score
vcl_vector<float> score_ord_all;
vcl_vector<float> score_min_all;
vcl_vector<float> score_ori_all;
vcl_vector<float> score_lnd_all;
if ( is_obj_reg_)
{
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++)
{
unsigned start_ind = ind_id * layer_size_;
for (unsigned cam_id = 0; cam_id < nc; cam_id++)
{
float score_order = 0.0f;
float score_min = 0.0f;
float score_ori = 0.0f;
float score_lnd = 0.0f;
unsigned mu_start_id = cam_id*no + ind_id*no*nc;
for (unsigned k = 0; k < no; k++)
{
// i am here
unsigned offset_id = k + no*cam_id;
unsigned start_obj = obj_id_offset_buff_[offset_id];
unsigned end_obj = obj_id_offset_buff_[offset_id+1];
unsigned min_k = obj_min_dist_buff_[k];
unsigned lnd_start = k*(*fallback_size_buff_);
unsigned lnd_end = (k+1)*(*fallback_size_buff_);
float score_ord_k = 0.0f;
float score_min_k = 0.0f;
float score_ori_k = 0.0f;
float score_lnd_k = 0.0f;
for (unsigned i = start_obj; i < end_obj; i++) {
unsigned id = start_ind + obj_id_buff_[i];
unsigned d = index[id];
unsigned s_ord = 1;
unsigned s_min = 0;
unsigned s_ori = 0;
if ( d < 253 && d < depth_interval_.size()) { // valid object voxel
// do the order checking
for (unsigned mu_id = 0; (mu_id < k && s_ord); mu_id++) {
float depth_d = depth_interval_[d];
float depth_m = mu[mu_id+mu_start_id];
if (mu[mu_id+mu_start_id]*mu[mu_id+mu_start_id] > 1E-7)
s_ord = s_ord * ( depth_interval_[d] - mu[mu_id+mu_start_id] > -1E-5 );
}
for (unsigned mu_id = k+1; (mu_id < no && s_ord); mu_id++) {
float depth_d = depth_interval_[d];
float depth_m = mu[mu_id+mu_start_id];
if (mu[mu_id+mu_start_id]*mu[mu_id+mu_start_id] > 1E-7)
s_ord = s_ord * ( depth_interval_[d] - mu[mu_id+mu_start_id] < 1E-5 );
}
if ( d > obj_min_dist_buff_[k] )
s_min = 1;
}
else {
s_ord = 0;
}
#if 0
if (ind_id == 0 && cam_id == 245 && k == 4) {
vcl_cout << " ind_id = " << ind_id
<< " cam_id = " << cam_id
<< " k = " << k
<< " i = " << i
<< " d_size = " << depth_interval_.size()
<< " d = " << d
<< " depth = " << depth_interval_[d]
<< " s_ord = " << s_ord
<< vcl_endl;
}
#endif
// check the orientation score
unsigned char ind_ori = index_orient[id];
if ( ind_ori > 0 && ind_ori < 10) {
if ( ind_ori == obj_orient_buff_[k] )
s_ori = 1;
if ( ind_ori != 1 && obj_orient_buff_[k]==2 )
s_ori = 1;
}
// check the land_type score
unsigned char ind_lnd = index_land[id];
//vcl_cout << (int)ind_lnd << vcl_endl;
if (ind_lnd != 0 && ind_lnd != 254) {
for (unsigned ii = lnd_start; ii < lnd_end; ii++) {
if (ind_lnd == obj_land_buff_[ii]) {
score_lnd_k += obj_land_wgt_buff_[ii];
break;
}
}
}
score_ord_k += (float)s_ord;
score_min_k += (float)s_min;
score_ori_k += (float)s_ori;
} // end for loop over voxel in object k
// normalized the order score for object k
score_ord_k = (end_obj != start_obj) ? score_ord_k/(end_obj-start_obj) : 0;
score_ord_k = score_ord_k * obj_weight_buff_[k] * this->obj_wgt_attri_buff_[4*k+3];
#if 0
if (ind_id == 0 && cam_id == 245) {
vcl_cout << " ind_id = " << ind_id
<< " cam_id = " << cam_id
<< " k = " << k
<< " num_rays = " << end_obj - start_obj
<< " score_ord = " << score_ord_k
<< vcl_endl;
}
#endif
// normalized the min_dist score for object k
score_min_k = (end_obj != start_obj) ? score_min_k/(end_obj-start_obj) : 0;
score_min_k = score_min_k * obj_weight_buff_[k] * this->obj_wgt_attri_buff_[4*k+2];
// normalized the orient score for object k
score_ori_k = (end_obj != start_obj) ? score_ori_k/(end_obj-start_obj) : 0;
score_ori_k = score_ori_k * obj_weight_buff_[k] * this->obj_wgt_attri_buff_[4*k+0];
// normalized the land type score for object k
score_lnd_k = (end_obj != start_obj) ? score_lnd_k/(end_obj-start_obj) : 0;
score_lnd_k = score_lnd_k * obj_weight_buff_[k] * this->obj_wgt_attri_buff_[4*k+1];
// summerize order score for index ind_id and camera cam_id
score_order += score_ord_k;
score_min += score_min_k;
score_ori += score_ori_k;
score_lnd += score_lnd_k;
} // end for loop over objects
score_ord_all.push_back(score_order);
score_min_all.push_back(score_min);
score_ori_all.push_back(score_ori);
score_lnd_all.push_back(score_lnd);
} // end of loop over cameras
} // end of loop over indices
}
// get the overall object score
vcl_vector<float> score_obj_all;
// get the overall score
vcl_vector<float> score_all;
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++) {
for (unsigned cam_id = 0; cam_id < nc; cam_id++) {
unsigned id = cam_id + ind_id * nc;
if (is_grd_reg_ && is_sky_reg_)
score_all.push_back(score_sky_all[id] + score_grd_all[id] + score_ord_all[id] + score_min_all[id] + score_ori_all[id] + score_lnd_all[id]);
else if ( !is_grd_reg_ && is_sky_reg_)
score_all.push_back(score_sky_all[id] + score_ord_all[id] + score_min_all[id] + score_ori_all[id] + score_lnd_all[id]);
else if ( !is_sky_reg_ && is_grd_reg_)
score_all.push_back(score_grd_all[id] + score_ord_all[id] + score_min_all[id] + score_ori_all[id] + score_lnd_all[id]);
else
score_all.push_back(score_ord_all[id] + score_min_all[id] + score_ori_all[id] + score_lnd_all[id]);
score_obj_all.push_back(score_ord_all[id] + score_min_all[id] + score_ori_all[id] + score_lnd_all[id]);
}
}
// output all sky and ground score for checking
#if 0
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++) {
for (unsigned cam_id = 0; cam_id < nc; cam_id++) {
unsigned id = cam_id + ind_id * nc;
if (cam_id == 245 && ind_id == 0)
vcl_cout << " ind_id = " << ind_id << " cam_id = " << cam_id
<< " score_sky[" << id << "] = " << score_sky_all[id]
//<< "\t score_grd[" << id << "] = " << score_grd_all[id]
//<< "\t score_grd_lnd[" << id << "] = " << score_grd_lnd_all[id]
<< "\t score_ord[" << id << "] = " << score_ord_all[id]
<< "\t score_min[" << id << "] = " << score_min_all[id]
<< "\t score_ori[" << id << "] = " << score_ori_all[id]
<< "\t score_lnd[" << id << "] = " << score_lnd_all[id]
<< "\t score_obj[" << id << "] = " << score_ord_all[id] + score_min_all[id] + score_ori_all[id]
<< vcl_endl;
}
}
#endif
// output for objects
#if 0
// mean values
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++)
for (unsigned cam_id = 0; cam_id < nc; cam_id++)
for (unsigned obj_id = 0; obj_id < no; obj_id++) {
unsigned start_ind = ind_id * layer_size_;
unsigned id = obj_id + cam_id * no + ind_id * nc * no;
//if ( mu[id] - mu_buff[id] != 0) {
if(ind_id == 0 && cam_id == 245) {
vcl_cout << " ind_id = " << ind_id
<< " cam_id = " << cam_id
<< " obj_id = " << obj_id
<< " start_ind = " << start_ind
<< " id = " << id
<< " mu_cpu = " << mu[id]
<< " mu_gpu = " << mu_buff[id]
<< " mu_diff = " << mu[id] - mu_buff[id]
<< vcl_endl;
}
//}
}
#endif
#if 0
// score for object
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++)
for (unsigned cam_id = 0; cam_id < nc; cam_id++)
for (unsigned obj_id = 0; obj_id < no; obj_id++) {
unsigned id = cam_id + ind_id * nc;
//if ( mu[id] - mu_buff[id] != 0) {
vcl_cout << " ind_id = " << ind_id
<< " cam_id = " << cam_id
<< " obj_id = " << obj_id
<< " id = " << id
<< "\t score_order = " << score_order_all[id]
<< "\t score_min = " << score_min_all[id]
<< "\t score_ori = " << score_ori_all[id]
<< vcl_endl;
}
#endif
// check the score output
for (unsigned ind_id = 0; ind_id < n_ind; ind_id++) {
for (unsigned cam_id = 0; cam_id < nc; cam_id++) {
unsigned id = cam_id + ind_id * nc;
//if(ind_id == 0 && cam_id == 245)
vcl_cout << " ind = " << ind_id << " cam = " << cam_id << " id = " << id
<< "\t score_cpu = " << score_all[id]
<< "\t score_gpu = " << score_buff[id]
<< "\t diff = " << score_all[id] - score_buff[id]
<< vcl_endl;
}
}
return true;
}