Skip to content

Commit 31909cd

Browse files
committed
Change Graph.Import interface.
1 parent de696fb commit 31909cd

7 files changed

Lines changed: 45 additions & 47 deletions

File tree

‎src/TensorFlowNET.Core/Framework/importer.py.cs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ public static ITensorOrOperation[] import_graph_def(GraphDef graph_def,
5454
input_map = _ConvertInputMapValues(name, input_map);
5555
});
5656

57-
var scoped_options = c_api_util.ScopedTFImportGraphDefOptions();
58-
_PopulateTFImportGraphDefOptions(scoped_options, prefix, input_map, return_elements);
59-
6057
var bytes = graph_def.ToByteString().ToArray();
61-
IntPtr buffer = c_api_util.tf_buffer(bytes);
62-
63-
var status = new Status();
64-
// need to create a class ImportGraphDefWithResults with IDisposal
65-
var results = c_api.TF_GraphImportGraphDefWithResults(graph, buffer, scoped_options, status);
66-
status.Check(true);
58+
using (var buffer = c_api_util.tf_buffer(bytes))
59+
using (var scoped_options = c_api_util.ScopedTFImportGraphDefOptions())
60+
using (var status = new Status())
61+
{
62+
_PopulateTFImportGraphDefOptions(scoped_options, prefix, input_map, return_elements);
63+
// need to create a class ImportGraphDefWithResults with IDisposal
64+
var results = c_api.TF_GraphImportGraphDefWithResults(graph, buffer, scoped_options, status);
65+
status.Check(true);
66+
c_api.TF_DeleteImportGraphDefResults(results);
67+
}
6768

6869
_ProcessNewOps(graph);
6970

‎src/TensorFlowNET.Core/Graphs/Graph.Import.cs‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎src/TensorFlowNET.Core/Graphs/ImportGraphDefOptions.cs‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ limitations under the License.
1818

1919
namespace Tensorflow
2020
{
21-
public class ImportGraphDefOptions : IDisposable
21+
public class ImportGraphDefOptions : DisposableObject
2222
{
23-
private IntPtr _handle;
24-
2523
public int NumReturnOutputs => c_api.TF_ImportGraphDefOptionsNumReturnOutputs(_handle);
2624

2725
public ImportGraphDefOptions()
@@ -39,10 +37,8 @@ public void AddReturnOutput(string name, int index)
3937
c_api.TF_ImportGraphDefOptionsAddReturnOutput(_handle, name, index);
4038
}
4139

42-
public void Dispose()
43-
{
44-
c_api.TF_DeleteImportGraphDefOptions(_handle);
45-
}
40+
protected override void DisposeUnManagedState(IntPtr handle)
41+
=> c_api.TF_DeleteImportGraphDefOptions(handle);
4642

4743
public static implicit operator IntPtr(ImportGraphDefOptions opts) => opts._handle;
4844
public static implicit operator ImportGraphDefOptions(IntPtr handle) => new ImportGraphDefOptions(handle);

‎src/TensorFlowNET.Core/TensorFlowNET.Core.csproj‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<AssemblyName>TensorFlow.NET</AssemblyName>
66
<RootNamespace>Tensorflow</RootNamespace>
77
<TargetTensorFlow>1.14.0</TargetTensorFlow>
8-
<Version>0.10.9</Version>
8+
<Version>0.10.10</Version>
99
<Authors>Haiping Chen, Meinrad Recheis</Authors>
1010
<Company>SciSharp STACK</Company>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -17,7 +17,7 @@
1717
<PackageTags>TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C#</PackageTags>
1818
<Description>Google's TensorFlow full binding in .NET Standard.
1919
Docs: https://tensorflownet.readthedocs.io</Description>
20-
<AssemblyVersion>0.10.9.0</AssemblyVersion>
20+
<AssemblyVersion>0.10.10.0</AssemblyVersion>
2121
<PackageReleaseNotes>Changes since v0.9.0:
2222

2323
1. Added full connected Convolution Neural Network example.
@@ -37,7 +37,7 @@ Docs: https://tensorflownet.readthedocs.io</Description>
3737
15. Fix Tensor memory leak.
3838
16. Rename with to tf_with that is only used to build graph purpose.</PackageReleaseNotes>
3939
<LangVersion>7.2</LangVersion>
40-
<FileVersion>0.10.9.0</FileVersion>
40+
<FileVersion>0.10.10.0</FileVersion>
4141
<PackageLicenseFile>LICENSE</PackageLicenseFile>
4242
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
4343
<SignAssembly>true</SignAssembly>

‎test/TensorFlowNET.Examples/ImageProcessing/ImageRecognitionInception.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public bool Run()
3131
{
3232
PrepareData();
3333

34-
var graph = new Graph().as_default();
34+
var graph = new Graph();
3535
//import GraphDef from pb file
3636
graph.Import(Path.Join(dir, pbFile));
3737

‎test/TensorFlowNET.Examples/ImageProcessing/InceptionArchGoogLeNet.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public bool Run()
4141
input_mean: input_mean,
4242
input_std: input_std);
4343

44-
var graph = Graph.ImportFromPB(Path.Join(dir, pbFile));
44+
var graph = new Graph();
45+
graph.Import(Path.Join(dir, pbFile));
4546
var input_operation = graph.get_operation_by_name(input_name);
4647
var output_operation = graph.get_operation_by_name(output_name);
4748

‎test/TensorFlowNET.Examples/ImageProcessing/RetrainImageClassifier.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,8 @@ public void Predict(Session sess_)
738738
var fileBytes = ReadTensorFromImageFile(img_path);
739739

740740
// import graph and variables
741-
var graph = Graph.ImportFromPB(output_graph, "");
741+
var graph = new Graph();
742+
graph.Import(output_graph, "");
742743

743744
Tensor input = graph.OperationByName("Placeholder");
744745
Tensor output = graph.OperationByName("final_result");
@@ -778,7 +779,8 @@ public void Test(Session sess_)
778779
if (!File.Exists(output_graph))
779780
return;
780781

781-
var graph = Graph.ImportFromPB(output_graph);
782+
var graph = new Graph();
783+
graph.Import(output_graph);
782784
var (jpeg_data_tensor, decoded_image_tensor) = add_jpeg_decoding();
783785

784786
tf_with(tf.Session(graph), sess =>

0 commit comments

Comments
 (0)