1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-26 13:53:03 +04:00
Files
opencv/modules/dnn/misc/java/test/DnnForwardAndRetrieve.java
omrope79 b67ad9a422 Merge pull request #28678 from omrope79:caffe-importer-cleanup
Caffe importer cleanup #28678

Merge with: https://github.com/opencv/opencv_extra/pull/1324

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
- [x] The feature is well documented and sample code can be built with the project CMake
2026-06-02 17:28:10 +03:00

72 lines
2.4 KiB
Java

package org.opencv.test.dnn;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Net;
import org.opencv.test.OpenCVTestCase;
public class DnnForwardAndRetrieve extends OpenCVTestCase {
private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
private String modelFileName = "";
@Override
protected void setUp() throws Exception {
super.setUp();
String dnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);
String generalTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
File model = null;
if (generalTestDataPath != null) {
model = new File(generalTestDataPath, "dnn/onnx/models/split_0.onnx");
}
if ((model == null || !model.isFile()) && dnnTestDataPath != null) {
model = new File(dnnTestDataPath, "dnn/onnx/models/split_0.onnx");
}
if (model == null || !model.isFile()) {
isTestCaseEnabled = false;
return;
}
modelFileName = model.getAbsolutePath();
}
public void testForwardAndRetrieve()
{
// Verifies forwardAndRetrieve nested list marshalling using a small ONNX model instead of the removed Caffe importer.
Net net = Dnn.readNetFromONNX(modelFileName, Dnn.ENGINE_CLASSIC);
net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
// split_0.onnx declares a single 4D input named "image" of shape [1, 3, 2, 2].
Mat inp = new Mat(new int[]{1, 3, 2, 2}, CvType.CV_32F);
Core.randu(inp, -1, 1);
net.setInput(inp);
List<String> outNames = net.getUnconnectedOutLayersNames();
assertFalse("Model has no output layers", outNames.isEmpty());
// Forward and retrieve every output blob of the requested layers.
List<List<Mat>> outBlobs = new ArrayList<>();
net.forwardAndRetrieve(outBlobs, outNames);
// One entry per requested layer name, each holding at least one valid blob.
assertEquals(outNames.size(), outBlobs.size());
for (List<Mat> blobs : outBlobs) {
assertFalse(blobs.isEmpty());
for (Mat blob : blobs)
assertFalse(blob.empty());
}
}
}