// Tencent is pleased to support the open source community by making ncnn available. // // Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. // // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except // in compliance with the License. You may obtain a copy of the License at // // https://opensource.org/licenses/BSD-3-Clause // // Unless required by applicable law or agreed to in writing, software distributed // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. #include #include #include #include #include #include "platform.h" #include "net.h" #if NCNN_VULKAN #include "gpu.h" #endif // NCNN_VULKAN static int detect_shufflenetv2(const cv::Mat& bgr, std::vector& cls_scores) { ncnn::Net shufflenetv2; #if NCNN_VULKAN shufflenetv2.opt.use_vulkan_compute = true; #endif // NCNN_VULKAN // https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe // models can be downloaded from https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe/releases shufflenetv2.load_param("shufflenet_v2_x0.5.param"); shufflenetv2.load_model("shufflenet_v2_x0.5.bin"); ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 224, 224); const float norm_vals[3] = {1/255.f, 1/255.f, 1/255.f}; in.substract_mean_normalize(0, norm_vals); ncnn::Extractor ex = shufflenetv2.create_extractor(); ex.input("data", in); ncnn::Mat out; ex.extract("fc", out); // manually call softmax on the fc output // convert result into probability // skip if your model already has softmax operation { ncnn::Layer* softmax = ncnn::create_layer("Softmax"); ncnn::ParamDict pd; softmax->load_param(pd); softmax->forward_inplace(out); delete softmax; } out = out.reshape(out.w * out.h * out.c); cls_scores.resize(out.w); for (int j=0; j& cls_scores, int topk) { // partial sort topk with index int size = cls_scores.size(); std::vector< std::pair > vec; vec.resize(size); for (int i=0; i >()); // print topk and score for (int i=0; i cls_scores; detect_shufflenetv2(m, cls_scores); #if NCNN_VULKAN ncnn::destroy_gpu_instance(); #endif // NCNN_VULKAN print_topk(cls_scores, 3); return 0; }