DnnTensorFlowTest.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package org.opencv.test.dnn;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.opencv.core.Core;
  8. import org.opencv.core.Mat;
  9. import org.opencv.core.MatOfFloat;
  10. import org.opencv.core.MatOfByte;
  11. import org.opencv.core.Scalar;
  12. import org.opencv.core.Size;
  13. import org.opencv.dnn.DictValue;
  14. import org.opencv.dnn.Dnn;
  15. import org.opencv.dnn.Layer;
  16. import org.opencv.dnn.Net;
  17. import org.opencv.imgcodecs.Imgcodecs;
  18. import org.opencv.imgproc.Imgproc;
  19. import org.opencv.test.OpenCVTestCase;
  20. public class DnnTensorFlowTest extends OpenCVTestCase {
  21. private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";
  22. private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
  23. String modelFileName = "";
  24. String sourceImageFile = "";
  25. Net net;
  26. private static void normAssert(Mat ref, Mat test) {
  27. final double l1 = 1e-5;
  28. final double lInf = 1e-4;
  29. double normL1 = Core.norm(ref, test, Core.NORM_L1) / ref.total();
  30. double normLInf = Core.norm(ref, test, Core.NORM_INF) / ref.total();
  31. assertTrue(normL1 < l1);
  32. assertTrue(normLInf < lInf);
  33. }
  34. @Override
  35. protected void setUp() throws Exception {
  36. super.setUp();
  37. String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);
  38. if(envDnnTestDataPath == null){
  39. isTestCaseEnabled = false;
  40. return;
  41. }
  42. File dnnTestDataPath = new File(envDnnTestDataPath);
  43. modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString();
  44. String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
  45. if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
  46. File testDataPath = new File(envTestDataPath);
  47. File f = new File(testDataPath, "dnn/grace_hopper_227.png");
  48. sourceImageFile = f.toString();
  49. if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);
  50. net = Dnn.readNetFromTensorflow(modelFileName);
  51. }
  52. public void testGetLayerTypes() {
  53. List<String> layertypes = new ArrayList();
  54. net.getLayerTypes(layertypes);
  55. assertFalse("No layer types returned!", layertypes.isEmpty());
  56. }
  57. public void testGetLayer() {
  58. List<String> layernames = net.getLayerNames();
  59. assertFalse("Test net returned no layers!", layernames.isEmpty());
  60. String testLayerName = layernames.get(0);
  61. DictValue layerId = new DictValue(testLayerName);
  62. assertEquals("DictValue did not return the string, which was used in constructor!", testLayerName, layerId.getStringValue());
  63. Layer layer = net.getLayer(layerId);
  64. assertEquals("Layer name does not match the expected value!", testLayerName, layer.get_name());
  65. }
  66. public void checkInceptionNet(Net net)
  67. {
  68. Mat image = Imgcodecs.imread(sourceImageFile);
  69. assertNotNull("Loading image from file failed!", image);
  70. Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
  71. assertNotNull("Converting image to blob failed!", inputBlob);
  72. net.setInput(inputBlob, "input");
  73. Mat result = new Mat();
  74. try {
  75. net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
  76. result = net.forward("softmax2");
  77. }
  78. catch (Exception e) {
  79. fail("DNN forward failed: " + e.getMessage());
  80. }
  81. assertNotNull("Net returned no result!", result);
  82. result = result.reshape(1, 1);
  83. Core.MinMaxLocResult minmax = Core.minMaxLoc(result);
  84. assertEquals("Wrong prediction", (int)minmax.maxLoc.x, 866);
  85. Mat top5RefScores = new MatOfFloat(new float[] {
  86. 0.63032645f, 0.2561979f, 0.032181446f, 0.015721032f, 0.014785315f
  87. }).reshape(1, 1);
  88. Core.sort(result, result, Core.SORT_DESCENDING);
  89. normAssert(result.colRange(0, 5), top5RefScores);
  90. }
  91. public void testTestNetForward() {
  92. checkInceptionNet(net);
  93. }
  94. public void testReadFromBuffer() {
  95. File modelFile = new File(modelFileName);
  96. byte[] modelBuffer = new byte[ (int)modelFile.length() ];
  97. try {
  98. FileInputStream fis = new FileInputStream(modelFile);
  99. fis.read(modelBuffer);
  100. fis.close();
  101. } catch (IOException e) {
  102. fail("Failed to read a model: " + e.getMessage());
  103. }
  104. net = Dnn.readNetFromTensorflow(new MatOfByte(modelBuffer));
  105. checkInceptionNet(net);
  106. }
  107. public void testGetAvailableTargets() {
  108. List<Integer> targets = Dnn.getAvailableTargets(Dnn.DNN_BACKEND_OPENCV);
  109. assertTrue(targets.contains(Dnn.DNN_TARGET_CPU));
  110. }
  111. }