|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +#include "../precomp.hpp" |
| 6 | +#include "layers_common.hpp" |
| 7 | + |
| 8 | +#include <opencv2/dnn/shape_utils.hpp> |
| 9 | + |
| 10 | +namespace cv { namespace dnn { |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +template<typename T> |
| 15 | +class ComparatorGreater { |
| 16 | +public: |
| 17 | + ComparatorGreater(const T* data, size_t step) |
| 18 | + : data_(data), step_(step) {} |
| 19 | + |
| 20 | + void addOffset(size_t offset) { |
| 21 | + data_ += offset; |
| 22 | + } |
| 23 | + |
| 24 | + void minusOffset(size_t offset) { |
| 25 | + data_ -= offset; |
| 26 | + } |
| 27 | + |
| 28 | + bool operator()(const size_t lhs_idx, const size_t rhs_idx) { |
| 29 | + T lhs = *(data_ + lhs_idx * step_), |
| 30 | + rhs = *(data_ + rhs_idx * step_); |
| 31 | + return (lhs > rhs || (lhs == rhs && lhs_idx < rhs_idx)); |
| 32 | + } |
| 33 | + |
| 34 | +private: |
| 35 | + const T* data_; |
| 36 | + size_t step_; |
| 37 | +}; |
| 38 | + |
| 39 | +template<typename T> |
| 40 | +class ComparatorLess { |
| 41 | +public: |
| 42 | + ComparatorLess(const T* data, size_t step) |
| 43 | + : data_(data), step_(step) {} |
| 44 | + |
| 45 | + void addOffset(size_t offset) { |
| 46 | + data_ += offset; |
| 47 | + } |
| 48 | + |
| 49 | + void minusOffset(size_t offset) { |
| 50 | + data_ -= offset; |
| 51 | + } |
| 52 | + |
| 53 | + bool operator()(const size_t lhs_idx, const size_t rhs_idx) { |
| 54 | + T lhs = *(data_ + lhs_idx * step_), |
| 55 | + rhs = *(data_ + rhs_idx * step_); |
| 56 | + return (lhs < rhs || (lhs == rhs && lhs_idx < rhs_idx)); |
| 57 | + } |
| 58 | + |
| 59 | +private: |
| 60 | + const T* data_; |
| 61 | + size_t step_; |
| 62 | +}; |
| 63 | +} |
| 64 | + |
| 65 | +class TopKLayerImpl CV_FINAL : public TopKLayer |
| 66 | +{ |
| 67 | +public: |
| 68 | + TopKLayerImpl(const LayerParams& params) |
| 69 | + { |
| 70 | + setParamsFrom(params); |
| 71 | + |
| 72 | + axis = params.get<int>("axis", -1); |
| 73 | + largest = params.get<int>("largest", 1) == 1; |
| 74 | + sorted = params.get<int>("sorted", 1) == 1; |
| 75 | + CV_CheckTrue(sorted, "TopK: sorted == false is not supported"); // TODO: support sorted |
| 76 | + |
| 77 | + CV_CheckTrue(params.has("k"), "TopK: parameter k is required but missing"); |
| 78 | + K = params.get<int>("k"); |
| 79 | + } |
| 80 | + |
| 81 | + virtual bool supportBackend(int backendId) CV_OVERRIDE |
| 82 | + { |
| 83 | + return backendId == DNN_BACKEND_OPENCV; |
| 84 | + } |
| 85 | + |
| 86 | + virtual bool getMemoryShapes(const std::vector<MatShape> &inputs, |
| 87 | + const int requiredOutputs, |
| 88 | + std::vector<MatShape> &outputs, |
| 89 | + std::vector<MatShape> &internals) const CV_OVERRIDE |
| 90 | + { |
| 91 | + const auto &input_shape = inputs.front(); |
| 92 | + int input_dims = input_shape.size(); |
| 93 | + |
| 94 | + // Check if axis is valid |
| 95 | + CV_CheckGE(axis, -input_dims, "TopK: axis is out of range"); |
| 96 | + CV_CheckLT(axis, input_dims, "TopK: axis is out of range"); |
| 97 | + // Normalize axis |
| 98 | + int axis_normalized = normalize_axis(axis, input_shape.size()); |
| 99 | + |
| 100 | + // Check if K is in range (0, input_shape[axis]) |
| 101 | + CV_CheckGT(K, 0, "TopK: K needs to be a positive integer"); |
| 102 | + CV_CheckLT(K, input_shape[axis_normalized], "TopK: K is out of range"); |
| 103 | + |
| 104 | + // Assign output shape |
| 105 | + auto output_shape = input_shape; |
| 106 | + output_shape[axis_normalized] = K; |
| 107 | + outputs.assign(1, output_shape); |
| 108 | + outputs.assign(2, output_shape); // TODO: support indices of type CV_32S on 5.x |
| 109 | + |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE { |
| 114 | + std::vector<Mat> inputs; |
| 115 | + inputs_arr.getMatVector(inputs); |
| 116 | + |
| 117 | + // Normalize axis |
| 118 | + auto input_shape = shape(inputs.front()); |
| 119 | + axis = normalize_axis(axis, input_shape.size()); |
| 120 | + } |
| 121 | + |
| 122 | + template<class Comparator> |
| 123 | + void FindTopK(const Mat &input, Mat &output_value, Mat &output_index) { |
| 124 | + const auto input_shape = shape(input); |
| 125 | + size_t loops = std::accumulate(input_shape.begin(), input_shape.begin() + axis, 1, std::multiplies<int>()); |
| 126 | + size_t step = std::accumulate(input_shape.begin() + axis + 1, input_shape.end(), 1, std::multiplies<int>()); |
| 127 | + int dim_axis = input_shape[axis]; |
| 128 | + if (loops == 1) { |
| 129 | + auto worker = [&](const Range &r) { |
| 130 | + const auto *input_ptr = input.ptr<const float>(); // TODO: support other input type |
| 131 | + auto *output_value_ptr = output_value.ptr<float>(); |
| 132 | + auto *output_index_ptr = output_index.ptr<float>(); // TODO: use CV_32S on 5.x |
| 133 | + |
| 134 | + Comparator cmp(input_ptr, step); |
| 135 | + |
| 136 | + AutoBuffer<int> buffer_index(dim_axis); |
| 137 | + auto *buffer_index_ptr = buffer_index.data(); |
| 138 | + for (int offset = r.start; offset < r.end; offset++) { |
| 139 | + const auto *input_offset_ptr = input_ptr + offset; |
| 140 | + cmp.addOffset(offset); |
| 141 | + |
| 142 | + std::iota(buffer_index_ptr, buffer_index_ptr + dim_axis, 0); |
| 143 | + std::stable_sort(buffer_index_ptr, buffer_index_ptr + dim_axis, cmp); |
| 144 | + |
| 145 | + auto *output_value_offset_ptr = output_value_ptr + offset; |
| 146 | + auto *output_index_offset_ptr = output_index_ptr + offset; |
| 147 | + for (int i = 0; i < K; i++) { |
| 148 | + int source_index = buffer_index_ptr[i]; |
| 149 | + output_value_offset_ptr[i * step] = *(input_offset_ptr + source_index * step); |
| 150 | + output_index_offset_ptr[i * step] = source_index; |
| 151 | + } |
| 152 | + cmp.minusOffset(offset); |
| 153 | + } |
| 154 | + }; |
| 155 | + parallel_for_(Range(0, step), worker); |
| 156 | + } else { |
| 157 | + auto worker = [&](const Range &r) { |
| 158 | + const auto *input_ptr = input.ptr<const float>(); |
| 159 | + auto *output_value_ptr = output_value.ptr<float>(); |
| 160 | + auto *output_index_ptr = output_index.ptr<float>(); |
| 161 | + |
| 162 | + Comparator cmp(input_ptr, step); |
| 163 | + |
| 164 | + AutoBuffer<int> buffer_index(dim_axis); |
| 165 | + auto *buffer_index_ptr = buffer_index.data(); |
| 166 | + for (int batch_index = r.start; batch_index < r.end; batch_index++) { |
| 167 | + for (size_t offset = 0; offset < step; offset++) { |
| 168 | + const auto *input_offset_ptr = input_ptr + batch_index * dim_axis * step + offset; |
| 169 | + cmp.addOffset(batch_index * dim_axis * step + offset); |
| 170 | + |
| 171 | + std::iota(buffer_index_ptr, buffer_index_ptr + dim_axis, 0); |
| 172 | + std::stable_sort(buffer_index_ptr, buffer_index_ptr + dim_axis, cmp); |
| 173 | + |
| 174 | + auto *output_value_offset_ptr = output_value_ptr + batch_index * K * step + offset; |
| 175 | + auto *output_index_offset_ptr = output_index_ptr + batch_index * K * step + offset; |
| 176 | + for (int i = 0; i < K; i++) { |
| 177 | + int source_index = buffer_index_ptr[i]; |
| 178 | + output_value_offset_ptr[i * step] = *(input_offset_ptr + source_index * step); |
| 179 | + output_index_offset_ptr[i * step] = source_index; |
| 180 | + } |
| 181 | + cmp.minusOffset(batch_index * dim_axis * step + offset); |
| 182 | + } |
| 183 | + } |
| 184 | + }; |
| 185 | + parallel_for_(Range(0, loops), worker); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE |
| 190 | + { |
| 191 | + CV_TRACE_FUNCTION(); |
| 192 | + CV_TRACE_ARG_VALUE(name, "name", name.c_str()); |
| 193 | + |
| 194 | + if (inputs_arr.depth() == CV_16F) |
| 195 | + { |
| 196 | + forward_fallback(inputs_arr, outputs_arr, internals_arr); |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + std::vector<Mat> inputs, outputs; |
| 201 | + inputs_arr.getMatVector(inputs); |
| 202 | + outputs_arr.getMatVector(outputs); |
| 203 | + |
| 204 | + const auto &input = inputs.front(); |
| 205 | + auto &output_value = outputs.front(); |
| 206 | + auto &output_index = outputs.back(); |
| 207 | + |
| 208 | + if (largest) { |
| 209 | + FindTopK<ComparatorGreater<float>>(input, output_value, output_index); |
| 210 | + } else { |
| 211 | + FindTopK<ComparatorLess<float>>(input, output_value, output_index); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | +private: |
| 216 | + int axis; |
| 217 | + bool largest; |
| 218 | + bool sorted; |
| 219 | + |
| 220 | + int K; // FIXIT: make it layer input once dynamic shape is supported |
| 221 | +}; |
| 222 | + |
| 223 | +Ptr<TopKLayer> TopKLayer::create(const LayerParams& params) |
| 224 | +{ |
| 225 | + return makePtr<TopKLayerImpl>(params); |
| 226 | +} |
| 227 | + |
| 228 | +}} // namespace cv::dnn |
0 commit comments