NStage.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // Created by wrede on 25.04.16.
  3. //
  4. #include "NStage.h"
  5. #include "../util/Logger.h"
  6. #include <boost/graph/dijkstra_shortest_paths.hpp>
  7. namespace algo
  8. {
  9. NStage::NStage(std::vector<size_t> max_frame_skip,
  10. std::vector<double> penalty_value,
  11. std::vector<size_t> max_tracklet_count)
  12. {
  13. max_frame_skips_ = max_frame_skip;
  14. penalty_values_ = penalty_value;
  15. max_tracklet_counts_ = max_tracklet_count;
  16. iterations_ = std::min(max_tracklet_count.size(), penalty_value.size());
  17. }
  18. void NStage::CreateObjectGraph(DirectedGraph& graph, const core::DetectionSequence& detections)
  19. {
  20. util::Logger::LogInfo("Creating object graph");
  21. std::vector<std::vector<Vertex>> layers;
  22. // Add source as the vertex with the lowest index
  23. Vertex source = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  24. // Add vertices from detection sequence to directed graph
  25. // Save the vertices which are in one frame/layer for later use to
  26. // link easily between vertices in adjacent frames/layers
  27. for (size_t i = 0; i < detections.GetFrameCount(); ++i)
  28. {
  29. std::vector<Vertex> layer;
  30. for (size_t j = 0; j < detections.GetObjectCount(i); ++j)
  31. {
  32. Vertex v = boost::add_vertex(detections.GetObject(i, j), graph);
  33. layer.push_back(v);
  34. }
  35. layers.push_back(layer);
  36. }
  37. // Add sink as the vertex with the highest index
  38. Vertex sink = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  39. // Vertex objects
  40. VertexValueMap values = boost::get(boost::vertex_name, graph);
  41. // Create edges
  42. for (size_t i = 0; i < layers.size(); ++i)
  43. {
  44. // For each edge in this frame/layer
  45. for (size_t j = 0; j < layers[i].size(); ++j)
  46. {
  47. Vertex u = layers[i][j];
  48. // For each next frame/layer until maxFrameSkip or end
  49. for (size_t k = 1; k <= max_frame_skips_[0] && i + k < layers.size(); ++k)
  50. {
  51. // To every edge in the next frame/layer
  52. for (size_t l = 0; l < layers[i + k].size(); ++l)
  53. {
  54. Vertex v = layers[i + k][l];
  55. boost::add_edge(u, v,
  56. values[u]->CompareTo(values[v]),
  57. graph);
  58. }
  59. }
  60. // From source to vertex and from vertex to sink
  61. boost::add_edge(source, u,
  62. (i + 1) * penalty_values_[0],
  63. graph);
  64. boost::add_edge(u, sink,
  65. (layers.size() - i) * penalty_values_[0],
  66. graph);
  67. }
  68. }
  69. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(graph)));
  70. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(graph)));
  71. }
  72. void NStage::CreateTrackletGraph(DirectedGraph& obj_graph, DirectedGraph& tlt_graph,
  73. size_t frame_count, size_t iteration)
  74. {
  75. util::Logger::LogInfo("Creating tracklet graph");
  76. // Add source to tracklet graph
  77. Vertex tlt_src = boost::add_vertex(
  78. core::ObjectDataPtr(new core::ObjectData()), tlt_graph);
  79. // Prepare variables for dijkstra
  80. size_t obj_graph_size = boost::num_vertices(obj_graph);
  81. std::vector<Vertex> obj_pred_list(obj_graph_size);
  82. std::vector<double> obj_dist_list(obj_graph_size);
  83. VertexIndexMap obj_indices = boost::get(boost::vertex_index, obj_graph);
  84. VertexValueMap obj_values = boost::get(boost::vertex_name, obj_graph);
  85. PredecessorMap obj_pred_map(&obj_pred_list[0], obj_indices);
  86. DistanceMap obj_dist_map(&obj_dist_list[0], obj_indices);
  87. // Source and sink of the object graph
  88. Vertex obj_src = obj_indices[0];
  89. Vertex obj_snk = obj_indices[obj_graph_size - 1];
  90. //TODO experimental
  91. EdgeWeightMap weight_map = boost::get(boost::edge_weight, obj_graph);
  92. // Iteratively run dijkstra to extract tracklets
  93. for (size_t i = 0; i != max_tracklet_counts_[iteration]; ++i)
  94. {
  95. util::Logger::LogDebug("tracklet iteration: " + std::to_string(i));
  96. boost::dijkstra_shortest_paths(obj_graph, obj_src,
  97. boost::predecessor_map(obj_pred_map)
  98. .distance_map(obj_dist_map));
  99. // No path from source to sink could be found
  100. if (obj_dist_map[obj_snk] == std::numeric_limits<double>::max())
  101. {
  102. break;
  103. }
  104. // Create the tracklet
  105. core::TrackletPtr tracklet(new core::Tracklet);
  106. for (Vertex u = obj_pred_map[obj_snk], v = obj_snk; u != v; v = u, u = obj_pred_map[v])
  107. {
  108. tracklet->AddPathObject(obj_values[u]);
  109. // Leave source and sink untouched
  110. if (!obj_values[u]->IsVirtual())
  111. {
  112. //TODO original
  113. // Remove the path by setting all used edges to a weight of
  114. // infinity
  115. // std::pair<DirectedGraph::out_edge_iterator,
  116. // DirectedGraph::out_edge_iterator> edge_iter = boost::out_edges(u, obj_graph);
  117. //
  118. // for (DirectedGraph::out_edge_iterator iter = edge_iter.first;
  119. // iter != edge_iter.second;
  120. // ++iter)
  121. // {
  122. // boost::get(boost::edge_weight, obj_graph, *iter)
  123. // = std::numeric_limits<double>::infinity();
  124. // }
  125. //TODO experimental
  126. OutEdgeIter oei, oei_end;
  127. for (boost::tie(oei, oei_end) = boost::out_edges(u, obj_graph);
  128. oei != oei_end;
  129. ++oei)
  130. {
  131. weight_map[*oei] = std::numeric_limits<double>::infinity();
  132. }
  133. }
  134. }
  135. core::ObjectDataPtr tracklet_base = tracklet;
  136. // Add tracklet into tracklet graph
  137. boost::add_vertex(tracklet_base, tlt_graph);
  138. }
  139. // Add sink to tracklet graph
  140. Vertex tlt_snk = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()),
  141. tlt_graph);
  142. util::Logger::LogDebug("adding edges");
  143. // Create edges
  144. size_t tlt_graph_size = boost::num_vertices(tlt_graph);
  145. VertexIndexMap tlt_indices = boost::get(boost::vertex_index, tlt_graph);
  146. VertexValueMap tlt_values = boost::get(boost::vertex_name, tlt_graph);
  147. // For every tracklet but source and sink
  148. for (size_t i = 1; i < tlt_graph_size - 1; ++i)
  149. {
  150. Vertex u = tlt_indices[i];
  151. core::TrackletPtr u_ptr = std::static_pointer_cast<core::Tracklet>(tlt_values[u]);
  152. size_t u_first_frame = u_ptr->GetFirstFrameIndex();
  153. size_t u_last_frame = u_ptr->GetLastFrameIndex();
  154. // Create edges between tracklets
  155. for (size_t j = 1; j < tlt_graph_size - 1; ++j)
  156. {
  157. if (i != j)
  158. {
  159. Vertex v = tlt_indices[j];
  160. core::TrackletPtr v_ptr =
  161. std::static_pointer_cast<core::Tracklet>(tlt_values[v]);
  162. size_t v_first_frame = v_ptr->GetFirstFrameIndex();
  163. // Link only tracklets that are in temporal order
  164. if (u_last_frame < v_first_frame &&
  165. (v_first_frame - u_last_frame < max_frame_skips_[iteration]))
  166. {
  167. boost::add_edge(u, v,
  168. tlt_values[u]->CompareTo(tlt_values[v]),
  169. tlt_graph);
  170. }
  171. }
  172. }
  173. // From source
  174. boost::add_edge(tlt_src, u,
  175. (u_first_frame + 1) * penalty_values_[iteration],
  176. tlt_graph);
  177. // To sink
  178. boost::add_edge(u, tlt_snk,
  179. (frame_count - u_last_frame) * penalty_values_[iteration],
  180. tlt_graph);
  181. }
  182. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(tlt_graph)));
  183. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(tlt_graph)));
  184. }
  185. void NStage::ExtractTracks(DirectedGraph& tlt_graph, size_t depth,
  186. std::vector<core::TrackletPtr>& tracks)
  187. {
  188. util::Logger::LogInfo("Extracting tracks");
  189. VertexValueMap tlt_values = boost::get(boost::vertex_name, tlt_graph);
  190. for (size_t i = 0; i < boost::num_vertices(tlt_graph); ++i)
  191. {
  192. core::ObjectDataPtr obj = tlt_values[i];
  193. if (!obj->IsVirtual())
  194. {
  195. core::TrackletPtr tlt = std::static_pointer_cast<core::Tracklet>(obj);
  196. for (size_t j = 0; j < depth; j++)
  197. {
  198. tlt->Flatten();
  199. }
  200. tracks.push_back(tlt);
  201. }
  202. }
  203. util::Logger::LogDebug("track count " + std::to_string(tracks.size()));
  204. }
  205. void NStage::Run(const core::DetectionSequence& sequence,
  206. std::vector<core::TrackletPtr>& tracks)
  207. {
  208. // Running the two stage graph algorithm
  209. DirectedGraph obj_graph;
  210. CreateObjectGraph(obj_graph, sequence);
  211. // Run the tracklet creation at least once
  212. DirectedGraph tlt_graph_1, tlt_graph_2;
  213. CreateTrackletGraph(obj_graph, tlt_graph_1, sequence.GetFrameCount(), 0);
  214. // Run the tracklet creation iteratively
  215. for (size_t i = 1; i < iterations_; ++i)
  216. {
  217. if (i % 2 == 0)
  218. {
  219. CreateTrackletGraph(tlt_graph_2, tlt_graph_1, sequence.GetFrameCount(), i);
  220. }
  221. else
  222. {
  223. CreateTrackletGraph(tlt_graph_1, tlt_graph_2, sequence.GetFrameCount(), i);
  224. }
  225. }
  226. // Extract tracklets and flatten tracklets
  227. if (iterations_ % 2 == 0)
  228. {
  229. ExtractTracks(tlt_graph_2, iterations_ - 1, tracks);
  230. }
  231. else
  232. {
  233. ExtractTracks(tlt_graph_1, iterations_ - 1, tracks);
  234. }
  235. }
  236. }