我无法找到有关如何加载图形与任何例子tensorflow.so
和c_api.h
C++中.我读了c_api.h
,但ReadBinaryProto
功能不在其中.如何在没有该ReadBinaryProto
功能的情况下加载图形?
如果您使用的是C++,则可能需要使用C++ API.该标签图像例如可能会是一个很好的样本,以帮助您开始.
如果您确实只想使用C API,请使用TF_GraphImportGraphDef
加载图表.请注意,C API使用起来不是特别方便(它打算用其他语言构建绑定,例如Go,Java,Rust,Haskell等)例如:
#include#include #include TF_Buffer* read_file(const char* file); void free_buffer(void* data, size_t length) { free(data); } int main() { // Graph definition from unzipped https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip // which is used in the Go, Java and Android examples TF_Buffer* graph_def = read_file("tensorflow_inception_graph.pb"); TF_Graph* graph = TF_NewGraph(); // Import graph_def into graph 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", TF_Message(status)); return 1; } fprintf(stdout, "Successfully imported graph"); TF_DeleteStatus(status); TF_DeleteBuffer(graph_def); // Use the graph TF_DeleteGraph(graph); return 0; } TF_Buffer* read_file(const char* file) { FILE *f = fopen(file, "rb"); fseek(f, 0, SEEK_END); long fsize = ftell(f); fseek(f, 0, SEEK_SET); //same as rewind(f); void* data = malloc(fsize); fread(data, fsize, 1, f); fclose(f); TF_Buffer* buf = TF_NewBuffer(); buf->data = data; buf->length = fsize; buf->data_deallocator = free_buffer; return buf; }