NStage.cpp 11 KB

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