#include #include "tkdnn.h" #include /* srand, rand */ #include #include #include const char *reg_bias = "../tests/yolo/layers/g31.bin"; int prob_sort(const void *pa, const void *pb) { tkDNN::box a = *(tkDNN::box *)pa; tkDNN::box b = *(tkDNN::box *)pb; float diff = a.prob - b.prob; if(diff < 0) return 1; else if(diff > 0) return -1; return 0; } cv::Mat GetSquareImage(const cv::Mat& img, int target_width) { int width = img.cols, height = img.rows; cv::Mat square = cv::Mat::zeros( target_width, target_width, img.type() ); int max_dim = ( width >= height ) ? width : height; float scale = ( ( float ) target_width ) / max_dim; cv::Rect roi; if ( width >= height ) { roi.width = target_width; roi.x = 0; roi.height = height * scale; roi.y = ( target_width - roi.height ) / 2; } else { roi.y = 0; roi.height = target_width; roi.width = width * scale; roi.x = ( target_width - roi.width ) / 2; } cv::resize( img, square( roi ), roi.size() ); return square; } void compute_image( cv::Mat imageORIG, tkDNN::NetworkRT *netRT, tkDNN::RegionInterpret *rI, dnnType *input, dnnType *output) { //Resize with padding and convert to float cv::Mat image = GetSquareImage(imageORIG, netRT->input_dim.w); cv::Mat imageF; image.convertTo(imageF, CV_32FC3, 1/255.0); //split channels cv::Mat bgr[3]; //destination array cv::split(imageF,bgr);//split source //write channels int idx = 0; memcpy((void*)&input[idx], (void*)bgr[2].data, imageF.rows*imageF.cols*sizeof(dnnType)); idx = imageF.rows*imageF.cols; memcpy((void*)&input[idx], (void*)bgr[1].data, imageF.rows*imageF.cols*sizeof(dnnType)); idx *= 2; memcpy((void*)&input[idx], (void*)bgr[0].data, imageF.rows*imageF.cols*sizeof(dnnType)); //DO INFERENCE printCenteredTitle(" TENSORRT inference ", '=', 30); TIMER_START checkCuda( cudaMemcpyAsync(netRT->buffersRT[netRT->buf_input_idx], input, netRT->input_dim.tot()*sizeof(float), cudaMemcpyHostToDevice, netRT->stream)); netRT->enqueue(); checkCuda( cudaMemcpyAsync(output, netRT->buffersRT[netRT->buf_output_idx], netRT->output_dim.tot()*sizeof(float), cudaMemcpyDeviceToHost, netRT->stream)); cudaStreamSynchronize(netRT->stream); TIMER_STOP rI->interpretData(output, imageORIG.cols, imageORIG.rows); } int main(int argc, char *argv[]) { if(argc < 3) { std::cout<<"usage: "< validation_list.txt format: \n" <<" path/to/image.jpg path/to/label.txt\n" <<"> label.txt format: \n" <<" \n" <<" x and y are the box center, " <<"all values are relative to the image size\n\n"; return 1; } if(!fileExist(argv[1])) FatalError("unable to read serialRT file"); //convert network to tensorRT tkDNN::NetworkRT netRT(NULL, argv[1]); tkDNN::RegionInterpret rI(netRT.input_dim, netRT.output_dim, 80, 4, 5, 0.3f, reg_bias); dnnType *input = new float[netRT.input_dim.tot()]; dnnType *output = new float[netRT.output_dim.tot()]; std::string line; std::ifstream imageset(argv[2]); if(!imageset.is_open()) FatalError("could not read imageset"); float mAP = 0; int processed_images; for(processed_images=1; getline(imageset, line); processed_images++) { std::string image_path = line.substr(0, line.find(" ")); std::string label_path = line.substr(line.find(" ")+1, line.size()); std::cout<>cl) { labels>>x>>y>>w>>h; w *= img.cols; x *= img.cols; h *= img.rows; y *= img.rows; std::cout<=1; i--) { //for each detected evaluate sub group int prec = 0; for(int j=0; j 0.6f && rI.res_boxes[j].cl == gt[z].cl) { prec++; break; } } } AP += float(prec)/i; } AP = AP/gt_n; std::cout<<"AP: "<