4

I saved a keras model in tf 2.2.0 in Python via:

model.save('model', save_format='tf')

And it gives me a saved_model.pb in the "model" directory. I want to make inference via c_api, and the following code using the function: TF_LoadSessionFromSavedModel works fine.

int main() {
    TF_Graph *Graph = TF_NewGraph();
    TF_Status *Status = TF_NewStatus();
    TF_SessionOptions *SessionOpts = TF_NewSessionOptions();
    TF_Buffer *RunOpts = NULL;

    const char *saved_model_dir = "model/";
    const char *tags = "serve";

    int ntags = 1;
    TF_Session *Session = TF_LoadSessionFromSavedModel(SessionOpts, RunOpts, saved_model_dir, &tags, ntags, Graph, NULL, Status);

    if (TF_GetCode(Status) == TF_OK)
    {
        printf("TF_LoadSessionFromSavedModel OK\n");
    }
    else
    {
        printf("%s", TF_Message(Status));
    }
    return 0;
}

However, if I wanted to directly use the saved_model.pb in the "model" directory via TF_GraphImportGraphDef, it gives me the "Invalid GraphDef" errors:

int main(int argc, char const *argv[]){
    TF_Buffer *Graph_Def = read_file("model/saved_model.pb");
    TF_Graph *Graph = TF_NewGraph();
    TF_Status *Status = TF_NewStatus();
    TF_ImportGraphDefOptions *Opts = TF_NewImportGraphDefOptions();
    TF_GraphImportGraphDef(Graph, Graph_Def, Opts, Status);
    TF_DeleteImportGraphDefOptions(Opts);
    if (TF_GetCode(Status) != TF_OK)
    {
        fprintf(stderr, "ERROR: Unable to import graph %s\n", TF_Message(Status));
        return 1;
    }
    fprintf(stdout, "Successfully imported graph\n");
    return 0;
}

I guess maybe the saved_model.pb is different from the freeze_graph version in v1. But I cannot find out why the failure happened. So, is there any way to save a tf2 keras model in a valid .pb file that can be used independently through
TF_GraphImportGraphDef?

Many thanks in advance.

Weishuo
  • 41
  • 3

0 Answers0