Berclaz.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // Created by wrede on 02.06.16.
  3. //
  4. #include "Berclaz.h"
  5. #include "KShortestPaths.h"
  6. #include "../util/Parser.h"
  7. #include "../util/Logger.h"
  8. #include "../util/FileIO.h"
  9. #include "KShortestPaths2.h"
  10. namespace algo
  11. {
  12. Berclaz::Berclaz(int h_res, int v_res, int vicinity_size)
  13. {
  14. h_res_ = h_res;
  15. v_res_ = v_res;
  16. vicinity_size_ = vicinity_size;
  17. }
  18. void Berclaz::CreateGraph(DirectedGraph& graph, Vertex& source, Vertex& sink, util::Grid& grid)
  19. {
  20. util::Logger::LogDebug("add vertices");
  21. // Add grid vertices
  22. for (int z = 0; z < grid.GetDepthCount(); ++z)
  23. {
  24. for (int y = 0; y < grid.GetHeightCount(); ++y)
  25. {
  26. for (int x = 0; x < grid.GetWidthCount(); ++x)
  27. {
  28. boost::add_vertex(grid.GetValue(x, y, z), graph);
  29. }
  30. }
  31. }
  32. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(graph)));
  33. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(graph)));
  34. // Add source and sink vertex
  35. source = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  36. sink = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  37. util::Logger::LogDebug("source index: " + std::to_string(source));
  38. util::Logger::LogDebug("sink index: " + std::to_string(sink));
  39. util::Logger::LogDebug("add edges");
  40. // Iterate all vertices but source and sink
  41. VertexIndexMap vertices = boost::get(boost::vertex_index, graph);
  42. VertexValueMap values = boost::get(boost::vertex_name, graph);
  43. int layer_size = grid.GetWidthCount() * grid.GetHeightCount();
  44. for (int z = 0; z < grid.GetDepthCount(); ++z)
  45. {
  46. for (int y = 0; y < grid.GetHeightCount(); ++y)
  47. {
  48. for (int x = 0; x < grid.GetWidthCount(); ++x)
  49. {
  50. // First vertex index
  51. int vi = x + y * grid.GetHeightCount() + z * layer_size;
  52. // Get the score, clamp it, prevent division by zero and
  53. // logarithm of zero
  54. double score = values[vi]->GetDetectionScore();
  55. if (score > MAX_SCORE_VALUE)
  56. {
  57. score = MAX_SCORE_VALUE;
  58. }
  59. else if (score < MIN_SCORE_VALUE)
  60. {
  61. score = MIN_SCORE_VALUE;
  62. }
  63. // Calculate the edge weight
  64. double weight = -std::log(score / (1 - score));
  65. // Connect with the next frame only if there is a next frame
  66. if (z < grid.GetDepthCount() - 1)
  67. {
  68. // Iterate all nearby cells in the next frame
  69. for (int ny = std::max(0, y - vicinity_size_);
  70. ny <
  71. std::min(grid.GetHeightCount(),
  72. y + vicinity_size_ + 1);
  73. ++ny)
  74. {
  75. for (int nx = std::max(0, x - vicinity_size_);
  76. nx < std::min(grid.GetWidthCount(),
  77. x + vicinity_size_ + 1);
  78. ++nx)
  79. {
  80. // Second vertex index
  81. int vj = nx + ny * grid.GetHeightCount() +
  82. (z + 1) * layer_size;
  83. // Connect to nearby cells
  84. boost::add_edge(vertices[vi], vertices[vj],
  85. weight, graph);
  86. }
  87. }
  88. boost::add_edge(vertices[vi], sink, VIRTUAL_EDGE_WEIGHT,
  89. graph);
  90. }
  91. else
  92. {
  93. boost::add_edge(vertices[vi], sink, weight, graph);
  94. }
  95. // Connect with source and sink
  96. boost::add_edge(source, vertices[vi], VIRTUAL_EDGE_WEIGHT,
  97. graph);
  98. }
  99. }
  100. }
  101. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(graph)));
  102. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(graph)));
  103. }
  104. void Berclaz::ExtractTracks(DirectedGraph& graph, MultiPredecessorMap& map, Vertex origin,
  105. std::vector<core::TrackletPtr>& tracks)
  106. {
  107. VertexValueMap values = boost::get(boost::vertex_name, graph);
  108. // Move along all paths in reverse, starting at the origin
  109. for (Vertex first : map[origin])
  110. {
  111. core::TrackletPtr tracklet(new core::Tracklet());
  112. // The paths are node disjoint, so there should always be only one
  113. // node to proceed to
  114. for (Vertex u = first, v = (*map[u].begin());
  115. u != v; u = v, v = (*map[v].begin()))
  116. {
  117. tracklet->AddPathObject(values[u]);
  118. }
  119. tracks.push_back(tracklet);
  120. }
  121. }
  122. void Berclaz::Run(core::DetectionSequence& sequence,
  123. size_t batch_size, size_t max_track_count,
  124. std::vector<core::TrackletPtr>& tracks)
  125. {
  126. for (size_t i = 0; i < sequence.GetFrameCount(); i += batch_size)
  127. {
  128. util::Logger::LogDebug("batch offset: " + std::to_string(i));
  129. util::Grid grid = util::Parser::ParseGrid(sequence, i, i + batch_size,
  130. 0.0, 1.0, h_res_, 0.0, 1.0, v_res_);
  131. util::Logger::LogDebug("create graph");
  132. DirectedGraph graph;
  133. Vertex source, sink;
  134. CreateGraph(graph, source, sink, grid);
  135. util::Logger::LogDebug("run ksp");
  136. KShortestPaths2 ksp;
  137. MultiPredecessorMap ksp_result = ksp.Run(graph, source, sink, max_track_count);
  138. util::Logger::LogDebug("extract tracks");
  139. ExtractTracks(graph, ksp_result, sink, tracks);
  140. }
  141. if (batch_size < sequence.GetFrameCount())
  142. {
  143. util::Logger::LogDebug("connect tracks");
  144. ConnectTracks(tracks);
  145. }
  146. }
  147. void Berclaz::ConnectTracks(std::vector<core::TrackletPtr>& tracks)
  148. {
  149. for (size_t i = 0; i < tracks.size(); ++i)
  150. {
  151. // find the best matching tracklet
  152. double best_value = std::numeric_limits<double>::max();
  153. size_t best_index = 0;
  154. for (size_t k = i + 1; k < tracks.size(); ++k)
  155. {
  156. if (tracks[i]->GetLastFrameIndex() < tracks[k]->GetFirstFrameIndex())
  157. {
  158. double value = tracks[i]->CompareTo(tracks[k]);
  159. if (value < best_value)
  160. {
  161. best_index = k;
  162. }
  163. }
  164. }
  165. // if a match was found
  166. if (best_index != 0)
  167. {
  168. // merge the two tracks
  169. tracks[i]->Combine(tracks[best_index]);
  170. tracks.erase(tracks.begin() + best_index);
  171. }
  172. }
  173. }
  174. }