当前位置:网站首页>deepsort源码解读(五)
deepsort源码解读(五)
2022-07-27 05:24:00 【德林恩宝】
nn_matching.py
# vim: expandtab:ts=4:sw=4
import numpy as np
def _pdist(a, b):
"""Compute pair-wise squared distance between points in `a` and `b`.
Parameters
----------
a : array_like
An NxM matrix of N samples of dimensionality M.
b : array_like
An LxM matrix of L samples of dimensionality M.
Returns
-------
ndarray
Returns a matrix of size len(a), len(b) such that eleement (i, j)
contains the squared distance between `a[i]` and `b[j]`.
"""
a, b = np.asarray(a), np.asarray(b)
if len(a) == 0 or len(b) == 0:
return np.zeros((len(a), len(b)))
a2, b2 = np.square(a).sum(axis=1), np.square(b).sum(axis=1)
r2 = -2. * np.dot(a, b.T) + a2[:, None] + b2[None, :]
r2 = np.clip(r2, 0., float(np.inf))
return r2
# 计算余弦距离
def _cosine_distance(a, b, data_is_normalized=False):
"""Compute pair-wise cosine distance between points in `a` and `b`.
Parameters
----------
a : array_like
An NxM matrix of N samples of dimensionality M.
b : array_like
An LxM matrix of L samples of dimensionality M.
data_is_normalized : Optional[bool]
If True, assumes rows in a and b are unit length vectors.
Otherwise, a and b are explicitly normalized to lenght 1.
Returns
-------
ndarray
Returns a matrix of size len(a), len(b) such that eleement (i, j)
contains the squared distance between `a[i]` and `b[j]`.
"""
if not data_is_normalized:
a = np.asarray(a) / np.linalg.norm(a, axis=1, keepdims=True)
b = np.asarray(b) / np.linalg.norm(b, axis=1, keepdims=True)
return 1. - np.dot(a, b.T)
# 计算欧氏距离
def _nn_euclidean_distance(x, y):
""" Helper function for nearest neighbor distance metric (Euclidean).
Parameters
----------
x : ndarray
A matrix of N row-vectors (sample points).
y : ndarray
A matrix of M row-vectors (query points).
Returns
-------
ndarray
A vector of length M that contains for each entry in `y` the
smallest Euclidean distance to a sample in `x`.
"""
distances = _pdist(x, y)
return np.maximum(0.0, distances.min(axis=0))
def _nn_cosine_distance(x, y):
""" Helper function for nearest neighbor distance metric (cosine).
Parameters
----------
x : ndarray
A matrix of N row-vectors (sample points).
y : ndarray
A matrix of M row-vectors (query points).
Returns
-------
ndarray
A vector of length M that contains for each entry in `y` the
smallest cosine distance to a sample in `x`.
"""
distances = _cosine_distance(x, y)
return distances.min(axis=0)
# 最近邻距离度量类
class NearestNeighborDistanceMetric(object):
"""
A nearest neighbor distance metric that, for each target, returns
the closest distance to any sample that has been observed so far.
Parameters
----------
metric : str
Either "euclidean" or "cosine".
matching_threshold: float
The matching threshold. Samples with larger distance are considered an
invalid match.
budget : Optional[int]
If not None, fix samples per class to at most this number. Removes
the oldest samples when the budget is reached.
Attributes
----------
samples : Dict[int -> List[ndarray]]
A dictionary that maps from target identities to the list of samples
that have been observed so far.
"""
def __init__(self, metric, matching_threshold, budget=None):
# 使用最近邻欧氏距离
if metric == "euclidean":
self._metric = _nn_euclidean_distance
# 使用最近邻余弦距离
elif metric == "cosine":
self._metric = _nn_cosine_distance
else:
raise ValueError(
"Invalid metric; must be either 'euclidean' or 'cosine'")
# 在级联匹配的函数中调用,最大的余弦距离
self.matching_threshold = matching_threshold
# budge 预算,控制feature的多少
self.budget = budget
# samples是一个字典{
id->feature list}
self.samples = {
}
# 该方法的作用就是调整存储特征的字典使其中存储的特征都是状态为confirmed的对象
# 作用:部分拟合,用新的数据更新测量距离
def partial_fit(self, features, targets, active_targets):
"""Update the distance metric with new data.
Parameters
----------
features : ndarray
An NxM matrix of N features of dimensionality M.
targets : ndarray
An integer array of associated target identities.
active_targets : List[int]
A list of targets that are currently present in the scene.
"""
# activate_targets:状态为confirmed的跟踪对象
# targets和features是目前所有状态为confirmed的跟踪对象的id和特征
for feature, target in zip(features, targets):
# setdefault方法就是如果这个字典中没有这target则加入,有则无u操作
self.samples.setdefault(target, []).append(feature)
# 设置预算,每个类最多多少个目标,超过则保留最新的budget特征
if self.budget is not None:
self.samples[target] = self.samples[target][-self.budget:]
# 筛选激活的目标
self.samples = {
k: self.samples[k] for k in active_targets}
# 作用:比较feature和targets之间的距离,返回一个代价矩阵
def distance(self, features, targets):
"""Compute distance between features and targets.
Parameters
----------
features : ndarray
An NxM matrix of N features of dimensionality M.
targets : List[int]
A list of targets to match the given `features` against.
Returns
-------
ndarray
Returns a cost matrix of shape len(targets), len(features), where
element (i, j) contains the closest squared distance between
`targets[i]` and `features[j]`.
"""
cost_matrix = np.zeros((len(targets), len(features)))
# 计算每一个track与detection的代价值,行表示track
for i, target in enumerate(targets):
cost_matrix[i, :] = self._metric(self.samples[target], features)
return cost_matrix
边栏推荐
- What is special about the rehabilitation orthopedic branch of 3D printing brand?
- Concept and principle of DHCP
- Linux安装Redis操作
- FTX US推出FTX Stocks,向主流金融行业迈进
- Express接收请求参数
- Linux安装与卸载MySql
- Shell sentence judgment exercise
- Numpy array and image conversion
- Summary of frequently asked questions in the interview [summarized after painstaking work all night]
- Soul 递交港股上市申请,加快社交聚集地多元化、场景化的布局
猜你喜欢

FTX.US推出股票和ETF交易服务,让交易更透明

px4源码编译之 建立自己的程序模块

Express框架

PSI|CSI和ROC|AUC和KS -备忘录

如何让最小 API 绑定查询字符串中的数组

Inventory of the world's six most technologically competent smart contract audit companies in 2022

Soul submitted an application for listing in Hong Kong stocks, accelerating the diversified and scene based layout of social gathering places

NFS introduction and configuration

Sunflower popularizes Science in an all-round way to avoid loopholes for your remote control equipment in time

ES6新特性(入门)
随机推荐
Do it yourself container
使用密钥方式登录阿里云服务器
创建一个不依赖于任何基础镜像的容器
shell常用命令-备忘录
Build cloud native operating environment
IoTDB 的C# 客户端发布 0.13.0.7
DNS域名解析服务
Iptables firewall
系统安全与应用
Shell -- circular statements (for, while, until)
客户案例 | 聚焦流程体验,助银行企业APP迭代
A cross domain problem of golang
NAT(网络地址转换)
Pruning - quantification - turn to onnx Chinese series tutorials
向日葵教大家如何防范拒绝服务攻击漏洞?
Recommended by the world's most technologically competent smart contract security audit company in 2022
如何让最小 API 绑定查询字符串中的数组
ES6的新特性(2)
多模态数据库 | 星环科技多模数据库ArgoDB“一库多用“,构建高性能湖仓集一体平台
Disk management and file system