nndeploy C++ API  0.2.0
nndeploy C++ API
util.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_CLASSIFICATION_CLASSIFICATION_UTIL_H_
3 #define _NNDEPLOY_CLASSIFICATION_CLASSIFICATION_UTIL_H_
4 
5 #include "nndeploy/base/any.h"
6 #include "nndeploy/base/common.h"
8 #include "nndeploy/base/log.h"
9 #include "nndeploy/base/macro.h"
10 #include "nndeploy/base/object.h"
11 #include "nndeploy/base/param.h"
12 #include "nndeploy/base/status.h"
13 #include "nndeploy/base/string.h"
14 #include "nndeploy/base/type.h"
16 #include "nndeploy/device/buffer.h"
17 #include "nndeploy/device/device.h"
19 #include "nndeploy/device/tensor.h"
20 
21 namespace nndeploy {
22 namespace classification {
23 
24 // topk sometimes is a very small value
25 // so this implementation is simple but I don't think it will
26 // cost too much time
27 // Also there may be cause problem since we suppose the minimum value is
28 // -99999999
29 // Do not use this function on array which topk contains value less than
30 // -99999999
31 template <typename T>
32 std::vector<int32_t> topKIndices(const T* array, int array_size, int topk) {
33  topk = std::min(array_size, topk);
34  std::vector<int32_t> res(topk);
35  std::set<int32_t> searched;
36  for (int32_t i = 0; i < topk; ++i) {
37  T min = static_cast<T>(-99999999);
38  for (int32_t j = 0; j < array_size; ++j) {
39  if (searched.find(j) != searched.end()) {
40  continue;
41  }
42  if (*(array + j) > min) {
43  res[i] = j;
44  min = *(array + j);
45  }
46  }
47  searched.insert(res[i]);
48  }
49  return res;
50 }
51 
52 } // namespace classification
53 } // namespace nndeploy
54 
55 #endif /* _NNDEPLOY_CLASSIFICATION_CLASSIFICATION_COMMON_H_ */
std::vector< int32_t > topKIndices(const T *array, int array_size, int topk)
Definition: util.h:32