使用TensorFlow C ++ API的MTCNN推理失败

时间:2018-12-24 03:26:06

标签: c++ tensorflow

我使用tensorflow c ++ api推断一张图像包含两个人使用的代码

#define COMPILER_MSVC
#define NOMINMAX
#define PLATFORM_WINDOWS

#include<iostream>
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
#include <opencv2/opencv.hpp>
#include <thread>
#include <Windows.h>
#include<tensorflow/c/c_api.h>

using namespace std;
using namespace tensorflow;
using namespace cv;

void CVMat_to_Tensor(Mat img, Tensor* output_tensor, int input_rows, int input_cols){
    resize(img, img, cv::Size(input_cols, input_rows));
    img.convertTo(img, CV_32FC1);
    float *p = output_tensor->flat<float>().data();
    cv::Mat tempMat(input_rows, input_cols, CV_32FC1, p);
    img.convertTo(tempMat, CV_32FC1);
}


void thread_pridect(string image_path) {
    string model_path = "D:/py_projects/tf-mtcnn/mtcnn.pb";
    string input_tensor_name = "input";
    string min_size = "min_size";
    string thresholds = "thresholds";
    string factor = "factor";
    string output_prob_name = "prob";
    string output_box_name = "box";
    Session* session;
    Status status = NewSession(SessionOptions(), &session);
    GraphDef graphdef;
    Status status_load = ReadBinaryProto(Env::Default(), model_path, &graphdef);
    session->Create(graphdef);
    Mat frame = imread("D:/images/people.jpg");
    int input_height = frame.rows;
    int input_width = frame.cols;
    Tensor resized_tensor(DT_FLOAT, TensorShape({ 1,input_height,input_width,3 }));
    CVMat_to_Tensor(frame, &resized_tensor, input_height, input_width);
    std::vector< std::pair< string, Tensor > > inputs;
    inputs.push_back({ input_tensor_name ,resized_tensor });
    Tensor min_size_tensor(DT_FLOAT, TensorShape());
    min_size_tensor.scalar<float>()() = 40.0f;
    inputs.push_back({ min_size ,min_size_tensor});
    Tensor thresholds_tensor(DT_FLOAT, TensorShape({ 1,3 }));
    std::initializer_list<float> v_a = { 0.6, 0.7, 0.7 };
    thresholds_tensor.matrix<float>().setValues({ v_a });
    inputs.push_back({ thresholds ,thresholds_tensor });
    Tensor factor_tensor(DT_FLOAT, TensorShape());
    factor_tensor.scalar<float>()() = 0.709f;
    inputs.push_back({ factor ,factor_tensor });
    vector<tensorflow::Tensor> outputs;
    Status run_status = session->Run(inputs, {output_box_name,output_prob_name }, {}, &outputs);
    Tensor t = outputs[0];
    cout << t.shape() << endl;
    auto tmap = t.tensor<float, 2>();
}



int main() {
    thread one(thread_pridect, "D:/images/people.jpg");
    one.join();
    return 0;
}

推断可以完成,但“输出”变量为空。我认为问题出在“输入”变量,但我尝试几次后现在可以工作。我不知道如何解决这个问题,可以理解吗可以帮忙吗?

0 个答案:

没有答案