1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

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
This commit is contained in:
omrope79
2026-06-02 19:58:10 +05:30
committed by GitHub
parent 42fc6939f8
commit b67ad9a422
34 changed files with 327 additions and 5674 deletions
@@ -1,67 +1,71 @@
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.core.MatOfByte;
import org.opencv.core.Range;
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()
{
// Create a simple Caffe prototxt with a Slice layer
String prototxt =
"input: \"data\"\n" +
"layer {\n" +
" name: \"testLayer\"\n" +
" type: \"Slice\"\n" +
" bottom: \"data\"\n" +
" top: \"firstCopy\"\n" +
" top: \"secondCopy\"\n" +
" slice_param {\n" +
" axis: 0\n" +
" slice_point: 2\n" +
" }\n" +
"}";
// Read network from prototxt
MatOfByte bufferProto = new MatOfByte();
bufferProto.fromArray(prototxt.getBytes());
MatOfByte bufferModel = new MatOfByte();
Net net = Dnn.readNetFromCaffe(bufferProto, bufferModel, Dnn.ENGINE_CLASSIC);
// 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);
// Create input data
Mat inp = new Mat(4, 5, CvType.CV_32F);
// 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);
// Define output names
List<String> outNames = new ArrayList<>();
outNames.add("testLayer");
List<String> outNames = net.getUnconnectedOutLayersNames();
assertFalse("Model has no output layers", outNames.isEmpty());
// Forward and retrieve multiple outputs
// Forward and retrieve every output blob of the requested layers.
List<List<Mat>> outBlobs = new ArrayList<>();
net.forwardAndRetrieve(outBlobs, outNames);
// Verify results
assertEquals(1, outBlobs.size());
assertEquals(2, outBlobs.get(0).size());
// Compare results
Mat expectedFirst = inp.rowRange(0, 2);
Mat expectedSecond = inp.rowRange(2, 4);
Mat actualFirst = outBlobs.get(0).get(0);
Mat actualSecond = outBlobs.get(0).get(1);
assertEquals(0, Core.norm(expectedFirst, actualFirst, Core.NORM_INF), EPS);
assertEquals(0, Core.norm(expectedSecond, actualSecond, Core.NORM_INF), EPS);
// 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());
}
}
}