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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 NodeDef GetNodeDef(Operation oper)
  47. {
  48. var s = new Status();
  49. var buffer = new Buffer();
  50. c_api.TF_OperationToNodeDef(oper, buffer, s);
  51. s.Check();
  52. var ret = NodeDef.Parser.ParseFrom(buffer);
  53. buffer.Dispose();
  54. s.Dispose();
  55. return ret;
  56. }
  57. public static bool IsAddN(NodeDef node_def, int n)
  58. {
  59. if (node_def.Op != "AddN" || node_def.Name != "add" ||
  60. node_def.Input.Count != n)
  61. {
  62. return false;
  63. }
  64. bool found_t = false;
  65. bool found_n = false;
  66. foreach (var attr in node_def.Attr)
  67. {
  68. if (attr.Key == "T")
  69. {
  70. if (attr.Value.Type == DataType.DtInt32)
  71. {
  72. found_t = true;
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79. else if (attr.Key == "N")
  80. {
  81. if (attr.Value.I == n)
  82. {
  83. found_n = true;
  84. }
  85. else
  86. {
  87. return false;
  88. }
  89. }
  90. }
  91. return found_t && found_n;
  92. }
  93. public static bool IsNeg(NodeDef node_def, string input)
  94. {
  95. return node_def.Op == "Neg" && node_def.Name == "neg" &&
  96. node_def.Input.Count == 1 && node_def.Input[0] == input;
  97. }
  98. public static bool IsPlaceholder(NodeDef node_def)
  99. {
  100. if (node_def.Op != "Placeholder" || node_def.Name != "feed")
  101. {
  102. return false;
  103. }
  104. bool found_dtype = false;
  105. bool found_shape = false;
  106. foreach (var attr in node_def.Attr)
  107. {
  108. if (attr.Key == "dtype")
  109. {
  110. if (attr.Value.Type == DataType.DtInt32)
  111. {
  112. found_dtype = true;
  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. }
  119. else if (attr.Key == "shape")
  120. {
  121. found_shape = true;
  122. }
  123. }
  124. return found_dtype && found_shape;
  125. }
  126. public static bool IsScalarConst(NodeDef node_def, int v)
  127. {
  128. if (node_def.Op != "Const" || node_def.Name != "scalar")
  129. {
  130. return false;
  131. }
  132. bool found_dtype = false;
  133. bool found_value = false;
  134. foreach (var attr in node_def.Attr) {
  135. if (attr.Key == "dtype")
  136. {
  137. if (attr.Value.Type == DataType.DtInt32)
  138. {
  139. found_dtype = true;
  140. }
  141. else
  142. {
  143. return false;
  144. }
  145. }
  146. else if (attr.Key == "value")
  147. {
  148. if (attr.Value.Tensor != null &&
  149. attr.Value.Tensor.IntVal.Count == 1 &&
  150. attr.Value.Tensor.IntVal[0] == v)
  151. {
  152. found_value = true;
  153. }
  154. else
  155. {
  156. return false;
  157. }
  158. }
  159. }
  160. return found_dtype && found_value;
  161. }
  162. public static Operation Neg(Operation n, Graph graph, Status s, string name = "neg")
  163. {
  164. return NegHelper(n, graph, s, name);
  165. }
  166. public static Operation NegHelper(Operation n, Graph graph, Status s, string name)
  167. {
  168. OperationDescription desc = c_api.TF_NewOperation(graph, "Neg", name);
  169. var neg_input = new TF_Output(n, 0);
  170. c_api.TF_AddInput(desc, neg_input);
  171. var op = c_api.TF_FinishOperation(desc, s);
  172. s.Check();
  173. return op;
  174. }
  175. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  176. {
  177. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  178. c_api.TF_SetAttrType(desc, "dtype", dtype);
  179. if (dims != null)
  180. {
  181. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  182. }
  183. var op = c_api.TF_FinishOperation(desc, s);
  184. s.Check();
  185. return op;
  186. }
  187. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  188. {
  189. var desc = c_api.TF_NewOperation(graph, "Const", name);
  190. c_api.TF_SetAttrTensor(desc, "value", t, s);
  191. s.Check();
  192. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  193. var op = c_api.TF_FinishOperation(desc, s);
  194. s.Check();
  195. return op;
  196. }
  197. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  198. {
  199. return Const(new Tensor(v), graph, s, name);
  200. }
  201. public static unsafe IntPtr Int32Tensor(int v)
  202. {
  203. bool deallocator_called = false;
  204. const int num_bytes = sizeof(int);
  205. var dotHandle = Marshal.AllocHGlobal(num_bytes * 1);
  206. *(int*)dotHandle = v;
  207. return c_api.TF_NewTensor(TF_DataType.TF_INT32, new long[0], 0, dotHandle, num_bytes,
  208. (IntPtr values, IntPtr len, ref bool closure) =>
  209. {
  210. // Free the original buffer and set flag
  211. // Marshal.FreeHGlobal(dotHandle);
  212. }, ref deallocator_called);
  213. }
  214. }
  215. }

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

Contributors (1)