yolov3 c++ tensorflow inference

    cv_data.image = cv::imread(image_path, -1);
    cv_data.image_nms = cv_data.image.clone();

    data_config.scale = std::min(1.0*data_config.iw/data_config.w, 1.0*data_config.ih/data_config.h);

    data_config.nw = int(data_config.scale * data_config.w);
    data_config.nh = int(data_config.scale * data_config.h);

    cv::Mat image_resized;
    cv::resize(cv_data.image, image_resized, cv::Size(data_config.nw, data_config.nh));

    cv::Mat image_paded(data_config.ih, data_config.iw, CV_32FC3, cv::Scalar(128, 128, 128));
    data_config.dw = (int)(data_config.iw - data_config.nw)/2.0;
    data_config.dh = (int)(data_config.ih - data_config.nh)/2.0;
    
    // std::cout << dw << " " << dh << " " << dw+nw << " " << dh+nh << std::endl;
    cv::Rect roi_rect = cv::Rect(data_config.dw, data_config.dh, data_config.nw, data_config.nh);
    image_resized.copyTo(image_paded(roi_rect));
    cv_data.image_padded = image_paded / 255.0;

    // auto input_tensor = mat_to_tensor(data_config, cv_data);

    tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, \
        tensorflow::TensorShape({1, data_config.ih, data_config.iw, data_config.depth}));
    auto input_tensor_mapped = input_tensor.tensor<float, 4>();

    const float * source_data = (float*) cv_data.image_padded.data;
    
    // copying the data into the corresponding tensor
    for (int y = 0; y < data_config.ih; ++y) {
        const float* source_row = source_data + (y * data_config.iw * data_config.depth);
        for (int x = 0; x < data_config.iw; ++x) {
            const float* source_pixel = source_row + (x * data_config.depth);
            for (int c = 0; c < data_config.depth; ++c) {
                const float* source_value = source_pixel + c;
                input_tensor_mapped(0, y, x, c) = *source_value;
            }
        }
    }

    std::vector<std::string> out_put_nodes; 
    std::vector<tensorflow::Tensor> finalOutput;

    out_put_nodes.push_back(tensor_nodes.s_output_name);
    out_put_nodes.push_back(tensor_nodes.m_output_name);
    out_put_nodes.push_back(tensor_nodes.l_output_name);

    tensorflow::Status status_run = _sess->Run({{tensor_nodes.input_tensor_name, input_tensor}}, \
        {out_put_nodes}, {}, &finalOutput);

    if (!status_run.ok()) {
        std::cout << "ERROR: run session failed..." << status_run.ToString() << std::endl;
        // return false;
    }

    // std::cout << "final output size=" << finalOutput[0] << std::endl;
    // std::cout << "final output size=" << finalOutput[1] << std::endl;
    // std::cout << "final output size=" << finalOutput[2] << std::endl;

    tensorflow::Scope con_name = tensorflow::Scope::NewRootScope();
    int num_classes = 12;

    tensorflow::Tensor image_features_reshaped1(tensorflow::DT_FLOAT, tensorflow::TensorShape({76 * 76 * 3, 5 + num_classes}));
    tensorflow::Tensor image_features_reshaped2(tensorflow::DT_FLOAT, tensorflow::TensorShape({38 * 38 * 3, 5 + num_classes}));
    tensorflow::Tensor image_features_reshaped3(tensorflow::DT_FLOAT, tensorflow::TensorShape({19 * 19 * 3, 5 + num_classes}));
    image_features_reshaped1.CopyFrom(finalOutput[0], tensorflow::TensorShape({76 * 76 * 3, 5 + num_classes}));
    image_features_reshaped2.CopyFrom(finalOutput[1], tensorflow::TensorShape({38 * 38 * 3, 5 + num_classes}));
    image_features_reshaped3.CopyFrom(finalOutput[2], tensorflow::TensorShape({19 * 19 * 3, 5 + num_classes}));

    auto outputs_con = tensorflow::ops::Concat(con_name.WithOpName("concat_outs"),\
    {image_features_reshaped1, image_features_reshaped2, image_features_reshaped3}, 0);

    std::vector<tensorflow::Tensor> outputs;
    tensorflow::ClientSession session2(con_name);
    TF_CHECK_OK(session2.Run({outputs_con}, &outputs));

    tensorflow::Tensor result = outputs[0];
    tensorflow::TensorShape inputTensorShape = outputs[0].shape();

    float *p_tensor = result.flat<float>().data();
    cv::Mat mat_out = cv::Mat(inputTensorShape.dim_size(0), inputTensorShape.dim_size(1), CV_32FC1, p_tensor);

    if (mat_out.empty()){
        std::cout << "can't open the image!" << std::endl;
    }

    std::vector<Bbox> box_info;

    for (int32_t idx = 0; idx < inputTensorShape.dim_size(0); ++idx){

        cv::Mat per_box_info = mat_out.row(idx);
        cv::Mat scores = per_box_info.colRange(5, per_box_info.cols).clone();
        cv::Point classId;
        double confidence;

        // Get the value and location of the maximum score
        minMaxLoc(scores, 0, &confidence, 0, &classId);
        confidence = confidence * mat_out.at<float>(idx, 4);

        if (confidence > 0.3){

            float center_x = (int)(mat_out.at<float>(idx, 0));
            float center_y = (int)(mat_out.at<float>(idx, 1));
            float box_w = (int)(mat_out.at<float>(idx, 2));
            float box_h = (int)(mat_out.at<float>(idx, 3));

            int tl_x = (int)(center_x - 0.5 * box_w);
            int tl_y = (int)(center_y - 0.5 * box_h);
            int br_x = (int)(center_x + 0.5 * box_w);
            int br_y = (int)(center_y + 0.5 * box_h);

            int box_tl_x = 1.0 * (tl_x - data_config.dw) / data_config.scale;
            int box_tl_y = 1.0 * (tl_y - data_config.dh) / data_config.scale;

            int box_br_x = 1.0 * (br_x - data_config.dw) / data_config.scale;
            int box_br_y = 1.0 * (br_y - data_config.dh) / data_config.scale;

            cv::Point tl, br;
            tl = cv::Point(box_tl_x, box_tl_y);
            br = cv::Point(box_br_x, box_br_y);

            cv::rectangle(cv_data.image, tl, br, mapcolor[classId.x], 2);
            double time_stamp = image_name_to_timestamp(image_path);
            Bbox box_tmp = {box_tl_x, box_tl_y, box_br_x, box_br_y, (float)confidence, classId.x, time_stamp, -1};
            box_info.push_back(box_tmp);
        }
    }

    std::sort(box_info.begin(), box_info.end(), sort_score);
    std::vector<Bbox> det_res;
    while (box_info.size() > 0){

        if (box_info[0].score > 0.5){
            det_res.push_back(box_info[0]);
        }
        int index = 1;
        while(index < box_info.size()){

            float iou = iou_box(box_info[index], box_info[0]);
            // std::cout << "iou: " << iou << std::endl;

            if (iou > 0.5 && box_info[index].classid == box_info[0].classid){
                box_info.erase(box_info.begin() + index);
            }
            else
            {
            index++;
            }
        }
        box_info.erase(box_info.begin());
    }
    cv_data.image = cv::imread(image_path, -1);
    cv_data.image_nms = cv_data.image.clone();

    data_config.scale = std::min(1.0*data_config.iw/data_config.w, 1.0*data_config.ih/data_config.h);

    data_config.nw = int(data_config.scale * data_config.w);
    data_config.nh = int(data_config.scale * data_config.h);

    cv::Mat image_resized;
    cv::resize(cv_data.image, image_resized, cv::Size(data_config.nw, data_config.nh));

    cv::Mat image_paded(data_config.ih, data_config.iw, CV_32FC3, cv::Scalar(128, 128, 128));
    data_config.dw = (int)(data_config.iw - data_config.nw)/2.0;
    data_config.dh = (int)(data_config.ih - data_config.nh)/2.0;
    
    // std::cout << dw << " " << dh << " " << dw+nw << " " << dh+nh << std::endl;
    cv::Rect roi_rect = cv::Rect(data_config.dw, data_config.dh, data_config.nw, data_config.nh);
    image_resized.copyTo(image_paded(roi_rect));
    cv_data.image_padded = image_paded / 255.0;

    // auto input_tensor = mat_to_tensor(data_config, cv_data);

    tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, \
        tensorflow::TensorShape({1, data_config.ih, data_config.iw, data_config.depth}));
    auto input_tensor_mapped = input_tensor.tensor<float, 4>();

    const float * source_data = (float*) cv_data.image_padded.data;
    
    // copying the data into the corresponding tensor
    for (int y = 0; y < data_config.ih; ++y) {
        const float* source_row = source_data + (y * data_config.iw * data_config.depth);
        for (int x = 0; x < data_config.iw; ++x) {
            const float* source_pixel = source_row + (x * data_config.depth);
            for (int c = 0; c < data_config.depth; ++c) {
                const float* source_value = source_pixel + c;
                input_tensor_mapped(0, y, x, c) = *source_value;
            }
        }
    }

    std::vector<std::string> out_put_nodes; 
    std::vector<tensorflow::Tensor> finalOutput;

    out_put_nodes.push_back(tensor_nodes.s_output_name);
    out_put_nodes.push_back(tensor_nodes.m_output_name);
    out_put_nodes.push_back(tensor_nodes.l_output_name);

    tensorflow::Status status_run = _sess->Run({{tensor_nodes.input_tensor_name, input_tensor}}, \
        {out_put_nodes}, {}, &finalOutput);

    if (!status_run.ok()) {
        std::cout << "ERROR: run session failed..." << status_run.ToString() << std::endl;
        // return false;
    }

    tensorflow::Tensor result1 = finalOutput[0];
    tensorflow::TensorShape inputTensorShape1 = finalOutput[0].shape();
    float *p_tensor1 = result1.flat<float>().data();
    cv::Mat mat_out1 = cv::Mat(inputTensorShape1.dim_size(0) * inputTensorShape1.dim_size(1) * \
        inputTensorShape1.dim_size(2) * inputTensorShape1.dim_size(3), inputTensorShape1.dim_size(4), CV_32FC1, p_tensor1);

    tensorflow::Tensor result2 = finalOutput[1];
    tensorflow::TensorShape inputTensorShape2 = finalOutput[1].shape();
    float *p_tensor2 = result2.flat<float>().data();
    cv::Mat mat_out2 = cv::Mat(inputTensorShape2.dim_size(0) * inputTensorShape2.dim_size(1) * \
        inputTensorShape2.dim_size(2) * inputTensorShape2.dim_size(3), inputTensorShape2.dim_size(4), CV_32FC1, p_tensor2);

    tensorflow::Tensor result3 = finalOutput[2];
    tensorflow::TensorShape inputTensorShape3 = finalOutput[2].shape();
    float *p_tensor3 = result3.flat<float>().data();
    cv::Mat mat_out3 = cv::Mat(inputTensorShape3.dim_size(0) * inputTensorShape3.dim_size(1) * \
        inputTensorShape3.dim_size(2) * inputTensorShape3.dim_size(3), inputTensorShape3.dim_size(4), CV_32FC1, p_tensor3);

    cv::Mat mat_out;
    cv::vconcat(mat_out1, mat_out2, mat_out);
    cv::vconcat(mat_out3, mat_out, mat_out);

    if (mat_out.empty()){
        std::cout << "can't open the image!" << std::endl;
    }

    std::vector<Bbox> box_info;

    for (int32_t idx = 0; idx < mat_out.rows; ++idx){

        cv::Mat per_box_info = mat_out.row(idx);
        cv::Mat scores = per_box_info.colRange(5, per_box_info.cols).clone();
        cv::Point classId;
        double confidence;

        // Get the value and location of the maximum score
        minMaxLoc(scores, 0, &confidence, 0, &classId);
        confidence = confidence * mat_out.at<float>(idx, 4);

        if (confidence > 0.3){

            float center_x = (int)(mat_out.at<float>(idx, 0));
            float center_y = (int)(mat_out.at<float>(idx, 1));
            float box_w = (int)(mat_out.at<float>(idx, 2));
            float box_h = (int)(mat_out.at<float>(idx, 3));

            int tl_x = (int)(center_x - 0.5 * box_w);
            int tl_y = (int)(center_y - 0.5 * box_h);
            int br_x = (int)(center_x + 0.5 * box_w);
            int br_y = (int)(center_y + 0.5 * box_h);

            int box_tl_x = 1.0 * (tl_x - data_config.dw) / data_config.scale;
            int box_tl_y = 1.0 * (tl_y - data_config.dh) / data_config.scale;

            int box_br_x = 1.0 * (br_x - data_config.dw) / data_config.scale;
            int box_br_y = 1.0 * (br_y - data_config.dh) / data_config.scale;

            cv::Point tl, br;
            tl = cv::Point(box_tl_x, box_tl_y);
            br = cv::Point(box_br_x, box_br_y);

            cv::rectangle(cv_data.image, tl, br, mapcolor[classId.x], 2);
            double time_stamp = image_name_to_timestamp(image_path);
            Bbox box_tmp = {box_tl_x, box_tl_y, box_br_x, box_br_y, (float)confidence, classId.x, time_stamp, -1};
            box_info.push_back(box_tmp);
        }
    }

    std::sort(box_info.begin(), box_info.end(), sort_score);
    std::vector<Bbox> det_res;
    while (box_info.size() > 0){

        if (box_info[0].score > 0.5){
            det_res.push_back(box_info[0]);
        }
        int index = 1;
        while(index < box_info.size()){

            float iou = iou_box(box_info[index], box_info[0]);
            // std::cout << "iou: " << iou << std::endl;

            if (iou > 0.5 && box_info[index].classid == box_info[0].classid){
                box_info.erase(box_info.begin() + index);
            }
            else
            {
            index++;
            }
        }
        box_info.erase(box_info.begin());
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值