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 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "net.h"
  15. #include <algorithm>
  16. #if defined(USE_NCNN_SIMPLEOCV)
  17. #include "simpleocv.h"
  18. #else
  19. #include <opencv2/core/core.hpp>
  20. #include <opencv2/highgui/highgui.hpp>
  21. #endif
  22. #include <stdio.h>
  23. #include <vector>
  24. static int detect_shufflenetv2(const cv::Mat& bgr, std::vector<float>& cls_scores)
  25. {
  26. ncnn::Net shufflenetv2;
  27. shufflenetv2.opt.use_vulkan_compute = true;
  28. // https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe
  29. // models can be downloaded from https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe/releases
  30. shufflenetv2.load_param("shufflenet_v2_x0.5.param");
  31. shufflenetv2.load_model("shufflenet_v2_x0.5.bin");
  32. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 224, 224);
  33. const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};
  34. in.substract_mean_normalize(0, norm_vals);
  35. ncnn::Extractor ex = shufflenetv2.create_extractor();
  36. ex.input("data", in);
  37. ncnn::Mat out;
  38. ex.extract("fc", out);
  39. // manually call softmax on the fc output
  40. // convert result into probability
  41. // skip if your model already has softmax operation
  42. {
  43. ncnn::Layer* softmax = ncnn::create_layer("Softmax");
  44. ncnn::ParamDict pd;
  45. softmax->load_param(pd);
  46. softmax->forward_inplace(out, shufflenetv2.opt);
  47. delete softmax;
  48. }
  49. out = out.reshape(out.w * out.h * out.c);
  50. cls_scores.resize(out.w);
  51. for (int j = 0; j < out.w; j++)
  52. {
  53. cls_scores[j] = out[j];
  54. }
  55. return 0;
  56. }
  57. static int print_topk(const std::vector<float>& cls_scores, int topk)
  58. {
  59. // partial sort topk with index
  60. int size = cls_scores.size();
  61. std::vector<std::pair<float, int> > vec;
  62. vec.resize(size);
  63. for (int i = 0; i < size; i++)
  64. {
  65. vec[i] = std::make_pair(cls_scores[i], i);
  66. }
  67. std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
  68. std::greater<std::pair<float, int> >());
  69. // print topk and score
  70. for (int i = 0; i < topk; i++)
  71. {
  72. float score = vec[i].first;
  73. int index = vec[i].second;
  74. fprintf(stderr, "%d = %f\n", index, score);
  75. }
  76. return 0;
  77. }
  78. int main(int argc, char** argv)
  79. {
  80. if (argc != 2)
  81. {
  82. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  83. return -1;
  84. }
  85. const char* imagepath = argv[1];
  86. cv::Mat m = cv::imread(imagepath, 1);
  87. if (m.empty())
  88. {
  89. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  90. return -1;
  91. }
  92. std::vector<float> cls_scores;
  93. detect_shufflenetv2(m, cls_scores);
  94. print_topk(cls_scores, 3);
  95. return 0;
  96. }