From ff6e0e010adc120bd010c605f82355111758dd08 Mon Sep 17 00:00:00 2001 From: Davide Sapienza Date: Fri, 30 Apr 2021 22:26:33 +0200 Subject: [PATCH] Fix a bug with batch > 1 Signed-off-by: Davide Sapienza --- include/tkDNN/CenternetDetection3DTrack.h | 9 +- src/CenternetDetection3DTrack.cpp | 184 +++++++++++----------- 2 files changed, 99 insertions(+), 94 deletions(-) diff --git a/include/tkDNN/CenternetDetection3DTrack.h b/include/tkDNN/CenternetDetection3DTrack.h index 451bf6e..258fe9d 100644 --- a/include/tkDNN/CenternetDetection3DTrack.h +++ b/include/tkDNN/CenternetDetection3DTrack.h @@ -148,10 +148,9 @@ private: std::vector det_res; int count_det; //tracks - std::vector tr_res; - std::vector> batchTracked; - int count_tr; - int track_id=0; + std::vector> tr_res; + std::vector count_tr; + std::vector track_id; bool init_preprocessing(); @@ -161,7 +160,7 @@ private: void pre_inf(const int bi); void _get_additional_inputs(); cv::Mat transform_preds_with_trans(float x1, float x2); - void tracking(); + void tracking(const int bi); public: tk::dnn::Network *pre_phase_net = nullptr; diff --git a/src/CenternetDetection3DTrack.cpp b/src/CenternetDetection3DTrack.cpp index 02db674..bdc1c59 100644 --- a/src/CenternetDetection3DTrack.cpp +++ b/src/CenternetDetection3DTrack.cpp @@ -17,8 +17,6 @@ bool CenternetDetection3DTrack::init(const std::string& tensor_path, const int n init_pre_inf(); init_postprocessing(); init_visualization(n_classes); - - count_tr = 0; } bool CenternetDetection3DTrack::init_preprocessing(){ @@ -201,6 +199,11 @@ bool CenternetDetection3DTrack::init_postprocessing(){ // Alloc array used in the kernel checkCuda( cudaMalloc(&src_out, K *sizeof(float)) ); checkCuda( cudaMalloc(&ids_out, K *sizeof(int)) ); + + for(int bi=0; bi& stream_size){ // -----------------------------------pre-process ------------------------------------------ - batchTracked.clear(); cv::Size sz = originalSize[bi]; float scale = 1.0; float new_height = sz.height * scale; @@ -414,8 +416,7 @@ cv::Mat CenternetDetection3DTrack::transform_preds_with_trans(float x1, float x2 return trans_out * target_coords; } -void CenternetDetection3DTrack::tracking(){ - +void CenternetDetection3DTrack::tracking(const int bi) { float item_size[count_det]; int item_cl[count_det]; float dets[2*count_det]; @@ -427,44 +428,44 @@ void CenternetDetection3DTrack::tracking(){ dets[i*2+1] = det_res[i].ct.at(0,1); } - float track_size[count_tr]; - int track_cl[count_tr]; - float tracks[2*count_tr]; - for(int i=0; i(0,0) - tr_res[i].det_res.bb0.at(0,0)) * - (tr_res[i].det_res.bb1.at(0,1) - tr_res[i].det_res.bb0.at(0,1)); - track_cl[i] = tr_res[i].det_res.cl; - tracks[i*2] = tr_res[i].det_res.ct.at(0,0); - tracks[i*2+1] = tr_res[i].det_res.ct.at(0,1); + float track_size[count_tr[bi]]; + int track_cl[count_tr[bi]]; + float tracks[2*count_tr[bi]]; + for(int i=0; i(0,0) - tr_res[bi][i].det_res.bb0.at(0,0)) * + (tr_res[bi][i].det_res.bb1.at(0,1) - tr_res[bi][i].det_res.bb0.at(0,1)); + track_cl[i] = tr_res[bi][i].det_res.cl; + tracks[i*2] = tr_res[bi][i].det_res.ct.at(0,0); + tracks[i*2+1] = tr_res[bi][i].det_res.ct.at(0,1); } - float dist[count_tr*count_det]; + float dist[count_tr[bi]*count_det]; bool invalid; - for(int i=0; i track_size[i] || dist[j*count_tr+i] > item_size[j] || item_cl[j] != track_cl[i]; - dist[j*count_tr+i] = dist[j*count_tr+i] + invalid * (1 << 18); + invalid = dist[j*count_tr[bi]+i] > track_size[i] || dist[j*count_tr[bi]+i] > item_size[j] || item_cl[j] != track_cl[i]; + dist[j*count_tr[bi]+i] = dist[j*count_tr[bi]+i] + invalid * (1 << 18); } } - int matched_indices[2*count_tr]; + int matched_indices[2*count_tr[bi]]; float min_tr; int min_idtr=-1; - for(int i=0; i new_tr_res; int id_new_tr=0; - for(int i=0; i new_thresh) { count_tr_ ++; @@ -583,17 +583,24 @@ void CenternetDetection3DTrack::tracking(){ new_tr_res_.det_res.y = det_res[i].y; new_tr_res_.det_res.z = det_res[i].z; new_tr_res_.det_res.rot_y = det_res[i].rot_y; - new_tr_res_.tracking_id = track_id++; + new_tr_res_.tracking_id = track_id[bi]++; new_tr_res_.age = 1; new_tr_res_.active = 1; new_tr_res_.color = rand() % 256; - tr_res.push_back(new_tr_res_); + if(tr_res.size() <= bi) { + std::vector v_new_tr_res_; + v_new_tr_res_.push_back(new_tr_res_); + tr_res.push_back(v_new_tr_res_); + } + else + tr_res[bi].push_back(new_tr_res_); } } - count_tr = count_tr_; + + count_tr[bi] = count_tr_; - if(track_id==1000) - track_id=0; + if(track_id[bi]==1000) + track_id[bi]=0; det_res.clear(); } @@ -729,8 +736,7 @@ void CenternetDetection3DTrack::postprocess(const int bi, const bool mAP) { } // track step - tracking(); - batchTracked.push_back(tr_res); + tracking(bi); } void CenternetDetection3DTrack::draw(std::vector& frames) { @@ -743,8 +749,8 @@ void CenternetDetection3DTrack::draw(std::vector& frames) { int thickness = 2; for(int bi=0; bi