From d8f8267700ffbfd783a52edcd95dd5c8de5c18fa Mon Sep 17 00:00:00 2001 From: Sikandar Date: Tue, 27 Jan 2026 09:35:36 +0500 Subject: [PATCH] Fix: TopK layer K boundary check allows K == input_dim (#28445) The TopK layer was incorrectly rejecting K values equal to the input dimension size. According to ONNX specification, K should be allowed to equal the dimension size (to retrieve all elements). Changed validation from 'K < input_shape[axis]' to 'K <= input_shape[axis]' This fixes the error when loading YOLOv10 ONNX models that use TopK with K equal to the dimension size. Fixes #28445 --- modules/dnn/src/layers/topk_layer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/dnn/src/layers/topk_layer.cpp b/modules/dnn/src/layers/topk_layer.cpp index 06b3ebdc37..8739e14c2b 100644 --- a/modules/dnn/src/layers/topk_layer.cpp +++ b/modules/dnn/src/layers/topk_layer.cpp @@ -97,9 +97,9 @@ public: // Normalize axis int axis_normalized = normalize_axis(axis, input_shape.size()); - // Check if K is in range (0, input_shape[axis]) + // Check if K is in range (0, input_shape[axis]] CV_CheckGT(K, 0, "TopK: K needs to be a positive integer"); - CV_CheckLT(K, input_shape[axis_normalized], "TopK: K is out of range"); + CV_CheckLE(K, input_shape[axis_normalized], "TopK: K is out of range"); // Assign output shape auto output_shape = input_shape;