Path: blob/master/modules/dnn/misc/java/test/DnnTensorFlowTest.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.MatOfFloat;10import org.opencv.core.MatOfByte;11import org.opencv.core.Scalar;12import org.opencv.core.Size;13import org.opencv.dnn.DictValue;14import org.opencv.dnn.Dnn;15import org.opencv.dnn.Layer;16import org.opencv.dnn.Net;17import org.opencv.imgcodecs.Imgcodecs;18import org.opencv.imgproc.Imgproc;19import org.opencv.test.OpenCVTestCase;2021public class DnnTensorFlowTest extends OpenCVTestCase {2223private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";2425private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";2627String modelFileName = "";28String sourceImageFile = "";2930Net net;3132private static void normAssert(Mat ref, Mat test) {33final double l1 = 1e-5;34final double lInf = 1e-4;35double normL1 = Core.norm(ref, test, Core.NORM_L1) / ref.total();36double normLInf = Core.norm(ref, test, Core.NORM_INF) / ref.total();37assertTrue(normL1 < l1);38assertTrue(normLInf < lInf);39}4041@Override42protected void setUp() throws Exception {43super.setUp();4445String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);4647if(envDnnTestDataPath == null){48isTestCaseEnabled = false;49return;50}5152File dnnTestDataPath = new File(envDnnTestDataPath);53modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString();5455String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);5657if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");5859File testDataPath = new File(envTestDataPath);6061File f = new File(testDataPath, "dnn/grace_hopper_227.png");62sourceImageFile = f.toString();63if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);6465net = Dnn.readNetFromTensorflow(modelFileName);66}6768public void testGetLayerTypes() {69List<String> layertypes = new ArrayList();70net.getLayerTypes(layertypes);7172assertFalse("No layer types returned!", layertypes.isEmpty());73}7475public void testGetLayer() {76List<String> layernames = net.getLayerNames();7778assertFalse("Test net returned no layers!", layernames.isEmpty());7980String testLayerName = layernames.get(0);8182DictValue layerId = new DictValue(testLayerName);8384assertEquals("DictValue did not return the string, which was used in constructor!", testLayerName, layerId.getStringValue());8586Layer layer = net.getLayer(layerId);8788assertEquals("Layer name does not match the expected value!", testLayerName, layer.get_name());8990}9192public void checkInceptionNet(Net net)93{94Mat image = Imgcodecs.imread(sourceImageFile);95assertNotNull("Loading image from file failed!", image);9697Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);98assertNotNull("Converting image to blob failed!", inputBlob);99100net.setInput(inputBlob, "input");101102Mat result = new Mat();103try {104net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);105result = net.forward("softmax2");106}107catch (Exception e) {108fail("DNN forward failed: " + e.getMessage());109}110assertNotNull("Net returned no result!", result);111112result = result.reshape(1, 1);113Core.MinMaxLocResult minmax = Core.minMaxLoc(result);114assertEquals("Wrong prediction", (int)minmax.maxLoc.x, 866);115116Mat top5RefScores = new MatOfFloat(new float[] {1170.63032645f, 0.2561979f, 0.032181446f, 0.015721032f, 0.014785315f118}).reshape(1, 1);119120Core.sort(result, result, Core.SORT_DESCENDING);121122normAssert(result.colRange(0, 5), top5RefScores);123}124125public void testTestNetForward() {126checkInceptionNet(net);127}128129public void testReadFromBuffer() {130File modelFile = new File(modelFileName);131byte[] modelBuffer = new byte[ (int)modelFile.length() ];132133try {134FileInputStream fis = new FileInputStream(modelFile);135fis.read(modelBuffer);136fis.close();137} catch (IOException e) {138fail("Failed to read a model: " + e.getMessage());139}140net = Dnn.readNetFromTensorflow(new MatOfByte(modelBuffer));141checkInceptionNet(net);142}143}144145146