Centernet: fix pooling problem, add centrnet demo

Signed-off-by: Micaela Verucchi <micaelaverucchi@gmail.com>
Signed-off-by: Davide Sapienza <sapienza.dav@gmail.com>
This commit is contained in:
Davide Sapienza
2020-01-21 12:50:18 +01:00
parent b7a6e8c8f5
commit 07da54efe9
12 changed files with 229 additions and 935 deletions
+4 -1
View File
@@ -105,9 +105,12 @@ target_link_libraries(test_resnet101_cnet tkDNN)
add_executable(test_rtinference tests/test_rtinference/rtinference.cpp)
target_link_libraries(test_rtinference tkDNN)
add_executable(yolo3_demo demo/demo/demo.cpp)
add_executable(yolo3_demo demo/demo/demo_yolo3.cpp)
target_link_libraries(yolo3_demo tkDNN)
add_executable(centernet_demo demo/demo/demo_centernet.cpp)
target_link_libraries(centernet_demo tkDNN)
#-------------------------------------------------------------------------------
# Install
+88
View File
@@ -0,0 +1,88 @@
#include <iostream>
#include <signal.h>
#include <stdlib.h> /* srand, rand */
#include <unistd.h>
#include <mutex>
#include "utils.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "CenternetDetection.h"
bool gRun;
bool SAVE_RESULT = false;
void sig_handler(int signo) {
std::cout<<"request gateway stop\n";
gRun = false;
}
int main(int argc, char *argv[]) {
std::cout<<"detection\n";
signal(SIGINT, sig_handler);
char *net = "resnet101_cnet.rt";
if(argc > 1)
net = argv[1];
char *input = "../demo/yolo_test.mp4";
if(argc > 2)
input = argv[2];
tk::dnn::CenternetDetection cnet;
cnet.init(net);
gRun = true;
cv::VideoCapture cap(input);
if(!cap.isOpened())
gRun = false;
else
std::cout<<"camera started\n";
cv::VideoWriter resultVideo;
if(SAVE_RESULT) {
int w = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int h = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
resultVideo.open("result.mp4", cv::VideoWriter::fourcc('M','P','4','V'), 30, cv::Size(w, h));
}
cv::Mat frame;
cv::Mat dnn_input;
cv::namedWindow("detection", cv::WINDOW_NORMAL);
while(gRun) {
cap >> frame;
if(!frame.data) {
break;
}
// this will be resized to the net format
dnn_input = frame.clone();
// TODO: async infer
cnet.update(dnn_input);
// draw dets
frame = cnet.draw(dnn_input);
cv::imshow("detection", frame);
cv::waitKey(1);
if(SAVE_RESULT)
resultVideo << frame;
}
std::cout<<"detection end\n";
std::cout<<COL_GREENB<<"\n\nTime stats:\n";
std::cout<<"Min: "<<*std::min_element(cnet.stats.begin(), cnet.stats.end())<<" ms\n";
std::cout<<"Max: "<<*std::max_element(cnet.stats.begin(), cnet.stats.end())<<" ms\n";
double mean = 0; for(int i=0; i<cnet.stats.size(); i++) mean += cnet.stats[i]; mean /= cnet.stats.size();
std::cout<<"Avg: "<<mean<<" ms\n"<<COL_END;
return 0;
}
+23 -27
View File
@@ -10,8 +10,7 @@
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc/imgproc.hpp>
// #include "Yolo3Detection.h"
#include "CenternetDetection.h"
#include "Yolo3Detection.h"
bool gRun;
bool SAVE_RESULT = false;
@@ -27,15 +26,14 @@ int main(int argc, char *argv[]) {
signal(SIGINT, sig_handler);
char *net = "resnet101_cnet.rt";
char *net = "yolo3_berkeley.rt";
if(argc > 1)
net = argv[1];
char *input = "../demo/yolo_test.mp4";
if(argc > 2)
input = argv[2];
// tk::dnn::Yolo3Detection yolo;
tk::dnn::CenternetDetection yolo;
tk::dnn::Yolo3Detection yolo;
yolo.init(net);
gRun = true;
@@ -69,30 +67,28 @@ int main(int argc, char *argv[]) {
// TODO: async infer
yolo.update(dnn_input);
frame = yolo.draw(dnn_input);
// // draw dets
// for(int i=0; i<yolo.detected.size(); i++) {
// tk::dnn::box b = yolo.detected[i];
// int x0 = b.x;
// int x1 = b.x + b.w;
// int y0 = b.y;
// int y1 = b.y + b.h;
// std::string det_class = yolo.coco_class_name[b.cl];
// // yolo.getYoloLayer()->classesNames[b.cl];
// float prob = b.prob;
// draw dets
for(int i=0; i<yolo.detected.size(); i++) {
tk::dnn::box b = yolo.detected[i];
int x0 = b.x;
int x1 = b.x + b.w;
int y0 = b.y;
int y1 = b.y + b.h;
std::string det_class = yolo.getYoloLayer()->classesNames[b.cl];
float prob = b.prob;
// // std::cout<<det_class<<" ("<<prob<<"): "<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<"\n";
// // draw rectangle
// cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), yolo.colors[b.cl], 2);
// std::cout<<det_class<<" ("<<prob<<"): "<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<"\n";
// draw rectangle
cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), yolo.colors[b.cl], 2);
// // draw label
// int baseline = 0;
// float fontScale = 0.5;
// int thickness = 2;
// cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
// cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), yolo.colors[b.cl], -1);
// cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(255, 255, 255), thickness);
// }
// draw label
int baseline = 0;
float fontScale = 0.5;
int thickness = 2;
cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), yolo.colors[b.cl], -1);
cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(255, 255, 255), thickness);
}
cv::imshow("detection", frame);
cv::waitKey(1);
+3
View File
@@ -93,6 +93,9 @@ class CenternetDetection {
// draw
std::vector<std::string> coco_class_name;
// keep track of inference times (ms)
std::vector<double> stats;
CenternetDetection() {}
virtual ~CenternetDetection() {}
@@ -0,0 +1,60 @@
#include<cassert>
#include "../kernels.h"
class ActivationSigmoidRT : public IPlugin {
public:
ActivationSigmoidRT() {
}
~ActivationSigmoidRT(){
}
int getNbOutputs() const override {
return 1;
}
Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override {
return inputs[0];
}
void configure(const Dims* inputDims, int nbInputs, const Dims* outputDims, int nbOutputs, int maxBatchSize) override {
size = 1;
for(int i=0; i<outputDims[0].nbDims; i++)
size *= outputDims[0].d[i];
}
int initialize() override {
return 0;
}
virtual void terminate() override {
}
virtual size_t getWorkspaceSize(int maxBatchSize) const override {
return 0;
}
virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override {
activationSIGMOIDForward((dnnType*)reinterpret_cast<const dnnType*>(inputs[0]),
reinterpret_cast<dnnType*>(outputs[0]), size, stream);
return 0;
}
virtual size_t getSerializationSize() override {
return 1*sizeof(int);
}
virtual void serialize(void* buffer) override {
char *buf = reinterpret_cast<char*>(buffer);
tk::dnn::writeBUF(buf, size);
}
int size;
};
@@ -90,7 +90,6 @@ public:
}
virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override {
std::cout<<"LOL\n";
dnnType *srcData = (dnnType*)reinterpret_cast<const dnnType*>(inputs[0]);
dnnType *output_conv = (dnnType*)reinterpret_cast<const dnnType*>(inputs[1]);
+42 -209
View File
@@ -229,58 +229,50 @@ void CenternetDetection::update(cv::Mat &imageORIG) {
src.at<float>(2,1)=src.at<float>(1,1) + (src.at<float>(0,0)-src.at<float>(1,0) );
dst.at<float>(2,0)=dst.at<float>(1,0) + (-dst.at<float>(0,1)+dst.at<float>(1,1) );
dst.at<float>(2,1)=dst.at<float>(1,1) + (dst.at<float>(0,0)-dst.at<float>(1,0) );
// std::cout<<"src: "<<src<<std::endl;
// std::cout<<"dst: "<<dst<<std::endl;
cv::Mat trans = cv::getAffineTransform( src, dst );
end_t = std::chrono::steady_clock::now();
std::cout << " TIME getAffinetr : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
std::cout << " TIME gett affine trans: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
resize(imageORIG, 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::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
cv::warpAffine(imageF, imageF, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR );
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::milliseconds>(end_t - step_t).count() << " ms" << 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 convert_to: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
std::cout<<"mean: "<<mean<<", std: "<<stddev<<std::endl;
dim2 = dim;
end_t = std::chrono::steady_clock::now();
std::cout << " TIME before split: " << std::chrono::duration_cast<std::chrono::microseconds>(end_t - step_t).count() << " us" << std::endl;
step_t = end_t;
//split channels
cv::split(imageF,bgr);//split source
end_t = std::chrono::steady_clock::now();
std::cout << " TIME split: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
std::cout << " TIME convert: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
dim2 = dim;
//split channels
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];
}
end_t = std::chrono::steady_clock::now();
std::cout << " TIME mean std: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
//write channels
for(int i=0; i<dim2.c; i++) {
int idx = i*imageF.rows*imageF.cols;
int ch = dim2.c-3 +i;
std::cout<<"i: "<<i<<", idx: "<<idx<<", ch: "<<ch<<std::endl;
// std::cout<<"i: "<<i<<", idx: "<<idx<<", ch: "<<ch<<std::endl;
memcpy((void*)&input[idx], (void*)bgr[ch].data, imageF.rows*imageF.cols*sizeof(dnnType));
}
@@ -292,241 +284,85 @@ void CenternetDetection::update(cv::Mat &imageORIG) {
netRT->infer(dim2, input_d);
TIMER_STOP
dim2.print();
stats.push_back(t_ns);
}
// checkResult(dim2.tot(), input_h, input);
std::cout<<" --- pre-process ---\n";
end_t = std::chrono::steady_clock::now();
std::cout << " TIME : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
step_t = std::chrono::steady_clock::now();
// ------------------------------------ process --------------------------------------------
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];
activationSIGMOIDForward(rt_out[0], rt_out[0], dim_hm.tot());
checkCuda( cudaDeviceSynchronize() );
end_t = std::chrono::steady_clock::now();
std::cout << " TIME sigmoid : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
subtractWithThreshold(rt_out[0], rt_out[0] + dim_hm.tot(), rt_out[1], rt_out[0]);
float *prova;
checkCuda( cudaMallocHost(&prova, K*sizeof(float)) );
checkCuda( cudaMemcpy(prova, rt_out[0], K*sizeof(float), cudaMemcpyDeviceToHost) );
std::cout<<"heat:\n";
for(int i=0; i<K; i++)
std::cout<<prova[i]<<" ";
std::cout<<"\n\n\n";
// for(int i=0; i < dim_hm.tot(); i++){
// if(hm_h[i]-hmax_h[i] > toll || hm_h[i]-hmax_h[i] < -toll){
// hm_h[i] = 0.0f;
// }
// }
// checkCuda( cudaFreeHost(hmax_h) );
std::cout<<" --- hmax ---\n";
end_t = std::chrono::steady_clock::now();
std::cout << " TIME : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
std::cout << " TIME threshold: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// ----------- nms end
// ----------- topk
// thrust::device_vector<int> ids_d;
// int ids[dim_hm.h * dim_hm.w];
// for(int i=0; i<dim_hm.h * dim_hm.w; i++){
// ids[i]=i;
// }
// std::vector<int> ids2( dim_hm.h * dim_hm.w );
// for(int i=0; i<dim_hm.h * dim_hm.w; i++){
// ids2[i]=i;
// }
// int ids2[dim_hm.h * dim_hm.w];
// checkCuda( cudaMemcpy(ids2_d, ids2, dim_hm.h * dim_hm.w*sizeof(int), cudaMemcpyHostToDevice) );
if(K > dim_hm.h * dim_hm.w){
printf ("Error topk (K is too large)\n");
return;
}
checkCuda( cudaMemcpy(ids_d, ids_, dim_hm.c * dim_hm.h * dim_hm.w*sizeof(int), cudaMemcpyHostToDevice) );
// checkCuda( cudaMemcpy(ids_2d, ids_2, dim_hm.h * dim_hm.w*sizeof(int), cudaMemcpyHostToDevice) );
// sortAndTopKonDevice(rt_out[0], ids_2d, topk_scores, topk_inds_ , topk_ys_ , topk_xs_ ,dim_hm.h * dim_hm.w, K, dim_hm.c);
// checkCuda( cudaDeviceSynchronize() );
// for(int i=0; i<dim_hm.c; i++){
// // get the hm->output_dim.h * hm->output_dim.w elements for each channel and sort it. Then find the first 100 elements
// // memcpy(ids2, ids, dim_hm.h * dim_hm.w);
// sort(rt_out[0]+ i * dim_hm.h * dim_hm.w,
// rt_out[0]+ i * dim_hm.h * dim_hm.w + dim_hm.h * dim_hm.w,
// ids_d);
// // end_t = std::chrono::steady_clock::now();
// // std::cout << " TIME sort channel "<<i<<": " << std::chrono::duration_cast<std::chrono::microseconds>(end_t - step_t).count() << " ms" << std::endl;
// // step_t = end_t;
// topk(rt_out[0]+ i * dim_hm.h * dim_hm.w, ids_d, K, topk_scores + i*K,
// topk_inds_ + i*K, topk_ys_ + i*K, topk_xs_ + i*K);
// // checkCuda( cudaMemcpy(ids2, ids2_d, dim_hm.h * dim_hm.w*sizeof(int), cudaMemcpyDeviceToHost) );
// // for (int j=0; j<dim_hm.h * dim_hm.w; j++) {
// // topk_scores[i*K + count] = hm_h[i * dim_hm.h * dim_hm.w + ids2[j]];
// // topk_inds_[i*K +count] = ids2[j];
// // topk_ys_[i*K +count] = (int)(ids2[j] / width);
// // topk_xs_[i*K +count] = (int)(ids2[j] % width);
// // if(++count == K)
// // break;
// // }
// // end_t = std::chrono::steady_clock::now();
// // std::cout << " TIME topk channel "<<i<<": " << std::chrono::duration_cast<std::chrono::microseconds>(end_t - step_t).count() << " ms" << std::endl;
// // step_t = end_t;
// }
// checkCuda( cudaFree(ids_d ));
std::cout<<" --- a 100 ---\n";
end_t = std::chrono::steady_clock::now();
std::cout << " TIME sort topk on 80 channel: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// final
// sort(topk_scores,
// topk_scores + dim_hm.c * K,
// topk_inds_);
sort(rt_out[0],
rt_out[0]+dim_hm.tot(),
ids_d);
checkCuda( cudaDeviceSynchronize() );
int *topk_inds;
checkCuda( cudaMallocHost(&topk_inds, K*sizeof(int)) );
// checkCuda( cudaMemcpy(topk_inds, ids_d, K*sizeof(int), cudaMemcpyDeviceToHost) );
// for(int i=0; i<K; i++)
// std::cout<<topk_inds[i]<<" ";
// std::cout<<"\n\n\n";
end_t = std::chrono::steady_clock::now();
std::cout << " TIME sort channel: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
std::cout << " TIME sort: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// topk(topk_scores, topk_inds_, 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() );
checkCuda( cudaDeviceSynchronize() );
end_t = std::chrono::steady_clock::now();
std::cout << " TIME topk channel: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
std::cout << " TIME topk: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
checkCuda( cudaMemcpy(topk_inds, topk_inds_d, K*sizeof(int), cudaMemcpyDeviceToHost) );
for(int i=0; i<K; i++)
std::cout<<topk_inds[i]<<" ";
std::cout<<std::endl;
checkCuda( cudaMemcpy(scores, scores_d, K *sizeof(float), cudaMemcpyDeviceToHost) );
std::cout<<"\n\nscores:\n";
for(int i=0; i<K;i++)
std::cout<<scores[i]<<" ";
std::cout<<std::endl;
std::cout<<"\n\n\n";
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::milliseconds>(end_t - step_t).count() << " ms" << 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) );
checkCuda( cudaMemcpy(clses, clses_d, K*sizeof(int), cudaMemcpyDeviceToHost) );
std::cout<<"\ntopk_ids: \n";
checkCuda( cudaMemcpy(topk_inds, topk_inds_d, K*sizeof(int), cudaMemcpyDeviceToHost) );
for(int i=0; i<K; i++)
std::cout<<topk_inds[i]<<" ";
std::cout<<std::endl;
std::cout<<"\ntopk_clses: \n";
checkCuda( cudaMemcpy(topk_inds, clses_d, K*sizeof(int), cudaMemcpyDeviceToHost) );
for(int i=0; i<K; i++)
std::cout<<topk_inds[i]<<" ";
std::cout<<std::endl;
std::cout<<"\nxs: \n";
checkCuda( cudaMemcpy(topk_inds, topk_xs_d, K*sizeof(int), cudaMemcpyDeviceToHost) );
for(int i=0; i<K; i++)
std::cout<<topk_inds[i]<<" ";
std::cout<<std::endl;
std::cout<<"\nys: \n";
checkCuda( cudaMemcpy(topk_inds, topk_ys_d, K*sizeof(int), cudaMemcpyDeviceToHost) );
for(int i=0; i<K; i++)
std::cout<<topk_inds[i]<<" ";
std::cout<<std::endl;
// return;
// checkCuda( cudaDeviceSynchronize() );
// checkCuda( cudaFree(topk_scores) );
// checkCuda( cudaFree(topk_inds_) );
// checkCuda( cudaFree(topk_ys_) );
// checkCuda( cudaFree(topk_xs_) );
// checkCuda( cudaFree(scores_d) );
// checkCuda( cudaFree(topk_inds_d) );
end_t = std::chrono::steady_clock::now();
std::cout << " TIME clses topk 1 time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// ----------- topk end
// dnnType *reg_aus;
// checkCuda( cudaMallocHost(&reg_aus, dim_reg.tot()*sizeof(dnnType)) );
// checkCuda( cudaMemcpy(reg_aus, rt_out[3], dim_reg.tot()*sizeof(dnnType), cudaMemcpyDeviceToHost) );
// for(int i = 0; i < K; i++){
// topk_xs[i] = topk_xs[i] + reg_aus[topk_inds[i]];
// topk_ys[i] = topk_ys[i] + reg_aus[topk_inds[i]+dim_reg.h*dim_reg.w];
// }
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]);
// checkCuda( cudaDeviceSynchronize() );
end_t = std::chrono::steady_clock::now();
std::cout << " TIME add offset: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// checkCuda( cudaFreeHost(reg_aus) );
// dnnType *wh_aus;
// checkCuda( cudaMemcpy(wh_aus, rt_out[2], dim_wh.tot()*sizeof(dnnType), cudaMemcpyDeviceToHost) );
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);
// checkCuda( cudaDeviceSynchronize() );
checkCuda( cudaMemcpy(bbx0, bbx0_d, K * sizeof(float), cudaMemcpyDeviceToHost) );
checkCuda( cudaMemcpy(bby0, bby0_d, K * sizeof(float), cudaMemcpyDeviceToHost) );
checkCuda( cudaMemcpy(bbx1, bbx1_d, K * sizeof(float), cudaMemcpyDeviceToHost) );
checkCuda( cudaMemcpy(bby1, bby1_d, K * sizeof(float), cudaMemcpyDeviceToHost) );
// for(int i = 0; i < K; i++){
// bboxes[i * 4] = topk_xs[i] - wh_aus[topk_inds[i]] / 2;
// bboxes[i * 4 + 1] = topk_ys[i] - wh_aus[topk_inds[i]+dim_reg.h*dim_reg.w] / 2;
// bboxes[i * 4 + 2] = topk_xs[i] + wh_aus[topk_inds[i]] / 2;
// bboxes[i * 4 + 3] = topk_ys[i] + wh_aus[topk_inds[i]+dim_reg.h*dim_reg.w] / 2;
// }
// for(int i = 0; i < K; i++){
// std::cout<<"-----\n(x0, y0) = ("<<bbx0<<", "<<bby0<<")\n(x1,y1) = ("<<bbx1<<", "<<bby1<<")\n";
// }
// checkCuda( cudaFreeHost(wh_aus) );
// checkCuda( cudaFreeHost(topk_inds) );
// checkCuda( cudaFreeHost(topk_ys) );
// checkCuda( cudaFreeHost(topk_xs) );
std::cout<<" --- bboxes ---\n";
end_t = std::chrono::steady_clock::now();
std::cout << " TIME : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
std::cout << " TIME bboxes: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// servono [bboxes, scores, clses]
// checkCuda( cudaDeviceSynchronize() );
std::cout<<" --- process ---\n";
end_t = std::chrono::steady_clock::now();
std::cout << " TIME : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
// ---------------------------------- post-process -----------------------------------------
// --------- ctdet_post_process
@@ -548,12 +384,13 @@ void CenternetDetection::update(cv::Mat &imageORIG) {
cv::Mat trans2(cv::Size(3,2), CV_32F);
trans2 = cv::getAffineTransform( dst, src );
end_t = std::chrono::steady_clock::now();
std::cout << " TIME getAffineTrans 2: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
cv::Mat new_pt1(cv::Size(1,2), CV_32F);
cv::Mat new_pt2(cv::Size(1,2), CV_32F);
cv::Mat new_pt2(cv::Size(1,2), CV_32F);
for(int i = 0; i<K; i++){
new_pt1.at<float>(0,0)=static_cast<float>(trans2.at<double>(0,0))*bbx0[i] +
@@ -570,23 +407,18 @@ void CenternetDetection::update(cv::Mat &imageORIG) {
static_cast<float>(trans2.at<double>(1,1))*bby1[i] +
static_cast<float>(trans2.at<double>(1,2))*1.0;
// std::cout<<"\n new: "<<new_pt1<<" - "<<new_pt2<<std::endl;
target_coords[i*4] = new_pt1.at<float>(0,0);
target_coords[i*4+1] = new_pt1.at<float>(0,1);
target_coords[i*4+2] = new_pt2.at<float>(0,0);
target_coords[i*4+3] = new_pt2.at<float>(0,1);
// std::cout<<new_pt1.at<float>(0,0)<<", "<<new_pt1.at<float>(0,1)<<", "<<new_pt2.at<float>(0,0)<<", "<<new_pt2.at<float>(0,1)<<std::endl;
// std::cout<<"target:cords "<<target_coords[i*4]<<" - "<<target_coords[i*4+1]<<std::endl;
}
// int *classes;
detected.clear();
for(int i = 0; i<classes; i++){
for(int j=0; j<K; j++)
if(clses[j] == i){
if(scores[j] > thresh){
std::cout<<"th: "<<scores[j]<<" - cl: "<<clses[j]<<" i: "<<i<<std::endl;
// std::cout<<"th: "<<scores[j]<<" - cl: "<<clses[j]<<" i: "<<i<<std::endl;
//add coco bbox
//det[0:4], i, det[4]
int x0 = target_coords[j*4];
@@ -607,10 +439,11 @@ void CenternetDetection::update(cv::Mat &imageORIG) {
}
}
}
std::cout<<" --- post_process ---\n";
end_t = std::chrono::steady_clock::now();
std::cout << " TIME : " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
std::cout << " TIME detections: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - step_t).count() << " ms" << std::endl;
step_t = end_t;
std::cout<<"TOTAL: \n";
TIMER_STOP
}
+1 -2
View File
@@ -290,8 +290,7 @@ ILayer* NetworkRT::convert_layer(ITensor *input, Pooling *l) {
if(l->pool_mode == tkdnnPoolingMode_t::POOLING_AVERAGE_EXCLUDE_PADDING) ptype = PoolingType::kMAX_AVERAGE_BLEND;
// if(l->input_dim.h % 2 == 1 && l->input_dim.w % 2 == 1)
if(l->input_dim.h == l->output_dim.h && l->input_dim.w == l->output_dim.w)
if(l->paddingH == 0 && l->paddingW == 0 && l->input_dim.h == l->output_dim.h && l->input_dim.w == l->output_dim.w)
{
IPlugin *plugin = new ResizeLayerRT( l->output_dim.c,l->output_dim.h+1,l->output_dim.w+1 );
IPluginLayer *lRT = networkRT->addPlugin(&input, 1, *plugin);
+2 -2
View File
@@ -55,8 +55,8 @@ Pooling::Pooling( Network *net, int winH, int winW, int strideH, int strideW,
int padW = paddingW == 0? winW -1 : paddingW;
if(final){
h = (h + padH - winH)/strideH +1 +1;
w = (w + padW - winW)/strideW +1 +1;
h = (h + 2*paddingH - winH)/strideH +1 ;
w = (w + 2*paddingW - winW)/strideW +1;
}
else{
h = (h + padH - winH)/strideH +1;
+4 -12
View File
@@ -1,21 +1,13 @@
#include "kernels.h"
__device__
__forceinline__
double sigmoid (double a)
{
return 1.0 / (1.0 + exp (-a));
}
#include <math.h>
__global__
void activation_sigmoid(dnnType *input, dnnType *output, int size) {
int stride = gridDim.x * blockDim.x;
int tid = blockDim.x * blockIdx.x + threadIdx.x;
for (int i = tid; i < size; i += stride) {
output[i] = sigmoid (input[i]);
}
int i = blockDim.x * blockIdx.x + threadIdx.x;
if(i < size)
output[i] = 1.0f / (1.0f + exp (-input[i]));
}
+1 -1
View File
@@ -45,7 +45,7 @@ struct threshold : public thrust::binary_function<float,float,float>
{
__host__ __device__
float operator()(float x, float y) {
float toll = 1e-6;
double toll = 1e-6;
if(fabsf(x-y)>toll)
return 0.0f;
else
+1 -680
View File
@@ -183,53 +183,6 @@ const char *output_bin[]={
"../tests/resnet101_cnet/debug/wh.bin",
"../tests/resnet101_cnet/debug/reg.bin"};
std::vector<size_t> sort_indexes(const std::vector<float> &v) {
// initialize original index locations
std::vector<size_t> idx(v.size());
iota(idx.begin(), idx.end(), 0);
// sort indexes based on comparing values in v
sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return v[i1] > v[i2];});
return idx;
}
float _colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };
float get_color(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;
}
int computeDetections(dnnType *hm_d, dnnType *wh_d, dnnType *reg_d, int hm_dim, int wh_dim, int reg_dim, bool cat_spec_wh, int k){
// _nms
int kernel = 3;
int pad = (kernel - 1)/2;
std::cout<<"computeDetections\n";
// dnnType *hmax;
// tk::dnn::Pooling maxpool(&hmax, 3, 3, 2, 2, 1, 1, tk::dnn::POOLING_MAX)
// = (dnnType *)
// net.functional.max_pool2d(
// heat, (kernel, kernel), stride=1, padding=pad)
// keep = (hmax == heat).float()
// return heat * keep
}
int process(dnnType *hm_d, dnnType *wh_d, dnnType *reg_d, int hm_dim, int wh_dim, int reg_dim){
std::cout<<"process\n";
// computeDetections(hm_d, wh_d, reg_d, hm_dim, wh_dim, reg_dim, false, 100);
}
int main()
{
@@ -450,637 +403,5 @@ int main()
checkResult(odim, rt_out, out);
std::cout << "CUDNN vs TRT ";
checkResult(odim, cudnn_out, rt_out);
}
TIMER_START
// -------- transofrm compose
cv::Mat imageOrig = cv::imread("/media/davide/DATA/shared_home/Projects/Professionale/repos/photo_2020-01-14_09-56-07.jpg");
cv::Mat imageF;
imageOrig.convertTo(imageF, CV_32FC3, 1/255.0);
cv::Mat image;
cv::Size sz = imageF.size();
std::cout<<"image: "<<sz.width<<", "<<sz.height<<std::endl;
resize(imageF, image, cv::Size(256, 256));
const int cropSize = 224;
const int offsetW = (image.cols - cropSize) / 2;
const int offsetH = (image.rows - cropSize) / 2;
const cv::Rect roi(offsetW, offsetH, cropSize, cropSize);
image = image(roi).clone();
std::cout << "Cropped image dimension: " << image.cols << " X " << image.rows << std::endl;
cv::Scalar mean_;
mean_ << 0.485, 0.456, 0.406;
cv::Scalar stddev_;
stddev_ << 0.229, 0.224, 0.225;
cv::Size s_im = imageF.size();
std::cout<<"size: "<<s_im.height<<" "<<s_im.width<<" - "<<std::endl;
std::cout<<"mean: "<<mean_<<", std: "<<stddev_<<std::endl;
cv::add(image, -mean_, image);
cv::divide(image, stddev_, image);
cv::Mat bgr[3];
dnnType *input, *input_d;
dim2 = dim;
checkCuda(cudaMalloc(&input_d, sizeof(dnnType)*dim2.tot()));
checkCuda(cudaMallocHost(&input, sizeof(dnnType)*dim2.tot()));
image.convertTo(image, CV_32FC3, 1/255.0);
//split channels
cv::split(image,bgr);//split source
//write channels
for(int i=0; i<dim2.c; i++) {
int idx = i*image.rows*image.cols;
int ch = dim2.c-1 -i;
memcpy((void*)&input[idx], (void*)bgr[ch].data, image.rows*image.cols*sizeof(dnnType));
}
checkCuda(cudaMemcpyAsync(input_d, input, dim2.tot()*sizeof(dnnType), cudaMemcpyHostToDevice));
printCenteredTitle(" TENSORRT inference ", '=', 30); {
dim2.print();
TIMER_START
netRT.infer(dim2, input_d);
TIMER_STOP
dim2.print();
}
checkResult(dim2.tot(), input_h, input);
checkCuda(cudaFree(input_d));
checkCuda(cudaFreeHost(input));
// -----------------------------------pre-process ------------------------------------------
// it will resize the images to `512 x 512` in GETTING_STARTED.md
float scale = 1.0;
float new_height = sz.height * scale;
float new_width = sz.width * scale;
float inp_height = 224;//512;
float inp_width = 224;//512;
float c[] = {new_width / 2.0, new_height /2.0};
float s[2];
if(sz.width > sz.height){
s[0] = sz.width * 1.0;
s[1] = sz.width * 1.0;
}
else{
s[0] = sz.height * 1.0;
s[1] = sz.height * 1.0;
}
std::cout<<" "<<new_height<<" "<<new_width<<" "<<s[0]<<"-"<<s[1]<<" "<<c[0]<<"-"<<c[1]<<std::endl;
// ----------- get_affine_transform
// rot_rad = pi * 0 / 100 --> 0
cv::Mat src(cv::Size(2,3), CV_32F);
cv::Mat dst(cv::Size(2,3), CV_32F);
src.at<float>(0,0)=c[0];
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;
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) );
dst.at<float>(2,0)=dst.at<float>(1,0) + (-dst.at<float>(0,1)+dst.at<float>(1,1) );
dst.at<float>(2,1)=dst.at<float>(1,1) + (dst.at<float>(0,0)-dst.at<float>(1,0) );
std::cout<<"src: "<<src<<std::endl;
std::cout<<"dst: "<<dst<<std::endl;
cv::Mat trans = cv::getAffineTransform( src, dst );
resize(imageOrig, image, cv::Size(new_width, new_height));
s_im = image.size();
std::cout<<"size: "<<s_im.height<<" "<<s_im.width<<" - "<<std::endl;
// image.convertTo(image, CV_32FC3, 1/255.0);
cv::warpAffine(image, image, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR );
// cv::Scalar mean, stddev;
s_im = image.size();
std::cout<<"size: "<<s_im.height<<" "<<s_im.width<<" - "<<std::endl;
// for(int i=0; i<dim.tot(); i++ ){
// std::cout<<(float)image.at<cv::Vec3b>(0,i)[0]<<" - "<<(float)image.at<cv::Vec3b>(0,i)[1]<<" - "<<(float)image.at<cv::Vec3b>(0,i)[2]<<" - "<<std::endl;
// if(i==10)
// break;
// }
// return 0;
/////////////////////////////// ok fin qui
// cv::meanStdDev(image, mean, stddev );
// cv::Scalar mean(0.408, 0.447, 0.47);
cv::Vec<float, 3> mean;
mean << 0.408, 0.447, 0.47;
// s_im = mean.size();
// std::cout<<"size: "<<s_im.height<<" "<<s_im.width<<" - "<<std::endl;
cv::Vec<float, 3> stddev;
stddev << 0.289, 0.274, 0.278;
cv::Size s_imag = image.size();
std::cout<<"size: "<<s_imag.height<<" "<<s_imag.width<<" - "<<std::endl;
image.convertTo(image, CV_32FC3, 1/255.0);
std::cout<<"mean: "<<mean<<", std: "<<stddev<<std::endl;
// cv::add(image, -mean, image);
// cv::divide(image, stddev, image);
cv::MatIterator_<cv::Vec<float, 3>> it;
for(it = image.begin<cv::Vec<float, 3>>(); it != image.end<cv::Vec<float, 3>>(); ++it)
{
(*it)[0] = (float)(*it)[0] - mean[0];
(*it)[1] = (float)(*it)[1] - mean[1];
(*it)[2] = (float)(*it)[2] - mean[2];
(*it)[0] = (float)(*it)[0] / stddev[0];
(*it)[1] = (float)(*it)[1] / stddev[1];
(*it)[2] = (float)(*it)[2] / stddev[2];
}
// for(int i=0; i<dim.tot(); i++ ){
// std::cout<<(float)image.at<cv::Vec<float, 3>>(0,i)[0]<<" - "<<(float)image.at<cv::Vec<float, 3>>(0,i)[1]<<" - "<<(float)image.at<cv::Vec<float, 3>>(0,i)[2]<<" - "<<std::endl;
// if(i==10)
// break;
// }
// return 0;
/////////////////////// ok fin qui
std::cout<<"size: "<<s_imag.height<<" "<<s_imag.width<<" - "<<std::endl;
cv::Mat bgr2[3];
dnnType *input2, *input_d2;
dim2 = dim;
checkCuda(cudaMalloc(&input_d2, sizeof(dnnType)*dim2.tot()));
checkCuda(cudaMallocHost(&input2, sizeof(dnnType)*dim2.tot()));
// image.convertTo(image, CV_32FC3, 1/255.0);
//split channels
cv::split(image,bgr2);//split source
// std::cout<<"ch:\n";
// for(int i=0; i<dim.tot(); i++ ){
// std::cout<<(float)bgr2[0].at<cv::Vec<float, 3>>(0,i)[0]<<" - "<<(float)bgr2[0].at<cv::Vec<float, 3>>(0,i)[1]<<" - "<<(float)bgr2[0].at<cv::Vec<float, 3>>(0,i)[2]<<std::endl;
// if(i==10)
// break;
// }
// std::cout<<"ch:\n";
// for(int i=0; i<dim.tot(); i++ ){
// std::cout<<(float)bgr2[1].at<float>(1,i)<<std::endl;
// if(i==10)
// break;
// }
// std::cout<<"ch:\n";
// for(int i=0; i<dim.tot(); i++ ){
// std::cout<<(float)bgr2[2].at<float>(2,i)<<std::endl;
// if(i==10)
// break;
// }
// return 0;
///////////////////// ok fin qui
std::cout<<"\n\n\ncome: \n"<<image.rows<<" - "<<image.cols<<std::endl;
std::cout<<"reprint shape dim2\n";
dim2.print();
std::cout<<std::endl;
//write channels
for(int i=0; i<dim2.c; i++) {
int idx = i*image.rows*image.cols;
int ch = dim2.c-3 +i;
std::cout<<"i: "<<i<<", idx: "<<idx<<", ch: "<<ch<<std::endl;
memcpy((void*)&input2[idx], (void*)bgr2[ch].data, image.rows*image.cols*sizeof(dnnType));
}
// int k100 = 0;
// for(int i=1; i<=image.rows*image.cols*dim2.c; i++ ){
// std::cout<<input2[i]<<" ";
// if (i % (image.rows*image.cols) == 0){
// std::cout<<"\n\n";
// k100 ++;
// }
// }
// std::cout<<std::endl;
// std::cout<<"ci sono "<<k100<<" r\n";
// return 0;
///////////////////// pseudo ok
checkCuda(cudaMemcpyAsync(input_d2, input2, dim2.tot()*sizeof(dnnType), cudaMemcpyHostToDevice));
printCenteredTitle(" TENSORRT inference ", '=', 30); {
dim2.print();
TIMER_START
netRT.infer(dim2, input_d2);
TIMER_STOP
dim2.print();
}
checkResult(dim2.tot(), input_h, input2);
for(int i=0; i<dim.tot(); i++ ){
std::cout<<input_h[i]<<" "<<input2[i]<<std::endl;
if(i==10)
break;
}
// for(int i=0; i<dim.tot(); i++ ){
// std::cout<<input2[i]<<" ";
// }
// std::cout<<std::endl;
// return 0;
checkCuda(cudaFree(input_d2));
checkCuda(cudaFreeHost(input2));
// ------------------------------------ process --------------------------------------------
dnnType *hm_h;
checkCuda( cudaMallocHost(&hm_h, hm->output_dim.tot()*sizeof(dnnType)) );
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];
// checkCuda( cudaMemcpy(hm_h, rt_out[0], hm->output_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToHost) );
std::cout<<"hm\n";
hm->output_dim.print();
// for(int i=0; i<hm->output_dim.tot(); i++ ){
// std::cout<<hm_h[i]<<" ";
// if(i==100)
// break;
// }
// std::cout<<"\n";
activationSIGMOIDForward(rt_out[0], rt_out[0], hm->output_dim.tot());
checkCuda( cudaDeviceSynchronize() );
checkCuda( cudaMemcpy(hm_h, rt_out[0], hm->output_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToHost) );
std::cout<<"hm\n";
hm->output_dim.print();
// for(int i=0; i<hm->output_dim.tot(); i++ ){
// std::cout<<hm_h[i]<<" ";
// if(i==100)
// break;
// }
// std::cout<<"\n";
// return 0;
//////////////////////////// ok
// ----------- ctdet_decode
// perform nms on heatmaps
// tk::dnn::Layer *route_hm_layers[1] = { hm };
// tk::dnn::Route *route_hm = new tk::dnn::Route(&net, route_hm_layers, 1);
// ----------- nms
// int kernel = 3;
// int pad = (kernel - 1)/2;
// tk::dnn::Pooling *hmax = new tk::dnn::Pooling(&net, kernel, kernel, 1, 1, pad, pad, tk::dnn::POOLING_MAX);
// hmax_d = hmax->infer(hmax->input_dim.tot(), rt_out[0]);
// keep = (hmax == heat).float()
// return heat * keep
dnnType *hmax_h;
checkCuda( cudaMallocHost(&hmax_h, hmax->output_dim.tot()*sizeof(dnnType)) );
checkCuda( cudaMemcpy(hmax_h, rt_out[1], hmax->output_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToHost) );
std::cout<<"hmax\n";
hmax->output_dim.print();
// for(int i=0; i<hmax->output_dim.tot(); i++ ){
// std::cout<<hmax_h[i]<<" ";
// if(i==100)
// break;
// }
// std::cout<<"\n";
// return 0;
// hm = hm * ( hmax == hm );
std::cout<<"hm:\n";
hm->output_dim.print();
std::cout<<"hmax:\n";
hmax->output_dim.print();
// return 0;
float toll = 0.000001;
for(int i=0; i < hm->output_dim.tot(); i++){
if(hm_h[i]-hmax_h[i] > toll || hm_h[i]-hmax_h[i] < -toll){
hm_h[i] = 0.0f;
}
}
// std::cout<<"\n";
// for(int i=0; i<hm->output_dim.tot(); i++ ){
// std::cout<<hm_h[i]<<" ";
// if(i==100)
// break;
// }
// std::cout<<"\n";
// return 0;
// checkCuda( cudaMemcpy(hm->dstData, hm_h, hm->output_dim.tot()*sizeof(dnnType), cudaMemcpyHostToDevice) );
checkCuda( cudaFreeHost(hmax_h) );
// ----------- nms end
// ----------- topk
int K = 100;
int width = 56; // TODO
float *topk_scores;
int *topk_inds_;
float *topk_ys_;
float *topk_xs_;
std::cout<<"mah: "<<hm->output_dim.c * K<<std::endl;
checkCuda( cudaMallocHost(&topk_scores, hm->output_dim.c * K *sizeof(float)) );
checkCuda( cudaMallocHost(&topk_inds_, hm->output_dim.c * K *sizeof(int)) );
checkCuda( cudaMallocHost(&topk_ys_, hm->output_dim.c * K *sizeof(float)) );
checkCuda( cudaMallocHost(&topk_xs_, hm->output_dim.c * K *sizeof(float)) );
std::cout<<"1\n";
dnnType *hm_aus;
checkCuda( cudaMallocHost(&hm_aus, hm->output_dim.h * hm->output_dim.w *sizeof(dnnType)) );
std::cout<<"2\n";
int count;
std::vector<float> v = {2.0, 3.0, 9.0};
for (auto i: sort_indexes(v)) {
std::cout << i<< "--" <<v[i] << std::endl;
}
for(int i=0; i<hm->output_dim.c; i++){
count = 0;
// get the hm->output_dim.h * hm->output_dim.w elements for each channel and sort it. Then find the first 100 elements
checkCuda( cudaMemcpy(hm_aus, hm_h + i * hm->output_dim.h * hm->output_dim.w,
hm->output_dim.h * hm->output_dim.w * sizeof(dnnType), cudaMemcpyHostToHost) );
// std::cout<<"top scores: "<<hm->output_dim.h * hm->output_dim.w<<"\n";
// for(int k=0; k<hm->output_dim.h * hm->output_dim.w; k++)
// std::cout<<hm_aus[k]<<" ";
// std::cout<<std::endl;
// std::vector<float> my_vector {arr, arr + arr_length}
std::vector<float> my_vector{hm_aus, hm_aus + hm->output_dim.h * hm->output_dim.w};
for (auto j: sort_indexes(my_vector)) {
// std::cout <<"j: "<<j<<" -> "<< hm_aus[j] << std::endl;
topk_scores[i*K + count] = hm_aus[j];
topk_inds_[i*K +count] = j;
topk_ys_[i*K +count] = (int)(j / width);
topk_xs_[i*K +count] = (int)(j % width);
if(++count == K)
break;
}
}
std::cout<<"topk_xs_[0]: "<<topk_xs_[0]<<std::endl;
for(int i = 0; i< hm->output_dim.c * K; i++)
std::cout<<topk_xs_[i]<<" ";
std::cout<<"\n3\n";
// final
float *scores;
int *clses;
int *topk_inds;
float *topk_ys;
float *topk_xs;
checkCuda( cudaMallocHost(&scores, K *sizeof(float)) );
checkCuda( cudaMallocHost(&clses, K *sizeof(int)) );
checkCuda( cudaMallocHost(&topk_inds, K *sizeof(int)) );
checkCuda( cudaMallocHost(&topk_ys, K *sizeof(float)) );
checkCuda( cudaMallocHost(&topk_xs, K *sizeof(float)) );
std::cout<<"4\n";
count = 0;
std::vector<float> my_vector{topk_scores, topk_scores + hm->output_dim.c * K };
for (auto j: sort_indexes(my_vector)) {
// std::cout <<"j: "<<j<<" -> "<< hm_aus[j] << std::endl;
scores[count] = topk_scores[j];
clses[count] = (int)(j / K);
topk_inds[count] = topk_inds_[j];
topk_ys[count] = topk_ys_[j];
topk_xs[count] = topk_xs_[j];
if(++count == K)
break;
}
checkCuda( cudaFreeHost(topk_scores) );
checkCuda( cudaFreeHost(topk_inds_) );
checkCuda( cudaFreeHost(topk_ys_) );
checkCuda( cudaFreeHost(topk_xs_) );
std::cout<<"5\n";
// ----------- topk end
std::cout<<"topk_xs[0]: "<<topk_xs[0]<<std::endl;
for(int i = 0; i< K; i++)
std::cout<<topk_xs[i]<<" ";
std::cout<<std::endl;
/////////////////////////////// fin qui ok
dnnType *reg_aus;
checkCuda( cudaMallocHost(&reg_aus, reg->output_dim.tot()*sizeof(dnnType)) );
checkCuda( cudaMemcpy(reg_aus, rt_out[3], reg->output_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToHost) );
std::cout<<"reg:\n";
reg->output_dim.print();
// for(int i=0; i<reg->output_dim.tot(); i++ ){
// std::cout<<reg_aus[i]<<" ";
// if(i==100)
// break;
// }
// std::cout<<"\n";
// return 0;
/////////////// ok
// for(int i=0; i<K; i++ ){
// std::cout<<reg_aus[topk_inds[i]]<<" "<<reg_aus[topk_inds[i]+56*56]<<std::endl;
// }
// std::cout<<"\n";
// return 0;
///////////////////// ok fin qui
for(int i = 0; i < K; i++){
topk_xs[i] = topk_xs[i] + reg_aus[topk_inds[i]];
topk_ys[i] = topk_ys[i] + reg_aus[topk_inds[i]+reg->output_dim.h*reg->output_dim.w];
}
std::cout<<"topk_xs[0]: "<<topk_xs[0]<<std::endl;
checkCuda( cudaFreeHost(reg_aus) );
std::cout<<"6\n";
dnnType *wh_aus;
float *bboxes;
checkCuda( cudaMallocHost(&wh_aus, wh->output_dim.tot()*sizeof(dnnType)) );
checkCuda( cudaMallocHost(&bboxes, 4 * K *sizeof(dnnType)) );
checkCuda( cudaMemcpy(wh_aus, rt_out[2], wh->output_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToHost) );
std::cout<<"7\n";
for(int i = 0; i< K; i++)
std::cout<<topk_xs[i]<<" ";
std::cout<<std::endl;
std::cout<<topk_xs[0]<<std::endl;
std::cout<<topk_inds[0]<<std::endl;
std::cout<<wh_aus[topk_inds[0]*2]<<std::endl;
for(int i = 0; i < K; i++){
bboxes[i * 4] = topk_xs[i] - wh_aus[topk_inds[i]] / 2;
bboxes[i * 4 + 1] = topk_ys[i] - wh_aus[topk_inds[i]+reg->output_dim.h*reg->output_dim.w] / 2;
bboxes[i * 4 + 2] = topk_xs[i] + wh_aus[topk_inds[i]] / 2;
bboxes[i * 4 + 3] = topk_ys[i] + wh_aus[topk_inds[i]+reg->output_dim.h*reg->output_dim.w] / 2;
}
////////////////// fin qui ok
checkCuda( cudaFreeHost(wh_aus) );
checkCuda( cudaFreeHost(topk_inds) );
checkCuda( cudaFreeHost(topk_ys) );
checkCuda( cudaFreeHost(topk_xs) );
std::cout<<"8\n";
float *detections;
std::cout<<"bboxes:\n";
for(int i = 0; i < K+1; i++){
std::cout<<bboxes[i]<<" ";
}
std::cout<<std::endl;
checkCuda( cudaMallocHost(&detections, 6 * K *sizeof(dnnType)) );
checkCuda( cudaMemcpy(detections, bboxes, 4 * K *sizeof(dnnType), cudaMemcpyHostToHost) );
checkCuda( cudaMemcpy(detections + 4 * K *sizeof(dnnType), scores, K *sizeof(dnnType), cudaMemcpyHostToHost) );
checkCuda( cudaMemcpy(detections + 5 * K *sizeof(dnnType), clses, K *sizeof(dnnType), cudaMemcpyHostToHost) );
checkCuda( cudaFreeHost(bboxes) );
// checkCuda( cudaFreeHost(scores) );
// checkCuda( cudaFreeHost(clses) );
// servono [bboxes, scores, clses]
checkCuda( cudaDeviceSynchronize() );
// ---------------------------------- post-process -----------------------------------------
// --------- ctdet_post_process
//for 1
// float *dets;
// checkCuda( cudaMallocHost(&dets, 2 * K *sizeof(float)) );
// checkCuda( cudaMemcpy(dets, detections, 2 * K *sizeof(float), cudaMemcpyHostToHost) );
// --------- transform_preds
float *target_coords;
checkCuda( cudaMallocHost(&target_coords, 4 * K *sizeof(float)) );
src.at<float>(0,0)=c[0];
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)=width * 0.5;
dst.at<float>(0,1)=width * 0.5;
dst.at<float>(1,0)=width * 0.5;
dst.at<float>(1,1)=width * 0.5 + width * -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) );
dst.at<float>(2,0)=dst.at<float>(1,0) + (-dst.at<float>(0,1)+dst.at<float>(1,1) );
dst.at<float>(2,1)=dst.at<float>(1,1) + (dst.at<float>(0,0)-dst.at<float>(1,0) );
std::cout<<"src: "<<src<<std::endl;
std::cout<<"dst: "<<dst<<std::endl;
cv::Size s_aus;
cv::Mat trans2(cv::Size(3,2), CV_32F);
trans2 = cv::getAffineTransform( dst, src );
s_aus = trans2.size();
std::cout<<"trnas2: "<<trans2<<std::endl;
std::cout<<trans2.at<double>(0,0)<<" - "<<trans2.at<double>(0,1)<<" - "<<trans2.at<double>(0,2)<<"\n"<<trans2.at<double>(1,0)<<" - "<<trans2.at<double>(1,1)<<" - "<<trans2.at<double>(1,2)<<std::endl;
std::cout<<"size: "<<s_aus.height<<" "<<s_aus.width<<" - "<<std::endl;
cv::Mat new_pt1(cv::Size(1,2), CV_32F);
cv::Mat new_pt2(cv::Size(1,2), CV_32F);
for(int i = 0; i<K; i++){
// new_pt1.at<float>(0,0)=detections[i*4];
// new_pt1.at<float>(0,1)=detections[i*4+1];
// new_pt1.at<float>(0,2)=1.0;
// new_pt1 << detections[i], detections[i+K], 1.0;
// std::cout<<"----\ni: "<<i<<std::endl;//<<" newpt: "<<new_pt1<<std::endl;
// std::cout<<"origi: "<<detections[i*4]<<", "<<detections[i*4+1]<<", "<<1.0<<std::endl;
s_aus = new_pt1.size();
// std::cout<<"size: "<<s_aus.height<<" "<<s_aus.width<<" - "<<std::endl;
// new_pt2 = trans2.dot(new_pt1);
new_pt1.at<float>(0,0)=static_cast<float>(trans2.at<double>(0,0))*detections[i*4] +
static_cast<float>(trans2.at<double>(0,1))*detections[i*4+1] +
static_cast<float>(trans2.at<double>(0,2))*1.0;
new_pt1.at<float>(0,1)=static_cast<float>(trans2.at<double>(1,0))*detections[i*4] +
static_cast<float>(trans2.at<double>(1,1))*detections[i*4+1] +
static_cast<float>(trans2.at<double>(1,2))*1.0;
new_pt2.at<float>(0,0)=static_cast<float>(trans2.at<double>(0,0))*detections[i*4+2] +
static_cast<float>(trans2.at<double>(0,1))*detections[i*4+3] +
static_cast<float>(trans2.at<double>(0,2))*1.0;
new_pt2.at<float>(0,1)=static_cast<float>(trans2.at<double>(1,0))*detections[i*4+2] +
static_cast<float>(trans2.at<double>(1,1))*detections[i*4+3] +
static_cast<float>(trans2.at<double>(1,2))*1.0;
// std::cout<<"\n new: "<<new_pt1<<" - "<<new_pt2<<std::endl;
target_coords[i*4] = new_pt1.at<float>(0,0);
target_coords[i*4+1] = new_pt1.at<float>(0,1);
target_coords[i*4+2] = new_pt2.at<float>(0,0);
target_coords[i*4+3] = new_pt2.at<float>(0,1);
// std::cout<<new_pt1.at<float>(0,0)<<", "<<new_pt1.at<float>(0,1)<<", "<<new_pt2.at<float>(0,0)<<", "<<new_pt2.at<float>(0,1)<<std::endl;
}
// return 0;
// /////////////// ok fin qui
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",
"sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
"umbrella", "handbag", "tie", "suitcase", "frisbee", "skis",
"snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
"skateboard", "surfboard", "tennis racket", "bottle", "wine glass",
"cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich",
"orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake",
"chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv",
"laptop", "mouse", "remote", "keyboard", "cell phone", "microwave",
"oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
"scissors", "teddy bear", "hair drier", "toothbrush"};
std::vector<std::string> coco_class_name(coco_class_name_, std::end( coco_class_name_ ));
int num_classes = 80;
float vis_threshold = 0.3;
// int *classes;
std::vector<tk::dnn::box> detected;
// checkCuda( cudaMallocHost(&classes, K *sizeof(int)) );
// checkCuda( cudaMemcpy(classes, detections + 5 * K *sizeof(dnnType), K *sizeof(dnnType), cudaMemcpyHostToHost) );
for(int i = 0; i<num_classes; i++){
for(int j=0; j<K; j++)
if(clses[j] == i){
//TODO recupera detections[j +0 +1 +2 +3 +4(+5 è la classes, già presa)]
// queste compongono un ogg assegnato alla classe i (0, 79) --> i+1 (1:80);
if(scores[j] > vis_threshold){
std::cout<<"th: "<<scores[j]<<" - cl: "<<clses[j]<<" i: "<<i<<std::endl;
//add coco bbox
//det[0:4], i, det[4]
int x0 = target_coords[j*4];
int y0 = target_coords[j*4+1];
int x1 = target_coords[j*4+2];
int y1 = target_coords[j*4+3];
int obj_class = clses[j];
float prob = scores[j];
std::cout<<"("<<x0<<", "<<y0<<"),("<<x1<<", "<<y1<<")"<<std::endl;
tk::dnn::box res;
res.cl = obj_class;
res.prob = prob;
res.x = x0;
res.y = y0;
res.w = x1 - x0;
res.h = y1 - y0;
detected.push_back(res);
}
}
}
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;
cv::Scalar colors[256];
for(int c=0; c<num_classes; c++) {
int offset = c*123457 % num_classes;
float r = get_color(2, offset, num_classes);
float g = get_color(1, offset, num_classes);
float b = get_color(0, offset, num_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);
}
cv::namedWindow("cnet", cv::WINDOW_NORMAL);
cv::imshow("cnet", imageOrig);
cv::waitKey(10000);
TIMER_STOP
return 0;
}
}