Berclaz.cpp 8.5 KB

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