You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

shufflenetv2.cpp 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "net.h"
  4. #include <algorithm>
  5. #if defined(USE_NCNN_SIMPLEOCV)
  6. #include "simpleocv.h"
  7. #else
  8. #include <opencv2/core/core.hpp>
  9. #include <opencv2/highgui/highgui.hpp>
  10. #endif
  11. #include <stdio.h>
  12. #include <vector>
  13. static int detect_shufflenetv2(const cv::Mat& bgr, std::vector<float>& cls_scores)
  14. {
  15. ncnn::Net shufflenetv2;
  16. shufflenetv2.opt.use_vulkan_compute = true;
  17. // https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe
  18. // models can be downloaded from https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe/releases
  19. if (shufflenetv2.load_param("shufflenet_v2_x0.5.param"))
  20. exit(-1);
  21. if (shufflenetv2.load_model("shufflenet_v2_x0.5.bin"))
  22. exit(-1);
  23. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 224, 224);
  24. const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};
  25. in.substract_mean_normalize(0, norm_vals);
  26. ncnn::Extractor ex = shufflenetv2.create_extractor();
  27. ex.input("data", in);
  28. ncnn::Mat out;
  29. ex.extract("fc", out);
  30. // manually call softmax on the fc output
  31. // convert result into probability
  32. // skip if your model already has softmax operation
  33. {
  34. ncnn::Layer* softmax = ncnn::create_layer("Softmax");
  35. ncnn::ParamDict pd;
  36. softmax->load_param(pd);
  37. softmax->forward_inplace(out, shufflenetv2.opt);
  38. delete softmax;
  39. }
  40. out = out.reshape(out.w * out.h * out.c);
  41. cls_scores.resize(out.w);
  42. for (int j = 0; j < out.w; j++)
  43. {
  44. cls_scores[j] = out[j];
  45. }
  46. return 0;
  47. }
  48. static int print_topk(const std::vector<float>& cls_scores, int topk)
  49. {
  50. // partial sort topk with index
  51. int size = cls_scores.size();
  52. std::vector<std::pair<float, int> > vec;
  53. vec.resize(size);
  54. for (int i = 0; i < size; i++)
  55. {
  56. vec[i] = std::make_pair(cls_scores[i], i);
  57. }
  58. std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
  59. std::greater<std::pair<float, int> >());
  60. // print topk and score
  61. for (int i = 0; i < topk; i++)
  62. {
  63. float score = vec[i].first;
  64. int index = vec[i].second;
  65. fprintf(stderr, "%d = %f\n", index, score);
  66. }
  67. return 0;
  68. }
  69. int main(int argc, char** argv)
  70. {
  71. if (argc != 2)
  72. {
  73. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  74. return -1;
  75. }
  76. const char* imagepath = argv[1];
  77. cv::Mat m = cv::imread(imagepath, 1);
  78. if (m.empty())
  79. {
  80. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  81. return -1;
  82. }
  83. std::vector<float> cls_scores;
  84. detect_shufflenetv2(m, cls_scores);
  85. print_topk(cls_scores, 3);
  86. return 0;
  87. }