himalayansailor
himalayansailor

Reputation: 31

cppflow model loading hangs unity

I'm trying to load a tensorflow graph model using cppflow in c++. I am able to load the model when I define the model loading globally in a standalone applicaiton:

cppflow::model model(std::string(model_path));
int main() {
   ...
}

But, when I build this to create the dll and call that dll from unity (c#), the whole unity application hangs. Is there another way to do so or am I doing something wrong?

Upvotes: 1

Views: 114

Answers (1)

himalayansailor
himalayansailor

Reputation: 31

I found a work around that stops unity from hanging. Basically instantiating the model with an empty model path:

cppflow::model model(std::string(""));

To make it work, I had to make changes to the model header file(model.h) in cppflow to handle empty model path:

inline model::model(const std::string &filename) {
    if (!filename.empty())
    {
        this->graph = { TF_NewGraph(), TF_DeleteGraph };

        // Create the session.
        std::unique_ptr<TF_SessionOptions, decltype(&TF_DeleteSessionOptions)> session_options = { TF_NewSessionOptions(), TF_DeleteSessionOptions };
        std::unique_ptr<TF_Buffer, decltype(&TF_DeleteBuffer)> run_options = { TF_NewBufferFromString("", 0), TF_DeleteBuffer };
        std::unique_ptr<TF_Buffer, decltype(&TF_DeleteBuffer)> meta_graph = { TF_NewBuffer(), TF_DeleteBuffer };

        auto session_deleter = [](TF_Session* sess) {
            TF_DeleteSession(sess, context::get_status());
            status_check(context::get_status());
        };

        int tag_len = 1;
        const char* tag = "serve";
        this->session = { TF_LoadSessionFromSavedModel(session_options.get(), run_options.get(), filename.c_str(),
                                &tag, tag_len, this->graph.get(), meta_graph.get(), context::get_status()),
                         session_deleter };

        status_check(context::get_status());
    }
}

I overwrite the model instance with a proper model path in the update function.

Maybe an expert in cppflow can explain better why this works but for now this solved my problem. While this works smoothly in the build version as well as the editor, but may not be a perfect solution.

Upvotes: 0

Related Questions