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.

TensorTest.cs 7.2 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using NumSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Tensorflow;
  11. using static Tensorflow.Python;
  12. namespace TensorFlowNET.UnitTest
  13. {
  14. [TestClass]
  15. public class TensorTest : CApiTest
  16. {
  17. [TestMethod]
  18. public void TensorDeallocationThreadSafety()
  19. {
  20. var tensors = new Tensor[1000];
  21. foreach (var i in range(1000))
  22. {
  23. tensors[i] = new Tensor(new int[1000]);
  24. }
  25. SemaphoreSlim s = new SemaphoreSlim(0, 2);
  26. SemaphoreSlim s_done = new SemaphoreSlim(0, 2);
  27. var t1 = new Thread(() =>
  28. {
  29. s.Wait();
  30. foreach (var t in tensors)
  31. t.Dispose();
  32. s_done.Release();
  33. });
  34. var t2 = new Thread(() =>
  35. {
  36. s.Wait();
  37. foreach (var t in tensors)
  38. t.Dispose();
  39. s_done.Release();
  40. });
  41. t1.Start();
  42. t2.Start();
  43. s.Release(2);
  44. s_done.Wait();
  45. s_done.Wait();
  46. foreach(var t in tensors)
  47. Assert.IsTrue(t.IsDisposed);
  48. }
  49. [TestMethod]
  50. public void AllocateTensor()
  51. {
  52. /*ulong num_bytes = 6 * sizeof(float);
  53. long[] dims = { 2, 3 };
  54. Tensor t = c_api.TF_AllocateTensor(TF_DataType.TF_FLOAT, dims, 2, num_bytes);
  55. EXPECT_EQ(TF_DataType.TF_FLOAT, t.dtype);
  56. EXPECT_EQ(2, t.NDims);
  57. Assert.IsTrue(Enumerable.SequenceEqual(dims, t.shape));
  58. EXPECT_EQ(num_bytes, t.bytesize);
  59. t.Dispose();*/
  60. }
  61. /// <summary>
  62. /// Port from c_api_test.cc
  63. /// `TEST(CAPI, MaybeMove)`
  64. /// </summary>
  65. [TestMethod]
  66. public void MaybeMove()
  67. {
  68. NDArray nd = np.array(2, 3);
  69. Tensor t = new Tensor(nd);
  70. Tensor o = t.MaybeMove();
  71. ASSERT_TRUE(o == IntPtr.Zero); // It is unsafe to move memory TF might not own.
  72. t.Dispose();
  73. }
  74. /// <summary>
  75. /// Port from c_api_test.cc
  76. /// `TEST(CAPI, Tensor)`
  77. /// </summary>
  78. [TestMethod]
  79. public void Tensor()
  80. {
  81. var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape(2, 3);
  82. var tensor = new Tensor(nd);
  83. var array = tensor.Data<float>();
  84. EXPECT_EQ(tensor.dtype, TF_DataType.TF_FLOAT);
  85. EXPECT_EQ(tensor.rank, nd.ndim);
  86. EXPECT_EQ((int)tensor.shape[0], nd.shape[0]);
  87. EXPECT_EQ((int)tensor.shape[1], nd.shape[1]);
  88. EXPECT_EQ(tensor.bytesize, (ulong)nd.size * sizeof(float));
  89. Assert.IsTrue(Enumerable.SequenceEqual(nd.Data<float>(), new float[] { 1, 2, 3, 4, 5, 6 }));
  90. }
  91. /// <summary>
  92. /// Port from tensorflow\c\c_api_test.cc
  93. /// `TEST(CAPI, SetShape)`
  94. /// </summary>
  95. [TestMethod]
  96. public void SetShape()
  97. {
  98. var s = new Status();
  99. var graph = new Graph();
  100. var feed = c_test_util.Placeholder(graph, s);
  101. var feed_out_0 = new TF_Output(feed, 0);
  102. // Fetch the shape, it should be completely unknown.
  103. int num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s);
  104. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  105. EXPECT_EQ(-1, num_dims);
  106. // Set the shape to be unknown, expect no change.
  107. c_api.TF_GraphSetTensorShape(graph, feed_out_0, null, -1, s);
  108. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  109. num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s);
  110. EXPECT_EQ(-1, num_dims);
  111. // Set the shape to be 2 x Unknown
  112. long[] dims = { 2, -1 };
  113. c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, dims.Length, s);
  114. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  115. num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s);
  116. EXPECT_EQ(2, num_dims);
  117. // Get the dimension vector appropriately.
  118. var returned_dims = new long[dims.Length];
  119. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s);
  120. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  121. Assert.IsTrue(Enumerable.SequenceEqual(dims, returned_dims));
  122. // Set to a new valid shape: [2, 3]
  123. dims[1] = 3;
  124. c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, dims.Length, s);
  125. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  126. // Fetch and see that the new value is returned.
  127. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s);
  128. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  129. Assert.IsTrue(Enumerable.SequenceEqual(dims, returned_dims));
  130. // Try to set 'unknown' with unknown rank on the shape and see that
  131. // it doesn't change.
  132. c_api.TF_GraphSetTensorShape(graph, feed_out_0, null, -1, s);
  133. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  134. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s);
  135. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  136. EXPECT_EQ(2, num_dims);
  137. EXPECT_EQ(2, (int)returned_dims[0]);
  138. EXPECT_EQ(3, (int)returned_dims[1]);
  139. // Try to set 'unknown' with same rank on the shape and see that
  140. // it doesn't change.
  141. dims[0] = -1;
  142. dims[1] = -1;
  143. c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, 2, s);
  144. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  145. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s);
  146. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  147. EXPECT_EQ(2, num_dims);
  148. EXPECT_EQ(2, (int)returned_dims[0]);
  149. EXPECT_EQ(3, (int)returned_dims[1]);
  150. // Try to fetch a shape with the wrong num_dims
  151. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, 5, s);
  152. Assert.IsTrue(s.Code == TF_Code.TF_INVALID_ARGUMENT);
  153. // Try to set an invalid shape (cannot change 2x3 to a 2x5).
  154. dims[1] = 5;
  155. c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, 2, s);
  156. Assert.IsTrue(s.Code == TF_Code.TF_INVALID_ARGUMENT);
  157. // Test for a scalar.
  158. var three = c_test_util.ScalarConst(3, graph, s);
  159. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  160. var three_out_0 = new TF_Output(three, 0);
  161. num_dims = c_api.TF_GraphGetTensorNumDims(graph, three_out_0, s);
  162. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  163. EXPECT_EQ(0, num_dims);
  164. c_api.TF_GraphGetTensorShape(graph, feed_out_0, null, num_dims, s);
  165. //Assert.IsTrue(s.Code == TF_Code.TF_OK);
  166. // graph.Dispose();
  167. s.Dispose();
  168. }
  169. }
  170. }