Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/misc/java/test/DnnListRegressionTest.java
16363 views
1
package org.opencv.test.dnn;
2
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.IOException;
6
import java.util.ArrayList;
7
import java.util.List;
8
import org.opencv.core.Core;
9
import org.opencv.core.Mat;
10
import org.opencv.core.MatOfInt;
11
import org.opencv.core.MatOfFloat;
12
import org.opencv.core.MatOfByte;
13
import org.opencv.core.Scalar;
14
import org.opencv.core.Size;
15
import org.opencv.dnn.DictValue;
16
import org.opencv.dnn.Dnn;
17
import org.opencv.dnn.Layer;
18
import org.opencv.dnn.Net;
19
import org.opencv.imgcodecs.Imgcodecs;
20
import org.opencv.imgproc.Imgproc;
21
import org.opencv.test.OpenCVTestCase;
22
23
/*
24
* regression test for #12324,
25
* testing various java.util.List invocations,
26
* which use the LIST_GET macro
27
*/
28
29
public class DnnListRegressionTest extends OpenCVTestCase {
30
31
private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";
32
33
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
34
35
String modelFileName = "";
36
String sourceImageFile = "";
37
38
Net net;
39
40
@Override
41
protected void setUp() throws Exception {
42
super.setUp();
43
44
String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);
45
46
if(envDnnTestDataPath == null){
47
isTestCaseEnabled = false;
48
return;
49
}
50
51
File dnnTestDataPath = new File(envDnnTestDataPath);
52
modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString();
53
54
String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
55
56
if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
57
58
File testDataPath = new File(envTestDataPath);
59
60
File f = new File(testDataPath, "dnn/grace_hopper_227.png");
61
sourceImageFile = f.toString();
62
if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);
63
64
net = Dnn.readNetFromTensorflow(modelFileName);
65
66
Mat image = Imgcodecs.imread(sourceImageFile);
67
assertNotNull("Loading image from file failed!", image);
68
69
Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
70
assertNotNull("Converting image to blob failed!", inputBlob);
71
72
net.setInput(inputBlob, "input");
73
}
74
75
public void testSetInputsNames() {
76
List<String> inputs = new ArrayList();
77
inputs.add("input");
78
try {
79
net.setInputsNames(inputs);
80
} catch(Exception e) {
81
fail("Net setInputsNames failed: " + e.getMessage());
82
}
83
}
84
85
public void testForward() {
86
List<Mat> outs = new ArrayList();
87
List<String> outNames = new ArrayList();
88
outNames.add("softmax2");
89
try {
90
net.forward(outs,outNames);
91
} catch(Exception e) {
92
fail("Net forward failed: " + e.getMessage());
93
}
94
}
95
96
public void testGetMemoryConsumption() {
97
int layerId = 1;
98
List<MatOfInt> netInputShapes = new ArrayList();
99
netInputShapes.add(new MatOfInt(1, 3, 224, 224));
100
long[] weights=null;
101
long[] blobs=null;
102
try {
103
net.getMemoryConsumption(layerId, netInputShapes, weights, blobs);
104
} catch(Exception e) {
105
fail("Net getMemoryConsumption failed: " + e.getMessage());
106
}
107
}
108
109
public void testGetFLOPS() {
110
int layerId = 1;
111
List<MatOfInt> netInputShapes = new ArrayList();
112
netInputShapes.add(new MatOfInt(1, 3, 224, 224));
113
try {
114
net.getFLOPS(layerId, netInputShapes);
115
} catch(Exception e) {
116
fail("Net getFLOPS failed: " + e.getMessage());
117
}
118
}
119
}
120
121