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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using FluentAssertions;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using NumSharp;
  4. using System.Linq;
  5. using Tensorflow;
  6. using Tensorflow.UnitTest;
  7. using static Tensorflow.Binding;
  8. namespace TensorFlowNET.UnitTest.Basics
  9. {
  10. /// <summary>
  11. /// Find more examples in https://www.programcreek.com/python/example/90444/tensorflow.read_file
  12. /// </summary>
  13. [TestClass]
  14. public class ImageTest : GraphModeTestBase
  15. {
  16. string imgPath = "shasta-daisy.jpg";
  17. Tensor contents;
  18. [TestInitialize]
  19. public void Initialize()
  20. {
  21. imgPath = TestHelper.GetFullPathFromDataDir(imgPath);
  22. contents = tf.io.read_file(imgPath);
  23. }
  24. [TestMethod]
  25. public void decode_image()
  26. {
  27. var img = tf.image.decode_image(contents);
  28. Assert.AreEqual(img.name, "decode_image/cond_jpeg/Merge:0");
  29. }
  30. [TestMethod]
  31. public void resize_image()
  32. {
  33. tf.enable_eager_execution();
  34. var image = tf.constant(new int[5, 5]
  35. {
  36. {1, 0, 0, 0, 0 },
  37. {0, 1, 0, 0, 0 },
  38. {0, 0, 1, 0, 0 },
  39. {0, 0, 0, 1, 0 },
  40. {0, 0, 0, 0, 1 }
  41. });
  42. image = image[tf.newaxis, tf.ellipsis, tf.newaxis];
  43. image = tf.image.resize(image, (3, 5));
  44. image = image[0, tf.ellipsis, 0];
  45. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0.6666667f, 0.3333333f, 0, 0, 0 },
  46. image[0].ToArray<float>()));
  47. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0, 0, 1, 0, 0 },
  48. image[1].ToArray<float>()));
  49. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0, 0, 0, 0.3333335f, 0.6666665f },
  50. image[2].ToArray<float>()));
  51. tf.compat.v1.disable_eager_execution();
  52. }
  53. [TestMethod]
  54. public void TestCropAndResize()
  55. {
  56. var graph = tf.Graph().as_default();
  57. // 3x3 'Image' with numbered coordinates
  58. var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f);
  59. var image = tf.reshape(input, new int[] { 1, 3, 3, 1 });
  60. // 4x4 'Image' with numbered coordinates
  61. var input2 = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f);
  62. var image2 = tf.reshape(input2, new int[] { 1, 4, 4, 1 });
  63. // create one box over the full image that flips it (y1 > y2)
  64. var box = tf.reshape(np.array(1f, 0f, 0f, 1f), new int[] { 1, 4 });
  65. var boxInd = tf.Variable(np.array(0));
  66. // crop first 3x3 imageto size 1x1
  67. var cropSize1_1 = tf.Variable(np.array(1, 1));
  68. // don't crop second 4x4 image
  69. var cropSize2_2 = tf.Variable(np.array(4, 4));
  70. var init = tf.global_variables_initializer();
  71. using (Session sess = tf.Session())
  72. {
  73. sess.run(init);
  74. var cropped = tf.image.crop_and_resize(image, box, boxInd, cropSize1_1);
  75. var result = sess.run(cropped);
  76. // check if cropped to 1x1 center was succesfull
  77. result.size.Should().Be(1);
  78. result[0, 0, 0, 0].Should().Be(4f);
  79. cropped = tf.image.crop_and_resize(image2, box, boxInd, cropSize2_2);
  80. result = sess.run(cropped);
  81. // check if flipped and no cropping occured
  82. result.size.Should().Be(16);
  83. result[0, 0, 0, 0].Should().Be(12f);
  84. }
  85. }
  86. }
  87. }