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