common.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #include "common.h"
  2. std::string locateFile(const std::string& input, const std::vector<std::string> & directories)
  3. {
  4. std::string file;
  5. const int MAX_DEPTH{10};
  6. bool found{false};
  7. for (auto &dir : directories)
  8. {
  9. file = dir + input;
  10. std::cout << file << std::endl;
  11. for (int i = 0; i < MAX_DEPTH && !found; i++)
  12. {
  13. std::ifstream checkFile(file);
  14. found = checkFile.is_open();
  15. if (found) break;
  16. file = "../" + file;
  17. }
  18. if (found) break;
  19. file.clear();
  20. }
  21. std::cout << file << std::endl;
  22. assert(!file.empty() && "Could not find a file due to it not existing in the data directory.");
  23. return file;
  24. }
  25. void readPGMFile(const std::string& fileName, uint8_t *buffer, int inH, int inW)
  26. {
  27. std::ifstream infile(fileName, std::ifstream::binary);
  28. assert(infile.is_open() && "Attempting to read from a file that is not open.");
  29. std::string magic, h, w, max;
  30. infile >> magic >> h >> w >> max;
  31. infile.seekg(1, infile.cur);
  32. infile.read(reinterpret_cast<char*>(buffer), inH*inW);
  33. }
  34. /*********************************/
  35. /* Updated date: 2018.3.7
  36. /*This is my own implementation of the detectout layer code, because I met a mistake with the detectout api of
  37. /*tensorrt3.0 a few months ago. You can use the detectout api of tensorrt3.0 correctly by adding an extra output
  38. /*in the deploy prototxt file. Please refer to my deploy prototxt.
  39. /********************************/
  40. // Retrieve all location predictions.
  41. void GetLocPredictions(const float* loc_data,
  42. const int num_preds_per_class, const int num_loc_classes,
  43. std::vector<std::vector<float> >* loc_preds) {
  44. for (int p = 0; p < num_preds_per_class; ++p) {
  45. int start_idx = p * num_loc_classes * 4;
  46. vector<float> labelbbox;
  47. for (int c = 0; c < num_loc_classes; ++c) {
  48. labelbbox.push_back(loc_data[start_idx + c * 4]);
  49. labelbbox.push_back(loc_data[start_idx + c * 4 + 1]);
  50. labelbbox.push_back(loc_data[start_idx + c * 4 + 2]);
  51. labelbbox.push_back(loc_data[start_idx + c * 4 + 3]);
  52. loc_preds->push_back(labelbbox);
  53. }
  54. }
  55. }
  56. // Retrieve all confidences.
  57. void GetConfidenceScores(const float* conf_data,
  58. const int num_preds_per_class, const int num_classes,
  59. vector<vector<float> >* conf_preds) {
  60. for (int p = 0; p < num_preds_per_class; ++p) {
  61. int start_idx = p * num_classes;
  62. vector<float> conf_classes;
  63. for (int c = 0; c < num_classes; ++c) {
  64. conf_classes.push_back(conf_data[start_idx + c]);
  65. }
  66. conf_preds->push_back(conf_classes);
  67. }
  68. }
  69. // Retrieve all prior bboxes. bboxes and variances
  70. void GetPriorBBoxes(const float* prior_data, const int num_priors,
  71. vector<vector<float> >* prior_bboxes,
  72. vector<vector<float> >* prior_variances) {
  73. for (int i = 0; i < num_priors; ++i) {
  74. int start_idx = i * 4;
  75. vector<float> prior_bbox;
  76. prior_bbox.push_back(prior_data[start_idx]);
  77. prior_bbox.push_back(prior_data[start_idx + 1]);
  78. prior_bbox.push_back(prior_data[start_idx + 2]);
  79. prior_bbox.push_back(prior_data[start_idx + 3]);
  80. prior_bboxes->push_back(prior_bbox);
  81. }
  82. for (int i = 0; i < num_priors; ++i) {
  83. int start_idx = (num_priors + i) * 4;
  84. vector<float> prior_variance;
  85. vector<float> var;
  86. for (int j = 0; j < 4; ++j) {
  87. prior_variance.push_back(prior_data[start_idx + j]);
  88. }
  89. prior_variances->push_back(prior_variance);
  90. }
  91. }
  92. /* code_type: 0 = CORNER; 1 = CENTER_SIZE; 2 = CORNER_SIZE
  93. *
  94. */
  95. void DecodeBBox(
  96. const vector<float>& prior_bbox, const vector<float>& prior_variance,
  97. const int code_type, const bool variance_encoded_in_target,
  98. const bool clip_bbox, const vector<float>& bbox,
  99. vector<float>* decode_bbox) {
  100. if (0 == code_type) {
  101. if (variance_encoded_in_target) {
  102. // variance is encoded in target, we simply need to add the offset
  103. // predictions.
  104. decode_bbox->push_back(prior_bbox[0] + bbox[0]);
  105. decode_bbox->push_back(prior_bbox[1] + bbox[1]);
  106. decode_bbox->push_back(prior_bbox[2] + bbox[2]);
  107. decode_bbox->push_back(prior_bbox[3] + bbox[3]);
  108. } else {
  109. // variance is encoded in bbox, we need to scale the offset accordingly.
  110. decode_bbox->push_back(
  111. prior_bbox[0]+ prior_variance[0] * bbox[0]);
  112. decode_bbox->push_back(
  113. prior_bbox[1] + prior_variance[1] * bbox[1]);
  114. decode_bbox->push_back(
  115. prior_bbox[2] + prior_variance[2] * bbox[2]);
  116. decode_bbox->push_back(
  117. prior_bbox[3] + prior_variance[3] * bbox[3]);
  118. }
  119. } else if (1 == code_type) {
  120. float prior_width = prior_bbox[2] - prior_bbox[0];
  121. //CHECK_GT(prior_width, 0);
  122. float prior_height = prior_bbox[3] - prior_bbox[1];
  123. //CHECK_GT(prior_height, 0);
  124. float prior_center_x = (prior_bbox[0] + prior_bbox[2]) / 2.;
  125. float prior_center_y = (prior_bbox[1] + prior_bbox[3]) / 2.;
  126. float decode_bbox_center_x, decode_bbox_center_y;
  127. float decode_bbox_width, decode_bbox_height;
  128. if (variance_encoded_in_target) {
  129. // variance is encoded in target, we simply need to retore the offset
  130. // predictions.
  131. decode_bbox_center_x = bbox[0] * prior_width + prior_center_x;
  132. decode_bbox_center_y = bbox[1] * prior_height + prior_center_y;
  133. decode_bbox_width = exp(bbox[2]) * prior_width;
  134. decode_bbox_height = exp(bbox[3]) * prior_height;
  135. } else {
  136. // variance is encoded in bbox, we need to scale the offset accordingly.
  137. decode_bbox_center_x =
  138. prior_variance[0] * bbox[0] * prior_width + prior_center_x;
  139. decode_bbox_center_y =
  140. prior_variance[1] * bbox[1] * prior_height + prior_center_y;
  141. decode_bbox_width =
  142. exp(prior_variance[2] * bbox[2]) * prior_width;
  143. decode_bbox_height =
  144. exp(prior_variance[3] * bbox[3]) * prior_height;
  145. }
  146. decode_bbox->push_back(decode_bbox_center_x - decode_bbox_width / 2.);
  147. decode_bbox->push_back(decode_bbox_center_y - decode_bbox_height / 2.);
  148. decode_bbox->push_back(decode_bbox_center_x + decode_bbox_width / 2.);
  149. decode_bbox->push_back(decode_bbox_center_y + decode_bbox_height / 2.);
  150. } else if (2 == code_type) {
  151. float prior_width = prior_bbox[2] - prior_bbox[0];
  152. //CHECK_GT(prior_width, 0);
  153. float prior_height = prior_bbox[3] - prior_bbox[1];
  154. //CHECK_GT(prior_height, 0);
  155. if (variance_encoded_in_target) {
  156. // variance is encoded in target, we simply need to add the offset
  157. // predictions.
  158. decode_bbox->push_back(prior_bbox[0] + bbox[0] * prior_width);
  159. decode_bbox->push_back(prior_bbox[1] + bbox[1] * prior_height);
  160. decode_bbox->push_back(prior_bbox[2] + bbox[2] * prior_width);
  161. decode_bbox->push_back(prior_bbox[3] + bbox[3] * prior_height);
  162. } else {
  163. // variance is encoded in bbox, we need to scale the offset accordingly.
  164. decode_bbox->push_back(
  165. prior_bbox[0] + prior_variance[0] * bbox[0] * prior_width);
  166. decode_bbox->push_back(
  167. prior_bbox[1] + prior_variance[1] * bbox[1] * prior_height);
  168. decode_bbox->push_back(
  169. prior_bbox[2] + prior_variance[2] * bbox[2] * prior_width);
  170. decode_bbox->push_back(
  171. prior_bbox[3] + prior_variance[3] * bbox[3] * prior_height);
  172. }
  173. } else {
  174. std::cout<< "Unknown LocLossType."<<std::endl;
  175. }
  176. //clip_bbox = false, 所以没实现
  177. /*if (clip_bbox) {
  178. ClipBBox(*decode_bbox, decode_bbox);
  179. }*/
  180. }
  181. void DecodeBBoxes(
  182. const vector<vector<float> >& prior_bboxes,
  183. const vector<vector<float> >& prior_variances,
  184. const int code_type, const bool variance_encoded_in_target,
  185. const bool clip_bbox, const vector<vector<float> >& bboxes,
  186. vector<vector<float> >* decode_bboxes) {
  187. //CHECK_EQ(prior_bboxes.size(), prior_variances.size());
  188. //CHECK_EQ(prior_bboxes.size(), bboxes.size());
  189. int num_bboxes = prior_bboxes.size();
  190. for (int i = 0; i < num_bboxes; ++i) {
  191. vector<float> decode_bbox;
  192. DecodeBBox(prior_bboxes[i], prior_variances[i], code_type,
  193. variance_encoded_in_target, clip_bbox, bboxes[i], &decode_bbox);
  194. decode_bboxes->push_back(decode_bbox);
  195. }
  196. }
  197. //
  198. void ConfData(const float* data, const int num_classes, const int num_prior, float* new_data) {
  199. int idx = 0;
  200. for (int c = 0; c < num_classes; ++c) {
  201. for (int p = 0; p < num_prior; ++p) {
  202. new_data[idx] = data[p*num_classes + c];
  203. idx++;
  204. }
  205. }
  206. //softmax
  207. for (int p = 0; p < num_prior; ++p) {
  208. int sum = 0;
  209. float _max = new_data[p];//new_data[0*num_prior + p]
  210. for (int c = 1; c < num_classes; ++c) {
  211. _max = std::max(_max, new_data[c*num_prior + p]);
  212. }
  213. for (int c = 0; c < num_classes; ++c) {
  214. sum += exp(new_data[c*num_prior + p]-_max);
  215. }
  216. for (int j = 0; j < num_classes; ++j) {
  217. new_data[j*num_prior + p] = exp(new_data[j*num_prior + p]-_max)/sum;
  218. }
  219. }
  220. }
  221. template <typename Dtype>
  222. void DecodeBBoxes_2(const Dtype* loc_data, const Dtype* prior_data,
  223. const int code_type, const bool variance_encoded_in_target,
  224. const int num_priors, const bool share_location,
  225. const int num_loc_classes, const int background_label_id,
  226. const bool clip_bbox, Dtype* bbox_data) {
  227. if(code_type == 0){
  228. for(int p = 0; p < num_priors; p++) {
  229. if (variance_encoded_in_target) {
  230. for (int i = 0; i < 4; i++) {
  231. bbox_data[4 * p + i] = prior_data[4 * p + i] + loc_data[4 * p + i];
  232. }
  233. } else {
  234. for (int i = 0; i < 4; i++) {
  235. bbox_data[4 * p + i] = prior_data[4 * p + i] + prior_data[4 * num_priors + 4 * p + i] + loc_data[4 * p + i];
  236. }
  237. }
  238. }
  239. }else if(code_type == 1){
  240. for(int p = 0; p < num_priors; p++) {
  241. float prior_width = prior_data[4 * p + 2] - prior_data[4 * p + 0];
  242. float prior_height = prior_data[4 * p + 3] - prior_data[4 * p + 1];
  243. float prior_center_x = (prior_data[4 * p + 0] + prior_data[4 * p + 2]) / 2.;
  244. float prior_center_y = (prior_data[4 * p + 1] + prior_data[4 * p + 3]) / 2.;
  245. float decode_bbox_center_x, decode_bbox_center_y;
  246. float decode_bbox_width, decode_bbox_height;;
  247. if (variance_encoded_in_target) {
  248. decode_bbox_center_x = loc_data[4 * p + 0] * prior_width + prior_center_x;
  249. decode_bbox_center_y = loc_data[4 * p + 1] * prior_height + prior_center_y;
  250. decode_bbox_width = exp(loc_data[4 * p + 2]) * prior_width;
  251. decode_bbox_height = exp(loc_data[4 * p + 3]) * prior_height;
  252. }else{
  253. decode_bbox_center_x = prior_data[4 * num_priors + 4 * p + 0] * loc_data[4 * p + 0] * prior_width + prior_center_x;
  254. decode_bbox_center_y = prior_data[4 * num_priors + 4 * p + 1] * loc_data[4 * p + 1] * prior_height + prior_center_y;
  255. decode_bbox_width = exp(prior_data[4 * num_priors + 4 * p + 2] * loc_data[4 * p + 2]) * prior_width;
  256. decode_bbox_height = exp(prior_data[4 * num_priors + 4 * p + 3] * loc_data[4 * p + 3]) * prior_height;
  257. }
  258. bbox_data[4 * p + 0] = (decode_bbox_center_x - decode_bbox_width / 2.);
  259. bbox_data[4 * p + 1] = (decode_bbox_center_y - decode_bbox_height / 2.);
  260. bbox_data[4 * p + 2] = (decode_bbox_center_x + decode_bbox_width / 2.);
  261. bbox_data[4 * p + 3] = (decode_bbox_center_y + decode_bbox_height / 2.);
  262. }
  263. }else if(code_type == 2){
  264. for(int p = 0; p < num_priors; p++) {
  265. float prior_width = prior_data[4 * p + 2] - prior_data[4 * p + 0];
  266. float prior_height = prior_data[4 * p + 3] - prior_data[4 * p + 1];
  267. if (variance_encoded_in_target) {
  268. bbox_data[4 * p + 0] = prior_data[4 * p + 0] + loc_data[4 * p + 0] * prior_width;
  269. bbox_data[4 * p + 1] = prior_data[4 * p + 1] + loc_data[4 * p + 1] * prior_height;
  270. bbox_data[4 * p + 2] = exp(prior_data[4 * p + 2]) + loc_data[4 * p + 2] * prior_width;
  271. bbox_data[4 * p + 3] = exp(prior_data[4 * p + 3]) + loc_data[4 * p + 3] * prior_height;
  272. }else {
  273. bbox_data[4 * p + 0] = prior_data[4 * p + 0] +
  274. prior_data[4 * num_priors + 4 * p + 0] * loc_data[4 * p + 0] * prior_width;
  275. bbox_data[4 * p + 1] = prior_data[4 * p + 1] +
  276. prior_data[4 * num_priors + 4 * p + 1] * loc_data[4 * p + 1] * prior_height;
  277. bbox_data[4 * p + 2] = prior_data[4 * p + 2] +
  278. prior_data[4 * num_priors + 4 * p + 2] * loc_data[4 * p + 2] * prior_width;
  279. bbox_data[4 * p + 3] = prior_data[4 * p + 3] +
  280. prior_data[4 * num_priors + 4 * p + 3] * loc_data[4 * p + 3] * prior_height;
  281. }
  282. }
  283. }else{
  284. std::cout << "Unknown LocLossType." << std::endl;
  285. }
  286. }
  287. template <typename Dtype>
  288. Dtype BBoxSize(const Dtype* bbox, const bool normalized = true) {
  289. if (bbox[2] < bbox[0] || bbox[3] < bbox[1]) {
  290. // If bbox is invalid (e.g. xmax < xmin or ymax < ymin), return 0.
  291. return Dtype(0.);
  292. } else {
  293. const Dtype width = bbox[2] - bbox[0];
  294. const Dtype height = bbox[3] - bbox[1];
  295. if (normalized) {
  296. return width * height;
  297. } else {
  298. // If bbox is not within range [0, 1].
  299. return (width + 1) * (height + 1);
  300. }
  301. }
  302. }
  303. template <typename Dtype>
  304. Dtype JaccardOverlap(const Dtype* bbox1, const Dtype* bbox2) {
  305. if (bbox2[0] > bbox1[2] || bbox2[2] < bbox1[0] ||
  306. bbox2[1] > bbox1[3] || bbox2[3] < bbox1[1]) {
  307. return Dtype(0.);
  308. } else {
  309. const Dtype inter_xmin = std::max(bbox1[0], bbox2[0]);
  310. const Dtype inter_ymin = std::max(bbox1[1], bbox2[1]);
  311. const Dtype inter_xmax = std::min(bbox1[2], bbox2[2]);
  312. const Dtype inter_ymax = std::min(bbox1[3], bbox2[3]);
  313. const Dtype inter_width = inter_xmax - inter_xmin;
  314. const Dtype inter_height = inter_ymax - inter_ymin;
  315. const Dtype inter_size = inter_width * inter_height;
  316. const Dtype bbox1_size = BBoxSize(bbox1);
  317. const Dtype bbox2_size = BBoxSize(bbox2);
  318. return inter_size / (bbox1_size + bbox2_size - inter_size);
  319. }
  320. }
  321. template <typename T>
  322. bool SortScorePairDescend(const pair<float, T>& pair1,
  323. const pair<float, T>& pair2) {
  324. return pair1.first > pair2.first;
  325. }
  326. template <typename Dtype>
  327. void GetMaxScoreIndex(const Dtype* scores, const int num, const float threshold,
  328. const int top_k, vector<pair<Dtype, int> >* score_index_vec) {
  329. // Generate index score pairs.
  330. for (int i = 0; i < num; ++i) {
  331. if (scores[i] > threshold) {
  332. score_index_vec->push_back(std::make_pair(scores[i], i));
  333. }
  334. }
  335. // Sort the score pair according to the scores in descending order
  336. std::sort(score_index_vec->begin(), score_index_vec->end(),
  337. SortScorePairDescend<int>);
  338. // Keep top_k scores if needed.
  339. if (top_k > -1 && top_k < score_index_vec->size()) {
  340. score_index_vec->resize(top_k);
  341. }
  342. }
  343. template <typename Dtype>
  344. void ApplyNMSFast(const Dtype* bboxes, const Dtype* scores, const int num,
  345. const float score_threshold, const float nms_threshold,
  346. const float eta, const int top_k, vector<int>* indices) {
  347. // Get top_k scores (with corresponding indices).
  348. vector<pair<Dtype, int> > score_index_vec;
  349. //float n1 = cv::getTickCount();
  350. GetMaxScoreIndex(scores, num, score_threshold, top_k, &score_index_vec);
  351. // n1 = (cv::getTickCount()-n1) / cv::getTickFrequency();
  352. //printf("======n==1 Forward_DetectionOutputLayer time is %f \n", n1);
  353. // Do nms.
  354. float adaptive_threshold = nms_threshold;
  355. indices->clear();
  356. //float n2 = cv::getTickCount();
  357. std::cout<<"======n==n" <<score_index_vec.size()<<std::endl;
  358. while (score_index_vec.size() != 0) {
  359. const int idx = score_index_vec.front().second;
  360. bool keep = true;
  361. for (int k = 0; k < indices->size(); ++k) {
  362. if (keep) {
  363. const int kept_idx = (*indices)[k];
  364. float overlap = JaccardOverlap(bboxes + idx * 4, bboxes + kept_idx * 4);
  365. keep = overlap <= adaptive_threshold;
  366. } else {
  367. break;
  368. }
  369. }
  370. if (keep) {
  371. indices->push_back(idx);
  372. }
  373. score_index_vec.erase(score_index_vec.begin());
  374. if (keep && eta < 1 && adaptive_threshold > 0.5) {
  375. adaptive_threshold *= eta;
  376. }
  377. }
  378. //n2 = (cv::getTickCount()-n2) / cv::getTickFrequency();
  379. //printf("======n==2 Forward_DetectionOutputLayer time is %f \n", n2);
  380. }
  381. void Forward_DetectionOutputLayer(float* loc_data, float* conf_data, float* prior_data, int num_priors_, int num_classes_, vector<vector<float> >* detecions) {
  382. // Retrieve all location predictions.
  383. /*vector<vector<float>> all_loc_preds;
  384. GetLocPredictions(loc_data, num_priors_, num_loc_classes_, &all_loc_preds);
  385. // Retrieve all confidences.
  386. vector <vector<float>> all_conf_scores;
  387. GetConfidenceScores(conf_data, num_priors_, num_classes_,
  388. &all_conf_scores);
  389. // Retrieve all prior bboxes.
  390. vector<vector<float>> prior_bboxes;
  391. vector<vector<float>> prior_variances;
  392. GetPriorBBoxes(prior_data, num_priors_, &prior_bboxes, &prior_variances);
  393. // Decode all loc predictions to bboxes.
  394. vector<vector<float>> all_decode_bboxes;
  395. //const bool clip_bbox = false;
  396. DecodeBBoxes(prior_bboxes, prior_variances, code_type_,
  397. variance_encoded_in_target_, clip_bbox, all_loc_preds,
  398. &all_decode_bboxes);*/
  399. int num_kept = 0;
  400. vector<map<int, vector<int> > > all_indices;
  401. map<int , vector<int>> indices;
  402. int num_det = 0;
  403. const int conf_idx = num_classes_ * num_priors_;
  404. const bool share_location_ = true;
  405. const int num_loc_classes = 1;
  406. int background_label_id_ = 0;
  407. float confidence_threshold_ = 0.1;
  408. float nms_threshold_ = 0.45;
  409. float eta_ = 1.0;//默认1.0
  410. int top_k_ = 400;
  411. int keep_top_k_ = 200;
  412. const int code_type = 1;//center
  413. const bool variance_encoded_in_target = false;//default
  414. const bool clip_bbox = false;
  415. float* decode_bboxes = new float[4 * num_priors_];
  416. float t = cv::getTickCount();
  417. DecodeBBoxes_2<float>(loc_data, prior_data, code_type, variance_encoded_in_target, num_priors_, share_location_, num_loc_classes,background_label_id_, clip_bbox, decode_bboxes);
  418. t = (cv::getTickCount()-t) / cv::getTickFrequency();
  419. printf("======1 Forward_DetectionOutputLayer time is %f \n", t);
  420. float* new_conf_data = new float[num_priors_ * num_classes_];
  421. float t1 = cv::getTickCount();
  422. ConfData(conf_data, num_classes_, num_priors_, new_conf_data);
  423. t1 = (cv::getTickCount()-t1) / cv::getTickFrequency();
  424. printf("======2 Forward_DetectionOutputLayer time is %f \n", t1);
  425. float t2 = cv::getTickCount();
  426. for(int c = 0; c < num_classes_; c++){
  427. if(c == background_label_id_){
  428. continue;
  429. }
  430. float* cur_conf_data = new_conf_data + c * num_priors_;
  431. //float* cur_bbox_data = all_decode_bboxes
  432. float tt = cv::getTickCount();
  433. ApplyNMSFast<float>(decode_bboxes, cur_conf_data, num_priors_,
  434. confidence_threshold_, nms_threshold_, eta_, top_k_, &(indices[c]));
  435. tt = (cv::getTickCount()-tt) / cv::getTickFrequency();
  436. std::cout<<"===nms==="<<c<<"==nms=="<<std::endl;
  437. printf("======nms Forward_DetectionOutputLayer time is %f \n", tt);
  438. num_det += indices[c].size();
  439. }
  440. t2 = (cv::getTickCount()-t2) / cv::getTickFrequency();
  441. printf("======3 Forward_DetectionOutputLayer time is %f \n", t2);
  442. float t3 = cv::getTickCount();
  443. if(keep_top_k_ > -1 && num_det > keep_top_k_){
  444. vector<pair<float, pair<int, int> > > score_index_pairs;
  445. for(map<int, vector<int> >::iterator it = indices.begin(); it != indices.end(); ++it){
  446. int label = it->first;
  447. const vector<int>& label_indices = it->second;
  448. for(int j = 0; j < label_indices.size(); ++j){
  449. int idx = label_indices[j];
  450. float score = new_conf_data[label * num_priors_ + idx];
  451. score_index_pairs.push_back(std::make_pair(score, std::make_pair(label, idx)));
  452. }
  453. }
  454. // Keep top k results per image.
  455. std::sort(score_index_pairs.begin(), score_index_pairs.end(), SortScorePairDescend<pair<int, int> >);
  456. score_index_pairs.resize(keep_top_k_);
  457. // Store the new indices.
  458. map<int, vector<int> > new_indices;
  459. for(int j = 0; j < score_index_pairs.size(); ++j){
  460. int label = score_index_pairs[j].second.first;
  461. int idx = score_index_pairs[j].second.second;
  462. new_indices[label].push_back(idx);
  463. }
  464. all_indices.push_back(new_indices);
  465. num_kept += keep_top_k_;
  466. }else{
  467. all_indices.push_back(indices);
  468. num_kept += num_det;
  469. }
  470. if(num_kept == 0){
  471. printf("Couldn't find any detections");
  472. }else{
  473. for(map<int, vector<int> >::iterator it = all_indices[0].begin(); it != all_indices[0].end(); ++it){
  474. int label = it->first;
  475. vector<int>& _indices = it->second;
  476. const float* _cur_conf_data = new_conf_data + label * num_priors_;
  477. for(int j = 0; j < _indices.size(); ++j){
  478. int idx = _indices[j];
  479. vector<float> detect;
  480. for(int k = 0; k < 4; ++k){
  481. detect.push_back(decode_bboxes[idx * 4 + k]);
  482. }
  483. detect.push_back(_cur_conf_data[idx]);
  484. detect.push_back(label);
  485. detecions->push_back(detect);
  486. }
  487. }
  488. }
  489. t3 = (cv::getTickCount()-t3) / cv::getTickFrequency();
  490. printf("======4 Forward_DetectionOutputLayer time is %f \n", t3);
  491. delete[] decode_bboxes;
  492. delete[] new_conf_data;
  493. }