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.

ImageTest.cs 7.4 kB

5 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Tensorflow.NumPy;
  3. using System.Linq;
  4. using Tensorflow;
  5. using static Tensorflow.Binding;
  6. using System;
  7. namespace TensorFlowNET.UnitTest
  8. {
  9. /// <summary>
  10. /// Find more examples in https://www.programcreek.com/python/example/90444/tensorflow.read_file
  11. /// </summary>
  12. [TestClass]
  13. public class ImageTest : GraphModeTestBase
  14. {
  15. string imgPath = "shasta-daisy.jpg";
  16. Tensor contents;
  17. [TestInitialize]
  18. public void Initialize()
  19. {
  20. imgPath = TestHelper.GetFullPathFromDataDir(imgPath);
  21. contents = tf.io.read_file(imgPath);
  22. }
  23. [TestMethod]
  24. public void adjust_contrast()
  25. {
  26. var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f);
  27. var image = tf.reshape(input, new int[] { 3, 3, 1 });
  28. var init = tf.global_variables_initializer();
  29. var sess = tf.Session();
  30. sess.run(init);
  31. var adjust_contrast = tf.image.adjust_contrast(image, 2.0f);
  32. var result = sess.run(adjust_contrast);
  33. var res = np.array(-4f, -2f, 0f, 2f, 4f, 6f, 8f, 10f, 12f).reshape((3,3,1));
  34. Assert.AreEqual(result.numpy(), res);
  35. }
  36. [Ignore]
  37. [TestMethod]
  38. public void adjust_hue()
  39. {
  40. var image = tf.constant(new int[] {1,2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18});
  41. image = tf.reshape(image, new int[] { 3, 2, 3 });
  42. var adjusted_image = tf.image.adjust_hue(image, 0.2f);
  43. var res = tf.constant(new int[] {2,1,3, 4, 5, 6,8,7,9,11,10,12,14,13,15,17,16,18});
  44. res = tf.reshape(res,(3,2,3));
  45. Assert.AreEqual(adjusted_image, res);
  46. }
  47. [TestMethod]
  48. public void combined_non_max_suppression()
  49. {
  50. var boxesX = tf.constant(new float[,] { { 200, 100, 150, 100 }, { 220, 120, 150, 100 }, { 190, 110, 150, 100 }, { 210, 112, 150, 100 } });
  51. var boxes1 = tf.reshape(boxesX, (1, 4, 1, 4));
  52. var scoresX = tf.constant(new float[,] { { 0.2f, 0.7f, 0.1f }, { 0.1f, 0.8f, 0.1f }, { 0.3f, 0.6f, 0.1f }, { 0.05f, 0.9f, 0.05f } });
  53. var scores1 = tf.reshape(scoresX, (1, 4, 3));
  54. var init = tf.global_variables_initializer();
  55. var sess = tf.Session();
  56. sess.run(init);
  57. var (boxes, scores, classes, valid_detections) = tf.image.combined_non_max_suppression(boxes1, scores1, 10, 10, 0.5f, 0.2f, clip_boxes: false);
  58. var result = sess.run((boxes, scores, classes, valid_detections));
  59. var boxes_gt = tf.constant(new float[,] { { 210f, 112f, 150f, 100f }, { 200f, 100f, 150f, 100f }, { 190f, 110f, 150f, 100f },
  60. { 0f, 0f, 0f, 0f},{ 0f, 0f, 0f, 0f},{ 0f, 0f, 0f, 0f},{ 0f, 0f, 0f , 0f},{ 0f, 0f, 0f, 0f},{ 0f , 0f, 0f, 0f},{ 0f, 0f, 0f, 0f} });
  61. boxes_gt = tf.reshape(boxes_gt, (1, 10, 4));
  62. Assert.AreEqual(result.Item1.numpy(), boxes_gt.numpy());
  63. var scores_gt = tf.constant(new float[,] { { 0.9f, 0.7f, 0.3f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } });
  64. scores_gt = tf.reshape(scores_gt, (1, 10));
  65. Assert.AreEqual(result.Item2.numpy(), scores_gt.numpy());
  66. var classes_gt = tf.constant(new float[,] { { 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } });
  67. classes_gt = tf.reshape(classes_gt, (1, 10));
  68. Assert.AreEqual(result.Item3.numpy(), classes_gt.numpy());
  69. var valid_detections_gt = tf.constant(new int[,] { { 3 } });
  70. valid_detections_gt = tf.reshape(valid_detections_gt, (1));
  71. Assert.AreEqual(result.Item4.numpy(), valid_detections_gt.numpy());
  72. }
  73. [TestMethod]
  74. public void crop_and_resize()
  75. {
  76. int BATCH_SIZE = 1;
  77. int NUM_BOXES = 5;
  78. int IMAGE_HEIGHT = 256;
  79. int IMAGE_WIDTH = 256;
  80. int CHANNELS = 3;
  81. var crop_size = tf.constant(new int[] { 24, 24 });
  82. var image = tf.random.uniform((BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS));
  83. var boxes = tf.random.uniform((NUM_BOXES, 4));
  84. var box_ind = tf.random.uniform((NUM_BOXES), minval: 0, maxval: BATCH_SIZE, dtype: TF_DataType.TF_INT32);
  85. var output = tf.image.crop_and_resize(image, boxes, box_ind, crop_size);
  86. Assert.AreEqual((5,24,24,3), output.shape);
  87. }
  88. [TestMethod]
  89. public void decode_image()
  90. {
  91. var img = tf.image.decode_image(contents);
  92. Assert.AreEqual(img.name, "decode_image/DecodeImage:0");
  93. }
  94. [TestMethod]
  95. public void resize_image()
  96. {
  97. tf.enable_eager_execution();
  98. var image = tf.constant(new int[5, 5]
  99. {
  100. {1, 0, 0, 0, 0 },
  101. {0, 1, 0, 0, 0 },
  102. {0, 0, 1, 0, 0 },
  103. {0, 0, 0, 1, 0 },
  104. {0, 0, 0, 0, 1 }
  105. });
  106. image = image[tf.newaxis, tf.ellipsis, tf.newaxis];
  107. image = tf.image.resize(image, (3, 5));
  108. image = image[0, tf.ellipsis, 0];
  109. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0.6666667f, 0.3333333f, 0, 0, 0 },
  110. image[0].ToArray<float>()));
  111. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0, 0, 1, 0, 0 },
  112. image[1].ToArray<float>()));
  113. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0, 0, 0, 0.3333335f, 0.6666665f },
  114. image[2].ToArray<float>()));
  115. tf.compat.v1.disable_eager_execution();
  116. }
  117. [TestMethod]
  118. public void TestCropAndResize()
  119. {
  120. var graph = tf.Graph().as_default();
  121. // 3x3 'Image' with numbered coordinates
  122. var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f);
  123. var image = tf.reshape(input, new int[] { 1, 3, 3, 1 });
  124. // 4x4 'Image' with numbered coordinates
  125. var input2 = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f);
  126. var image2 = tf.reshape(input2, new int[] { 1, 4, 4, 1 });
  127. // create one box over the full image that flips it (y1 > y2)
  128. var box = tf.reshape(np.array(1f, 0f, 0f, 1f), new int[] { 1, 4 });
  129. var boxInd = tf.Variable(np.array(0));
  130. // crop first 3x3 imageto size 1x1
  131. var cropSize1_1 = tf.Variable(np.array(1, 1));
  132. // don't crop second 4x4 image
  133. var cropSize2_2 = tf.Variable(np.array(4, 4));
  134. var init = tf.global_variables_initializer();
  135. var sess = tf.Session();
  136. sess.run(init);
  137. var cropped = tf.image.crop_and_resize(image, box, boxInd, cropSize1_1);
  138. var result = sess.run(cropped);
  139. // check if cropped to 1x1 center was succesfull
  140. Assert.AreEqual(result.size, 1ul);
  141. Assert.AreEqual(result[0, 0, 0, 0], 4f);
  142. cropped = tf.image.crop_and_resize(image2, box, boxInd, cropSize2_2);
  143. result = sess.run(cropped);
  144. // check if flipped and no cropping occured
  145. Assert.AreEqual(result.size, 16ul);
  146. Assert.AreEqual(result[0, 0, 0, 0], 12f);
  147. }
  148. }
  149. }