// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2020 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 "layer.h"
#include "net.h"
#if defined(USE_NCNN_SIMPLEOCV)
#include "simpleocv.h"
#else
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif
#include <float.h>
#include <stdio.h>
#include <vector>
//#define YOLOV5_V60 1 //YOLOv5 v6.0
#define YOLOV5_V62 1 //YOLOv5 v6.2 export onnx model method https://github.com/shaoshengsong/yolov5_62_export_ncnn
#if YOLOV5_V60 || YOLOV5_V62
#define MAX_STRIDE 64
#else
#define MAX_STRIDE 32
class YoloV5Focus : public ncnn::Layer
{
public:
YoloV5Focus()
{
one_blob_only = true;
}
virtual int forward(const ncnn::Mat& bottom_blob, ncnn::Mat& top_blob, const ncnn::Option& opt) const
{
int w = bottom_blob.w;
int h = bottom_blob.h;
int channels = bottom_blob.c;
int outw = w / 2;
int outh = h / 2;
int outc = channels * 4;
top_blob.create(outw, outh, outc, 4u, 1, opt.blob_allocator);
if (top_blob.empty())
return -100;
#pragma omp parallel for num_threads(opt.num_threads)
for (int p = 0; p < outc; p++)
{
const float* ptr = bottom_blob.channel(p % channels).row((p / channels) % 2) + ((p / channels) / 2);
float* outptr = top_blob.channel(p);
for (int i = 0; i < outh; i++)
{
for (int j = 0; j < outw; j++)
{
*outptr = *ptr;
outptr += 1;
ptr += 2;
}
ptr += w;
}
}
return 0;
}
};
DEFINE_LAYER_CREATOR(YoloV5Focus)
#endif //YOLOV5_V60 YOLOV5_V62
struct Object
{
cv::Rect_<float> rect;
int label;
float prob;
};
static inline float intersection_area(const Object& a, const Object& b)
{
cv::Rect_<float> inter = a.rect & b.rect;
return inter.area();
}
static void qsort_descent_inplace(std::vector<Object>& faceobjects, int left, int right)
{
int i = left;
int j = right;
float p = faceobjects[(left + right) / 2].prob;
while (i <= j)
{
while (faceobjects[i].prob > p)
i++;
while (faceobjects[j].prob < p)
j--;
if (i <= j)
{
// swap
std::swap(faceobjects[i], faceobjects[j]);
i++;
j--;
}
}
#pragma omp parallel sections
{
#pragma omp section
{
if (left < j) qsort_descent_inplace(faceobjects, left, j);
}
#pragma omp section
{
if (i < right) qsort_descent_inplace(faceobjects, i, right);
}
}
}
static void qsort_descent_inplace(std::vector<Object>& faceobjects)
{
if (faceobjects.empty())
return;
qsort_descent_inplace(faceobjects, 0, faceobjects.size() - 1);
}
static void nms_sorted_bboxes(const std::vector<Object>& faceobjects, std::vector<int>& picked, float nms_threshold, bool agnostic = false)
{
picked.clear();
const int n = faceobjects.size();
std::vector<float> areas(n);
for (int i = 0; i < n; i++)
{
areas[i] = faceobjects[i].rect.area();
}
for (int i = 0; i < n; i++)
{
const Object& a = faceobjects[i];
int keep = 1;
for (int j = 0; j < (int)picked.size(); j++)
{
const Object& b = faceobjects[picked[j]];
if (!agnostic && a.label != b.label)
continue;
// intersection over union
float inter_area = intersection_area(a, b);
float union_area = areas[i] + areas[picked[j]] - inter_area;
// float IoU = inter_area / union_area
if (inter_area / union_area > nms_threshold)
keep = 0;
}
if (keep)
picked.push_back(i);
}
}
static inline float sigmoid(float x)
{
return static_cast<float>(1.f / (1.f + exp(-x)));
}
static void generate_proposals(const ncnn::Mat& anchors, int stride, const ncnn::Mat& in_pad, const ncnn::Mat& feat_blob, float prob_threshold, std::vector<Object>& objects)
{
const int num_grid = feat_blob.h;
int num_grid_x;
int num_grid_y;
if (in_pad.w > in_pad.h)
{
num_grid_x = in_pad.w / stride;
num_grid_y = num_grid / num_grid_x;
}
else
{
num_grid_y = in_pad.h / stride;
num_grid_x = num_grid / num_grid_y;
}
const int num_class = feat_blob.w - 5;
const int num_anchors = anchors.w / 2;
for (int q = 0; q < num_anchors; q++)
{
const float anchor_w = anchors[q * 2];
const float anchor_h = anchors[q * 2 + 1];
const ncnn::Mat feat = feat_blob.channel(q);
for (int i = 0; i < num_grid_y; i++)
{
for (int j = 0; j < num_grid_x; j++)
{
const float* featptr = feat.row(i * num_grid_x + j);
float box_confidence = sigmoid(featptr[4]);
if (box_confidence >= prob_threshold)
{
// find class index with max class score
int class_index = 0;
float class_score = -FLT_MAX;
for (int k = 0; k < num_class; k++)
{
float score = featptr[5 + k];
if (score > class_score)
{
class_index = k;
class_score = score;
}
}
float confidence = box_confidence * sigmoid(class_score);
if (confidence >= prob_threshold)
{
// yolov5/models/yolo.py Detect forward
// y = x[i].sigmoid()
// y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i].to(x[i].device)) * self.stride[i] # xy
// y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
float dx = sigmoid(featptr[0]);
float dy = sigmoid(featptr[1]);
float dw = sigmoid(featptr[2]);
float dh = sigmoid(featptr[3]);
float pb_cx = (dx * 2.f - 0.5f + j) * stride;
float pb_cy = (dy * 2.f - 0.5f + i) * stride;
float pb_w = pow(dw * 2.f, 2) * anchor_w;
float pb_h = pow(dh * 2.f, 2) * anchor_h;
float x0 = pb_cx - pb_w * 0.5f;
float y0 = pb_cy - pb_h * 0.5f;
float x1 = pb_cx + pb_w * 0.5f;
float y1 = pb_cy + pb_h * 0.5f;
Object obj;
obj.rect.x = x0;
obj.rect.y = y0;
obj.rect.width = x1 - x0;
obj.rect.height = y1 - y0;
obj.label = class_index;
obj.prob = confidence;
objects.push_back(obj);
}
}
}
}
}
}
static int detect_yolov5(const cv::Mat& bgr, std::vector<Object>& objects)
{
没有合适的资源?快使用搜索试试~ 我知道了~
ncnn vulkan x86-64下的使用 完整源码
共278个文件
hpp:143个
h:93个
cmake:15个
需积分: 0 0 下载量 11 浏览量
2023-07-14
16:10:18
上传
评论 2
收藏 32.41MB ZIP 举报
温馨提示
ncnn vulkan x86_64下的使用 完整源码
资源推荐
资源详情
资源评论
收起资源包目录
ncnn vulkan x86-64下的使用 完整源码 (278个子文件)
libncnn.a 19.39MB
libopencv_imgproc.a 12.26MB
libopencv_core.a 8.17MB
libMachineIndependent.a 4.31MB
libopencv_features2d.a 1.7MB
libopencv_photo.a 1.28MB
libSPIRV.a 1.12MB
libopencv_video.a 1020KB
libopencv_highgui.a 311KB
libglslang.a 40KB
libGenericCodeGen.a 24KB
libOSDependent.a 4KB
libOGLCompiler.a 3KB
caffe2ncnn 918KB
OpenCVConfig.cmake 14KB
OpenCVModules.cmake 5KB
glslang-targets.cmake 5KB
ncnn.cmake 5KB
glslang-targets-release.cmake 3KB
OpenCVModules-release.cmake 3KB
glslang-config-version.cmake 3KB
ncnnConfig.cmake 1KB
glslang-config.cmake 975B
ncnn-release.cmake 789B
glslangTargets.cmake 644B
OpenCVConfig-version.cmake 418B
OSDependentTargets.cmake 357B
OGLCompilerTargets.cmake 357B
SPIRVTargets.cmake 333B
yolov5.cpp 16KB
darknet2ncnn 90KB
core_c.h 126KB
Types.h 101KB
msa_macros.h 81KB
types_c.h 70KB
mat.h 66KB
intermediate.h 58KB
imgproc_c.h 50KB
localintermediate.h 49KB
SpvBuilder.h 43KB
ShaderLang.h 43KB
hex_float.h 39KB
cvdef.h 36KB
ConstantUnion.h 36KB
SymbolTable.h 36KB
ParseHelper.h 31KB
glslang_tab.cpp.h 27KB
cv_cpu_helper.h 27KB
PpContext.h 25KB
Versions.h 23KB
BaseTypes.h 21KB
types_c.h 18KB
spvIR.h 17KB
vulkan_header_fix.h 17KB
c_api.h 16KB
gpu.h 15KB
iomapper.h 14KB
parseVersions.h 13KB
allocator.h 13KB
SPVRemapper.h 12KB
PoolAlloc.h 11KB
simplestl.h 11KB
glslang_c_interface.h 11KB
arrays.h 11KB
layer_shader_type_enum.h 11KB
gl_types.h 10KB
Common.h 10KB
simpleocv.h 10KB
Scan.h 9KB
glslang_c_shader_types.h 9KB
cv_cpu_dispatch.h 8KB
reflection.h 8KB
net.h 8KB
doc.h 8KB
NonSemanticShaderDebugInfo100.h 8KB
platform.h 7KB
layer.h 6KB
LiveTraverser.h 6KB
ShHandle.h 6KB
PpTokens.h 6KB
ResourceLimits.h 6KB
cpu.h 5KB
InfoSink.h 5KB
Initialize.h 5KB
interface.h 5KB
command.h 5KB
option.h 4KB
attribute.h 4KB
GLSL.std.450.h 4KB
SpirvIntrinsics.h 4KB
GLSL.ext.KHR.h 4KB
SpvTools.h 4KB
GLSL.ext.AMD.h 4KB
pipeline.h 3KB
GLSL.ext.NV.h 3KB
datareader.h 3KB
bitutils.h 3KB
cvconfig.h 3KB
ScanContext.h 3KB
pipelinecache.h 3KB
共 278 条
- 1
- 2
- 3
资源评论
西笑生
- 粉丝: 2w+
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 农村信用社联合社计算机信息系统投产与变更管理办.docx
- 农村信用社联合社计算机信息系统数据管理办法.docx
- 利用SPSS作临床效度分析线上计算网站介绍-医学研究部统计谘.(医学PPT课件).ppt
- 利用Zabbix监控mysqldump定时备份数据库状态.docx
- 利用计算机解决问题的基本过程.doc
- 化工铁路通信工程总结.doc
- 北京大学网络教育软件工程作业.docx
- 医药公司(连锁店)计算机操作规程未新系统的自行按照旧制修改-新系统过制的编号加修模版.doc
- 医药公司(连锁店)计算机系统操作规程模版.doc
- 医药连锁门店计算机系统的操作和管理程序未新系统的自行按照旧制修改-新系统过制的编号加修模版.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功