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.

squeezenet.cpp 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
  25. {
  26. ncnn::Net squeezenet;
  27. #if NCNN_VULKAN
  28. squeezenet.opt.use_vulkan_compute = true;
  29. #endif // NCNN_VULKAN
  30. // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
  31. squeezenet.load_param("squeezenet_v1.1.param");
  32. squeezenet.load_model("squeezenet_v1.1.bin");
  33. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
  34. const float mean_vals[3] = {104.f, 117.f, 123.f};
  35. in.substract_mean_normalize(mean_vals, 0);
  36. ncnn::Extractor ex = squeezenet.create_extractor();
  37. ex.input("data", in);
  38. ncnn::Mat out;
  39. ex.extract("prob", out);
  40. cls_scores.resize(out.w);
  41. for (int j=0; j<out.w; j++)
  42. {
  43. cls_scores[j] = out[j];
  44. }
  45. return 0;
  46. }
  47. static int print_topk(const std::vector<float>& cls_scores, int topk)
  48. {
  49. // partial sort topk with index
  50. int size = cls_scores.size();
  51. std::vector< std::pair<float, int> > vec;
  52. vec.resize(size);
  53. for (int i=0; i<size; i++)
  54. {
  55. vec[i] = std::make_pair(cls_scores[i], i);
  56. }
  57. std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
  58. std::greater< std::pair<float, int> >());
  59. // print topk and score
  60. for (int i=0; i<topk; i++)
  61. {
  62. float score = vec[i].first;
  63. int index = vec[i].second;
  64. fprintf(stderr, "%d = %f\n", index, score);
  65. }
  66. return 0;
  67. }
  68. int main(int argc, char** argv)
  69. {
  70. if (argc != 2)
  71. {
  72. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  73. return -1;
  74. }
  75. const char* imagepath = argv[1];
  76. cv::Mat m = cv::imread(imagepath, 1);
  77. if (m.empty())
  78. {
  79. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  80. return -1;
  81. }
  82. #if NCNN_VULKAN
  83. ncnn::create_gpu_instance();
  84. #endif // NCNN_VULKAN
  85. std::vector<float> cls_scores;
  86. detect_squeezenet(m, cls_scores);
  87. #if NCNN_VULKAN
  88. ncnn::destroy_gpu_instance();
  89. #endif // NCNN_VULKAN
  90. print_topk(cls_scores, 3);
  91. return 0;
  92. }