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

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