Path: blob/master/modules/dnn/misc/java/test/DnnListRegressionTest.java
16363 views
package org.opencv.test.dnn;12import java.io.File;3import java.io.FileInputStream;4import java.io.IOException;5import java.util.ArrayList;6import java.util.List;7import org.opencv.core.Core;8import org.opencv.core.Mat;9import org.opencv.core.MatOfInt;10import org.opencv.core.MatOfFloat;11import org.opencv.core.MatOfByte;12import org.opencv.core.Scalar;13import org.opencv.core.Size;14import org.opencv.dnn.DictValue;15import org.opencv.dnn.Dnn;16import org.opencv.dnn.Layer;17import org.opencv.dnn.Net;18import org.opencv.imgcodecs.Imgcodecs;19import org.opencv.imgproc.Imgproc;20import org.opencv.test.OpenCVTestCase;2122/*23* regression test for #12324,24* testing various java.util.List invocations,25* which use the LIST_GET macro26*/2728public class DnnListRegressionTest extends OpenCVTestCase {2930private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";3132private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";3334String modelFileName = "";35String sourceImageFile = "";3637Net net;3839@Override40protected void setUp() throws Exception {41super.setUp();4243String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);4445if(envDnnTestDataPath == null){46isTestCaseEnabled = false;47return;48}4950File dnnTestDataPath = new File(envDnnTestDataPath);51modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString();5253String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);5455if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");5657File testDataPath = new File(envTestDataPath);5859File f = new File(testDataPath, "dnn/grace_hopper_227.png");60sourceImageFile = f.toString();61if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);6263net = Dnn.readNetFromTensorflow(modelFileName);6465Mat image = Imgcodecs.imread(sourceImageFile);66assertNotNull("Loading image from file failed!", image);6768Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);69assertNotNull("Converting image to blob failed!", inputBlob);7071net.setInput(inputBlob, "input");72}7374public void testSetInputsNames() {75List<String> inputs = new ArrayList();76inputs.add("input");77try {78net.setInputsNames(inputs);79} catch(Exception e) {80fail("Net setInputsNames failed: " + e.getMessage());81}82}8384public void testForward() {85List<Mat> outs = new ArrayList();86List<String> outNames = new ArrayList();87outNames.add("softmax2");88try {89net.forward(outs,outNames);90} catch(Exception e) {91fail("Net forward failed: " + e.getMessage());92}93}9495public void testGetMemoryConsumption() {96int layerId = 1;97List<MatOfInt> netInputShapes = new ArrayList();98netInputShapes.add(new MatOfInt(1, 3, 224, 224));99long[] weights=null;100long[] blobs=null;101try {102net.getMemoryConsumption(layerId, netInputShapes, weights, blobs);103} catch(Exception e) {104fail("Net getMemoryConsumption failed: " + e.getMessage());105}106}107108public void testGetFLOPS() {109int layerId = 1;110List<MatOfInt> netInputShapes = new ArrayList();111netInputShapes.add(new MatOfInt(1, 3, 224, 224));112try {113net.getFLOPS(layerId, netInputShapes);114} catch(Exception e) {115fail("Net getFLOPS failed: " + e.getMessage());116}117}118}119120121