NStage.cpp 11 KB

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