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.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. using (var s = new Status())
  34. using (var buffer = new Buffer())
  35. {
  36. c_api.TF_GraphToGraphDef(graph, buffer, s);
  37. s.Check();
  38. return GraphDef.Parser.ParseFrom(buffer);
  39. }
  40. }
  41. public static bool IsAddN(NodeDef node_def, int n)
  42. {
  43. if (node_def.Op != "AddN" || node_def.Name != "add" ||
  44. node_def.Input.Count != n)
  45. {
  46. return false;
  47. }
  48. bool found_t = false;
  49. bool found_n = false;
  50. foreach (var attr in node_def.Attr)
  51. {
  52. if (attr.Key == "T")
  53. {
  54. if (attr.Value.Type == DataType.DtInt32)
  55. {
  56. found_t = true;
  57. }
  58. else
  59. {
  60. return false;
  61. }
  62. }
  63. else if (attr.Key == "N")
  64. {
  65. if (attr.Value.I == n)
  66. {
  67. found_n = true;
  68. }
  69. else
  70. {
  71. return false;
  72. }
  73. }
  74. }
  75. return found_t && found_n;
  76. }
  77. public static bool IsNeg(NodeDef node_def, string input)
  78. {
  79. return node_def.Op == "Neg" && node_def.Name == "neg" &&
  80. node_def.Input.Count == 1 && node_def.Input[0] == input;
  81. }
  82. public static bool IsPlaceholder(NodeDef node_def)
  83. {
  84. if (node_def.Op != "Placeholder" || node_def.Name != "feed")
  85. {
  86. return false;
  87. }
  88. bool found_dtype = false;
  89. bool found_shape = false;
  90. foreach (var attr in node_def.Attr)
  91. {
  92. if (attr.Key == "dtype")
  93. {
  94. if (attr.Value.Type == DataType.DtInt32)
  95. {
  96. found_dtype = true;
  97. }
  98. else
  99. {
  100. return false;
  101. }
  102. }
  103. else if (attr.Key == "shape")
  104. {
  105. found_shape = true;
  106. }
  107. }
  108. return found_dtype && found_shape;
  109. }
  110. public static bool IsScalarConst(NodeDef node_def, int v)
  111. {
  112. if (node_def.Op != "Const" || node_def.Name != "scalar")
  113. {
  114. return false;
  115. }
  116. bool found_dtype = false;
  117. bool found_value = false;
  118. foreach (var attr in node_def.Attr) {
  119. if (attr.Key == "dtype")
  120. {
  121. if (attr.Value.Type == DataType.DtInt32)
  122. {
  123. found_dtype = true;
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. else if (attr.Key == "value")
  131. {
  132. if (attr.Value.Tensor != null &&
  133. attr.Value.Tensor.IntVal.Count == 1 &&
  134. attr.Value.Tensor.IntVal[0] == v)
  135. {
  136. found_value = true;
  137. }
  138. else
  139. {
  140. return false;
  141. }
  142. }
  143. }
  144. return found_dtype && found_value;
  145. }
  146. public static Operation Neg(Operation n, Graph graph, Status s, string name = "neg")
  147. {
  148. OperationDescription desc = c_api.TF_NewOperation(graph, "Neg", name);
  149. var neg_input = new TF_Output(n, 0);
  150. c_api.TF_AddInput(desc, neg_input);
  151. var op = c_api.TF_FinishOperation(desc, s);
  152. s.Check();
  153. return op;
  154. }
  155. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  156. {
  157. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  158. c_api.TF_SetAttrType(desc, "dtype", dtype);
  159. if (dims != null)
  160. {
  161. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  162. }
  163. var op = c_api.TF_FinishOperation(desc, s);
  164. s.Check();
  165. return op;
  166. }
  167. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  168. {
  169. var desc = c_api.TF_NewOperation(graph, "Const", name);
  170. c_api.TF_SetAttrTensor(desc, "value", t, s);
  171. s.Check();
  172. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  173. var op = c_api.TF_FinishOperation(desc, s);
  174. s.Check();
  175. return op;
  176. }
  177. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  178. {
  179. return Const(new Tensor(v), graph, s, name);
  180. }
  181. }
  182. }