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.8 kB

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