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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. var graph = tf.Graph().as_default();
  15. // 3x3 'Image' with numbered coordinates
  16. var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f);
  17. var image = tf.reshape(input, new int[] { 1, 3, 3, 1 });
  18. // 4x4 'Image' with numbered coordinates
  19. var input2 = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f);
  20. var image2 = tf.reshape(input2, new int[] { 1, 4, 4, 1 });
  21. // create one box over the full image that flips it (y1 > y2)
  22. var box = tf.reshape(np.array(1f, 0f, 0f, 1f), new int[] {1, 4});
  23. var boxInd = tf.Variable(np.array(0));
  24. // crop first 3x3 imageto size 1x1
  25. var cropSize1_1 = tf.Variable(np.array(1, 1));
  26. // don't crop second 4x4 image
  27. var cropSize2_2 = tf.Variable(np.array(4, 4));
  28. var init = tf.global_variables_initializer();
  29. using (Session sess = tf.Session())
  30. {
  31. sess.run(init);
  32. var cropped = tf.image.crop_and_resize(image, box, boxInd, cropSize1_1);
  33. var result = sess.run(cropped);
  34. // check if cropped to 1x1 center was succesfull
  35. result.size.Should().Be(1);
  36. result[0, 0, 0, 0].Should().Be(4f);
  37. cropped = tf.image.crop_and_resize(image2, box, boxInd, cropSize2_2);
  38. result = sess.run(cropped);
  39. // check if flipped and no cropping occured
  40. result.size.Should().Be(16);
  41. result[0, 0, 0, 0].Should().Be(12f);
  42. }
  43. }
  44. }
  45. }