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.

TestCrop.cs 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using FluentAssertions;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using NumSharp;
  4. using Tensorflow;
  5. using static Tensorflow.Binding;
  6. namespace TensorFlowNET.UnitTest.img_test
  7. {
  8. [TestClass]
  9. public class TestCrop
  10. {
  11. [TestMethod]
  12. public void TestCropAndResize()
  13. {
  14. // 3x3 'Image' with numbered coordinates
  15. var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f);
  16. var image = tf.reshape(input, new int[] { 1, 3, 3, 1 });
  17. // 4x4 'Image' with numbered coordinates
  18. var input2 = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f);
  19. var image2 = tf.reshape(input2, new int[] { 1, 4, 4, 1 });
  20. // create one box over the full image that flips it (y1 > y2)
  21. var box = tf.reshape(np.array(1f, 0f, 0f, 1f), new int[] {1, 4});
  22. var boxInd = tf.Variable(np.array(0));
  23. // crop first 3x3 imageto size 1x1
  24. var cropSize1_1 = tf.Variable(np.array(1, 1));
  25. // don't crop second 4x4 image
  26. var cropSize2_2 = tf.Variable(np.array(4, 4));
  27. var init = tf.global_variables_initializer();
  28. using (Session sess = tf.Session())
  29. {
  30. sess.run(init);
  31. var cropped = tf.image.crop_and_resize(image, box, boxInd, cropSize1_1);
  32. var result = sess.run(cropped);
  33. // check if cropped to 1x1 center was succesfull
  34. result.size.Should().Be(1);
  35. result[0, 0, 0, 0].Should().Be(4f);
  36. cropped = tf.image.crop_and_resize(image2, box, boxInd, cropSize2_2);
  37. result = sess.run(cropped);
  38. // check if flipped and no cropping occured
  39. result.size.Should().Be(16);
  40. result[0, 0, 0, 0].Should().Be(12f);
  41. }
  42. }
  43. }
  44. }