Py-faster-rcnn: Using fast RCNN with C++

Created on 8 Sep 2016  路  6Comments  路  Source: rbgirshick/py-faster-rcnn

I am trying to run the code using C++ following https://github.com/YihangLou/FasterRCNN-Encapsulation-Cplusplus here . I can successfully compile it but after that the results are very bad. I expect it could be the problem of copying data to blob
`

void Detector::Detect(const string& im_name)
{

float CONF_THRESH = 0.8;
float NMS_THRESH = 0.3;
    const int  max_input_side=1000;
    const int  min_input_side=600;
cv::Mat cv_img = cv::imread(im_name);
cv::Mat cv_new(cv_img.rows, cv_img.cols, CV_32FC3, cv::Scalar(0,0,0));  
if(cv_img.empty())
{
    std::cout<<"Can not get the image file !"<<endl;
    return ;
}
int max_side = max(cv_img.rows, cv_img.cols);
int min_side = min(cv_img.rows, cv_img.cols);

float max_side_scale = float(max_side) / float(max_input_side);
float min_side_scale = float(min_side) /float( min_input_side);
float max_scale=max(max_side_scale, min_side_scale);

float img_scale = 1;

if(max_scale > 1)
{
    img_scale = float(1) / max_scale;
}
img_scale=2.5;
int height = int(cv_img.rows * img_scale);
int width = int(cv_img.cols * img_scale);
int num_out;
cv::Mat cv_resized;
std::cout<<"imagename "<<im_name<<endl;
float im_info[3];
float data_buf[height*width*3];
float *boxes = NULL;
float *pred = NULL;
float *pred_per_class = NULL;
float *sorted_pred_cls = NULL;
int *keep = NULL;
const float* bbox_delt;
const float* rois;
const float* pred_cls;
int num;

for (int h = 0; h < cv_img.rows; ++h )
{
    for (int w = 0; w < cv_img.cols; ++w)
    {
        cv_new.at<cv::Vec3f>(cv::Point(w, h))[0] = float(cv_img.at<cv::Vec3b>(cv::Point(w, h))[0])-float(102.9801);
        cv_new.at<cv::Vec3f>(cv::Point(w, h))[1] = float(cv_img.at<cv::Vec3b>(cv::Point(w, h))[1])-float(115.9465);
        cv_new.at<cv::Vec3f>(cv::Point(w, h))[2] = float(cv_img.at<cv::Vec3b>(cv::Point(w, h))[2])-float(122.7717);

    }
}

cv::resize(cv_new, cv_resized, cv::Size(width, height));
cv::imwrite("cv_new.jpg",cv_resized);

im_info[0] = cv_resized.rows;
im_info[1] = cv_resized.cols;
im_info[2] = img_scale;
for (int h = 0; h < height; ++h )
{
    for (int w = 0; w < width; ++w)
    {
        data_buf[(0*height+h)*width+w] = float(cv_resized.at<cv::Vec3f>(cv::Point(w, h))[0]);
        data_buf[(1*height+h)*width+w] = float(cv_resized.at<cv::Vec3f>(cv::Point(w, h))[1]);
        data_buf[(2*height+h)*width+w] = float(cv_resized.at<cv::Vec3f>(cv::Point(w, h))[2]);
    }
}

net_->blob_by_name("data")->Reshape(1, 3, height, width);
Blob<float> * input_blobs= net_->input_blobs()[0];
switch(Caffe::mode()){
case Caffe::CPU:
    memcpy(input_blobs->mutable_cpu_data(), data_buf, sizeof(float) * input_blobs->count());
    break;
case Caffe::GPU:
    caffe_gpu_memcpy(sizeof(float)* input_blobs->count(), data_buf, input_blobs->mutable_gpu_data());
    break;
default:
    LOG(FATAL)<<"Unknow Caffe mode";
}

//net_->blob_by_name("data")->set_cpu_data(data_buf);
net_->blob_by_name("im_info")->set_cpu_data(im_info);
net_->ForwardFrom(0);
bbox_delt = net_->blob_by_name("bbox_pred")->cpu_data();
num = net_->blob_by_name("rois")->num();


rois = net_->blob_by_name("rois")->cpu_data();
pred_cls = net_->blob_by_name("cls_prob")->cpu_data();
boxes = new float[num*4];
pred = new float[num*5*class_num];
pred_per_class = new float[num*5];
sorted_pred_cls = new float[num*5];
keep = new int[num];

for (int n = 0; n < num; n++)
{
    for (int c = 0; c < 4; c++)
    {
        boxes[n*4+c] = rois[n*5+c+1] / img_scale;
    }
}

bbox_transform_inv(num, bbox_delt, pred_cls, boxes, pred, cv_img.rows, cv_img.cols);
for (int i = 1; i < class_num; i ++)
{
    if(i==7)
    {
        for (int j = 0; j< num; j++)
        {
            for (int k=0; k<5; k++)
                pred_per_class[j*5+k] = pred[(i*num+j)*5+k];
        }
        boxes_sort(num, pred_per_class, sorted_pred_cls);
        _nms(keep, &num_out, sorted_pred_cls, num, 5, NMS_THRESH, 0);

    //for visualize only
    vis_detections(cv_img, keep, num_out, sorted_pred_cls, CONF_THRESH);
    }
}

cv::imwrite("vis.jpg",cv_img);

delete []boxes;
delete []pred;
delete []pred_per_class;
delete []keep;
delete []sorted_pred_cls;

}
`

In this code I set im_scale=2.5 to match with the test from python version. Would you please help to show that are there anything wrong with the code?

Most helpful comment

Hi, I reimplement the Faster RCNN in C++, and got a similar results on VOC.
You may try https://github.com/D-X-Y/caffe-faster-rcnn/tree/dev

All 6 comments

why don't you post to the c++'s repository?

@buingocnam87 I have been trying to get this to compile for quite some time. Besides linking headers and libraries what all did you change? I can't get past:

1>faster_rcnn.obj : error LNK2001: unresolved external symbol "void __cdecl _nms(int *,int *,float const *,int,int,float,int)" (?_nms@@YAXPAH0PBMHHMH@Z)

Hi, I reimplement the Faster RCNN in C++, and got a similar results on VOC.
You may try https://github.com/D-X-Y/caffe-faster-rcnn/tree/dev

@rtgoring Is problem there?

@buingocnam87 hi,can the code run on any one of the gpus? I test the code on gpu 1,failed like this:F0528 14:16:35.352493 38786 cudnn_conv_layer.cu:28] Check failed: status == CUDNN_STATUS_SUCCESS (8 vs. 0) CUDNN_STATUS_EXECUTION_FAILED. But the code running on gpu 0 is ok

@D-X-Y thanks. Why the test code of https://github.com/YihangLou/FasterRCNN-Encapsulation-Cplusplus, occur error like this:Check failed: status == CUDNN_STATUS_SUCCESS (8 vs. 0) CUDNN_STATUS_EXECUTION_FAILED? Is it that the pythonlayer can't work on gpu except gpu 0.,So you do rewrite the rpn code with c++

Was this page helpful?
0 / 5 - 0 ratings