Refactoring for detection NN
Signed-off-by: Micaela Verucchi <micaelaverucchi@gmail.com>
This commit is contained in:
+145
-178
@@ -1,33 +1,19 @@
|
||||
#ifndef CENTERNETDETECTION_H
|
||||
#define CENTERNETDETECTION_H
|
||||
#include "CenternetDetection.h"
|
||||
|
||||
#include "CenternetDetection.h"
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
// #include <opencv2/cudawarping.hpp>
|
||||
// #include <opencv2/cudaarithm.hpp>
|
||||
|
||||
|
||||
|
||||
namespace tk { namespace dnn {
|
||||
|
||||
float __colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };
|
||||
float get_color2(int c, int x, int max)
|
||||
{
|
||||
float ratio = ((float)x/max)*5;
|
||||
int i = floor(ratio);
|
||||
int j = ceil(ratio);
|
||||
ratio -= i;
|
||||
float r = (1-ratio) * __colors[i % 6][c % 3] + ratio*__colors[j % 6][c % 3];
|
||||
//printf("%f\n", r);
|
||||
return r;
|
||||
}
|
||||
|
||||
bool CenternetDetection::init(std::string tensor_path) {
|
||||
bool CenternetDetection::init(const std::string& tensor_path, const int n_classes)
|
||||
{
|
||||
std::cout<<(tensor_path).c_str()<<"\n";
|
||||
netRT = new tk::dnn::NetworkRT(NULL, (tensor_path).c_str() );
|
||||
|
||||
dim = tk::dnn::dataDim_t(1, 3, 512, 512, 1);
|
||||
const char *coco_class_name_[] = {
|
||||
classes = n_classes;
|
||||
|
||||
dim = netRT->input_dim;
|
||||
|
||||
const char *coco_class_name[] = {
|
||||
"person", "bicycle", "car", "motorcycle", "airplane",
|
||||
"bus", "train", "truck", "boat", "traffic light", "fire hydrant",
|
||||
"stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse",
|
||||
@@ -42,19 +28,24 @@ bool CenternetDetection::init(std::string tensor_path) {
|
||||
"oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
|
||||
"scissors", "teddy bear", "hair drier", "toothbrush"
|
||||
};
|
||||
coco_class_name = std::vector<std::string>(coco_class_name_, std::end( coco_class_name_ ));
|
||||
classesNames = std::vector<std::string>(coco_class_name, std::end( coco_class_name));
|
||||
|
||||
for(int c=0; c<classes; c++) {
|
||||
int offset = c*123457 % classes;
|
||||
float r = getColor(2, offset, classes);
|
||||
float g = getColor(1, offset, classes);
|
||||
float b = getColor(0, offset, classes);
|
||||
colors[c] = cv::Scalar(int(255.0*b), int(255.0*g), int(255.0*r));
|
||||
}
|
||||
|
||||
src = cv::Mat(cv::Size(2,3), CV_32F);
|
||||
dst = cv::Mat(cv::Size(2,3), CV_32F);
|
||||
dst2 = cv::Mat(cv::Size(2,3), CV_32F);
|
||||
trans = cv::Mat(cv::Size(3,2), CV_32F);
|
||||
trans2 = cv::Mat(cv::Size(3,2), CV_32F);
|
||||
// dets = tk::dnn::Yolo::allocateDetections(tk::dnn::Yolo::MAX_DETECTIONS, classes);
|
||||
|
||||
checkCuda(cudaMalloc(&input_d, sizeof(dnnType)*netRT->input_dim.tot()));
|
||||
|
||||
// dim_hm = tk::dnn::dataDim_t(1, 80, 56, 56, 1);
|
||||
// dim_wh = tk::dnn::dataDim_t(1, 2, 56, 56, 1);
|
||||
// dim_reg = tk::dnn::dataDim_t(1, 2, 56, 56, 1);
|
||||
dim_hm = tk::dnn::dataDim_t(1, 80, 128, 128, 1);
|
||||
dim_wh = tk::dnn::dataDim_t(1, 2, 128, 128, 1);
|
||||
dim_reg = tk::dnn::dataDim_t(1, 2, 128, 128, 1);
|
||||
@@ -79,19 +70,16 @@ bool CenternetDetection::init(std::string tensor_path) {
|
||||
|
||||
checkCuda( cudaMallocHost(&scores, K *sizeof(float)) );
|
||||
checkCuda( cudaMalloc(&scores_d, K *sizeof(float)) );
|
||||
|
||||
checkCuda( cudaMallocHost(&clses, K *sizeof(int)) );
|
||||
checkCuda( cudaMalloc(&clses_d, K *sizeof(int)) );
|
||||
// checkCuda( cudaMallocHost(&topk_inds, K *sizeof(int)) );
|
||||
|
||||
checkCuda( cudaMalloc(&topk_inds_d, K *sizeof(int)) );
|
||||
checkCuda( cudaMalloc(&topk_ys_d, K *sizeof(float)) );
|
||||
checkCuda( cudaMalloc(&topk_xs_d, K *sizeof(float)) );
|
||||
// checkCuda( cudaMalloc(&intid, K *sizeof(int)) );
|
||||
checkCuda( cudaMalloc(&inttopk_ys_d, K *sizeof(int)) );
|
||||
checkCuda( cudaMalloc(&inttopk_xs_d, K *sizeof(int)) );
|
||||
|
||||
// checkCuda( cudaMalloc(&ids_d, dim_hm.c * K*sizeof(int)) );
|
||||
|
||||
// checkCuda( cudaMallocHost(&wh_aus, dim_wh.tot()*sizeof(dnnType)) );
|
||||
checkCuda( cudaMallocHost(&bbx0, K * sizeof(float)) );
|
||||
checkCuda( cudaMallocHost(&bby0, K * sizeof(float)) );
|
||||
checkCuda( cudaMallocHost(&bbx1, K * sizeof(float)) );
|
||||
@@ -119,14 +107,11 @@ bool CenternetDetection::init(std::string tensor_path) {
|
||||
#endif
|
||||
|
||||
checkCuda( cudaMalloc(&d_ptrs, dim.c * dim.h*dim.w * sizeof(float)) );
|
||||
// mean << 0.408, 0.447, 0.47;
|
||||
// stddev << 0.289, 0.274, 0.278;
|
||||
|
||||
// Alloc array used in the kernel
|
||||
checkCuda( cudaMalloc(&src_out, K *sizeof(float)) );
|
||||
checkCuda( cudaMalloc(&ids_out, K *sizeof(int)) );
|
||||
// checkCuda( cudaFree(src_out) );
|
||||
// checkCuda( cudaFree(ids_out) );
|
||||
|
||||
dst2.at<float>(0,0)=width * 0.5;
|
||||
dst2.at<float>(0,1)=width * 0.5;
|
||||
dst2.at<float>(1,0)=width * 0.5;
|
||||
@@ -137,60 +122,17 @@ bool CenternetDetection::init(std::string tensor_path) {
|
||||
|
||||
}
|
||||
|
||||
cv::Mat CenternetDetection::draw(cv::Mat &imageOrig) {
|
||||
|
||||
tk::dnn::box b;
|
||||
int x0, w, x1, y0, h, y1;
|
||||
int objClass;
|
||||
std::string det_class;
|
||||
int baseline = 0;
|
||||
float fontScale = 0.5;
|
||||
int thickness = 2;
|
||||
|
||||
for(int c=0; c<classes; c++) {
|
||||
int offset = c*123457 % classes;
|
||||
float r = get_color2(2, offset, classes);
|
||||
float g = get_color2(1, offset, classes);
|
||||
float b = get_color2(0, offset, classes);
|
||||
colors[c] = cv::Scalar(int(255.0*b), int(255.0*g), int(255.0*r));
|
||||
}
|
||||
int num_detected = detected.size();
|
||||
for (int i = 0; i < num_detected; i++){
|
||||
b = detected[i];
|
||||
x0 = b.x;
|
||||
w = b.w;
|
||||
x1 = b.x + w;
|
||||
y0 = b.y;
|
||||
h = b.h;
|
||||
y1 = b.y + h;
|
||||
objClass = b.cl;
|
||||
det_class = coco_class_name[objClass];
|
||||
cv::rectangle(imageOrig, cv::Point(x0, y0), cv::Point(x1, y1), colors[objClass], 2);
|
||||
// draw label
|
||||
cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
|
||||
cv::rectangle(imageOrig, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), colors[b.cl], -1);
|
||||
cv::putText(imageOrig, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(255, 255, 255), thickness);
|
||||
|
||||
}
|
||||
return imageOrig;
|
||||
// cv::namedWindow("cnet", cv::WINDOW_NORMAL);
|
||||
// cv::imshow("cnet", imageOrig);
|
||||
// cv::waitKey(10000);
|
||||
}
|
||||
|
||||
|
||||
void CenternetDetection::preprocess()
|
||||
void CenternetDetection::preprocess(cv::Mat &frame)
|
||||
{
|
||||
|
||||
auto start_t = std::chrono::steady_clock::now();
|
||||
auto step_t = std::chrono::steady_clock::now();
|
||||
auto end_t = std::chrono::steady_clock::now();
|
||||
|
||||
// -----------------------------------pre-process ------------------------------------------
|
||||
// it will resize the images to `224 x 224` in GETTING_STARTED.md
|
||||
cv::Size sz = imageOrig.size();
|
||||
std::cout<<"image: "<<sz.width<<", "<<sz.height<<std::endl;
|
||||
|
||||
// auto start_t = std::chrono::steady_clock::now();
|
||||
// auto step_t = std::chrono::steady_clock::now();
|
||||
// auto end_t = std::chrono::steady_clock::now();
|
||||
cv::Size sz = originalSize;
|
||||
std::cout<<"image: "<<sz.width<<", "<<sz.height<<std::endl;
|
||||
cv::Size sz_old;
|
||||
float scale = 1.0;
|
||||
float new_height = sz.height * scale;
|
||||
float new_width = sz.width * scale;
|
||||
@@ -214,10 +156,10 @@ void CenternetDetection::preprocess()
|
||||
src.at<float>(0,1)=c[1];
|
||||
src.at<float>(1,0)=c[0];
|
||||
src.at<float>(1,1)=c[1] + s[0] * -0.5;
|
||||
dst.at<float>(0,0)=inp_width * 0.5;
|
||||
dst.at<float>(0,1)=inp_height * 0.5;
|
||||
dst.at<float>(1,0)=inp_width * 0.5;
|
||||
dst.at<float>(1,1)=inp_height * 0.5 + inp_width * -0.5;
|
||||
dst.at<float>(0,0)=netRT->input_dim.w * 0.5;
|
||||
dst.at<float>(0,1)=netRT->input_dim.h * 0.5;
|
||||
dst.at<float>(1,0)=netRT->input_dim.w * 0.5;
|
||||
dst.at<float>(1,1)=netRT->input_dim.h * 0.5 + netRT->input_dim.w * -0.5;
|
||||
|
||||
src.at<float>(2,0)=src.at<float>(1,0) + (-src.at<float>(0,1)+src.at<float>(1,1) );
|
||||
src.at<float>(2,1)=src.at<float>(1,1) + (src.at<float>(0,0)-src.at<float>(1,0) );
|
||||
@@ -225,91 +167,87 @@ void CenternetDetection::preprocess()
|
||||
dst.at<float>(2,1)=dst.at<float>(1,1) + (dst.at<float>(0,0)-dst.at<float>(1,0) );
|
||||
|
||||
trans = cv::getAffineTransform( src, dst );
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME gett affine trans: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME gett affine trans: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
trans2 = cv::getAffineTransform( dst2, src );
|
||||
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME getAffineTrans 2: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME getAffineTrans 2: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
}
|
||||
sz_old = sz;
|
||||
#ifdef OPENCV_CUDA
|
||||
cv::cuda::GpuMat im_Orig;
|
||||
cv::cuda::GpuMat imageF1_d, imageF2_d;
|
||||
|
||||
im_Orig = cv::cuda::GpuMat(imageOrig);
|
||||
im_Orig = cv::cuda::GpuMat(frame);
|
||||
cv::cuda::resize (im_Orig, imageF1_d, cv::Size(new_width, new_height));
|
||||
checkCuda( cudaDeviceSynchronize() );
|
||||
|
||||
sz = imageF1_d.size();
|
||||
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME resize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME resize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
cv::cuda::warpAffine(imageF1_d, imageF2_d, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR );
|
||||
cv::cuda::warpAffine(imageF1_d, imageF2_d, trans, cv::Size(netRT->input_dim.w, netRT->input_dim.h), cv::INTER_LINEAR );
|
||||
checkCuda( cudaDeviceSynchronize() );
|
||||
|
||||
|
||||
imageF2_d.convertTo(imageF1_d, CV_32FC3, 1/255.0);
|
||||
checkCuda( cudaDeviceSynchronize() );
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME convert: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME convert: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
dim2 = dim;
|
||||
cv::cuda::GpuMat bgr[3];
|
||||
cv::cuda::split(imageF1_d,bgr);//split source
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME split: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME split: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
for(int i=0; i<dim.c; i++)
|
||||
checkCuda( cudaMemcpy(d_ptrs + i*dim.h * dim.w, (float*)bgr[i].data, dim.h * dim.w * sizeof(float), cudaMemcpyDeviceToDevice) );
|
||||
|
||||
normalize(d_ptrs, dim.c, dim.h, dim.w, mean_d, stddev_d);
|
||||
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME normalize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME normalize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
checkCuda(cudaMemcpy(input_d, d_ptrs, dim2.tot()*sizeof(dnnType), cudaMemcpyDeviceToDevice));
|
||||
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME Memcpy to input_d: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME Memcpy to input_d: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
#else
|
||||
|
||||
cv::Mat imageF;
|
||||
resize(imageOrig, imageF, cv::Size(new_width, new_height));
|
||||
resize(frame, imageF, cv::Size(new_width, new_height));
|
||||
sz = imageF.size();
|
||||
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME resize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME resize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
cv::Mat trans = cv::getAffineTransform( src, dst );
|
||||
cv::warpAffine(imageF, imageF, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR );
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME warpAffine: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
cv::warpAffine(imageF, imageF, trans, cv::Size(netRT->input_dim.w, netRT->input_dim.h), cv::INTER_LINEAR );
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME warpAffine: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
sz = imageF.size();
|
||||
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
|
||||
imageF.convertTo(imageF, CV_32FC3, 1/255.0);
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME convertto: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME convertto: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
dim2 = dim;
|
||||
|
||||
//split channels
|
||||
cv::Mat bgr[3];
|
||||
cv::split(imageF,bgr);//split source
|
||||
|
||||
for(int i=0; i<3; i++){
|
||||
bgr[i] = bgr[i] - mean[i];
|
||||
bgr[i] = bgr[i] / stddev[i];
|
||||
@@ -322,28 +260,20 @@ void CenternetDetection::preprocess()
|
||||
// std::cout<<"i: "<<i<<", idx: "<<idx<<", ch: "<<ch<<std::endl;
|
||||
memcpy((void*)&input[idx], (void*)bgr[ch].data, imageF.rows*imageF.cols*sizeof(dnnType));
|
||||
}
|
||||
|
||||
checkCuda(cudaMemcpyAsync(input_d, input, dim2.tot()*sizeof(dnnType), cudaMemcpyHostToDevice));
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void CenternetDetection::update(cv::Mat &image_orig) {
|
||||
|
||||
imageOrig = image_orig;
|
||||
if(!imageOrig.data) {
|
||||
void CenternetDetection::update(cv::Mat &frame)
|
||||
{
|
||||
originalSize = frame.size();
|
||||
if(!frame.data) {
|
||||
std::cout<<"CENTERNET: NO IMAGE DATA\n";
|
||||
return;
|
||||
}
|
||||
TIMER_START
|
||||
auto start_t = std::chrono::steady_clock::now();
|
||||
auto step_t = std::chrono::steady_clock::now();
|
||||
auto end_t = std::chrono::steady_clock::now();
|
||||
|
||||
preprocess();
|
||||
preprocess(frame);
|
||||
|
||||
printCenteredTitle(" TENSORRT inference ", '=', 30); {
|
||||
dim2.print();
|
||||
@@ -352,22 +282,33 @@ void CenternetDetection::update(cv::Mat &image_orig) {
|
||||
TIMER_STOP
|
||||
dim2.print();
|
||||
}
|
||||
step_t = std::chrono::steady_clock::now();
|
||||
|
||||
// ------------------------------------ process --------------------------------------------
|
||||
|
||||
|
||||
dnnType *rt_out[4];
|
||||
rt_out[0] = (dnnType *)netRT->buffersRT[1];
|
||||
rt_out[1] = (dnnType *)netRT->buffersRT[2];
|
||||
rt_out[2] = (dnnType *)netRT->buffersRT[3];
|
||||
rt_out[3] = (dnnType *)netRT->buffersRT[4];
|
||||
|
||||
postprocess(rt_out, 4);
|
||||
|
||||
// std::cout<<"TOTAL: \n";
|
||||
TIMER_STOP
|
||||
stats.push_back(t_ns);
|
||||
}
|
||||
|
||||
void CenternetDetection::postprocess(dnnType **rt_out, const int n_out)
|
||||
{
|
||||
// auto start_t = std::chrono::steady_clock::now();
|
||||
// auto step_t = std::chrono::steady_clock::now();
|
||||
// auto end_t = std::chrono::steady_clock::now();
|
||||
// ------------------------------------ process --------------------------------------------
|
||||
activationSIGMOIDForward(rt_out[0], rt_out[0], dim_hm.tot());
|
||||
checkCuda( cudaDeviceSynchronize() );
|
||||
|
||||
subtractWithThreshold(rt_out[0], rt_out[0] + dim_hm.tot(), rt_out[1], rt_out[0], op);
|
||||
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME threshold: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME threshold: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
// ----------- nms end
|
||||
// ----------- topk
|
||||
|
||||
@@ -378,28 +319,25 @@ void CenternetDetection::update(cv::Mat &image_orig) {
|
||||
|
||||
checkCuda( cudaMemcpy(ids_d, ids_, dim_hm.c * dim_hm.h * dim_hm.w*sizeof(int), cudaMemcpyHostToDevice) );
|
||||
|
||||
sort(rt_out[0],
|
||||
rt_out[0]+dim_hm.tot(),
|
||||
ids_d);
|
||||
sort(rt_out[0],rt_out[0]+dim_hm.tot(),ids_d);
|
||||
checkCuda( cudaDeviceSynchronize() );
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME sort: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME sort: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
topk(rt_out[0], ids_d, K, scores_d,
|
||||
topk_inds_d, topk_ys_d, topk_xs_d);
|
||||
topk(rt_out[0], ids_d, K, scores_d, topk_inds_d, topk_ys_d, topk_xs_d);
|
||||
checkCuda( cudaDeviceSynchronize() );
|
||||
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME topk: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME topk: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
checkCuda( cudaMemcpy(scores, scores_d, K *sizeof(float), cudaMemcpyDeviceToHost) );
|
||||
|
||||
topKxyclasses(topk_inds_d, topk_inds_d+K, K, width, dim_hm.w*dim_hm.h, clses_d, inttopk_xs_d, inttopk_ys_d);
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME topk x y clses 2: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME topk x y clses 2: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
checkCuda( cudaMemcpy(topk_xs_d, (float *)inttopk_xs_d, K*sizeof(float), cudaMemcpyDeviceToDevice) );
|
||||
checkCuda( cudaMemcpy(topk_ys_d, (float *)inttopk_ys_d, K*sizeof(float), cudaMemcpyDeviceToDevice) );
|
||||
@@ -411,9 +349,9 @@ void CenternetDetection::update(cv::Mat &image_orig) {
|
||||
topKxyAddOffset(topk_inds_d, K, dim_reg.h*dim_reg.w, inttopk_xs_d, inttopk_ys_d, topk_xs_d, topk_ys_d, rt_out[3], src_out, ids_out);
|
||||
// checkCuda( cudaDeviceSynchronize() );
|
||||
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME add offset: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME add offset: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
bboxes(topk_inds_d, K, dim_wh.h*dim_wh.w, topk_xs_d, topk_ys_d, rt_out[2], bbx0_d, bbx1_d, bby0_d, bby1_d, src_out, ids_out);
|
||||
// checkCuda( cudaDeviceSynchronize() );
|
||||
@@ -423,9 +361,9 @@ void CenternetDetection::update(cv::Mat &image_orig) {
|
||||
checkCuda( cudaMemcpy(bbx1, bbx1_d, K * sizeof(float), cudaMemcpyDeviceToHost) );
|
||||
checkCuda( cudaMemcpy(bby1, bby1_d, K * sizeof(float), cudaMemcpyDeviceToHost) );
|
||||
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME bboxes: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME bboxes: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
|
||||
// ---------------------------------- post-process -----------------------------------------
|
||||
|
||||
@@ -459,7 +397,7 @@ void CenternetDetection::update(cv::Mat &image_orig) {
|
||||
for(int i = 0; i<classes; i++){
|
||||
for(int j=0; j<K; j++)
|
||||
if(clses[j] == i){
|
||||
if(scores[j] > thresh){
|
||||
if(scores[j] > confThreshold){
|
||||
// std::cout<<"th: "<<scores[j]<<" - cl: "<<clses[j]<<" i: "<<i<<std::endl;
|
||||
//add coco bbox
|
||||
//det[0:4], i, det[4]
|
||||
@@ -482,14 +420,43 @@ void CenternetDetection::update(cv::Mat &image_orig) {
|
||||
}
|
||||
}
|
||||
|
||||
end_t = std::chrono::steady_clock::now();
|
||||
std::cout << " TIME detections: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
step_t = end_t;
|
||||
|
||||
std::cout<<"TOTAL: \n";
|
||||
TIMER_STOP
|
||||
stats.push_back(t_ns);
|
||||
// end_t = std::chrono::steady_clock::now();
|
||||
// std::cout << " TIME detections: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
|
||||
// step_t = end_t;
|
||||
}
|
||||
|
||||
cv::Mat CenternetDetection::draw(cv::Mat &frame)
|
||||
{
|
||||
tk::dnn::box b;
|
||||
int x0, w, x1, y0, h, y1;
|
||||
int objClass;
|
||||
std::string det_class;
|
||||
int baseline = 0;
|
||||
float font_scale = 0.5;
|
||||
int thickness = 2;
|
||||
|
||||
int num_detected = detected.size();
|
||||
for (int i = 0; i < num_detected; i++){
|
||||
b = detected[i];
|
||||
x0 = b.x;
|
||||
w = b.w;
|
||||
x1 = b.x + w;
|
||||
y0 = b.y;
|
||||
h = b.h;
|
||||
y1 = b.y + h;
|
||||
objClass = b.cl;
|
||||
det_class = classesNames[objClass];
|
||||
cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), colors[objClass], 2);
|
||||
// draw label
|
||||
cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
|
||||
cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), colors[b.cl], -1);
|
||||
cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
|
||||
|
||||
}
|
||||
return frame;
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
|
||||
#endif /*CENTERNETDETECTION_H*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user