forked from hellonico/opencv4_java_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensorflowDemo.java
More file actions
59 lines (42 loc) · 1.93 KB
/
Copy pathTensorflowDemo.java
File metadata and controls
59 lines (42 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package dnn;
import org.opencv.core.*;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;
import org.scijava.nativelib.NativeLoader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class TensorflowDemo {
private static void normAssert(Mat ref, Mat test) {
double normL1 = Core.norm(ref, test, Core.NORM_L1) / ref.total();
double normLInf = Core.norm(ref, test, Core.NORM_INF) / ref.total();
System.out.println(normL1);
System.out.println(normLInf);
}
public static void main(String[] args) throws Exception {
NativeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String sourceImageFile = "data/tf/grace_hopper_227.png";
String tfnetFile = "data/tf/tensorflow_inception_graph.pb";
Net net = Dnn.readNetFromTensorflow(tfnetFile);
Mat image = Imgcodecs.imread(sourceImageFile);
Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
net.setInput(inputBlob, "input");
net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
List<String> layernames = net.getLayerNames();
// System.out.println(layernames);
Mat result = net.forward("softmax2");
result = result.reshape(1, 1);
Core.MinMaxLocResult minmax = Core.minMaxLoc(result);
// System.out.println(minmax.maxLoc.toString());
// System.out.println(result.get(0, 866)[0]);
List<String> labels =
Files.readAllLines(Paths.get("data/tf/imagenet_slim_labels.txt"));
System.out.println(labels.get((int) minmax.maxLoc.x ));
// check scores
Mat top5RefScores =
new MatOfFloat(0.63032645f, 0.2561979f, 0.032181446f, 0.015721032f, 0.014785315f).reshape(1, 1);
Core.sort(result, result, Core.SORT_DESCENDING);
normAssert(result.colRange(0, 5), top5RefScores);
}
}