static TF_Buffer *read_graph(const char *model_filename) { TF_Buffer *graph_buf; unsigned char *graph_data = NULL; AVIOContext *model_file_context; long size, bytes_read; if (avio_open(&model_file_context, model_filename, AVIO_FLAG_READ) < 0){ return NULL; } size = avio_size(model_file_context); graph_data = av_malloc(size); if (!graph_data){ avio_closep(&model_file_context); return NULL; } bytes_read = avio_read(model_file_context, graph_data, size); avio_closep(&model_file_context); if (bytes_read != size){ av_freep(&graph_data); return NULL; } graph_buf = TF_NewBuffer(); graph_buf->data = (void *)graph_data; graph_buf->length = size; graph_buf->data_deallocator = free_buffer; return graph_buf; }
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 TF_Buffer* TF_NewBufferFromString(const void* proto, size_t proto_len); // extern TF_Buffer* TF_NewBuffer(); static PHP_METHOD(TensorFlow_Buffer, __construct) { // arguments zend_string* buffer; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_STR(buffer) ZEND_PARSE_PARAMETERS_END(); // this t_tf_buffer_object* intern; t_tf_buffer* php_tf_buffer; intern = TF_BUFFER_P_ZV(getThis()); php_tf_buffer = intern->ptr; if (ZEND_NUM_ARGS() == 1) { php_tf_buffer->src = TF_NewBufferFromString(ZSTR_VAL(buffer), ZSTR_LEN(buffer)); } else { php_tf_buffer->src = TF_NewBuffer(); } }