当前位置:网站首页>Interpretation of deepsort source code (V)
Interpretation of deepsort source code (V)
2022-07-27 07:02:00 【Delin enbao】
Catalog
deepsort Principle and analysis summary
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
# Calculate the cosine distance
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)
# Calculate Euclidean distance
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)
# Nearest neighbor distance measurement class
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):
# Use nearest neighbor Euclidean distance
if metric == "euclidean":
self._metric = _nn_euclidean_distance
# Use nearest neighbor cosine distance
elif metric == "cosine":
self._metric = _nn_cosine_distance
else:
raise ValueError(
"Invalid metric; must be either 'euclidean' or 'cosine'")
# Call , Maximum cosine distance
self.matching_threshold = matching_threshold
# budge The budget , control feature The amount of
self.budget = budget
# samples It's a dictionary {
id->feature list}
self.samples = {
}
# The function of this method is to adjust the dictionary of stored features so that the stored features are in the state of confirmed The object of
# effect : Partial fit , Update the measured distance with new data
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: Status as confirmed Tracking object of
# targets and features Yes, all current status is confirmed Of the tracking object id And characteristics
for feature, target in zip(features, targets):
# setdefault The way is if this is not in the dictionary target Join in , Yes, no u operation
self.samples.setdefault(target, []).append(feature)
# Set budget , How many targets can each class have at most , If it exceeds, keep the latest budget features
if self.budget is not None:
self.samples[target] = self.samples[target][-self.budget:]
# Filter active targets
self.samples = {
k: self.samples[k] for k in active_targets}
# effect : Compare feature and targets Distance between , Return a cost matrix
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)))
# Calculate each track And detection The generation value of , Row representation track
for i, target in enumerate(targets):
cost_matrix[i, :] = self._metric(self.samples[target], features)
return cost_matrix
边栏推荐
- deepsort源码解读(五)
- 最新!国资委发布国有企业数字化转型新举措
- 含有偶氮苯单体的肽核酸寡聚体(NH2-TNT4,N-PNAs)齐岳生物定制
- Where to connect with user-defined functions leads to slow queries
- EasyCVR设备管理列表页面搜索时,分页数据不显示的问题修复
- jest单测样式问题【identity-obj-proxy】npm包
- How to delete or replace the loading style of easyplayer streaming media player?
- 脱氧核糖核酸DNA修饰氧化锌|DNA修饰纳米金颗粒|DNA偶联修饰碳纳米材料
- Derivative, partial derivative and gradient
- Cyclegan parsing
猜你喜欢

基于SSM医院预约管理系统

事件捕获方式和冒泡方式—它们的区别是什么?

MangoDB

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

The problem of torch loading custom models
![[unity URP] the code obtains the universalrendererdata of the current URP configuration and dynamically adds the rendererfeature](/img/be/812ccb05d7763effcece51945f0460.png)
[unity URP] the code obtains the universalrendererdata of the current URP configuration and dynamically adds the rendererfeature

Matlab drawing (ultra detailed)

工控用Web组态软件比组态软件更高效

DNA modified noble metal nanoparticles | DNA modified gold nanoparticles (scientific research level)

Dsgan degenerate network
随机推荐
The issuing process of individual developers applying for code signing certificates
【Latex格式】双栏双图左右并排有小标题、上下并列有小标题
Alibaba cloud SMS authentication third-party interface (fast use)
Dsgan degenerate network
deepsort源码解读(五)
ZnS DNA QDs near infrared zinc sulfide ZnS quantum dots modified deoxyribonucleic acid dna|dna modified ZnS quantum dots
MySQL的基本语句(1)—增删改查
Image super-resolution evaluation index
Account management and authority
AI: play games in your spare time - earn it a small goal - [Alibaba security × ICDM 2022] large scale e-commerce map of risk commodity inspection competition
Soul continues to make efforts to list its social channels in Hong Kong. Why is "soul style social" popular?
Numpy array and image conversion
网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...
After adding a camera (camera) to the UAV in gazebo, the UAV cannot take off
Many of the world's top 500 enterprises gathered at the second digital Expo, and the digital industry curtain is about to open!
基于SSM实现的校园新闻发布管理系统
GoLand writes Go program
Log in to Alibaba cloud server with a key
Reasoning speed of model
事件捕获方式和冒泡方式—它们的区别是什么?