计算机视觉——P2PNet基于点估计的人群计数原理与C++模型推理

本文主要是介绍计算机视觉——P2PNet基于点估计的人群计数原理与C++模型推理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

人群计数是计算机视觉领域的一个核心任务,旨在估算静止图像或视频帧中的行人数量。在过去几十年中,研究人员在这个领域投入了大量的精力,并在提高现有主流基准数据集性能方面取得了显著进展。然而,训练卷积神经网络需要大规模且高质量的标记数据集,而标记像素级别的行人位置成本昂贵,令人望而却步。

此外,由于数据分布之间存在领域转移,即在标签丰富的数据领域(源领域)上训练的模型无法很好地泛化到另一个标签稀缺的数据领域(目标领域),这严重限制了现有方法的实际应用。

《Rethinking Counting and Localization in Crowds: A Purely Point-Based Framework》提出了一个全新的基于点的框架,可以同时用于人群计数和个体定位。与传统的基于定位的方法不同,该框架完全依赖于点级别的表示,避免了中间表示(如密度图或伪目标框)可能引入的误差,并提出了一种新的性能评价指标,称为密度归一化平均精度,以更全面、更准确地评估模型性能。

研究团队还提出了一个名为点对点网络(P2PNet)的示例模型,该模型直接预测一系列人头点的集合来定位图像中的人群个体,避免了冗余步骤,并实现了与真实人工标注一致的定位。通过深入分析,研究者发现了实现该方法的核心策略,即为预测的候选点分配最优的学习目标,并通过基于匈牙利算法的一对一匹配策略来实现。实验证明,P2PNet在人群计数基准上显著超越了现有的最先进方法,并取得了非常高的定位精度。
在这里插入图片描述

网络结构

在这里插入图片描述
P2PNet的网络结构并不复杂。它建立在VGG16的基础上,并引入了一个上采样路径来获取细粒度的深度特征图,类似于特征金字塔网络(FPN)。然后,它利用两个分支来同时预测一组点及其置信度分数。在我们的流程中,关键步骤是确保预测点和真实点之间的一对一匹配,这决定了这些预测点的学习目标。

预测

在这里插入图片描述
Point proposals的初始化有两种方式,一种是全部初始化在中心点,另一种是网格式分布。Feature Map上的一个pixel对应着原图上的一个patch(sxs),并在这上面初始化K个Point proposal。
在这里插入图片描述
这些point proposals的坐标加上回归头分支得到的偏置就可以得到预测点的坐标。

匹配与损失计算

在这里插入图片描述
预测点与真实点之间的匹配用的是匈牙利算法,代价矩阵的计算方式如上图,它是坐标偏差与置信度分数的一个综合的考量。
在这里插入图片描述
分类损失函数是交叉熵损失,回归损失函数是欧氏距离。

在这里插入图片描述
文章还提出了一种新的度量指标nAP。nAP是根据平均精度计算出来的,平均精度是精度-召回率(PR)曲线下的面积。具体来说,给定所有预测的头部点ˆP,我们首先将其置信度得分从高到低进行排序。然后,根据预定义的密度感知标准,依次确定所调查的点是TP或FP。密度感知标准如上左图所示。

实验结果

在这里插入图片描述
研究者考虑了从ShanghaiTech Part A到Trancos的实验,如上表所示。显然,所提出的方法比现有的适应方法提高了2.9%。
在这里插入图片描述
由双重鉴别器生成的不同级别(分别为像素、补丁像素、补丁、图像)级别分数的可视化。图中的正方形代表一个标量。注意白色方块代表1,黑色方块代表0。

实现代码

训练代码可以参考:https://github.com/TencentYoutuResearch/CrowdCounting-P2PNet

推理代码可以参考下面的代码:

#include <sstream>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>using namespace cv;
using namespace dnn;
using namespace std;struct CrowdPoint
{cv::Point pt;float prob;
};static void shift(int w, int h, int stride, vector<float> anchor_points, vector<float>& shifted_anchor_points)
{vector<float> x_, y_;for (int i = 0; i < w; i++){float x = (i + 0.5) * stride;x_.push_back(x);}for (int i = 0; i < h; i++){float y = (i + 0.5) * stride;y_.push_back(y);}vector<float> shift_x((size_t)w * h, 0), shift_y((size_t)w * h, 0);for (int i = 0; i < h; i++){for (int j = 0; j < w; j++){shift_x[i * w + j] = x_[j];}}for (int i = 0; i < h; i++){for (int j = 0; j < w; j++){shift_y[i * w + j] = y_[i];}}vector<float> shifts((size_t)w * h * 2, 0);for (int i = 0; i < w * h; i++){shifts[i * 2] = shift_x[i];shifts[i * 2 + 1] = shift_y[i];}shifted_anchor_points.resize((size_t)2 * w * h * anchor_points.size() / 2, 0);for (int i = 0; i < w * h; i++){for (int j = 0; j < anchor_points.size() / 2; j++){float x = anchor_points[j * 2] + shifts[i * 2];float y = anchor_points[j * 2 + 1] + shifts[i * 2 + 1];shifted_anchor_points[i * anchor_points.size() / 2 * 2 + j * 2] = x;shifted_anchor_points[i * anchor_points.size() / 2 * 2 + j * 2 + 1] = y;}}
}
static void generate_anchor_points(int stride, int row, int line, vector<float>& anchor_points)
{float row_step = (float)stride / row;float line_step = (float)stride / line;vector<float> x_, y_;for (int i = 1; i < line + 1; i++){float x = (i - 0.5) * line_step - stride / 2;x_.push_back(x);}for (int i = 1; i < row + 1; i++){float y = (i - 0.5) * row_step - stride / 2;y_.push_back(y);}vector<float> shift_x((size_t)row * line, 0), shift_y((size_t)row * line, 0);for (int i = 0; i < row; i++){for (int j = 0; j < line; j++){shift_x[i * line + j] = x_[j];}}for (int i = 0; i < row; i++){for (int j = 0; j < line; j++){shift_y[i * line + j] = y_[i];}}anchor_points.resize((size_t)row * line * 2, 0);for (int i = 0; i < row * line; i++){float x = shift_x[i];float y = shift_y[i];anchor_points[i * 2] = x;anchor_points[i * 2 + 1] = y;}
}
static void generate_anchor_points(int img_w, int img_h, vector<int> pyramid_levels, int row, int line, vector<float>& all_anchor_points)
{vector<pair<int, int> > image_shapes;vector<int> strides;for (int i = 0; i < pyramid_levels.size(); i++){int new_h = floor((img_h + pow(2, pyramid_levels[i]) - 1) / pow(2, pyramid_levels[i]));int new_w = floor((img_w + pow(2, pyramid_levels[i]) - 1) / pow(2, pyramid_levels[i]));image_shapes.push_back(make_pair(new_w, new_h));strides.push_back(pow(2, pyramid_levels[i]));}all_anchor_points.clear();for (int i = 0; i < pyramid_levels.size(); i++){vector<float> anchor_points;generate_anchor_points(pow(2, pyramid_levels[i]), row, line, anchor_points);vector<float> shifted_anchor_points;shift(image_shapes[i].first, image_shapes[i].second, strides[i], anchor_points, shifted_anchor_points);all_anchor_points.insert(all_anchor_points.end(), shifted_anchor_points.begin(), shifted_anchor_points.end());}
}class P2PNet
{
public:P2PNet(const float confThreshold = 0.5){this->confThreshold = confThreshold;this->net = readNet("SHTechA.onnx");}void detect(Mat& frame);
private:float confThreshold;Net net;Mat preprocess(Mat srcimgt);const float mean[3] = { 0.485, 0.456, 0.406 };const float std[3] = { 0.229, 0.224, 0.225 };vector<String> output_names = { "pred_logits", "pred_points" };
};Mat P2PNet::preprocess(Mat srcimg)
{int srch = srcimg.rows, srcw = srcimg.cols;int new_width = srcw / 128 * 128;int new_height = srch / 128 * 128;Mat dstimg;cvtColor(srcimg, dstimg, cv::COLOR_BGR2RGB);resize(dstimg, dstimg, Size(new_width, new_height), INTER_AREA);dstimg.convertTo(dstimg, CV_32F);int i = 0, j = 0;for (i = 0; i < dstimg.rows; i++){float* pdata = (float*)(dstimg.data + i * dstimg.step);for (j = 0; j < dstimg.cols; j++){pdata[0] = (pdata[0] / 255.0 - this->mean[0]) / this->std[0];pdata[1] = (pdata[1] / 255.0 - this->mean[1]) / this->std[1];pdata[2] = (pdata[2] / 255.0 - this->mean[2]) / this->std[2];pdata += 3;}}return dstimg;
}void P2PNet::detect(Mat& frame)
{const int width = frame.cols;const int height = frame.rows;Mat img = this->preprocess(frame);const int new_width = img.cols;const int new_height = img.rows;Mat blob = blobFromImage(img);this->net.setInput(blob);vector<Mat> outs;//this->net.forward(outs, this->net.getUnconnectedOutLayersNames());this->net.forward(outs, output_names);vector<int> pyramid_levels(1, 3);vector<float> all_anchor_points;generate_anchor_points(img.cols, img.rows, pyramid_levels, 2, 2, all_anchor_points);const int num_proposal = outs[0].cols;int i = 0;float* pscore = (float*)outs[0].data;float* pcoord = (float*)outs[1].data;vector<CrowdPoint> crowd_points;for (i = 0; i < num_proposal; i++){if (pscore[i] > this->confThreshold){float x = (pcoord[i] + all_anchor_points[i * 2]) / (float)new_width * (float)width;float y = (pcoord[i + 1] + all_anchor_points[i * 2 + 1]) / (float)new_height * (float)height;crowd_points.push_back({ Point(int(x), int(y)), pscore[i] });}pcoord += 2;}cout << "have " << crowd_points.size() << " people" << endl;for (i = 0; i < crowd_points.size(); i++){cv::circle(frame, crowd_points[i].pt, 2, cv::Scalar(0, 0, 255), -1, 8, 0);}
}int main()
{P2PNet net(0.3);string imgpath = "2.jpeg";Mat srcimg = imread(imgpath);net.detect(srcimg);static const string kWinName = "dst";namedWindow(kWinName, WINDOW_NORMAL);imshow(kWinName, srcimg);waitKey(0);destroyAllWindows();
}

实现结果:
在这里插入图片描述
在这里插入图片描述
工程源码下载:https://download.csdn.net/download/matt45m/88936724?spm=1001.2014.3001.5503

这篇关于计算机视觉——P2PNet基于点估计的人群计数原理与C++模型推理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/793557

相关文章

C++ Primer 标准库vector示例详解

《C++Primer标准库vector示例详解》该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,... 目录3.3标准库Vector定义和初始化vector对象通列表初始化vector对象创建指定数量的元素值

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Spring Cloud Hystrix原理与注意事项小结

《SpringCloudHystrix原理与注意事项小结》本文介绍了Hystrix的基本概念、工作原理以及其在实际开发中的应用方式,通过对Hystrix的深入学习,开发者可以在分布式系统中实现精细... 目录一、Spring Cloud Hystrix概述和设计目标(一)Spring Cloud Hystr

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没