@@ -23,6 +23,7 @@ public partial class Graph
2323 {
2424 public unsafe TF_Output [ ] ImportGraphDefWithReturnOutputs ( Buffer graph_def , ImportGraphDefOptions opts , Status s )
2525 {
26+ as_default ( ) ;
2627 var num_return_outputs = opts . NumReturnOutputs ;
2728 var return_outputs = new TF_Output [ num_return_outputs ] ;
2829 int size = Marshal . SizeOf < TF_Output > ( ) ;
@@ -35,40 +36,37 @@ public unsafe TF_Output[] ImportGraphDefWithReturnOutputs(Buffer graph_def, Impo
3536 return_outputs [ i ] = Marshal . PtrToStructure < TF_Output > ( handle ) ;
3637 }
3738
39+ Marshal . FreeHGlobal ( return_output_handle ) ;
40+
3841 return return_outputs ;
3942 }
4043
41- public Status Import ( string file_path )
44+ public bool Import ( string file_path , string prefix = "" )
4245 {
4346 var bytes = File . ReadAllBytes ( file_path ) ;
44- var graph_def = new Tensorflow . Buffer ( bytes ) ;
45- var opts = c_api . TF_NewImportGraphDefOptions ( ) ;
46- var status = new Status ( ) ;
47- c_api . TF_GraphImportGraphDef ( _handle , graph_def , opts , status ) ;
48- return status ;
49- }
50-
51- public Status Import ( byte [ ] bytes , string prefix = "" )
52- {
53- var graph_def = new Tensorflow . Buffer ( bytes ) ;
54- var opts = c_api . TF_NewImportGraphDefOptions ( ) ;
55- c_api . TF_ImportGraphDefOptionsSetPrefix ( opts , prefix ) ;
56- var status = new Status ( ) ;
57- c_api . TF_GraphImportGraphDef ( _handle , graph_def , opts , status ) ;
58- c_api . TF_DeleteImportGraphDefOptions ( opts ) ;
59- return status ;
47+ return Import ( bytes , prefix : prefix ) ;
6048 }
6149
62- static object locker = new object ( ) ;
63- public static Graph ImportFromPB ( string file_path , string name = null )
50+ public bool Import ( byte [ ] bytes , string prefix = "" )
6451 {
65- lock ( locker )
52+ using ( var opts = new ImportGraphDefOptions ( ) )
53+ using ( var status = new Status ( ) )
54+ using ( var graph_def = new Buffer ( bytes ) )
6655 {
67- var graph = tf . Graph ( ) . as_default ( ) ;
68- var graph_def = GraphDef . Parser . ParseFrom ( File . ReadAllBytes ( file_path ) ) ;
69- importer . import_graph_def ( graph_def , name : name ) ;
70- return graph ;
56+ as_default ( ) ;
57+ c_api . TF_ImportGraphDefOptionsSetPrefix ( opts , prefix ) ;
58+ c_api . TF_GraphImportGraphDef ( _handle , graph_def , opts , status ) ;
59+ status . Check ( true ) ;
60+ return status . Code == TF_Code . TF_OK ;
7161 }
7262 }
63+
64+ /*public Graph Import(string file_path, string name = null)
65+ {
66+ as_default();
67+ var graph_def = GraphDef.Parser.ParseFrom(File.ReadAllBytes(file_path));
68+ importer.import_graph_def(graph_def, name: name);
69+ return this;
70+ }*/
7371 }
7472}
0 commit comments