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