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.3 kB

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