DnnListRegressionTest.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.MatOfInt;
  10. import org.opencv.core.MatOfFloat;
  11. import org.opencv.core.MatOfByte;
  12. import org.opencv.core.Scalar;
  13. import org.opencv.core.Size;
  14. import org.opencv.dnn.DictValue;
  15. import org.opencv.dnn.Dnn;
  16. import org.opencv.dnn.Layer;
  17. import org.opencv.dnn.Net;
  18. import org.opencv.imgcodecs.Imgcodecs;
  19. import org.opencv.imgproc.Imgproc;
  20. import org.opencv.test.OpenCVTestCase;
  21. /*
  22. * regression test for #12324,
  23. * testing various java.util.List invocations,
  24. * which use the LIST_GET macro
  25. */
  26. public class DnnListRegressionTest extends OpenCVTestCase {
  27. private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";
  28. private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
  29. String modelFileName = "";
  30. String sourceImageFile = "";
  31. Net net;
  32. @Override
  33. protected void setUp() throws Exception {
  34. super.setUp();
  35. String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);
  36. if(envDnnTestDataPath == null){
  37. isTestCaseEnabled = false;
  38. return;
  39. }
  40. File dnnTestDataPath = new File(envDnnTestDataPath);
  41. modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString();
  42. String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
  43. if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
  44. File testDataPath = new File(envTestDataPath);
  45. File f = new File(testDataPath, "dnn/grace_hopper_227.png");
  46. sourceImageFile = f.toString();
  47. if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);
  48. net = Dnn.readNetFromTensorflow(modelFileName);
  49. Mat image = Imgcodecs.imread(sourceImageFile);
  50. assertNotNull("Loading image from file failed!", image);
  51. Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
  52. assertNotNull("Converting image to blob failed!", inputBlob);
  53. net.setInput(inputBlob, "input");
  54. }
  55. public void testSetInputsNames() {
  56. List<String> inputs = new ArrayList();
  57. inputs.add("input");
  58. try {
  59. net.setInputsNames(inputs);
  60. } catch(Exception e) {
  61. fail("Net setInputsNames failed: " + e.getMessage());
  62. }
  63. }
  64. public void testForward() {
  65. List<Mat> outs = new ArrayList();
  66. List<String> outNames = new ArrayList();
  67. outNames.add("softmax2");
  68. try {
  69. net.forward(outs,outNames);
  70. } catch(Exception e) {
  71. fail("Net forward failed: " + e.getMessage());
  72. }
  73. }
  74. public void testGetMemoryConsumption() {
  75. int layerId = 1;
  76. List<MatOfInt> netInputShapes = new ArrayList();
  77. netInputShapes.add(new MatOfInt(1, 3, 224, 224));
  78. long[] weights=null;
  79. long[] blobs=null;
  80. try {
  81. net.getMemoryConsumption(layerId, netInputShapes, weights, blobs);
  82. } catch(Exception e) {
  83. fail("Net getMemoryConsumption failed: " + e.getMessage());
  84. }
  85. }
  86. public void testGetFLOPS() {
  87. int layerId = 1;
  88. List<MatOfInt> netInputShapes = new ArrayList();
  89. netInputShapes.add(new MatOfInt(1, 3, 224, 224));
  90. try {
  91. net.getFLOPS(layerId, netInputShapes);
  92. } catch(Exception e) {
  93. fail("Net getFLOPS failed: " + e.getMessage());
  94. }
  95. }
  96. }