From 5d0dff1aac293532d5b58bf775f868a78dcc7c01 Mon Sep 17 00:00:00 2001 From: Samaresh Kumar Singh Date: Wed, 20 May 2026 12:48:50 -0500 Subject: [PATCH] dnn: register RandomNormalLike layer explicitly in init.cpp randomnormallike_layer.cpp registered itself via CV_DNN_REGISTER_LAYER_CLASS_STATIC at file scope. Nothing else in the translation unit was referenced from elsewhere, so when OpenCV is built statically (BUILD_SHARED_LIBS=OFF, as in the reporter's MSVC build) the linker drops the object file and the static-init registration never runs. The ONNX importer then sets layerParams.type = "RandomNormalLike" but getLayerInstance has no factory for that type, and parsing fails with "Can't create layer of type RandomNormalLike". Switch to the standard pattern used by every other layer in the module: declare RandomNormalLikeLayer in all_layers.hpp, expose a static create() factory from the .cpp, and register it explicitly from initializeLayerFactory in init.cpp. Because init.cpp is referenced by the DNN module init path, this pulls in the layer .cpp regardless of build mode and the registration always runs. The failure was incorrectly attributed to MSVC in the bug report. The bug is build-mode sensitive (static vs shared), not platform sensitive. Verified locally on linux/gcc-13 by building modules/dnn and opencv_test_dnn against the patched tree, then running opencv_test_dnn --gtest_filter='Test_ONNX_layers.RandomNormalLike_basic/0:Test_ONNX_layers.RandomNormalLike_complex/0' Both tests pass; without the patch the same binary reproduces the "Can't create layer of type RandomNormalLike" error from the report. --- modules/dnn/include/opencv2/dnn/all_layers.hpp | 6 ++++++ modules/dnn/src/init.cpp | 1 + modules/dnn/src/layers/randomnormallike_layer.cpp | 8 +++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/all_layers.hpp b/modules/dnn/include/opencv2/dnn/all_layers.hpp index d9d1833780..24e792cb11 100644 --- a/modules/dnn/include/opencv2/dnn/all_layers.hpp +++ b/modules/dnn/include/opencv2/dnn/all_layers.hpp @@ -1204,6 +1204,12 @@ CV__DNN_INLINE_NS_BEGIN static Ptr create(const LayerParams& params); }; + class CV_EXPORTS RandomNormalLikeLayer : public Layer + { + public: + static Ptr create(const LayerParams& params); + }; + //! @} //! @} CV__DNN_INLINE_NS_END diff --git a/modules/dnn/src/init.cpp b/modules/dnn/src/init.cpp index 61db2c1ba6..eea68d6af1 100644 --- a/modules/dnn/src/init.cpp +++ b/modules/dnn/src/init.cpp @@ -200,6 +200,7 @@ void initializeLayerFactory() CV_DNN_REGISTER_LAYER_CLASS(ScatterND, ScatterNDLayer); CV_DNN_REGISTER_LAYER_CLASS(Tile, TileLayer); CV_DNN_REGISTER_LAYER_CLASS(TopK, TopKLayer); + CV_DNN_REGISTER_LAYER_CLASS(RandomNormalLike, RandomNormalLikeLayer); CV_DNN_REGISTER_LAYER_CLASS(Quantize, QuantizeLayer); CV_DNN_REGISTER_LAYER_CLASS(Dequantize, DequantizeLayer); diff --git a/modules/dnn/src/layers/randomnormallike_layer.cpp b/modules/dnn/src/layers/randomnormallike_layer.cpp index 30713390c9..4bd6ca5728 100644 --- a/modules/dnn/src/layers/randomnormallike_layer.cpp +++ b/modules/dnn/src/layers/randomnormallike_layer.cpp @@ -6,12 +6,11 @@ #include "../precomp.hpp" #include "layers_common.hpp" -#include #include namespace cv { namespace dnn { -class RandomNormalLikeLayerImpl CV_FINAL : public Layer +class RandomNormalLikeLayerImpl CV_FINAL : public RandomNormalLikeLayer { public: RandomNormalLikeLayerImpl(const LayerParams& params) @@ -117,6 +116,9 @@ private: int depth; }; -CV_DNN_REGISTER_LAYER_CLASS_STATIC(RandomNormalLike, RandomNormalLikeLayerImpl); +Ptr RandomNormalLikeLayer::create(const LayerParams& params) +{ + return makePtr(params); +} }} // namespace cv::dnn