Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/misc/java/test/DnnTensorFlowTest.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.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
public class DnnTensorFlowTest extends OpenCVTestCase {
23
24
private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";
25
26
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
27
28
String modelFileName = "";
29
String sourceImageFile = "";
30
31
Net net;
32
33
private static void normAssert(Mat ref, Mat test) {
34
final double l1 = 1e-5;
35
final double lInf = 1e-4;
36
double normL1 = Core.norm(ref, test, Core.NORM_L1) / ref.total();
37
double normLInf = Core.norm(ref, test, Core.NORM_INF) / ref.total();
38
assertTrue(normL1 < l1);
39
assertTrue(normLInf < lInf);
40
}
41
42
@Override
43
protected void setUp() throws Exception {
44
super.setUp();
45
46
String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);
47
48
if(envDnnTestDataPath == null){
49
isTestCaseEnabled = false;
50
return;
51
}
52
53
File dnnTestDataPath = new File(envDnnTestDataPath);
54
modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString();
55
56
String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
57
58
if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
59
60
File testDataPath = new File(envTestDataPath);
61
62
File f = new File(testDataPath, "dnn/grace_hopper_227.png");
63
sourceImageFile = f.toString();
64
if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);
65
66
net = Dnn.readNetFromTensorflow(modelFileName);
67
}
68
69
public void testGetLayerTypes() {
70
List<String> layertypes = new ArrayList();
71
net.getLayerTypes(layertypes);
72
73
assertFalse("No layer types returned!", layertypes.isEmpty());
74
}
75
76
public void testGetLayer() {
77
List<String> layernames = net.getLayerNames();
78
79
assertFalse("Test net returned no layers!", layernames.isEmpty());
80
81
String testLayerName = layernames.get(0);
82
83
DictValue layerId = new DictValue(testLayerName);
84
85
assertEquals("DictValue did not return the string, which was used in constructor!", testLayerName, layerId.getStringValue());
86
87
Layer layer = net.getLayer(layerId);
88
89
assertEquals("Layer name does not match the expected value!", testLayerName, layer.get_name());
90
91
}
92
93
public void checkInceptionNet(Net net)
94
{
95
Mat image = Imgcodecs.imread(sourceImageFile);
96
assertNotNull("Loading image from file failed!", image);
97
98
Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
99
assertNotNull("Converting image to blob failed!", inputBlob);
100
101
net.setInput(inputBlob, "input");
102
103
Mat result = new Mat();
104
try {
105
net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
106
result = net.forward("softmax2");
107
}
108
catch (Exception e) {
109
fail("DNN forward failed: " + e.getMessage());
110
}
111
assertNotNull("Net returned no result!", result);
112
113
result = result.reshape(1, 1);
114
Core.MinMaxLocResult minmax = Core.minMaxLoc(result);
115
assertEquals("Wrong prediction", (int)minmax.maxLoc.x, 866);
116
117
Mat top5RefScores = new MatOfFloat(new float[] {
118
0.63032645f, 0.2561979f, 0.032181446f, 0.015721032f, 0.014785315f
119
}).reshape(1, 1);
120
121
Core.sort(result, result, Core.SORT_DESCENDING);
122
123
normAssert(result.colRange(0, 5), top5RefScores);
124
}
125
126
public void testTestNetForward() {
127
checkInceptionNet(net);
128
}
129
130
public void testReadFromBuffer() {
131
File modelFile = new File(modelFileName);
132
byte[] modelBuffer = new byte[ (int)modelFile.length() ];
133
134
try {
135
FileInputStream fis = new FileInputStream(modelFile);
136
fis.read(modelBuffer);
137
fis.close();
138
} catch (IOException e) {
139
fail("Failed to read a model: " + e.getMessage());
140
}
141
net = Dnn.readNetFromTensorflow(new MatOfByte(modelBuffer));
142
checkInceptionNet(net);
143
}
144
}
145
146