forked from migueldeicaza/TensorFlowSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowLevelTests.cs
More file actions
372 lines (314 loc) · 10.4 KB
/
LowLevelTests.cs
File metadata and controls
372 lines (314 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// Low-level tests
//
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using TensorFlow;
using System.IO;
using System.Collections.Generic;
using Learn.Mnist;
using System.Linq;
namespace SampleTest
{
partial class MainClass
{
TFOperation Placeholder (TFGraph graph, TFStatus s)
{
var desc = new TFOperationDesc (graph, "Placeholder", "feed");
desc.SetAttrType ("dtype", TFDataType.Int32);
Console.WriteLine ("Handle: {0}", desc.Handle);
var j = desc.FinishOperation ();
Console.WriteLine ("FinishHandle: {0}", j.Handle);
return j;
}
TFOperation ScalarConst (TFTensor v, TFGraph graph, TFStatus status, string name = null)
{
var desc = new TFOperationDesc (graph, "Const", name == null ? "scalar" : name);
desc.SetAttr ("value", v, status);
if (status.StatusCode != TFCode.Ok)
return null;
desc.SetAttrType ("dtype", TFDataType.Int32);
return desc.FinishOperation ();
}
TFOperation Add (TFOperation left, TFOperation right, TFGraph graph, TFStatus status)
{
var op = new TFOperationDesc (graph, "AddN", "add");
op.AddInputs (new TFOutput (left, 0), new TFOutput (right, 0));
return op.FinishOperation ();
}
public void TestImportGraphDef ()
{
var status = new TFStatus ();
TFBuffer graphDef;
// Create graph with two nodes, "x" and "3"
using (var graph = new TFGraph ()) {
Assert (status);
Placeholder (graph, status);
Assert (graph ["feed"] != null);
ScalarConst (3, graph, status);
Assert (graph ["scalar"] != null);
// Export to GraphDef
graphDef = new TFBuffer ();
graph.ToGraphDef (graphDef, status);
Assert (status);
};
// Import it again, with a prefix, in a fresh graph
using (var graph = new TFGraph ()) {
using (var options = new TFImportGraphDefOptions ()) {
options.SetPrefix ("imported");
graph.Import (graphDef, options, status);
Assert (status);
}
graphDef.Dispose ();
var scalar = graph ["imported/scalar"];
var feed = graph ["imported/feed"];
Assert (scalar != null);
Assert (feed != null);
// Can add nodes to the imported graph without trouble
Add (feed, scalar, graph, status);
Assert (status);
}
}
public void TestSession ()
{
var status = new TFStatus ();
using (var graph = new TFGraph ()) {
var feed = Placeholder (graph, status);
var two = ScalarConst (2, graph, status);
var add = Add (feed, two, graph, status);
Assert (status);
// Create a session for this graph
using (var session = new TFSession (graph, status)) {
Assert (status);
// Run the graph
var inputs = new TFOutput [] {
new TFOutput (feed, 0)
};
var input_values = new TFTensor [] {
3
};
var add_output = new TFOutput (add, 0);
var outputs = new TFOutput [] {
add_output
};
var results = session.Run (runOptions: null,
inputs: inputs,
inputValues: input_values,
outputs: outputs,
targetOpers: null,
runMetadata: null,
status: status);
Assert (status);
var res = results [0];
Assert (res.TensorType == TFDataType.Int32);
Assert (res.NumDims == 0); // Scalar
Assert (res.TensorByteSize == (UIntPtr)4);
Assert (Marshal.ReadInt32 (res.Data) == 3 + 2);
// Use runner API
var runner = session.GetRunner ();
runner.AddInput (new TFOutput (feed, 0), 3);
runner.Fetch (add_output);
results = runner.Run (status: status);
res = results [0];
Assert (res.TensorType == TFDataType.Int32);
Assert (res.NumDims == 0); // Scalar
Assert (res.TensorByteSize == (UIntPtr)4);
Assert (Marshal.ReadInt32 (res.Data) == 3 + 2);
}
}
}
public void TestOperationOutputListSize ()
{
using (var graph = new TFGraph ()) {
var c1 = graph.Const (1L, "c1");
var cl = graph.Const (new int [] { 1, 2 }, "cl");
var c2 = graph.Const (new long [,] { { 1, 2 }, { 3, 4 } }, "c2");
var outputs = graph.ShapeN (new TFOutput [] { c1, c2 });
var op = outputs [0].Operation;
Assert (op.OutputListLength ("output") == 2);
Assert (op.NumOutputs == 2);
}
}
public void TestOutputShape ()
{
using (var graph = new TFGraph ()) {
var c1 = graph.Const (0L, "c1");
var s1 = graph.GetShape (c1);
var c2 = graph.Const (new long [] { 1, 2, 3 }, "c2");
var s2 = graph.GetShape (c2);
var c3 = graph.Const (new long [,] { { 1, 2, 3 }, { 4, 5, 6 } }, "c3");
var s3 = graph.GetShape (c3);
}
}
class WhileTester : IDisposable
{
public TFStatus status;
public TFGraph graph;
public TFSession session;
public TFSession.Runner runner;
public TFOutput [] inputs, outputs;
public WhileTester ()
{
status = new TFStatus ();
graph = new TFGraph ();
}
public void Init (int ninputs, TFGraph.WhileConstructor constructor)
{
inputs = new TFOutput [ninputs];
for (int i = 0; i < ninputs; ++i)
inputs [i] = graph.Placeholder (TFDataType.Int32, operName: "p" + i);
Assert (status);
outputs = graph.While (inputs, constructor, status);
Assert (status);
}
public TFTensor [] Run (params int [] inputValues)
{
Assert (inputValues.Length == inputs.Length);
session = new TFSession (graph);
runner = session.GetRunner ();
for (int i = 0; i < inputs.Length; i++)
runner.AddInput (inputs [i], (TFTensor)inputValues [i]);
runner.Fetch (outputs);
return runner.Run ();
}
public void Dispose ()
{
if (session != null)
session.Dispose ();
if (graph != null)
graph.Dispose ();
}
}
public void WhileTest ()
{
using (var j = new WhileTester ()) {
// Create loop: while (input1 < input2) input1 += input2 + 1
j.Init (2, (TFGraph conditionGraph, TFOutput [] condInputs, out TFOutput condOutput, TFGraph bodyGraph, TFOutput [] bodyInputs, TFOutput [] bodyOutputs, out string name) => {
Assert (bodyGraph.Handle != IntPtr.Zero);
Assert (conditionGraph.Handle != IntPtr.Zero);
var status = new TFStatus ();
var lessThan = conditionGraph.Less (condInputs [0], condInputs [1]);
Assert (status);
condOutput = new TFOutput (lessThan.Operation, 0);
var add1 = bodyGraph.Add (bodyInputs [0], bodyInputs [1]);
var one = bodyGraph.Const (1);
var add2 = bodyGraph.Add (add1, one);
bodyOutputs [0] = new TFOutput (add2, 0);
bodyOutputs [1] = bodyInputs [1];
name = "Simple1";
});
var res = j.Run (-9, 2);
Assert (3 == (int)res [0].GetValue ());
Assert (2 == (int)res [1].GetValue ());
};
}
// For this to work, we need to surface REGISTER_OP from C++ to C
class AttributeTest : IDisposable
{
static int counter;
public TFStatus Status;
TFGraph graph;
//TFOperationDesc desc;
public AttributeTest ()
{
Status = new TFStatus ();
graph = new TFGraph ();
}
public TFOperationDesc Init (string op)
{
string opname = "AttributeTest";
if (op.StartsWith ("list(")) {
op = op.Substring (5, op.Length - 6);
opname += "List";
}
opname += op;
return new TFOperationDesc (graph, opname, "name" + (counter++));
}
public void Dispose ()
{
graph.Dispose ();
Status.Dispose ();
}
}
void ExpectMeta (TFOperation op, string name, int expectedListSize, TFAttributeType expectedType, int expectedTotalSize)
{
var meta = op.GetAttributeMetadata (name);
Assert (meta.IsList == (expectedListSize >= 0 ? true : false));
Assert (expectedListSize == meta.ListSize);
Assert (expectedTotalSize == meta.TotalSize);
Assert (expectedType == meta.Type);
}
public void AttributesTest ()
{
using (var x = new AttributeTest ()) {
var shape1 = new TFShape (new long [] { 1, 3 });
var shape2 = new TFShape (2, 4, 6);
var desc = x.Init ("list(shape)");
desc.SetAttrShape ("v", new TFShape [] { shape1, shape2 });
var op = desc.FinishOperation ();
ExpectMeta (op, "v", 2, TFAttributeType.Shape, 5);
}
}
public void AddControlInput ()
{
Console.WriteLine ("Testing AddControlInput for assertions");
var status = new TFStatus ();
using (var g = new TFGraph ()) {
var s = new TFSession (g, status);
TFTensor yes = true;
TFTensor no = false;
var placeholder = g.Placeholder (TFDataType.Bool, operName: "boolean");
var check = new TFOperationDesc (g, "Assert", "assert")
.AddInput (placeholder)
.AddInputs (placeholder)
.FinishOperation ();
var noop = new TFOperationDesc (g, "NoOp", "noop")
.AddControlInput (check)
.FinishOperation ();
var runner = s.GetRunner ();
runner.AddInput (placeholder, yes);
runner.AddTarget (noop);
// No problems when the Assert check succeeds
runner.Run ();
// Exception thrown by the execution of the Assert node
try {
runner = s.GetRunner ();
runner.AddInput (placeholder, no);
runner.AddTarget (noop);
runner.Run ();
throw new Exception ("This should have thrown an exception");
} catch (Exception) {
Console.WriteLine ("Success, got the expected exception when using tensorflow control inputs to assert");
}
}
}
public void TestParametersWithIndexes ()
{
Console.WriteLine ("Testing Parameters with indexes");
var status = new TFStatus ();
using (var g = new TFGraph ()) {
var s = new TFSession (g, status);
var split = new TFOperationDesc (g,"Split", "Split")
.AddInput (ScalarConst (0, g, status) [0])
.AddInput (ScalarConst (new TFTensor (new int [] { 1, 2, 3, 4 }), g, status, "array") [0])
.SetAttr ("num_split", 2)
.FinishOperation ();
var add = new TFOperationDesc (g, "Add", "Add")
.AddInput (split [0]).AddInput (split [1]).FinishOperation () [0];
// fetch using colon separated names
var fetched = s.GetRunner ().Fetch ("Split:1").Run () [0];
var vals = fetched.GetValue () as int [];
if (vals [0] != 3 || vals [1] != 4)
throw new Exception ("Expected the values 3 and 4");
// Add inputs using colon separated names.
var t = new TFTensor (new int [] { 4, 3, 2, 1 });
var ret = (s.GetRunner ().AddInput ("Split:0", t).AddInput ("Split:1", t).Fetch ("Add").Run ()).GetValue (0) as TFTensor;
var val = ret.GetValue () as int [];
if (val [0] != 8 || val [1] != 6 || val [2] != 4 || val [3] != 2)
throw new Exception ("Expected 8, 6, 4, 2");
}
Console.WriteLine ("success");
}
}
}