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;
}

 

 

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