CNN model does not load inside Visual Studio 2017 using dnn.readNetFromTensorflow

asked 2018-11-26 08:32:08 -0500

StefanCepa995 gravatar image

Hello everyone! I have created a simple CNN model which I froze into tf_model.pb file and I have tried loading it using open cv dnn.readNetFromTesnorflow(model_path) method inside Jupyter Notebook and it works perfectly. The problem is when I try to load it inside Visual Studio 2017, for some reason it does not load correctly. When I try to print out number of layers I get 1, and that 1 is not even a layer.

Here is my code:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <vector>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
using namespace cv;
using namespace cv::dnn;

int main(int argc, char** argv) {


    class CharNet {

        private:
            Net network;
            string output_layer;

        public:

            CharNet(string path) {
                try {
                    cout << "Path: "<< path << endl;

                    network = readNetFromTensorflow(path);

                    vector<String> network_layers = network.getLayerNames();
                    cout << "There are "<<network_layers.size()<< " layers" << endl;
                    for (int i = 0; i < network_layers.size(); i++) {
                        cout << network_layers[i] << endl;
                        if ( network_layers[i].find("Softmax") != std::string::npos ) {
                            cout << "Output Layer: "<< network_layers[i] << endl;
                            output_layer = network_layers[i];
                            break;
                        }
                    }

                }
                catch (cv::Exception& e) {
                    cerr << "Exception: " << e.what() << endl;
                    if (network.empty()) {
                        cerr << "Can't load the model" << endl;
                    }
                }
            }

    };

    string model = "C:/Users/stefan_cepa995/source/repos/OpenCV_Test/OpenCV_Test/tf_model.pb";

    CharNet* obj = new CharNet(model);

    return 0;

}

Since the model is not loaded correctly I never enter the for loop. Does anyone have any idea whats going on?

edit retag flag offensive close merge delete