SCM tf_graph_import_(SCM scm_graph, SCM scm_file_name) { struct tf_graph_t *graph = get_tf_graph(scm_graph); char *file_name = scm_to_locale_string(scm_file_name); FILE *file = fopen(file_name, "r"); free(file_name); if (!file) scm_misc_error("tf-graph-import_", strerror(errno), SCM_EOL); int fd = fileno(file); struct stat st; fstat(fd, &st); size_t size = st.st_size; TF_Buffer *buffer = TF_NewBuffer(); void *data = scm_gc_malloc(size, "tf-graph-import_"); fread(data, size, 1, file); buffer->data = data; buffer->length = size; fclose(file); TF_ImportGraphDefOptions* opts = TF_NewImportGraphDefOptions(); TF_GraphImportGraphDef(graph->graph, buffer, opts, status()); TF_DeleteImportGraphDefOptions(opts); TF_DeleteBuffer(buffer); if (TF_GetCode(_status) != TF_OK) scm_misc_error("tf-graph-import_", TF_Message(_status), SCM_EOL); return SCM_UNDEFINED; }
SCM tf_graph_export_(SCM scm_graph, SCM scm_file_name) { struct tf_graph_t *graph = get_tf_graph(scm_graph); TF_Buffer *buffer = TF_NewBuffer(); TF_GraphToGraphDef(graph->graph, buffer, status()); if (TF_GetCode(_status) != TF_OK) { TF_DeleteBuffer(buffer); scm_misc_error("tf-graph-export_", TF_Message(_status), SCM_EOL); }; char *file_name = scm_to_locale_string(scm_file_name); FILE *file = fopen(file_name, "w"); free(file_name); if (!file) scm_misc_error("tf-graph-export_", strerror(errno), SCM_EOL); fwrite(buffer->data, buffer->length, 1, file); fclose(file); TF_DeleteBuffer(buffer); return SCM_UNDEFINED; }
// extern void TF_DeleteBuffer(TF_Buffer*); static PHP_METHOD(TensorFlow_Buffer, __destruct) { // this t_tf_buffer_object* intern; t_tf_buffer* node; intern = TF_BUFFER_P_ZV(getThis()); node = intern->ptr; TF_DeleteBuffer(node->src); }
DNNModel *ff_dnn_load_model_tf(const char *model_filename) { DNNModel *model = NULL; TFModel *tf_model = NULL; TF_Buffer *graph_def; TF_ImportGraphDefOptions *graph_opts; model = av_malloc(sizeof(DNNModel)); if (!model){ return NULL; } tf_model = av_malloc(sizeof(TFModel)); if (!tf_model){ av_freep(&model); return NULL; } tf_model->session = NULL; tf_model->input_tensor = NULL; tf_model->output_data = NULL; graph_def = read_graph(model_filename); if (!graph_def){ av_freep(&tf_model); av_freep(&model); return NULL; } tf_model->graph = TF_NewGraph(); tf_model->status = TF_NewStatus(); graph_opts = TF_NewImportGraphDefOptions(); TF_GraphImportGraphDef(tf_model->graph, graph_def, graph_opts, tf_model->status); TF_DeleteImportGraphDefOptions(graph_opts); TF_DeleteBuffer(graph_def); if (TF_GetCode(tf_model->status) != TF_OK){ TF_DeleteGraph(tf_model->graph); TF_DeleteStatus(tf_model->status); av_freep(&tf_model); av_freep(&model); return NULL; } model->model = (void *)tf_model; model->set_input_output = &set_input_output_tf; return model; }
int main(void) { GC_INIT(); TF_Buffer *buffer = TF_GetAllOpList(); Tensorflow__OpList *op_list = tensorflow__op_list__unpack(NULL, buffer->length, buffer->data); HDF *hdf; hdf_init(&hdf); for (int i=0; i<op_list->n_op; i++) { struct _Tensorflow__OpDef *op = op_list->op[i]; char variable[256]; char value[256]; snprintf(variable, 256, "Op.%s.name", op->name); snprintf(value, 256, "tf-%s", kebab_case(op->name)); hdf_set_value(hdf, variable, value); for (int j=0; j<op->n_input_arg; j++) { Tensorflow__OpDef__ArgDef *arg = op->input_arg[j]; snprintf(variable, 256, "Op.%s.input_arg.%s", op->name, arg->name); const char *multiple = arg->number_attr && *arg->number_attr ? "list" : "single"; hdf_set_value(hdf, variable, multiple); }; for (int j=0; j<op->n_attr; j++) { Tensorflow__OpDef__AttrDef *attr = op->attr[j]; snprintf(variable, 256, "Op.%s.attr.%s", op->name, attr->name); snprintf(value, 256, "%s", attr->type); hdf_set_value(hdf, variable, value); }; snprintf(variable, 256, "Op.%s.n_output", op->name); snprintf(value, 256, "%d", op->n_output_arg); hdf_set_value(hdf, variable, value); }; CSPARSE *parse; cs_init(&parse, hdf); cs_parse_file(parse, "tensorflow.scm.in"); cs_render(parse, stdout, output); cs_destroy(&parse); hdf_destroy(&hdf); tensorflow__op_list__free_unpacked(op_list, NULL); TF_DeleteBuffer(buffer); }
static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename) { TF_Buffer *graph_def; TF_ImportGraphDefOptions *graph_opts; graph_def = read_graph(model_filename); if (!graph_def){ return DNN_ERROR; } tf_model->graph = TF_NewGraph(); tf_model->status = TF_NewStatus(); graph_opts = TF_NewImportGraphDefOptions(); TF_GraphImportGraphDef(tf_model->graph, graph_def, graph_opts, tf_model->status); TF_DeleteImportGraphDefOptions(graph_opts); TF_DeleteBuffer(graph_def); if (TF_GetCode(tf_model->status) != TF_OK){ TF_DeleteGraph(tf_model->graph); TF_DeleteStatus(tf_model->status); return DNN_ERROR; } return DNN_SUCCESS; }