Skip to content

Commit 6623162

Browse files
committed
fix default graph and operation issue when import model.
1 parent b03bb19 commit 6623162

9 files changed

Lines changed: 89 additions & 44 deletions

File tree

src/TensorFlowNET.Core/Buffers/Buffer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public byte[] Data
3030
get
3131
{
3232
var data = new byte[buffer.length];
33-
if (buffer.length > 0)
34-
Marshal.Copy(buffer.data, data, 0, (int)buffer.length);
33+
if (data.Length > 0)
34+
Marshal.Copy(buffer.data, data, 0, data.Length);
3535
return data;
3636
}
3737
}

src/TensorFlowNET.Core/Framework/c_api_util.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static IEnumerable<Operation> tf_operations(Graph graph)
128128
IntPtr c_op;
129129
while ((c_op = c_api.TF_GraphNextOperation(graph, ref pos)) != IntPtr.Zero)
130130
{
131-
yield return c_op;
131+
yield return new Operation(c_op, graph);
132132
}
133133
}
134134
}

src/TensorFlowNET.Core/Graphs/Graph.Operation.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ public OperationDescription NewOperation(string opType, string opName)
3838
return c_api.TF_NewOperation(_handle, opType, opName);
3939
}
4040

41+
public unsafe Operation[] ReturnOperations(IntPtr results)
42+
{
43+
TF_Operation return_oper_handle = new TF_Operation();
44+
int num_return_opers = 0;
45+
c_api.TF_ImportGraphDefResultsReturnOperations(results, ref num_return_opers, ref return_oper_handle);
46+
Operation[] return_opers = new Operation[num_return_opers];
47+
for (int i = 0; i < num_return_opers; i++)
48+
{
49+
var handle = return_oper_handle.node + Marshal.SizeOf<TF_Operation>() * i;
50+
return_opers[i] = new Operation(*(IntPtr*)handle);
51+
}
52+
53+
return return_opers;
54+
}
55+
56+
public Operation OperationByName(string operName)
57+
{
58+
return c_api.TF_GraphOperationByName(_handle, operName);
59+
}
60+
61+
public ITensorOrOperation[] get_operations()
62+
{
63+
return _nodes_by_name.Values.Select(x => x).ToArray();
64+
}
65+
4166
/// <summary>
4267
/// Returns the `Operation` with the given `name`.
4368
///

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using System;
18+
using System.Collections;
1819
using System.Collections.Generic;
1920
using System.Linq;
2021
using System.Runtime.InteropServices;
@@ -72,7 +73,7 @@ that are identified by name. For convenience when building a large
7273
all variables that are created during the construction of a graph. The caller
7374
may define additional collections by specifying a new name.
7475
*/
75-
public partial class Graph : IPython, IDisposable
76+
public partial class Graph : IPython, IDisposable, IEnumerable<Operation>
7677
{
7778
private IntPtr _handle;
7879
private Dictionary<int, ITensorOrOperation> _nodes_by_id;
@@ -121,6 +122,10 @@ public Graph(IntPtr handle)
121122
_nodes_by_name = new Dictionary<string, ITensorOrOperation>();
122123
_names_in_use = new Dictionary<string, int>();
123124
_graph_key = $"grap-key-{ops.uid()}/";
125+
}
126+
127+
public void __enter__()
128+
{
124129
}
125130

126131
public ITensorOrOperation as_graph_element(object obj, bool allow_tensor = true, bool allow_operation = true)
@@ -409,31 +414,6 @@ public TF_Output[] ReturnOutputs(IntPtr results)
409414
return return_outputs;
410415
}
411416

412-
public unsafe Operation[] ReturnOperations(IntPtr results)
413-
{
414-
TF_Operation return_oper_handle = new TF_Operation();
415-
int num_return_opers = 0;
416-
c_api.TF_ImportGraphDefResultsReturnOperations(results, ref num_return_opers, ref return_oper_handle);
417-
Operation[] return_opers = new Operation[num_return_opers];
418-
for (int i = 0; i < num_return_opers; i++)
419-
{
420-
var handle = return_oper_handle.node + Marshal.SizeOf<TF_Operation>() * i;
421-
return_opers[i] = new Operation(*(IntPtr*)handle);
422-
}
423-
424-
return return_opers;
425-
}
426-
427-
public Operation OperationByName(string operName)
428-
{
429-
return c_api.TF_GraphOperationByName(_handle, operName);
430-
}
431-
432-
public ITensorOrOperation[] get_operations()
433-
{
434-
return _nodes_by_name.Values.Select(x => x).ToArray();
435-
}
436-
437417
public string[] get_all_collection_keys()
438418
{
439419
return _collections.Keys.Where(x => !x.StartsWith("__")).ToArray();
@@ -481,17 +461,46 @@ public void Dispose()
481461
public Tensor get_tensor_by_name(string name)
482462
{
483463
return (Tensor)this.as_graph_element(name, allow_tensor: true, allow_operation: false);
484-
}
485-
486-
public void __enter__()
487-
{
464+
}
465+
466+
public TensorShape GetTensorShape(TF_Output output)
467+
{
468+
var status = new Status();
469+
var ndim = c_api.TF_GraphGetTensorNumDims(_handle, output, status);
470+
status.Check();
471+
472+
if (ndim == -1)
473+
return new TensorShape();
474+
475+
var dims = new long[ndim];
476+
c_api.TF_GraphGetTensorShape(_handle, output, dims, dims.Length, status);
477+
status.Check();
478+
479+
return new TensorShape(dims.Select(x => (int)x).ToArray());
480+
}
481+
482+
public override string ToString()
483+
{
484+
int len = 0;
485+
return c_api.TF_GraphDebugString(_handle, out len);
488486
}
489487

490488
public void __exit__()
491489
{
492490

493-
}
491+
}
492+
493+
private IEnumerable<Operation> GetEnumerable()
494+
=> c_api_util.tf_operations(this);
494495

496+
IEnumerator<Operation> IEnumerable<Operation>.GetEnumerator()
497+
=> GetEnumerable().GetEnumerator();
498+
499+
IEnumerator IEnumerable.GetEnumerator()
500+
{
501+
throw new NotImplementedException();
502+
}
503+
495504
public static implicit operator IntPtr(Graph graph)
496505
{
497506
return graph._handle;

src/TensorFlowNET.Core/Graphs/c_api.graph.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public partial class c_api
4343
[DllImport(TensorFlowLibName)]
4444
public static extern void TF_DeleteImportGraphDefResults(IntPtr results);
4545

46+
[DllImport(TensorFlowLibName)]
47+
public static extern string TF_GraphDebugString(IntPtr graph, out int len);
48+
4649
[DllImport(TensorFlowLibName)]
4750
public static extern void TF_GraphGetOpDef(IntPtr graph, string op_name, IntPtr output_op_def, IntPtr status);
4851

@@ -100,6 +103,7 @@ public partial class c_api
100103
/// <param name="status">TF_Status*</param>
101104
[DllImport(TensorFlowLibName)]
102105
public static extern void TF_GraphImportGraphDef(IntPtr graph, IntPtr graph_def, IntPtr options, IntPtr status);
106+
103107
/// <summary>
104108
/// Iterate through the operations of a graph.
105109
/// </summary>

src/TensorFlowNET.Core/Operations/Operation.Implicit.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ namespace Tensorflow
2323
/// </summary>
2424
public partial class Operation
2525
{
26-
public static implicit operator Operation(IntPtr handle) => new Operation(handle);
26+
// make sure the new op is in the same graph instance
27+
public static implicit operator Operation(IntPtr handle)
28+
=> new Operation(handle);
29+
2730
public static implicit operator IntPtr(Operation op) => op._handle;
2831
public static implicit operator Tensor(Operation op) => op.output;
2932

src/TensorFlowNET.Core/Operations/Operation.Output.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public partial class Operation
3535

3636
public int OutputNumConsumers(int index) => c_api.TF_OperationOutputNumConsumers(new TF_Output(_handle, index));
3737

38+
public TF_Output this[int index] => _tf_output(index);
39+
3840
public unsafe TF_Input[] OutputConsumers(int index, int max_consumers)
3941
{
4042
int size = Marshal.SizeOf<TF_Input>();

src/TensorFlowNET.Core/Sessions/Session.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using System;
18+
using System.Runtime.InteropServices;
1819

1920
namespace Tensorflow
2021
{
@@ -26,8 +27,8 @@ public Session(string target = "", Graph g = null)
2627

2728
}
2829

29-
public Session(IntPtr handle)
30-
: base("", null, null)
30+
public Session(IntPtr handle, Graph g = null)
31+
: base("", g, null)
3132
{
3233
_session = handle;
3334
}
@@ -50,8 +51,10 @@ public static Session LoadFromSavedModel(string path)
5051
var graph = c_api.TF_NewGraph();
5152
var status = new Status();
5253
var opt = c_api.TF_NewSessionOptions();
54+
5355
var tags = new string[] { "serve" };
5456
var buffer = new TF_Buffer();
57+
5558
var sess = c_api.TF_LoadSessionFromSavedModel(opt,
5659
IntPtr.Zero,
5760
path,
@@ -61,14 +64,13 @@ public static Session LoadFromSavedModel(string path)
6164
ref buffer,
6265
status);
6366

64-
//var bytes = new Buffer(buffer.data).Data;
65-
//var meta_graph = MetaGraphDef.Parser.ParseFrom(bytes);
66-
67+
// load graph bytes
68+
// var data = new byte[buffer.length];
69+
// Marshal.Copy(buffer.data, data, 0, (int)buffer.length);
70+
// var meta_graph = MetaGraphDef.Parser.ParseFrom(data);*/
6771
status.Check();
6872

69-
new Graph(graph).as_default();
70-
71-
return sess;
73+
return new Session(sess, g: new Graph(graph).as_default());
7274
}
7375

7476
public static implicit operator IntPtr(Session session) => session._session;

test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public bool Run()
118118
float acc = accuracy.eval(new FeedItem(x, mnist.Test.Data), new FeedItem(y, mnist.Test.Labels));
119119
print($"Accuracy: {acc.ToString("F4")}");
120120

121-
return acc > 0.88;
121+
return acc > 0.9;
122122
});
123123
}
124124

0 commit comments

Comments
 (0)