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

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. return GraphDef.Parser.ParseFrom(buffer);
  47. }
  48. public static bool GetNodeDef(Operation oper, ref NodeDef node_def)
  49. {
  50. var s = new Status();
  51. var buffer = new Buffer();
  52. c_api.TF_OperationToNodeDef(oper, buffer, s);
  53. return s.Code == TF_Code.TF_OK;
  54. }
  55. public static bool IsPlaceholder(NodeDef node_def)
  56. {
  57. if (node_def.Op != "Placeholder" || node_def.Name != "feed")
  58. {
  59. return false;
  60. }
  61. bool found_dtype = false;
  62. bool found_shape = false;
  63. foreach (var attr in node_def.Attr)
  64. {
  65. if (attr.Key == "dtype")
  66. {
  67. if (attr.Value.Type == DataType.DtInt32)
  68. {
  69. found_dtype = true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. else if (attr.Key == "shape")
  77. {
  78. found_shape = true;
  79. }
  80. }
  81. return found_dtype && found_shape;
  82. }
  83. public static void PlaceholderHelper(Graph graph, Status s, string name, TF_DataType dtype, long[] dims, ref Operation op)
  84. {
  85. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  86. c_api.TF_SetAttrType(desc, "dtype", dtype);
  87. if(dims != null)
  88. {
  89. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  90. }
  91. op = c_api.TF_FinishOperation(desc, s);
  92. s.Check();
  93. }
  94. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  95. {
  96. Operation op = null;
  97. PlaceholderHelper(graph, s, name, dtype, dims, ref op);
  98. return op;
  99. }
  100. public static void ConstHelper(Tensor t, Graph graph, Status s, string name, ref Operation op)
  101. {
  102. var desc = c_api.TF_NewOperation(graph, "Const", name);
  103. c_api.TF_SetAttrTensor(desc, "value", t, s);
  104. s.Check();
  105. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  106. op = c_api.TF_FinishOperation(desc, s);
  107. s.Check();
  108. }
  109. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  110. {
  111. Operation op = null;
  112. ConstHelper(t, graph, s, name, ref op);
  113. return op;
  114. }
  115. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  116. {
  117. return Const(new Tensor(v), graph, s, name);
  118. }
  119. }
  120. }

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

Contributors (1)