Berclaz.cpp 8.0 KB

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