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