opencv 加载tensorflow pb模型

opencv加载的pb模型必须是用tf.layers 和 tf.nn 下的api构建的,使用slim会在加载时报未知的layer的错误

基本流程:

1.加载pb

bool CardDetect::load_model(string mode_path) {
    this->net = cv::dnn::readNetFromTensorflow(mode_path);
    if (this->net.empty()) {
        cout << "Card net load fail." << endl;
    } else {
        cout << "Card net load success." << endl;
    }
}

2.定义输入和输出,当多输出时,输出使用一个vector包裹

CardResult CardDetect::predict(Mat &src) {
    // 指定输入数据和输入节点
    Mat inputBlob = cv::dnn::blobFromImage(src, 1. / 255, Size(96, 96), cv::Scalar(), false, false);
    net.setInput(inputBlob, "input");//set the network input, "data" is the name of the input layer

    // 指定输出节点, 多输出使用一个vector包裹
    vector<cv::String> blobNames;
    blobNames.emplace_back("card_net/type_pred");
    blobNames.emplace_back("card_net/available_pred");

    // 分配输出结果空间
    vector<Mat> outputs;
    // 前向传播
    net.forward(outputs, blobNames);
    
    // 解析结果
    int type = argmax(outputs[0]);
    int avaiable = argmax(outputs[1]);
    CardResult result;
    result.card_type = type;
    result.avaiable = avaiable;
    result.card_name = this->char_dict[to_string(type).data()].GetString();
    return result;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章