From 46331314a56846350c2cee4a5bcc17bd6bc763e3 Mon Sep 17 00:00:00 2001 From: Abhishek Gola Date: Fri, 19 Jun 2026 13:14:32 +0530 Subject: [PATCH] removed classic engine --- apps/model-diagnostics/model_diagnostics.cpp | 14 +++-- modules/dnn/src/layers/recurrent2_layers.cpp | 56 ++++++++------------ modules/dnn/src/onnx/onnx_importer2.cpp | 20 ++----- samples/cpp/macbeth_chart_detection.cpp | 5 +- samples/dnn/alpha_matting.cpp | 6 +-- samples/dnn/alpha_matting.py | 4 +- samples/dnn/classification.cpp | 5 +- samples/dnn/classification.py | 4 +- samples/dnn/colorization.cpp | 5 +- samples/dnn/colorization.py | 4 +- samples/dnn/deblurring.cpp | 5 +- samples/dnn/deblurring.py | 5 +- samples/dnn/edge_detection.cpp | 5 +- samples/dnn/edge_detection.py | 4 +- samples/dnn/inpainting.cpp | 5 +- samples/dnn/inpainting.py | 5 +- samples/dnn/ldm_inpainting.py | 4 +- samples/dnn/object_detection.cpp | 5 +- samples/dnn/object_detection.py | 4 +- samples/dnn/person_reid.cpp | 5 +- samples/dnn/person_reid.py | 5 +- samples/dnn/segmentation.cpp | 5 +- samples/dnn/segmentation.py | 4 +- samples/python/color_correction_model.py | 4 +- samples/python/macbeth_chart_detection.py | 4 +- 25 files changed, 52 insertions(+), 140 deletions(-) diff --git a/apps/model-diagnostics/model_diagnostics.cpp b/apps/model-diagnostics/model_diagnostics.cpp index 00537e90b0..70f88f27ca 100644 --- a/apps/model-diagnostics/model_diagnostics.cpp +++ b/apps/model-diagnostics/model_diagnostics.cpp @@ -53,7 +53,7 @@ std::string diagnosticKeys = "{ model m | | Path to the model file. }" "{ config c | | Path to the model configuration file. }" "{ framework f | | [Optional] Name of the model framework. }" - "{ engine e | auto | [Optional] Graph negine selector: auto or classic or new}" + "{ engine e | new | [Optional] DNN engine selector: new (default) or ort}" "{ input0_name | | [Optional] Name of input0. Use with input0_shape}" "{ input0_shape | | [Optional] Shape of input0. Use with input0_name}" "{ input1_name | | [Optional] Name of input1. Use with input1_shape}" @@ -98,19 +98,17 @@ int main( int argc, const char** argv ) std::string input4_name = argParser.get("input4_name"); std::string input4_shape = argParser.get("input4_shape"); - dnn::EngineType engine = dnn::ENGINE_AUTO; + dnn::EngineType engine = dnn::ENGINE_NEW; if (argParser.has("engine")) { std::string eng_name = argParser.get("engine"); - if(eng_name == "auto") - engine = dnn::ENGINE_AUTO; - else if(eng_name == "classic") - engine = dnn::ENGINE_CLASSIC; - else if(eng_name == "new") + if(eng_name == "new") engine = dnn::ENGINE_NEW; + else if(eng_name == "ort") + engine = dnn::ENGINE_ORT; else { - std::cerr << "Unknown DNN graph engine \"" << eng_name << "\"\n"; + std::cerr << "Unknown DNN graph engine \"" << eng_name << "\" (use 'new' or 'ort')\n"; return -1; } } diff --git a/modules/dnn/src/layers/recurrent2_layers.cpp b/modules/dnn/src/layers/recurrent2_layers.cpp index 2808c0d8d9..36cdd4ebef 100644 --- a/modules/dnn/src/layers/recurrent2_layers.cpp +++ b/modules/dnn/src/layers/recurrent2_layers.cpp @@ -254,45 +254,31 @@ class LSTM2LayerImpl CV_FINAL : public LSTM2Layer batchSize = input[0].size[0]; } + // ONNX LSTM inputs: X(0), W(1), R(2), B(3), sequence_lens(4), + // initial_h(5), initial_c(6), P(7). Inputs 3..7 are optional and + // may be present-but-empty (e.g. CNTK exports keep all 8 slots with + // empties for unused ones). Gather by presence/non-emptiness rather + // than by the raw input count so optional/empty inputs are ignored. + auto hasInput = [&](int idx) { + return idx < numInputs && !input[idx].empty(); + }; + std::vector blobs_; int hidShape [] = {1 + static_cast(bidirectional), batchSize, numHidden}; int biasShape [] = {1 + static_cast(bidirectional), 8 * numHidden}; - blobs_.push_back(input[1].clone()); - blobs_.push_back(input[2].clone()); - switch (numInputs) { - case 3: - // X, W, R are given - // create bias - blobs_.push_back(Mat::zeros(2, biasShape, input[0].type())); - // create h0, c0 - blobs_.push_back(Mat::zeros(3, hidShape, input[0].type())); - blobs_.push_back(Mat::zeros(3, hidShape, input[0].type())); - break; - case 4: - // X, W, R, B are given - blobs_.push_back(input[3]); - // create h0, c0 - blobs_.push_back(Mat::zeros(3, hidShape, input[0].type())); - blobs_.push_back(Mat::zeros(3, hidShape, input[0].type())); - break; - case 7: - // X, W, R, B, h0, c0 are given - blobs_.push_back(input[3]); - blobs_.push_back(input[5]); - blobs_.push_back(input[6]); - break; - case 8: - // X, W, R, B, seqlen, h0, c0, P are given - blobs_.push_back(input[3]); - blobs_.push_back(input[5]); - blobs_.push_back(input[6]); - blobs_.push_back(input[7]); - break; - default: - CV_Error(Error::StsNotImplemented, "Insufficient inputs for LSTM layer. " - "Required inputs: X, W, R, B, seqLen, h0, c0 [, P for peephole]"); - } + CV_Assert(numInputs >= 3); // X, W, R are mandatory + blobs_.push_back(input[1].clone()); // W + blobs_.push_back(input[2].clone()); // R + // B + blobs_.push_back(hasInput(3) ? input[3] : Mat::zeros(2, biasShape, input[0].type())); + // initial_h + blobs_.push_back(hasInput(5) ? input[5] : Mat::zeros(3, hidShape, input[0].type())); + // initial_c + blobs_.push_back(hasInput(6) ? input[6] : Mat::zeros(3, hidShape, input[0].type())); + // P (peephole) - only when the layer was configured to use it and the input is present + if (usePeephole && hasInput(7)) + blobs_.push_back(input[7]); // set outputs to 0 for (auto& out : output) diff --git a/modules/dnn/src/onnx/onnx_importer2.cpp b/modules/dnn/src/onnx/onnx_importer2.cpp index 90feb2939c..1eb9c7b154 100644 --- a/modules/dnn/src/onnx/onnx_importer2.cpp +++ b/modules/dnn/src/onnx/onnx_importer2.cpp @@ -777,22 +777,6 @@ bool ONNXImporter2::parseValueInfo(const opencv_onnx::ValueInfoProto& valueInfoP } else { // ONNX allows dimensions without dim_value and dim_param. // Treat them as unnamed symbolic dimensions. - // NOTE: LSTM with unnamed dimensions is not ready in the new graph - // engine yet. The classic parser used to handle it, but it has been - // removed, so such models are reported as unsupported. - if (curr_graph_proto) - { - const int n_nodes = curr_graph_proto->node_size(); - for (int i = 0; i < n_nodes; ++i) - { - const std::string& op = curr_graph_proto->node(i).op_type(); - if (op == "LSTM") - { - raiseError(); - return false; - } - } - } val_j = net.findDim("", true); } //CV_Assert(0 <= val_j && val_j <= INT_MAX); @@ -1364,7 +1348,9 @@ void ONNXImporter2::parseLSTM(LayerParams& layerParams, const opencv_onnx::NodeP layerParams.set("produce_sequence_y", need_y); - if (lstm_proto.input_size() == 8) + // The 8th input (P, peephole weights) is optional; an absent ONNX input is + // encoded as an empty name. Only enable peephole when it is actually present. + if (lstm_proto.input_size() == 8 && !lstm_proto.input(7).empty()) layerParams.set("use_peephole", true); diff --git a/samples/cpp/macbeth_chart_detection.cpp b/samples/cpp/macbeth_chart_detection.cpp index a351a1d869..3308caf4df 100644 --- a/samples/cpp/macbeth_chart_detection.cpp +++ b/samples/cpp/macbeth_chart_detection.cpp @@ -135,10 +135,7 @@ int main(int argc, char *argv[]) Ptr detector; #ifdef HAVE_OPENCV_DNN if (model_path != "" && pbtxt_path != ""){ - EngineType engine = ENGINE_AUTO; - if (backend != "default" || target != "cpu"){ - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; Net net = readNetFromTensorflow(model_path, pbtxt_path, engine); net.setPreferableBackend(getBackendID(backend)); net.setPreferableTarget(getTargetID(target)); diff --git a/samples/dnn/alpha_matting.cpp b/samples/dnn/alpha_matting.cpp index 950e26d37a..a6a2bba0a0 100644 --- a/samples/dnn/alpha_matting.cpp +++ b/samples/dnn/alpha_matting.cpp @@ -165,11 +165,7 @@ int main(int argc, char **argv) parser.about(about); - EngineType engine = ENGINE_AUTO; - if (backend != "default" || target != "cpu") - { - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; Net net; loadModel(model, backend, target, net, engine); diff --git a/samples/dnn/alpha_matting.py b/samples/dnn/alpha_matting.py index 38db3608ff..61be31bedd 100644 --- a/samples/dnn/alpha_matting.py +++ b/samples/dnn/alpha_matting.py @@ -140,9 +140,7 @@ def apply_modnet(args, model, image): def main(func_args=None): args = get_args_parser(func_args) - engine = cv.dnn.ENGINE_AUTO - if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW image = cv.imread(cv.samples.findFile(args.input)) if image is None: diff --git a/samples/dnn/classification.cpp b/samples/dnn/classification.cpp index dd505fac18..7815118e05 100644 --- a/samples/dnn/classification.cpp +++ b/samples/dnn/classification.cpp @@ -146,10 +146,7 @@ int main(int argc, char** argv) } CV_Assert(!model.empty()); //! [Read and initialize network] - EngineType engine = ENGINE_AUTO; - if (backend != "default" || target != "cpu"){ - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; Net net = readNetFromONNX(model, engine); net.setPreferableBackend(getBackendID(backend)); net.setPreferableTarget(getTargetID(target)); diff --git a/samples/dnn/classification.py b/samples/dnn/classification.py index c80f35240c..48d7efbfef 100644 --- a/samples/dnn/classification.py +++ b/samples/dnn/classification.py @@ -83,9 +83,7 @@ def main(func_args=None): labels = f.read().rstrip('\n').split('\n') # Load a network - engine = cv.dnn.ENGINE_AUTO - if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW net = cv.dnn.readNetFromONNX(args.model, engine) net.setPreferableBackend(get_backend_id(args.backend)) net.setPreferableTarget(get_target_id(args.target)) diff --git a/samples/dnn/colorization.cpp b/samples/dnn/colorization.cpp index 745dde9473..ac0ca26b3c 100644 --- a/samples/dnn/colorization.cpp +++ b/samples/dnn/colorization.cpp @@ -83,10 +83,7 @@ int main(int argc, char** argv) { resize(imgL, imgLResized, Size(256, 256), 0, 0, INTER_CUBIC); // Prepare the model - EngineType engine = ENGINE_AUTO; - if (backendId != 0 || targetId != 0){ - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; dnn::Net net = dnn::readNetFromONNX(onnxModelPath, engine); net.setPreferableBackend(backendId); net.setPreferableTarget(targetId); diff --git a/samples/dnn/colorization.py b/samples/dnn/colorization.py index ebe59d4e46..6ee7e6c40b 100644 --- a/samples/dnn/colorization.py +++ b/samples/dnn/colorization.py @@ -44,9 +44,7 @@ if __name__ == '__main__': img_gray_rs *= (100.0 / 255.0) # Scale L channel to 0-100 range onnx_model_path = args.onnx_model_path # Update this path to your ONNX model's path - engine = cv.dnn.ENGINE_AUTO - if args.backend != 0 or args.target != 0: - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW session = cv.dnn.readNetFromONNX(onnx_model_path, engine) session.setPreferableBackend(args.backend) session.setPreferableTarget(args.target) diff --git a/samples/dnn/deblurring.cpp b/samples/dnn/deblurring.cpp index 4c910be678..af3ed5e97c 100644 --- a/samples/dnn/deblurring.cpp +++ b/samples/dnn/deblurring.cpp @@ -95,10 +95,7 @@ int main(int argc, char **argv) bool swapRB = parser.get("rgb"); Scalar mean_v = parser.get("mean"); - EngineType engine = ENGINE_AUTO; - if (backend != "default" || target != "cpu"){ - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; Net net = readNetFromONNX(modelPath, engine); net.setPreferableBackend(getBackendID(backend)); diff --git a/samples/dnn/deblurring.py b/samples/dnn/deblurring.py index 0908a23385..96cf665b19 100644 --- a/samples/dnn/deblurring.py +++ b/samples/dnn/deblurring.py @@ -83,10 +83,7 @@ def main(): args.model = findModel(args.model, args.sha1) - engine = cv.dnn.ENGINE_AUTO - - if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW net = cv.dnn.readNetFromONNX(args.model, engine) net.setPreferableBackend(get_backend_id(args.backend)) diff --git a/samples/dnn/edge_detection.cpp b/samples/dnn/edge_detection.cpp index a6993c647e..83916e7b76 100644 --- a/samples/dnn/edge_detection.cpp +++ b/samples/dnn/edge_detection.cpp @@ -159,10 +159,7 @@ int main(int argc, char** argv) { string method = parser.get("method"); String sha1 = parser.get("sha1"); string model = findModel(parser.get("model"), sha1); - EngineType engine = ENGINE_AUTO; - if (backend != "default" || target != "cpu"){ - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; parser.about(about); VideoCapture cap; diff --git a/samples/dnn/edge_detection.py b/samples/dnn/edge_detection.py index ee912baf25..bbb175bcd2 100644 --- a/samples/dnn/edge_detection.py +++ b/samples/dnn/edge_detection.py @@ -110,9 +110,7 @@ def apply_dexined(model, image): def main(func_args=None): args = get_args_parser(func_args) - engine = cv.dnn.ENGINE_AUTO - if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW cap = cv.VideoCapture(cv.samples.findFile(args.input) if args.input else 0) if not cap.isOpened(): diff --git a/samples/dnn/inpainting.cpp b/samples/dnn/inpainting.cpp index ea9d247c44..164bbfe026 100644 --- a/samples/dnn/inpainting.cpp +++ b/samples/dnn/inpainting.cpp @@ -129,10 +129,7 @@ int main(int argc, char **argv) cout<<"Model loading..."<("backend") != "default") || (parser.get("target") != "cpu")){ - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; Net net = readNet(modelPath, configPath, "", engine); int backend = getBackendID(parser.get("backend")); net.setPreferableBackend(backend); diff --git a/samples/dnn/object_detection.py b/samples/dnn/object_detection.py index db59fbaa6e..e16d5a1692 100644 --- a/samples/dnn/object_detection.py +++ b/samples/dnn/object_detection.py @@ -99,9 +99,7 @@ if args.labels: labels = f.read().rstrip('\n').split('\n') # Load a network -engine = cv.dnn.ENGINE_AUTO -if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC +engine = cv.dnn.ENGINE_NEW net = cv.dnn.readNet(args.model, args.config, "", engine) net.setPreferableBackend(get_backend_id(args.backend)) net.setPreferableTarget(get_target_id(args.target)) diff --git a/samples/dnn/person_reid.cpp b/samples/dnn/person_reid.cpp index 3f128df9f0..3c55ca0446 100644 --- a/samples/dnn/person_reid.cpp +++ b/samples/dnn/person_reid.cpp @@ -258,10 +258,7 @@ int main(int argc, char **argv) int fontSize = 50; int fontWeight = 500; - EngineType engine = ENGINE_AUTO; - if (backend != "default" || target != "cpu"){ - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; Net reidNet = readNetFromONNX(modelPath, engine); reidNet.setPreferableBackend(getBackendID(backend)); reidNet.setPreferableTarget(getTargetID(target)); diff --git a/samples/dnn/person_reid.py b/samples/dnn/person_reid.py index 5493fc6afb..4d04cdc89a 100644 --- a/samples/dnn/person_reid.py +++ b/samples/dnn/person_reid.py @@ -183,10 +183,7 @@ def main(): else: args.yolo_model = findModel(args.yolo_model, args.yolo_sha1) - engine = cv.dnn.ENGINE_AUTO - - if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW yolo_net = cv.dnn.readNetFromONNX(args.yolo_model, engine) reid_net = cv.dnn.readNetFromONNX(args.model, engine) reid_net.setPreferableBackend(get_backend_id(args.backend)) diff --git a/samples/dnn/segmentation.cpp b/samples/dnn/segmentation.cpp index 622ad1f505..1a2ebb2b35 100644 --- a/samples/dnn/segmentation.cpp +++ b/samples/dnn/segmentation.cpp @@ -214,10 +214,7 @@ int main(int argc, char **argv) CV_Assert(!model.empty()); //! [Read and initialize network] - EngineType engine = ENGINE_AUTO; - if (backend != "default" || target != "cpu"){ - engine = ENGINE_CLASSIC; - } + EngineType engine = ENGINE_NEW; Net net = readNetFromONNX(model, engine); net.setPreferableBackend(getBackendID(backend)); net.setPreferableTarget(getTargetID(target)); diff --git a/samples/dnn/segmentation.py b/samples/dnn/segmentation.py index d681a723b0..7d92fcaed4 100644 --- a/samples/dnn/segmentation.py +++ b/samples/dnn/segmentation.py @@ -100,9 +100,7 @@ def main(func_args=None): colors = [np.array(color.split(' '), np.uint8) for color in f.read().rstrip('\n').split('\n')] # Load a network - engine = cv.dnn.ENGINE_AUTO - if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW net = cv.dnn.readNetFromONNX(args.model, engine) net.setPreferableBackend(get_backend_id(args.backend)) net.setPreferableTarget(get_target_id(args.target)) diff --git a/samples/python/color_correction_model.py b/samples/python/color_correction_model.py index c8e601640f..16453d7990 100644 --- a/samples/python/color_correction_model.py +++ b/samples/python/color_correction_model.py @@ -122,9 +122,7 @@ def main(func_args=None): # Create color checker detector if args.model and args.config: # Load the DNN from TensorFlow model - engine = cv.dnn.ENGINE_AUTO - if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW net = cv.dnn.readNetFromTensorflow(args.model, args.config, engine) net.setPreferableBackend(get_backend_id(args.backend)) net.setPreferableTarget(get_target_id(args.target)) diff --git a/samples/python/macbeth_chart_detection.py b/samples/python/macbeth_chart_detection.py index 85e7ab9aaf..88a544bc5f 100644 --- a/samples/python/macbeth_chart_detection.py +++ b/samples/python/macbeth_chart_detection.py @@ -89,9 +89,7 @@ def main(func_args=None): if args.model and args.config: # Load the DNN from TensorFlow model - engine = cv.dnn.ENGINE_AUTO - if args.backend != "default" or args.target != "cpu": - engine = cv.dnn.ENGINE_CLASSIC + engine = cv.dnn.ENGINE_NEW net = cv.dnn.readNetFromTensorflow(args.model, args.config, engine) net.setPreferableBackend(get_backend_id(args.backend)) net.setPreferableTarget(get_target_id(args.target))