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 7.7 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. Operation op = null;
  17. AddOpHelper(l, r, graph, s, name, ref op, true);
  18. return op;
  19. }
  20. public static void AddOpHelper(Operation l, Operation r, Graph graph, Status s, string name, ref Operation op, bool check)
  21. {
  22. var desc = c_api.TF_NewOperation(graph, "AddN", name);
  23. var inputs = new TF_Output[]
  24. {
  25. new TF_Output(l, 0),
  26. new TF_Output(r, 0),
  27. };
  28. c_api.TF_AddInputList(desc, inputs, inputs.Length);
  29. op = c_api.TF_FinishOperation(desc, s);
  30. s.Check();
  31. }
  32. public static bool GetAttrValue(Operation oper, string attr_name, ref AttrValue attr_value, Status s)
  33. {
  34. var buffer = new Buffer();
  35. c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s);
  36. attr_value = AttrValue.Parser.ParseFrom(buffer);
  37. buffer.Dispose();
  38. return s.Code == TF_Code.TF_OK;
  39. }
  40. public static GraphDef GetGraphDef(Graph graph)
  41. {
  42. var s = new Status();
  43. var buffer = new Buffer();
  44. c_api.TF_GraphToGraphDef(graph, buffer, s);
  45. s.Check();
  46. var def = GraphDef.Parser.ParseFrom(buffer);
  47. buffer.Dispose();
  48. s.Dispose();
  49. return def;
  50. }
  51. public static NodeDef GetNodeDef(Operation oper)
  52. {
  53. var s = new Status();
  54. var buffer = new Buffer();
  55. c_api.TF_OperationToNodeDef(oper, buffer, s);
  56. s.Check();
  57. var ret = NodeDef.Parser.ParseFrom(buffer);
  58. buffer.Dispose();
  59. s.Dispose();
  60. return ret;
  61. }
  62. public static bool IsAddN(NodeDef node_def, int n)
  63. {
  64. if (node_def.Op != "AddN" || node_def.Name != "add" ||
  65. node_def.Input.Count != n)
  66. {
  67. return false;
  68. }
  69. bool found_t = false;
  70. bool found_n = false;
  71. foreach (var attr in node_def.Attr)
  72. {
  73. if (attr.Key == "T")
  74. {
  75. if (attr.Value.Type == DataType.DtInt32)
  76. {
  77. found_t = true;
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. }
  84. else if (attr.Key == "N")
  85. {
  86. if (attr.Value.I == n)
  87. {
  88. found_n = true;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94. }
  95. }
  96. return found_t && found_n;
  97. }
  98. public static bool IsNeg(NodeDef node_def, string input)
  99. {
  100. return node_def.Op == "Neg" && node_def.Name == "neg" &&
  101. node_def.Input.Count == 1 && node_def.Input[0] == input;
  102. }
  103. public static bool IsPlaceholder(NodeDef node_def)
  104. {
  105. if (node_def.Op != "Placeholder" || node_def.Name != "feed")
  106. {
  107. return false;
  108. }
  109. bool found_dtype = false;
  110. bool found_shape = false;
  111. foreach (var attr in node_def.Attr)
  112. {
  113. if (attr.Key == "dtype")
  114. {
  115. if (attr.Value.Type == DataType.DtInt32)
  116. {
  117. found_dtype = true;
  118. }
  119. else
  120. {
  121. return false;
  122. }
  123. }
  124. else if (attr.Key == "shape")
  125. {
  126. found_shape = true;
  127. }
  128. }
  129. return found_dtype && found_shape;
  130. }
  131. public static bool IsScalarConst(NodeDef node_def, int v)
  132. {
  133. if (node_def.Op != "Const" || node_def.Name != "scalar")
  134. {
  135. return false;
  136. }
  137. bool found_dtype = false;
  138. bool found_value = false;
  139. foreach (var attr in node_def.Attr) {
  140. if (attr.Key == "dtype")
  141. {
  142. if (attr.Value.Type == DataType.DtInt32)
  143. {
  144. found_dtype = true;
  145. }
  146. else
  147. {
  148. return false;
  149. }
  150. }
  151. else if (attr.Key == "value")
  152. {
  153. if (attr.Value.Tensor != null &&
  154. attr.Value.Tensor.IntVal.Count == 1 &&
  155. attr.Value.Tensor.IntVal[0] == v)
  156. {
  157. found_value = true;
  158. }
  159. else
  160. {
  161. return false;
  162. }
  163. }
  164. }
  165. return found_dtype && found_value;
  166. }
  167. public static Operation Neg(Operation n, Graph graph, Status s, string name = "neg")
  168. {
  169. return NegHelper(n, graph, s, name);
  170. }
  171. public static Operation NegHelper(Operation n, Graph graph, Status s, string name)
  172. {
  173. OperationDescription desc = c_api.TF_NewOperation(graph, "Neg", name);
  174. var neg_input = new TF_Output(n, 0);
  175. c_api.TF_AddInput(desc, neg_input);
  176. var op = c_api.TF_FinishOperation(desc, s);
  177. s.Check();
  178. return op;
  179. }
  180. public static void PlaceholderHelper(Graph graph, Status s, string name, TF_DataType dtype, long[] dims, ref Operation op)
  181. {
  182. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  183. c_api.TF_SetAttrType(desc, "dtype", dtype);
  184. if(dims != null)
  185. {
  186. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  187. }
  188. op = c_api.TF_FinishOperation(desc, s);
  189. s.Check();
  190. }
  191. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  192. {
  193. Operation op = null;
  194. PlaceholderHelper(graph, s, name, dtype, dims, ref op);
  195. return op;
  196. }
  197. public static void ConstHelper(Tensor t, Graph graph, Status s, string name, ref Operation op)
  198. {
  199. var desc = c_api.TF_NewOperation(graph, "Const", name);
  200. c_api.TF_SetAttrTensor(desc, "value", t, s);
  201. s.Check();
  202. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  203. op = c_api.TF_FinishOperation(desc, s);
  204. s.Check();
  205. }
  206. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  207. {
  208. Operation op = null;
  209. ConstHelper(t, graph, s, name, ref op);
  210. return op;
  211. }
  212. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  213. {
  214. return Const(new Tensor(v), graph, s, name);
  215. }
  216. }
  217. }

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

Contributors (1)