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

5 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 img = tf.image.adjust_contrast(image, 2.0f);
  29. var res = np.array(-4f, -2f, 0f, 2f, 4f, 6f, 8f, 10f, 12f).reshape((3,3,1));
  30. Assert.AreEqual(img.numpy(), res);
  31. }
  32. [Ignore]
  33. [TestMethod]
  34. public void adjust_hue()
  35. {
  36. var image = tf.constant(new int[] {1,2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18});
  37. image = tf.reshape(image, new int[] { 3, 2, 3 });
  38. var adjusted_image = tf.image.adjust_hue(image, 0.2f);
  39. var res = tf.constant(new int[] {2,1,3, 4, 5, 6,8,7,9,11,10,12,14,13,15,17,16,18});
  40. res = tf.reshape(res,(3,2,3));
  41. Assert.AreEqual(adjusted_image, res);
  42. }
  43. [TestMethod]
  44. public void combined_non_max_suppression()
  45. {
  46. var boxesX = tf.constant(new float[,] { { 200, 100, 150, 100 }, { 220, 120, 150, 100 }, { 190, 110, 150, 100 },{ 210, 112, 150, 100 } });
  47. var boxes1 = tf.reshape(boxesX, (1, 4, 1, 4));
  48. 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 } });
  49. var scores1 = tf.reshape(scoresX, (1, 4, 3));
  50. var (boxes, scores, classes, valid_detections) = tf.image.combined_non_max_suppression(boxes1, scores1, 10, 10, 0.5f, 0.2f, clip_boxes:false);
  51. var boxes_gt = tf.constant(new float[,] { { 210f, 112f, 150f, 100f }, { 200f, 100f, 150f, 100f }, { 190f, 110f, 150f, 100f },
  52. { 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} });
  53. boxes_gt = tf.reshape(boxes_gt,(1, 10, 4));
  54. Assert.AreEqual(boxes.numpy(), boxes_gt.numpy());
  55. var scores_gt = tf.constant(new float[,] { { 0.9f, 0.7f, 0.3f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } });
  56. scores_gt = tf.reshape(scores_gt, (1, 10));
  57. Assert.AreEqual(scores.numpy(), scores_gt.numpy());
  58. var classes_gt = tf.constant(new float[,] { { 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } });
  59. classes_gt = tf.reshape(classes_gt, (1, 10));
  60. Assert.AreEqual(classes.numpy(), classes_gt.numpy());
  61. var valid_detections_gt = tf.constant(new int[,] { { 3 } });
  62. valid_detections_gt = tf.reshape(valid_detections_gt, (1));
  63. Assert.AreEqual(valid_detections.numpy(), valid_detections_gt.numpy());
  64. }
  65. [TestMethod]
  66. public void crop_and_resize()
  67. {
  68. int BATCH_SIZE = 1;
  69. int NUM_BOXES = 5;
  70. int IMAGE_HEIGHT = 256;
  71. int IMAGE_WIDTH = 256;
  72. int CHANNELS = 3;
  73. var crop_size = tf.constant(new int[] { 24, 24 });
  74. var image = tf.random.uniform((BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS));
  75. var boxes = tf.random.uniform((NUM_BOXES, 4));
  76. var box_ind = tf.random.uniform((NUM_BOXES), minval: 0, maxval: BATCH_SIZE, dtype: TF_DataType.TF_INT32);
  77. var output = tf.image.crop_and_resize(image, boxes, box_ind, crop_size);
  78. Assert.AreEqual((5,24,24,3), output.shape);
  79. }
  80. [TestMethod]
  81. public void decode_image()
  82. {
  83. var img = tf.image.decode_image(contents);
  84. Assert.AreEqual(img.name, "decode_image/DecodeImage:0");
  85. }
  86. [TestMethod]
  87. public void resize_image()
  88. {
  89. tf.enable_eager_execution();
  90. var image = tf.constant(new int[5, 5]
  91. {
  92. {1, 0, 0, 0, 0 },
  93. {0, 1, 0, 0, 0 },
  94. {0, 0, 1, 0, 0 },
  95. {0, 0, 0, 1, 0 },
  96. {0, 0, 0, 0, 1 }
  97. });
  98. image = image[tf.newaxis, tf.ellipsis, tf.newaxis];
  99. image = tf.image.resize(image, (3, 5));
  100. image = image[0, tf.ellipsis, 0];
  101. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0.6666667f, 0.3333333f, 0, 0, 0 },
  102. image[0].ToArray<float>()));
  103. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0, 0, 1, 0, 0 },
  104. image[1].ToArray<float>()));
  105. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0, 0, 0, 0.3333335f, 0.6666665f },
  106. image[2].ToArray<float>()));
  107. tf.compat.v1.disable_eager_execution();
  108. }
  109. [TestMethod]
  110. public void TestCropAndResize()
  111. {
  112. var graph = tf.Graph().as_default();
  113. // 3x3 'Image' with numbered coordinates
  114. var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f);
  115. var image = tf.reshape(input, new int[] { 1, 3, 3, 1 });
  116. // 4x4 'Image' with numbered coordinates
  117. var input2 = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f);
  118. var image2 = tf.reshape(input2, new int[] { 1, 4, 4, 1 });
  119. // create one box over the full image that flips it (y1 > y2)
  120. var box = tf.reshape(np.array(1f, 0f, 0f, 1f), new int[] { 1, 4 });
  121. var boxInd = tf.Variable(np.array(0));
  122. // crop first 3x3 imageto size 1x1
  123. var cropSize1_1 = tf.Variable(np.array(1, 1));
  124. // don't crop second 4x4 image
  125. var cropSize2_2 = tf.Variable(np.array(4, 4));
  126. var init = tf.global_variables_initializer();
  127. var sess = tf.Session();
  128. sess.run(init);
  129. var cropped = tf.image.crop_and_resize(image, box, boxInd, cropSize1_1);
  130. var result = sess.run(cropped);
  131. // check if cropped to 1x1 center was succesfull
  132. Assert.AreEqual(result.size, 1ul);
  133. Assert.AreEqual(result[0, 0, 0, 0], 4f);
  134. cropped = tf.image.crop_and_resize(image2, box, boxInd, cropSize2_2);
  135. result = sess.run(cropped);
  136. // check if flipped and no cropping occured
  137. Assert.AreEqual(result.size, 16ul);
  138. Assert.AreEqual(result[0, 0, 0, 0], 12f);
  139. }
  140. }
  141. }