main.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #include "../core/DetectionSequence.h"
  5. #include "../util/FileIO.h"
  6. #include "../util/Parser.h"
  7. #include "../algo/NStage.h"
  8. #include "../util/Visualizer.h"
  9. #include "../util/Logger.h"
  10. #include "../core/ObjectDataAngular.h"
  11. #include "../algo/Berclaz.h"
  12. #include "../algo/KShortestPaths.h"
  13. #include <boost/program_options.hpp>
  14. #include <boost/graph/named_function_params.hpp>
  15. #include <boost/graph/bellman_ford_shortest_paths.hpp>
  16. #include <iomanip>
  17. struct
  18. {
  19. std::string max_frame_skip;
  20. std::string max_tracklet_count;
  21. std::string penalty_value;
  22. double edge_weight_threshold;
  23. } n_stage_params;
  24. void RunNStage(core::DetectionSequence& sequence, std::vector<core::TrackletPtr>& tracks)
  25. {
  26. util::Logger::LogInfo("Running n-stage");
  27. std::vector<size_t> max_frame_skips;
  28. std::vector<double> penalty_values;
  29. std::vector<size_t> max_tracklet_counts;
  30. // Parse strings to vectors
  31. size_t d_index;
  32. std::string str, part;
  33. str = n_stage_params.max_frame_skip;
  34. do
  35. {
  36. d_index = str.find(",");
  37. part = str.substr(0, d_index);
  38. if (part.size() > 0)
  39. {
  40. max_frame_skips.push_back((unsigned long&&) atoi(part.c_str()));
  41. }
  42. str = str.substr(d_index + 1);
  43. }
  44. while (d_index != std::string::npos);
  45. str = n_stage_params.penalty_value;
  46. do
  47. {
  48. d_index = str.find(",");
  49. part = str.substr(0, d_index);
  50. if (part.size() > 0)
  51. {
  52. penalty_values.push_back(std::atof(part.c_str()));
  53. }
  54. str = str.substr(d_index + 1);
  55. }
  56. while (d_index != std::string::npos);
  57. str = n_stage_params.max_tracklet_count;
  58. do
  59. {
  60. d_index = str.find(",");
  61. part = str.substr(0, d_index);
  62. if (part.size() > 0)
  63. {
  64. max_tracklet_counts.push_back((unsigned long&&) atoi(part.c_str()));
  65. }
  66. str = str.substr(d_index + 1);
  67. }
  68. while (d_index != std::string::npos);
  69. // Init n-stage
  70. algo::NStage n_stage(max_frame_skips, penalty_values, max_tracklet_counts,
  71. n_stage_params.edge_weight_threshold);
  72. n_stage.Run(sequence, tracks);
  73. // Interpolate tracks
  74. for (auto track : tracks)
  75. {
  76. track->InterpolateMissingFrames();
  77. }
  78. util::Logger::LogInfo("Finished");
  79. }
  80. struct
  81. {
  82. int h_res;
  83. int v_res;
  84. int vicinity_size;
  85. size_t batch_size;
  86. size_t max_track_count;
  87. util::Filter2D filter;
  88. } berclaz_params;
  89. void RunBerclaz(core::DetectionSequence & sequence, std::vector<core::TrackletPtr> & tracks)
  90. {
  91. util::Logger::LogInfo("Running berclaz");
  92. // Init berclaz
  93. algo::Berclaz berclaz(berclaz_params.h_res,
  94. berclaz_params.v_res,
  95. berclaz_params.vicinity_size);
  96. berclaz.Run(sequence,
  97. berclaz_params.batch_size,
  98. berclaz_params.max_track_count,
  99. tracks,
  100. berclaz_params.filter);
  101. util::Logger::LogInfo("Interpolate tracks");
  102. // Interpolate tracks
  103. for (auto track : tracks)
  104. {
  105. track->InterpolateMissingFrames();
  106. }
  107. util::Logger::LogInfo("Finished");
  108. }
  109. void Run(int argc, char const * const * argv)
  110. {
  111. // Algorithm independent values
  112. std::string input_file, output_path, images_folder, algorithm, config_path, header;
  113. std::string input_format, berclaz_filter;
  114. bool info, debug, display, output, output_images, show_grid;
  115. char input_delimiter, output_delimiter;
  116. double temporal_weight, spatial_weight, angular_weight, image_width, image_height;
  117. boost::program_options::options_description opts("Allowed options");
  118. opts.add_options()
  119. ("help",
  120. "produce help message")
  121. ("info",
  122. boost::program_options::value<bool>(&info)
  123. ->default_value(false),
  124. "if the program should show progress information")
  125. ("debug",
  126. boost::program_options::value<bool>(&debug)
  127. ->default_value(false),
  128. "if the program should show debug messages")
  129. ("display.enabled",
  130. boost::program_options::value<bool>(&display)
  131. ->default_value(false),
  132. "if a window with the images and the detected tracks should be opened")
  133. ("display.show-grid",
  134. boost::program_options::value<bool>(&show_grid)
  135. ->default_value(false),
  136. "if a grid should be shown in the visualized tracks (will only be shown if a resolution is given)")
  137. ("config",
  138. boost::program_options::value<std::string>(&config_path),
  139. "the path to the config file, if no path is given the command line arguments are read")
  140. ("input.file",
  141. boost::program_options::value<std::string>(&input_file),
  142. "set detections file path")
  143. ("output.enabled",
  144. boost::program_options::value<bool>(&output)
  145. ->default_value(false),
  146. "if the results should be written into the specified output folder")
  147. ("output.images",
  148. boost::program_options::value<bool>(&output_images)
  149. ->default_value(false),
  150. "if the images containing the visualized detections should be written to the output")
  151. ("output.path",
  152. boost::program_options::value<std::string>(&output_path),
  153. "set the output file path")
  154. ("output.delimiter",
  155. boost::program_options::value<char>(&output_delimiter)
  156. ->default_value(';'),
  157. "the delimiter used to separate values in the specified output file")
  158. ("input.images-folder",
  159. boost::program_options::value<std::string>(&images_folder),
  160. "set images folder path")
  161. ("input.header",
  162. boost::program_options::value<std::string>(&header),
  163. "sets the input header, this value is optional if the input file has a header labeling the values,"
  164. "the delimiter used for the header needs to be the same as for the rest of the file")
  165. ("input.format",
  166. boost::program_options::value<std::string>(&input_format)
  167. ->default_value("ObjectData"),
  168. "the format the input should be parsed into, valid formats are: "
  169. "2D, Box, Angular")
  170. ("input.delimiter",
  171. boost::program_options::value<char>(&input_delimiter)
  172. ->default_value(';'),
  173. "the delimiter used to separate values in the specified input file")
  174. ("input.image-width",
  175. boost::program_options::value<double>(&image_width)
  176. ->default_value(1.0),
  177. "the width of the image")
  178. ("input.image-height",
  179. boost::program_options::value<double>(&image_height)
  180. ->default_value(1.0),
  181. "the height of the image")
  182. ("algorithm",
  183. boost::program_options::value<std::string>(&algorithm),
  184. "set the algorithm to use, current viable options: n-stage berclaz")
  185. ("n-stage.max-frame-skip",
  186. boost::program_options::value<std::string>(&n_stage_params.max_frame_skip)
  187. ->default_value("1,1"),
  188. "(n-stage) set the maximum number of frames a track can skip between two detections,"
  189. " if set to less or equal than zero all frames are linked")
  190. ("n-stage.max-tracklet-count",
  191. boost::program_options::value<std::string>(&n_stage_params.max_tracklet_count)
  192. ->default_value("-1,1"),
  193. "(n-stage) set the maximum number of tracklets to be extracted")
  194. ("n-stage.penalty-value",
  195. boost::program_options::value<std::string>(&n_stage_params.penalty_value)
  196. ->default_value("0,0"),
  197. "(n-stage) set the penalty value for edges from and to source and sink")
  198. ("n-stage.temporal-weight",
  199. boost::program_options::value<double>(&temporal_weight)
  200. ->default_value(1.0),
  201. "(n-stage) temporal weight for difference calculations between two detections")
  202. ("n-stage.spatial-weight",
  203. boost::program_options::value<double>(&spatial_weight)
  204. ->default_value(1.0),
  205. "(n-stage) spatial weight for difference calculations between two detections")
  206. ("n-stage.angular-weight",
  207. boost::program_options::value<double>(&angular_weight)
  208. ->default_value(1.0),
  209. "(n-stage) angular weight for difference calculations between two detections")
  210. ("n-stage.edge-weight-threshold",
  211. boost::program_options::value<double>(&n_stage_params.edge_weight_threshold)
  212. ->default_value(1.0),
  213. "(n-stage) the maximum weight an edge can have in the initial graph, edges with"
  214. "higher edge weights are discarded")
  215. ("berclaz.h-res",
  216. boost::program_options::value<int>(&berclaz_params.h_res)
  217. ->default_value(10),
  218. "(berclaz) the number of horizontal grid cells")
  219. ("berclaz.v-res",
  220. boost::program_options::value<int>(&berclaz_params.v_res)
  221. ->default_value(10),
  222. "(berclaz) the number of vertical grid cells")
  223. ("berclaz.vicinity-size",
  224. boost::program_options::value<int>(&berclaz_params.vicinity_size)
  225. ->default_value(1),
  226. "(berclaz) the vicinity size, the number of cells a detection can travel between two frames")
  227. ("berclaz.max-track-count",
  228. boost::program_options::value<size_t>(&berclaz_params.max_track_count)
  229. ->default_value(1),
  230. "(berclaz) the maximal number of tracks to extract")
  231. ("berclaz.batch-size",
  232. boost::program_options::value<size_t>(&berclaz_params.batch_size)
  233. ->default_value(100),
  234. "(berclaz) the size of one processing batch")
  235. ("berclaz.filter",
  236. boost::program_options::value<std::string>(&berclaz_filter)
  237. ->default_value(""),
  238. "(berclaz) the filter used in the berclaz grid (comma separated),"
  239. " e.g. multiplier, m01, m02,...,mnn");
  240. boost::program_options::variables_map opt_var_map;
  241. boost::program_options::store(
  242. boost::program_options::parse_command_line(argc, argv, opts),
  243. opt_var_map);
  244. boost::program_options::notify(opt_var_map);
  245. // Display help
  246. if (opt_var_map.count("help") != 0)
  247. {
  248. std::cout << opts << std::endl;
  249. exit(0);
  250. }
  251. // Read config
  252. if (opt_var_map.count("config") != 0)
  253. {
  254. std::ifstream config_file(config_path, std::ifstream::in);
  255. if (config_file.is_open())
  256. {
  257. boost::program_options::store(
  258. boost::program_options::parse_config_file(config_file,
  259. opts),
  260. opt_var_map);
  261. config_file.close();
  262. boost::program_options::notify(opt_var_map);
  263. }
  264. else
  265. {
  266. util::Logger::LogError("Unable to open config file!");
  267. exit(0);
  268. }
  269. }
  270. else if (opt_var_map.count("input-file") == 0 ||
  271. opt_var_map.count("input-format") == 0 ||
  272. (opt_var_map.count("output-path") == 0 && (output || output_images)))
  273. {
  274. std::cout << opts << std::endl;
  275. exit(0);
  276. }
  277. // Enable info logging
  278. if (info != 0)
  279. {
  280. util::Logger::SetInfo(true);
  281. util::Logger::LogInfo("Enabled");
  282. }
  283. // Enable debug logging
  284. if (debug != 0)
  285. {
  286. util::Logger::SetDebug(true);
  287. util::Logger::LogDebug("Enabled");
  288. }
  289. // Reading the input file
  290. util::Logger::LogInfo("Read input");
  291. util::ValueMapVector values;
  292. try
  293. {
  294. if (header.size() > 0)
  295. {
  296. util::Logger::LogDebug("header specified");
  297. util::FileIO::ReadCSV(values, header, input_file, input_delimiter);
  298. }
  299. else
  300. {
  301. util::Logger::LogDebug("read header from file");
  302. util::FileIO::ReadCSV(values, input_file, input_delimiter);
  303. }
  304. }
  305. catch (std::exception& e)
  306. {
  307. util::Logger::LogError("Failed to read input file!");
  308. util::Logger::LogError(e.what());
  309. exit(0);
  310. }
  311. // Parsing the read input
  312. core::DetectionSequence sequence;
  313. if (input_format == "2D")
  314. {
  315. util::Parser::ParseObjectData2D(values,
  316. sequence,
  317. image_width,
  318. image_height,
  319. temporal_weight,
  320. spatial_weight);
  321. }
  322. else if (input_format == "Box")
  323. {
  324. util::Parser::ParseObjectDataBox(values,
  325. sequence,
  326. image_width,
  327. image_height,
  328. temporal_weight,
  329. spatial_weight);
  330. }
  331. else if (input_format == "Angular")
  332. {
  333. util::Parser::ParseObjectDataAngular(values,
  334. sequence,
  335. image_width,
  336. image_height,
  337. temporal_weight,
  338. spatial_weight,
  339. angular_weight);
  340. }
  341. else
  342. {
  343. // No valid input-format specified
  344. std::cout << opts << std::endl;
  345. exit(0);
  346. }
  347. // Running the specified algorithm
  348. std::vector<core::TrackletPtr> tracks;
  349. time_t begin_time, end_time;
  350. std::string output_file_name = algorithm + "_";
  351. util::Logger::LogInfo("Start time measurement");
  352. begin_time = time(0);
  353. if (algorithm == "n-stage")
  354. {
  355. //TODO set the output file name
  356. RunNStage(sequence, tracks);
  357. }
  358. else if (algorithm == "berclaz")
  359. {
  360. output_file_name += std::to_string(berclaz_params.h_res) + "x"
  361. + std::to_string(berclaz_params.v_res) + "_"
  362. + std::to_string(berclaz_params.vicinity_size) + "_"
  363. + std::to_string(berclaz_params.max_track_count) + "_"
  364. + std::to_string(berclaz_params.batch_size);
  365. berclaz_params.filter = util::Filter2D(berclaz_filter, ',');
  366. RunBerclaz(sequence, tracks);
  367. }
  368. else
  369. {
  370. // No valid algorithm specified
  371. std::cout << opts << std::endl;
  372. exit(0);
  373. }
  374. output_file_name += ".csv";
  375. end_time = time(0);
  376. util::Logger::LogInfo("Time measurement stopped");
  377. util::Logger::LogInfo("Time passed: "
  378. + std::to_string(difftime(end_time, begin_time) / 60.0)
  379. + " minutes");
  380. // Write the output file
  381. if (output)
  382. {
  383. util::FileIO::WriteTracks(tracks, output_path + "/" + output_file_name, output_delimiter);
  384. }
  385. // Display the tracking data
  386. if (display)
  387. {
  388. util::Visualizer vis;
  389. if (algorithm == "berclaz")
  390. vis.Display(tracks, sequence.GetFrameOffset(),
  391. images_folder, output_images, output_path, "Visualizer",
  392. 0, 24, show_grid, berclaz_params.h_res, berclaz_params.v_res);
  393. else
  394. vis.Display(tracks, sequence.GetFrameOffset(),
  395. images_folder, output_images, output_path);
  396. }
  397. }
  398. void CreateTestGraph(DirectedGraph& graph, Vertex& source, Vertex& sink)
  399. {
  400. // Create test graph (suurballe wikipedia example)
  401. // std::vector<Vertex> vertices;
  402. // for (size_t i = 0; i < 6; ++i)
  403. // {
  404. // vertices.push_back(
  405. // boost::add_vertex(
  406. // core::ObjectDataPtr(new core::ObjectData(i)),graph));
  407. // }
  408. //
  409. // // AB
  410. // boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  411. // boost::add_edge(vertices[1], vertices[0], 1.0, graph);
  412. //
  413. // // AC
  414. // boost::add_edge(vertices[0], vertices[2], 2.0, graph);
  415. // boost::add_edge(vertices[2], vertices[0], 2.0, graph);
  416. //
  417. // // BD
  418. // boost::add_edge(vertices[1], vertices[3], 1.0, graph);
  419. // boost::add_edge(vertices[3], vertices[1], 1.0, graph);
  420. //
  421. // // BE
  422. // boost::add_edge(vertices[1], vertices[4], 2.0, graph);
  423. // boost::add_edge(vertices[4], vertices[1], 2.0, graph);
  424. //
  425. // // CD
  426. // boost::add_edge(vertices[2], vertices[3], 2.0, graph);
  427. // boost::add_edge(vertices[3], vertices[2], 2.0, graph);
  428. //
  429. // // DF
  430. // boost::add_edge(vertices[3], vertices[5], 1.0, graph);
  431. // boost::add_edge(vertices[5], vertices[3], 1.0, graph);
  432. //
  433. // // EF
  434. // boost::add_edge(vertices[4], vertices[5], 2.0, graph);
  435. // boost::add_edge(vertices[5], vertices[4], 2.0, graph);
  436. //
  437. // source = vertices[0];
  438. // sink = vertices[5];
  439. // Create test graph
  440. std::vector<Vertex> vertices;
  441. for (size_t i = 0; i < 11; ++i)
  442. {
  443. vertices.push_back(
  444. boost::add_vertex(
  445. core::ObjectDataPtr(new core::ObjectData(i)),graph));
  446. }
  447. // boost::add_edge(vertices[0], vertices[1], 0.0, graph);
  448. // boost::add_edge(vertices[0], vertices[8], 0.0, graph);
  449. // boost::add_edge(vertices[0], vertices[4], 0.0, graph);
  450. // boost::add_edge(vertices[1], vertices[2], -1.0, graph);
  451. // boost::add_edge(vertices[1], vertices[5], -1.0, graph);
  452. // boost::add_edge(vertices[2], vertices[3], -1.0, graph);
  453. // boost::add_edge(vertices[2], vertices[6], -1.0, graph);
  454. // boost::add_edge(vertices[2], vertices[10], -1.0, graph);
  455. // boost::add_edge(vertices[3], vertices[7], 4.0, graph);
  456. // boost::add_edge(vertices[4], vertices[2], 1.0, graph);
  457. // boost::add_edge(vertices[4], vertices[5], 1.0, graph);
  458. // boost::add_edge(vertices[4], vertices[9], 1.0, graph);
  459. // boost::add_edge(vertices[5], vertices[6], 2.0, graph);
  460. // boost::add_edge(vertices[5], vertices[3], 2.0, graph);
  461. // boost::add_edge(vertices[6], vertices[7], 4.0, graph);
  462. // boost::add_edge(vertices[8], vertices[2], -3.0, graph);
  463. // boost::add_edge(vertices[8], vertices[9], -3.0, graph);
  464. // boost::add_edge(vertices[9], vertices[3], 3.0, graph);
  465. // boost::add_edge(vertices[9], vertices[10], 3.0, graph);
  466. // boost::add_edge(vertices[10], vertices[7], 4.0, graph);
  467. source = vertices[0];
  468. sink = vertices[10];
  469. for (int i = 1; i < vertices.size() - 1; ++i)
  470. {
  471. boost::add_edge(source, vertices[i], 0.0, graph);
  472. }
  473. boost::add_edge(vertices[1], vertices[4], -1.0, graph);
  474. boost::add_edge(vertices[1], vertices[5], -1.0, graph);
  475. boost::add_edge(vertices[1], vertices[10], 0.0, graph);
  476. boost::add_edge(vertices[4], vertices[7], -1.0, graph);
  477. boost::add_edge(vertices[4], vertices[8], -1.0, graph);
  478. boost::add_edge(vertices[4], vertices[10], 0.0, graph);
  479. boost::add_edge(vertices[7], vertices[10], -1.0, graph);
  480. boost::add_edge(vertices[2], vertices[4], -2.0, graph);
  481. boost::add_edge(vertices[2], vertices[5], -2.0, graph);
  482. boost::add_edge(vertices[2], vertices[6], -2.0, graph);
  483. boost::add_edge(vertices[2], vertices[10], 0.0, graph);
  484. boost::add_edge(vertices[5], vertices[7], -2.0, graph);
  485. boost::add_edge(vertices[5], vertices[8], -2.0, graph);
  486. boost::add_edge(vertices[5], vertices[9], -2.0, graph);
  487. boost::add_edge(vertices[5], vertices[10], 0.0, graph);
  488. boost::add_edge(vertices[8], vertices[10], -2.0, graph);
  489. boost::add_edge(vertices[3], vertices[5], -3.0, graph);
  490. boost::add_edge(vertices[3], vertices[6], -3.0, graph);
  491. boost::add_edge(vertices[3], vertices[10], 0.0, graph);
  492. boost::add_edge(vertices[6], vertices[8], -3.0, graph);
  493. boost::add_edge(vertices[6], vertices[9], -3.0, graph);
  494. boost::add_edge(vertices[6], vertices[10], 0.0, graph);
  495. boost::add_edge(vertices[9], vertices[10], -3.0, graph);
  496. // Connect all with source and sink
  497. // boost::add_edge(vertices[1], sink, 0, graph);
  498. // boost::add_edge(source, vertices[2], 0, graph);
  499. // boost::add_edge(vertices[2], sink, 0, graph);
  500. // boost::add_edge(source, vertices[3], 0, graph);
  501. // boost::add_edge(vertices[4], sink, 0, graph);
  502. // boost::add_edge(source, vertices[5], 0, graph);
  503. // boost::add_edge(vertices[5], sink, 0, graph);
  504. // boost::add_edge(source, vertices[6], 0, graph);
  505. // boost::add_edge(vertices[8], sink, 0, graph);
  506. // boost::add_edge(source, vertices[9], 0, graph);
  507. // boost::add_edge(vertices[9], sink, 0, graph);
  508. // boost::add_edge(source, vertices[10], 0, graph);
  509. // boost::add_edge(vertices[1], vertices[7], 0.0, graph);
  510. // boost::add_edge(vertices[8], vertices[7], 0.0, graph);
  511. }
  512. void TestKBellmanFord(DirectedGraph graph, Vertex source, Vertex sink, size_t n_paths)
  513. {
  514. util::FileIO::WriteCSVMatlab(graph, "/home/wrede/Dokumente/graph_kbf.csv");
  515. MultiPredecessorMap paths;
  516. for (size_t i = 0; i < n_paths; ++i)
  517. {
  518. // Prepare variables for path finding
  519. size_t graph_size = boost::num_vertices(graph);
  520. std::vector<Vertex> pred_list(graph_size);
  521. std::vector<double> dist_list(graph_size);
  522. VertexIndexMap graph_indices = boost::get(boost::vertex_index, graph);
  523. EdgeWeightMap weight_map = boost::get(boost::edge_weight, graph);
  524. PredecessorMap pred_map(&pred_list[0], graph_indices);
  525. DistanceMap dist_map(&dist_list[0], graph_indices);
  526. // Find the shortest path
  527. boost::bellman_ford_shortest_paths(graph, graph_size,
  528. boost::root_vertex(source)
  529. .weight_map(weight_map)
  530. .predecessor_map(pred_map)
  531. .distance_map(dist_map));
  532. // Add path
  533. for (Vertex u = sink, v = pred_map[u]; u != v; u = v, v = pred_map[v])
  534. {
  535. paths[u].insert(v);
  536. if (u != sink && u != source)
  537. boost::clear_out_edges(u, graph);
  538. }
  539. }
  540. util::FileIO::WriteCSVMatlab(paths, source, sink, "/home/wrede/Dokumente/paths_kbf.csv");
  541. }
  542. void TestGrid()
  543. {
  544. int lower_index = 0;
  545. int upper_index = 5;
  546. double lower_bound = 0.0;
  547. double upper_bound = 50.0;
  548. util::Grid grid(upper_index, upper_index, upper_index,
  549. upper_bound, upper_bound, upper_bound);
  550. std::uniform_int_distribution<int> unii(lower_index, upper_index - 1);
  551. std::uniform_real_distribution<double> unif(lower_bound, upper_bound);
  552. std::default_random_engine re;
  553. // Fill with empty values
  554. std::cout << "fill with empty values\n";
  555. for (int z = lower_index; z < upper_index; ++z)
  556. {
  557. for (int y = lower_index; y < upper_index; ++y)
  558. {
  559. for (int x = lower_index; y < upper_index; ++y)
  560. {
  561. grid.SetValue(nullptr, x, y, z);
  562. }
  563. }
  564. }
  565. // Randomly add data
  566. std::cout << "randomly add data\n";
  567. for (int i = 0; i < 10; ++i)
  568. {
  569. int xi = unii(re);
  570. int yi = unii(re);
  571. int zi = unii(re);
  572. core::ObjectDataPtr value(new core::ObjectData((size_t)i));
  573. grid.SetValue(value, xi, yi, zi);
  574. std::cout << xi << "," << yi << "," << zi << " = " << *value << std::endl;
  575. }
  576. // Randomly get data
  577. std::cout << "randomly get data\n";
  578. for (int i = 0; i < 10; ++i)
  579. {
  580. double x = unif(re);
  581. double y = unif(re);
  582. double z = unif(re);
  583. std::cout << x << "," << y << "," << z << " = ";
  584. core::ObjectDataPtr value = grid.GetValue(x, y, z);
  585. if (value)
  586. {
  587. std::cout << *value << std::endl;
  588. }
  589. else
  590. {
  591. std::cout << "nullptr" << std::endl;
  592. }
  593. }
  594. }
  595. void CreateBerclazGraph(DirectedGraph& graph, Vertex& source, Vertex& sink)
  596. {
  597. util::Logger::SetDebug(true);
  598. util::Logger::SetInfo(true);
  599. util::Logger::LogInfo("Test berclaz graph");
  600. // Init grid with data
  601. util::Grid grid(3, 3, 3, 9.0, 9.0, 9.0);
  602. for (int z = 0; z < grid.GetDepthCount(); ++z)
  603. {
  604. for (int y = 0; y < grid.GetHeightCount(); ++y)
  605. {
  606. for (int x = 0; x < grid.GetWidthCount(); ++x)
  607. {
  608. core::ObjectDataPtr value(new core::ObjectData(10));
  609. grid.SetValue(value, x, y, z);
  610. }
  611. }
  612. }
  613. // Add path source->0,0,0->0,0,1->0,0,2->sink
  614. core::ObjectDataPtr value0(new core::ObjectData(1));
  615. value0->SetDetectionScore(1.0);
  616. grid.SetValue(value0, 0, 0, 0);
  617. core::ObjectDataPtr value1(new core::ObjectData(2));
  618. value1->SetDetectionScore(1.0);
  619. grid.SetValue(value1, 0, 0, 1);
  620. core::ObjectDataPtr value2(new core::ObjectData(3));
  621. value2->SetDetectionScore(1.0);
  622. grid.SetValue(value2, 0, 0, 2);
  623. // Add path source->0,1,0->0,1,1->0,1,2->sink
  624. core::ObjectDataPtr value3(new core::ObjectData(4));
  625. value3->SetDetectionScore(0.6);
  626. grid.SetValue(value3, 0, 1, 0);
  627. core::ObjectDataPtr value4(new core::ObjectData(5));
  628. value4->SetDetectionScore(0.6);
  629. grid.SetValue(value4, 0, 1, 1);
  630. core::ObjectDataPtr value5(new core::ObjectData(6));
  631. value5->SetDetectionScore(0.6);
  632. grid.SetValue(value5, 0, 1, 2);
  633. // Add path source->0,2,0->0,2,1->0,2,2->sink
  634. core::ObjectDataPtr value6(new core::ObjectData(7));
  635. value6->SetDetectionScore(0.55);
  636. grid.SetValue(value6, 0, 2, 0);
  637. core::ObjectDataPtr value7(new core::ObjectData(8));
  638. value7->SetDetectionScore(0.55);
  639. grid.SetValue(value7, 0, 2, 1);
  640. core::ObjectDataPtr value8(new core::ObjectData(9));
  641. value8->SetDetectionScore(0.55);
  642. grid.SetValue(value8, 0, 2, 2);
  643. util::Logger::LogDebug("add vertices");
  644. // Add grid vertices
  645. graph.clear();
  646. for (int z = 0; z < grid.GetDepthCount(); ++z)
  647. {
  648. for (int y = 0; y < grid.GetHeightCount(); ++y)
  649. {
  650. for (int x = 0; x < grid.GetWidthCount(); ++x)
  651. {
  652. boost::add_vertex(grid.GetValue(x, y, z), graph);
  653. }
  654. }
  655. }
  656. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(graph)));
  657. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(graph)));
  658. // Add source and sink vertex
  659. source = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  660. sink = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  661. util::Logger::LogDebug("add edges");
  662. // Iterate all vertices but source and sink
  663. VertexIndexMap vertices = boost::get(boost::vertex_index, graph);
  664. VertexValueMap values = boost::get(boost::vertex_name, graph);
  665. int vicinity_size = 1;
  666. int layer_size = grid.GetWidthCount() * grid.GetHeightCount();
  667. for (int z = 0; z < grid.GetDepthCount(); ++z)
  668. {
  669. for (int y = 0; y < grid.GetHeightCount(); ++y)
  670. {
  671. for (int x = 0; x < grid.GetWidthCount(); ++x)
  672. {
  673. // First vertex index
  674. int vi = x + y * grid.GetHeightCount() + z * layer_size;
  675. // Get the score, clamp it, prevent division by zero and
  676. // logarithm of zero
  677. double score = values[vi]->GetDetectionScore();
  678. if (score > 0.999999)
  679. {
  680. score = 0.999999;
  681. }
  682. else if (score < 0.000001)
  683. {
  684. score = 0.000001;
  685. }
  686. // Calculate the edge weight
  687. double weight = -std::log(score / (1 - score));
  688. // Connect with the next frame only if there is a next frame
  689. if (z < grid.GetDepthCount() - 1)
  690. {
  691. // Iterate all nearby cells in the next frame
  692. for (int ny = std::max(0, y - vicinity_size);
  693. ny < std::min(grid.GetHeightCount(), y + vicinity_size + 1);
  694. ++ny)
  695. {
  696. for (int nx = std::max(0, x - vicinity_size);
  697. nx < std::min(grid.GetWidthCount(), x + vicinity_size + 1);
  698. ++nx)
  699. {
  700. // Second vertex index
  701. int vj = nx + ny * grid.GetHeightCount() + (z + 1) * layer_size;
  702. // Connect to nearby cells
  703. boost::add_edge(vertices[vi], vertices[vj], weight, graph);
  704. }
  705. }
  706. // boost::add_edge(vertices[vi], sink, 0.0, graph);
  707. }
  708. else
  709. {
  710. boost::add_edge(vertices[vi], sink, weight, graph);
  711. }
  712. if (z < 1)
  713. {
  714. // Connect with source
  715. boost::add_edge(source, vertices[vi], 0.0, graph);
  716. }
  717. }
  718. }
  719. }
  720. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(graph)));
  721. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(graph)));
  722. }
  723. void CreatePresentationGraph(DirectedGraph& graph, Vertex& source, Vertex& sink, bool two_paths)
  724. {
  725. std::vector<Vertex> vertices;
  726. if (two_paths)
  727. {
  728. for (size_t i = 0; i < 10; ++i)
  729. {
  730. vertices.push_back(
  731. boost::add_vertex(core::ObjectDataPtr(new core::ObjectData(i)), graph));
  732. }
  733. source = vertices[0];
  734. sink = vertices[9];
  735. boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  736. boost::add_edge(vertices[0], vertices[2], 1.0, graph);
  737. boost::add_edge(vertices[1], vertices[3], 12.0, graph);
  738. boost::add_edge(vertices[1], vertices[4], 15.0, graph);
  739. boost::add_edge(vertices[2], vertices[3], 15.0, graph);
  740. boost::add_edge(vertices[2], vertices[4], 10.0, graph);
  741. boost::add_edge(vertices[3], vertices[5], 15.0, graph);
  742. boost::add_edge(vertices[3], vertices[6], 12.0, graph);
  743. boost::add_edge(vertices[4], vertices[5], 12.0, graph);
  744. boost::add_edge(vertices[4], vertices[6], 11.0, graph);
  745. boost::add_edge(vertices[5], vertices[7], 12.0, graph);
  746. boost::add_edge(vertices[5], vertices[8], 12.0, graph);
  747. boost::add_edge(vertices[6], vertices[7], 11.0, graph);
  748. boost::add_edge(vertices[6], vertices[8], 10.0, graph);
  749. boost::add_edge(vertices[7], vertices[9], 1.0, graph);
  750. boost::add_edge(vertices[8], vertices[9], 1.0, graph);
  751. }
  752. else
  753. {
  754. for (size_t i = 0; i < 14; ++i)
  755. {
  756. vertices.push_back(
  757. boost::add_vertex(core::ObjectDataPtr(new core::ObjectData(i)), graph));
  758. }
  759. source = vertices[0];
  760. sink = vertices[9];
  761. boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  762. boost::add_edge(vertices[0], vertices[2], 1.0, graph);
  763. boost::add_edge(vertices[1], vertices[3], 12.0, graph);
  764. boost::add_edge(vertices[1], vertices[4], 15.0, graph);
  765. boost::add_edge(vertices[2], vertices[3], 15.0, graph);
  766. boost::add_edge(vertices[2], vertices[4], 10.0, graph);
  767. boost::add_edge(vertices[3], vertices[5], 15.0, graph);
  768. boost::add_edge(vertices[3], vertices[6], 12.0, graph);
  769. boost::add_edge(vertices[4], vertices[5], 12.0, graph);
  770. boost::add_edge(vertices[4], vertices[6], 11.0, graph);
  771. boost::add_edge(vertices[5], vertices[7], 12.0, graph);
  772. boost::add_edge(vertices[5], vertices[8], 12.0, graph);
  773. boost::add_edge(vertices[6], vertices[7], 11.0, graph);
  774. boost::add_edge(vertices[6], vertices[8], 10.0, graph);
  775. boost::add_edge(vertices[7], vertices[9], 1.0, graph);
  776. boost::add_edge(vertices[8], vertices[9], 1.0, graph);
  777. boost::add_edge(vertices[0], vertices[10], 20.0, graph);
  778. boost::add_edge(vertices[10], vertices[11], 20.0, graph);
  779. boost::add_edge(vertices[10], vertices[3], 20.0, graph);
  780. boost::add_edge(vertices[10], vertices[4], 20.0, graph);
  781. boost::add_edge(vertices[11], vertices[12], 20.0, graph);
  782. boost::add_edge(vertices[11], vertices[5], 20.0, graph);
  783. boost::add_edge(vertices[12], vertices[6], 20.0, graph);
  784. boost::add_edge(vertices[13], vertices[9], 20.0, graph);
  785. }
  786. }
  787. void CreateSuurballeGraph(DirectedGraph& graph, Vertex& source, Vertex& sink, bool first)
  788. {
  789. std::vector<Vertex> vertices;
  790. if (first)
  791. {
  792. // First example graph
  793. for (int i = 0; i < 7; ++i)
  794. {
  795. vertices.push_back(boost::add_vertex(graph));
  796. }
  797. source = vertices[0];
  798. sink = vertices[6];
  799. boost::add_edge(vertices[0], vertices[1], 5.0, graph);
  800. boost::add_edge(vertices[0], vertices[4], 2.0, graph);
  801. boost::add_edge(vertices[1], vertices[2], 1.0, graph);
  802. boost::add_edge(vertices[1], vertices[4], 1.0, graph);
  803. boost::add_edge(vertices[2], vertices[6], 1.0, graph);
  804. boost::add_edge(vertices[3], vertices[2], 1.0, graph);
  805. boost::add_edge(vertices[4], vertices[3], 2.0, graph);
  806. boost::add_edge(vertices[4], vertices[5], 1.0, graph);
  807. boost::add_edge(vertices[5], vertices[2], 1.0, graph);
  808. boost::add_edge(vertices[5], vertices[6], 1.0, graph);
  809. }
  810. else
  811. {
  812. // Second example graph
  813. for (int i = 0; i < 8; ++i)
  814. {
  815. vertices.push_back(boost::add_vertex(graph));
  816. }
  817. source = vertices[0];
  818. sink = vertices[7];
  819. boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  820. boost::add_edge(vertices[0], vertices[4], 8.0, graph);
  821. boost::add_edge(vertices[0], vertices[5], 1.0, graph);
  822. boost::add_edge(vertices[1], vertices[2], 1.0, graph);
  823. boost::add_edge(vertices[1], vertices[7], 8.0, graph);
  824. boost::add_edge(vertices[2], vertices[3], 1.0, graph);
  825. boost::add_edge(vertices[3], vertices[4], 1.0, graph);
  826. boost::add_edge(vertices[3], vertices[6], 2.0, graph);
  827. boost::add_edge(vertices[4], vertices[7], 1.0, graph);
  828. boost::add_edge(vertices[5], vertices[2], 2.0, graph);
  829. boost::add_edge(vertices[5], vertices[6], 6.0, graph);
  830. boost::add_edge(vertices[6], vertices[7], 1.0, graph);
  831. }
  832. }
  833. void TestYAOKSP()
  834. {
  835. Vertex source, sink;
  836. DirectedGraph graph;
  837. // CreatePresentationGraph(graph, source, sink, true);
  838. CreateSuurballeGraph(graph, source, sink, false);
  839. // CreateBerclazGraph(graph, source, sink);
  840. algo::KShortestPaths ksp(graph, source, sink);
  841. ksp.Run(3);
  842. std::vector<std::vector<Vertex>> paths;
  843. ksp.GetPaths(paths);
  844. for (auto path : paths)
  845. {
  846. std::cout << "path: ";
  847. for (auto v : path)
  848. {
  849. std::cout << std::setw(2) << v << " ";
  850. }
  851. std::cout << std::endl;
  852. }
  853. }
  854. int main(int argc, char const * const * argv)
  855. {
  856. //TODO load with frame offset
  857. //TODO check visualizer for offset errors
  858. //TODO check why SEGFAULT is caused by some berclaz resolutions
  859. Run(argc, argv);
  860. return 0;
  861. }