You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

c_test_util.cs 6.5 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using Tensorflow;
  6. using Buffer = Tensorflow.Buffer;
  7. namespace TensorFlowNET.UnitTest
  8. {
  9. /// <summary>
  10. /// Port from `tensorflow\c\c_test_util.cc`
  11. /// </summary>
  12. public static class c_test_util
  13. {
  14. public static Operation Add(Operation l, Operation r, Graph graph, Status s, string name = "add")
  15. {
  16. var desc = c_api.TF_NewOperation(graph, "AddN", name);
  17. var inputs = new TF_Output[]
  18. {
  19. new TF_Output(l, 0),
  20. new TF_Output(r, 0),
  21. };
  22. c_api.TF_AddInputList(desc, inputs, inputs.Length);
  23. var op = c_api.TF_FinishOperation(desc, s);
  24. s.Check();
  25. return op;
  26. }
  27. public static bool GetAttrValue(Operation oper, string attr_name, ref AttrValue attr_value, Status s)
  28. {
  29. var buffer = new Buffer();
  30. c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s);
  31. attr_value = AttrValue.Parser.ParseFrom(buffer);
  32. buffer.Dispose();
  33. return s.Code == TF_Code.TF_OK;
  34. }
  35. public static GraphDef GetGraphDef(Graph graph)
  36. {
  37. var s = new Status();
  38. var buffer = new Buffer();
  39. c_api.TF_GraphToGraphDef(graph, buffer, s);
  40. s.Check();
  41. var def = GraphDef.Parser.ParseFrom(buffer);
  42. buffer.Dispose();
  43. s.Dispose();
  44. return def;
  45. }
  46. public static bool IsAddN(NodeDef node_def, int n)
  47. {
  48. if (node_def.Op != "AddN" || node_def.Name != "add" ||
  49. node_def.Input.Count != n)
  50. {
  51. return false;
  52. }
  53. bool found_t = false;
  54. bool found_n = false;
  55. foreach (var attr in node_def.Attr)
  56. {
  57. if (attr.Key == "T")
  58. {
  59. if (attr.Value.Type == DataType.DtInt32)
  60. {
  61. found_t = true;
  62. }
  63. else
  64. {
  65. return false;
  66. }
  67. }
  68. else if (attr.Key == "N")
  69. {
  70. if (attr.Value.I == n)
  71. {
  72. found_n = true;
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79. }
  80. return found_t && found_n;
  81. }
  82. public static bool IsNeg(NodeDef node_def, string input)
  83. {
  84. return node_def.Op == "Neg" && node_def.Name == "neg" &&
  85. node_def.Input.Count == 1 && node_def.Input[0] == input;
  86. }
  87. public static bool IsPlaceholder(NodeDef node_def)
  88. {
  89. if (node_def.Op != "Placeholder" || node_def.Name != "feed")
  90. {
  91. return false;
  92. }
  93. bool found_dtype = false;
  94. bool found_shape = false;
  95. foreach (var attr in node_def.Attr)
  96. {
  97. if (attr.Key == "dtype")
  98. {
  99. if (attr.Value.Type == DataType.DtInt32)
  100. {
  101. found_dtype = true;
  102. }
  103. else
  104. {
  105. return false;
  106. }
  107. }
  108. else if (attr.Key == "shape")
  109. {
  110. found_shape = true;
  111. }
  112. }
  113. return found_dtype && found_shape;
  114. }
  115. public static bool IsScalarConst(NodeDef node_def, int v)
  116. {
  117. if (node_def.Op != "Const" || node_def.Name != "scalar")
  118. {
  119. return false;
  120. }
  121. bool found_dtype = false;
  122. bool found_value = false;
  123. foreach (var attr in node_def.Attr) {
  124. if (attr.Key == "dtype")
  125. {
  126. if (attr.Value.Type == DataType.DtInt32)
  127. {
  128. found_dtype = true;
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. }
  135. else if (attr.Key == "value")
  136. {
  137. if (attr.Value.Tensor != null &&
  138. attr.Value.Tensor.IntVal.Count == 1 &&
  139. attr.Value.Tensor.IntVal[0] == v)
  140. {
  141. found_value = true;
  142. }
  143. else
  144. {
  145. return false;
  146. }
  147. }
  148. }
  149. return found_dtype && found_value;
  150. }
  151. public static Operation Neg(Operation n, Graph graph, Status s, string name = "neg")
  152. {
  153. OperationDescription desc = c_api.TF_NewOperation(graph, "Neg", name);
  154. var neg_input = new TF_Output(n, 0);
  155. c_api.TF_AddInput(desc, neg_input);
  156. var op = c_api.TF_FinishOperation(desc, s);
  157. s.Check();
  158. return op;
  159. }
  160. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  161. {
  162. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  163. c_api.TF_SetAttrType(desc, "dtype", dtype);
  164. if (dims != null)
  165. {
  166. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  167. }
  168. var op = c_api.TF_FinishOperation(desc, s);
  169. s.Check();
  170. return op;
  171. }
  172. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  173. {
  174. var desc = c_api.TF_NewOperation(graph, "Const", name);
  175. c_api.TF_SetAttrTensor(desc, "value", t, s);
  176. s.Check();
  177. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  178. var op = c_api.TF_FinishOperation(desc, s);
  179. s.Check();
  180. return op;
  181. }
  182. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  183. {
  184. return Const(new Tensor(v), graph, s, name);
  185. }
  186. }
  187. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。