mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 13:16:32 -06:00
TopKLayerImpl::finalize previously normalized the axis attribute by overwriting this->axis with normalize_axis(axis, input_shape.size()). When the layer was created with a negative axis (such as the default -1), this replaced the relative index with a positive index fixed to the rank of the first input. Subsequent runs on an input of different rank then either: - evaluated TopK along the wrong axis (e.g. axis 1 instead of axis 2 when moving from a 2D tensor to a 3D tensor), or - triggered an out-of-range assertion in getMemoryShapes (e.g. axis 2 on a 2D tensor when moving from a 3D tensor to a 2D tensor). Fix this by keeping this->axis unmodified and storing the normalized axis separately for execution. Add unit tests for both increasing and decreasing rank inputs on the same TopK layer instance.