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

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