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

5 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Tensorflow.NumPy;
  3. using System.Linq;
  4. using Tensorflow;
  5. using static Tensorflow.Binding;
  6. using System;
  7. using System.IO;
  8. namespace TensorFlowNET.UnitTest
  9. {
  10. /// <summary>
  11. /// Find more examples in https://www.programcreek.com/python/example/90444/tensorflow.read_file
  12. /// </summary>
  13. [TestClass]
  14. public class ImageTest : GraphModeTestBase
  15. {
  16. string imgPath = "shasta-daisy.jpg";
  17. Tensor contents;
  18. [TestInitialize]
  19. public void Initialize()
  20. {
  21. imgPath = TestHelper.GetFullPathFromDataDir(imgPath);
  22. contents = tf.io.read_file(imgPath);
  23. }
  24. [TestMethod]
  25. public void adjust_contrast()
  26. {
  27. var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f);
  28. var image = tf.reshape(input, new int[] { 3, 3, 1 });
  29. var init = tf.global_variables_initializer();
  30. var sess = tf.Session();
  31. sess.run(init);
  32. var adjust_contrast = tf.image.adjust_contrast(image, 2.0f);
  33. var result = sess.run(adjust_contrast);
  34. var res = np.array(-4f, -2f, 0f, 2f, 4f, 6f, 8f, 10f, 12f).reshape((3,3,1));
  35. Assert.AreEqual(result.numpy(), res);
  36. }
  37. [Ignore]
  38. [TestMethod]
  39. public void adjust_hue()
  40. {
  41. var image = tf.constant(new int[] {1,2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18});
  42. image = tf.reshape(image, new int[] { 3, 2, 3 });
  43. var adjusted_image = tf.image.adjust_hue(image, 0.2f);
  44. var res = tf.constant(new int[] {2,1,3, 4, 5, 6,8,7,9,11,10,12,14,13,15,17,16,18});
  45. res = tf.reshape(res,(3,2,3));
  46. Assert.AreEqual(adjusted_image, res);
  47. }
  48. [TestMethod]
  49. public void combined_non_max_suppression()
  50. {
  51. var boxesX = tf.constant(new float[,] { { 200, 100, 150, 100 }, { 220, 120, 150, 100 }, { 190, 110, 150, 100 }, { 210, 112, 150, 100 } });
  52. var boxes1 = tf.reshape(boxesX, (1, 4, 1, 4));
  53. var scoresX = tf.constant(new float[,] { { 0.2f, 0.7f, 0.1f }, { 0.1f, 0.8f, 0.1f }, { 0.3f, 0.6f, 0.1f }, { 0.05f, 0.9f, 0.05f } });
  54. var scores1 = tf.reshape(scoresX, (1, 4, 3));
  55. var init = tf.global_variables_initializer();
  56. var sess = tf.Session();
  57. sess.run(init);
  58. var (boxes, scores, classes, valid_detections) = tf.image.combined_non_max_suppression(boxes1, scores1, 10, 10, 0.5f, 0.2f, clip_boxes: false);
  59. var result = sess.run((boxes, scores, classes, valid_detections));
  60. var boxes_gt = tf.constant(new float[,] { { 210f, 112f, 150f, 100f }, { 200f, 100f, 150f, 100f }, { 190f, 110f, 150f, 100f },
  61. { 0f, 0f, 0f, 0f},{ 0f, 0f, 0f, 0f},{ 0f, 0f, 0f, 0f},{ 0f, 0f, 0f , 0f},{ 0f, 0f, 0f, 0f},{ 0f , 0f, 0f, 0f},{ 0f, 0f, 0f, 0f} });
  62. boxes_gt = tf.reshape(boxes_gt, (1, 10, 4));
  63. Assert.AreEqual(result.Item1.numpy(), boxes_gt.numpy());
  64. var scores_gt = tf.constant(new float[,] { { 0.9f, 0.7f, 0.3f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } });
  65. scores_gt = tf.reshape(scores_gt, (1, 10));
  66. Assert.AreEqual(result.Item2.numpy(), scores_gt.numpy());
  67. var classes_gt = tf.constant(new float[,] { { 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } });
  68. classes_gt = tf.reshape(classes_gt, (1, 10));
  69. Assert.AreEqual(result.Item3.numpy(), classes_gt.numpy());
  70. var valid_detections_gt = tf.constant(new int[,] { { 3 } });
  71. valid_detections_gt = tf.reshape(valid_detections_gt, (1));
  72. Assert.AreEqual(result.Item4.numpy(), valid_detections_gt.numpy());
  73. }
  74. [TestMethod]
  75. public void crop_and_resize()
  76. {
  77. int BATCH_SIZE = 1;
  78. int NUM_BOXES = 5;
  79. int IMAGE_HEIGHT = 256;
  80. int IMAGE_WIDTH = 256;
  81. int CHANNELS = 3;
  82. var crop_size = tf.constant(new int[] { 24, 24 });
  83. var image = tf.random.uniform((BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS));
  84. var boxes = tf.random.uniform((NUM_BOXES, 4));
  85. var box_ind = tf.random.uniform((NUM_BOXES), minval: 0, maxval: BATCH_SIZE, dtype: TF_DataType.TF_INT32);
  86. var output = tf.image.crop_and_resize(image, boxes, box_ind, crop_size);
  87. Assert.AreEqual((5,24,24,3), output.shape);
  88. }
  89. [TestMethod]
  90. public void decode_image()
  91. {
  92. var img = tf.image.decode_image(contents);
  93. Assert.AreEqual(img.name, "decode_image/DecodeImage:0");
  94. }
  95. [TestMethod]
  96. public void resize_image()
  97. {
  98. tf.enable_eager_execution();
  99. var image = tf.constant(new int[5, 5]
  100. {
  101. {1, 0, 0, 0, 0 },
  102. {0, 1, 0, 0, 0 },
  103. {0, 0, 1, 0, 0 },
  104. {0, 0, 0, 1, 0 },
  105. {0, 0, 0, 0, 1 }
  106. });
  107. image = image[tf.newaxis, tf.ellipsis, tf.newaxis];
  108. image = tf.image.resize(image, (3, 5));
  109. image = image[0, tf.ellipsis, 0];
  110. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0.6666667f, 0.3333333f, 0, 0, 0 },
  111. image[0].ToArray<float>()));
  112. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0, 0, 1, 0, 0 },
  113. image[1].ToArray<float>()));
  114. Assert.IsTrue(Enumerable.SequenceEqual(new float[] { 0, 0, 0, 0.3333335f, 0.6666665f },
  115. image[2].ToArray<float>()));
  116. tf.compat.v1.disable_eager_execution();
  117. }
  118. [TestMethod]
  119. public void TestCropAndResize()
  120. {
  121. var graph = tf.Graph().as_default();
  122. // 3x3 'Image' with numbered coordinates
  123. var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f);
  124. var image = tf.reshape(input, new int[] { 1, 3, 3, 1 });
  125. // 4x4 'Image' with numbered coordinates
  126. var input2 = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f);
  127. var image2 = tf.reshape(input2, new int[] { 1, 4, 4, 1 });
  128. // create one box over the full image that flips it (y1 > y2)
  129. var box = tf.reshape(np.array(1f, 0f, 0f, 1f), new int[] { 1, 4 });
  130. var boxInd = tf.Variable(np.array(0));
  131. // crop first 3x3 imageto size 1x1
  132. var cropSize1_1 = tf.Variable(np.array(1, 1));
  133. // don't crop second 4x4 image
  134. var cropSize2_2 = tf.Variable(np.array(4, 4));
  135. var init = tf.global_variables_initializer();
  136. var sess = tf.Session();
  137. sess.run(init);
  138. var cropped = tf.image.crop_and_resize(image, box, boxInd, cropSize1_1);
  139. var result = sess.run(cropped);
  140. // check if cropped to 1x1 center was succesfull
  141. Assert.AreEqual(result.size, 1ul);
  142. Assert.AreEqual(result[0, 0, 0, 0], 4f);
  143. cropped = tf.image.crop_and_resize(image2, box, boxInd, cropSize2_2);
  144. result = sess.run(cropped);
  145. // check if flipped and no cropping occured
  146. Assert.AreEqual(result.size, 16ul);
  147. Assert.AreEqual(result[0, 0, 0, 0], 12f);
  148. }
  149. [TestMethod]
  150. public void ImageSaveTest()
  151. {
  152. var imgPath = TestHelper.GetFullPathFromDataDir("img001.bmp");
  153. var jpegImgPath = TestHelper.GetFullPathFromDataDir("img001.jpeg");
  154. var pngImgPath = TestHelper.GetFullPathFromDataDir("img001.png");
  155. File.Delete(jpegImgPath);
  156. File.Delete(pngImgPath);
  157. var contents = tf.io.read_file(imgPath);
  158. var bmp = tf.image.decode_image(contents);
  159. Assert.AreEqual(bmp.name, "decode_image/DecodeImage:0");
  160. var jpeg = tf.image.encode_jpeg(bmp);
  161. var op1 = tf.io.write_file(jpegImgPath, jpeg);
  162. var png = tf.image.encode_png(bmp);
  163. var op2 = tf.io.write_file(pngImgPath, png);
  164. this.session().run(op1);
  165. this.session().run(op2);
  166. Assert.IsTrue(File.Exists(jpegImgPath), "not find file:" + jpegImgPath);
  167. Assert.IsTrue(File.Exists(pngImgPath), "not find file:" + pngImgPath);
  168. // 如果要测试图片正确性,需要注释下面两行代码
  169. File.Delete(jpegImgPath);
  170. File.Delete(pngImgPath);
  171. }
  172. [TestMethod]
  173. public void ImageFlipTest()
  174. {
  175. var imgPath = TestHelper.GetFullPathFromDataDir("img001.bmp");
  176. var contents = tf.io.read_file(imgPath);
  177. var bmp = tf.image.decode_image(contents);
  178. // 左右翻转
  179. var lrImgPath = TestHelper.GetFullPathFromDataDir("img001_lr.png");
  180. File.Delete(lrImgPath);
  181. var lr = tf.image.flip_left_right(bmp);
  182. var png = tf.image.encode_png(lr);
  183. var op = tf.io.write_file(lrImgPath, png);
  184. this.session().run(op);
  185. Assert.IsTrue(File.Exists(lrImgPath), "not find file:" + lrImgPath);
  186. // 上下翻转
  187. var updownImgPath = TestHelper.GetFullPathFromDataDir("img001_updown.png");
  188. File.Delete(updownImgPath);
  189. var updown = tf.image.flip_up_down(bmp);
  190. var pngupdown = tf.image.encode_png(updown);
  191. var op2 = tf.io.write_file(updownImgPath, pngupdown);
  192. this.session().run(op2);
  193. Assert.IsTrue(File.Exists(updownImgPath));
  194. // 暂时先人工观测图片是否翻转,观测时需要删除下面这两行代码
  195. File.Delete(lrImgPath);
  196. File.Delete(updownImgPath);
  197. // 多图翻转
  198. // 目前直接通过 bmp 拿到 shape ,这里先用默认定义图片大小来构建了
  199. var mImg = tf.stack(new[] { bmp, lr }, axis:0);
  200. print(mImg.shape);
  201. var up2 = tf.image.flip_up_down(mImg);
  202. var updownImgPath_m1 = TestHelper.GetFullPathFromDataDir("img001_m_ud.png"); // 直接上下翻转
  203. File.Delete(updownImgPath_m1);
  204. var img001_updown_m2 = TestHelper.GetFullPathFromDataDir("img001_m_lr_ud.png"); // 先左右再上下
  205. File.Delete(img001_updown_m2);
  206. var png2 = tf.image.encode_png(up2[0]);
  207. tf.io.write_file(updownImgPath_m1, png2);
  208. png2 = tf.image.encode_png(up2[1]);
  209. tf.io.write_file(img001_updown_m2, png2);
  210. // 如果要测试图片正确性,需要注释下面两行代码
  211. File.Delete(updownImgPath_m1);
  212. File.Delete(img001_updown_m2);
  213. }
  214. }
  215. }