Berclaz.cpp 8.2 KB

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