YOLO5+DeepSORT code structure

code address

DeepSORT

Re-ID

checkpoint (reID network weight of deepsort)

reid_model_factory.py (reid network model library)

SORT

detection.py

(The detection results (frame coordinates and confidence scores) of each frame of the video in the video are encapsulated in the Detection class through the corresponding target detection model. In addition, this class includes the appearance semantic features extracted by the ReID module and conversion methods of different frame coordinate forms)

iou_matching.py

def iou(bbox, candidates):  # 计算IOU
def iou_cost(tracks, detections, track_indices=None,
             detection_indices=None):  # 计算IOU cost distance  最后返回一个matrix

kalman_filter.py

def __init__(self):  # 初始化参数
def initiate(self, measurement): # 对于未匹配到的目标新建一个track,一般这样的检测目标都是新生目标
def predict(self, mean, covariance): # 卡尔曼滤波根据当前时刻状态预测下一时刻状态
def project(self, mean, covariance): # 投影目标状态分布到测量空间
def update(self, mean, covariance, measurement): # 根据观测值和预测值更新轨迹值
def gating_distance(self, mean, covariance, measurements,
                        only_position=False): # 计算马氏距离

linear_assignment.py (Hungarian assignment)

def min_cost_matching(
        distance_metric, max_distance, tracks, detections, track_indices=None,
        detection_indices=None): # 最小cost(IOU)匹配
def matching_cascade(
        distance_metric, max_distance, cascade_depth, tracks, detections,
        track_indices=None, detection_indices=None): # 级联匹配
def gate_cost_matrix(
        kf, cost_matrix, tracks, detections, track_indices, detection_indices,
        gated_cost=INFTY_COST, only_position=False): # 代价矩阵中的数据由余弦距离计算出来,然后通过马氏距离(实现在gating_distance)对该代价矩阵做出修正,将代价矩阵(costing_matix)中的每个大于阈值的数,置为一个极大的值。

nn.matching.py (For each target, returns the distance metric for the nearest neighbor, i.e. the closest distance to any sample that has been observed so far.)

def _pdist(a, b): # 计算两个向量之间的欧式距离
def _cosine_distance(a, b, data_is_normalized=False): # 计算余弦距离并归一化
def _nn_euclidean_distance(x, y): # 计算欧氏距离最近邻距离
def _nn_cosine_distance(x, y): # 计算余弦距离最近邻距离
class NearestNeighborDistanceMetric(object): # 度量轨迹和目标框最近距离,即根据对应确认态轨迹(同一ID)以往所有时刻的外观语义特征与当前目标框的外观语义特征,计算轨迹和目标框的关联距离(相似度)
def __init__(self, metric, matching_threshold, budget=None): # 初始化
def partial_fit(self, features, targets, active_targets): # 调整存储特征的字典使其中存储的特征都是状态为confirmed的对象的
def distance(self, features, targets): # 计算目标框的外观语义特征与对应确认态轨迹(同一ID)以往所有的外观语义特征(欧式或余弦)距离矩阵

preprocessing.py (detection frame pre-processing)

def non_max_suppression(boxes, max_bbox_overlap, scores=None): # 对检测结果进行一个前处理,主要是抑制重叠冗余的检测框

track.py (stores the status and information of a single track and is responsible for the prediction and update of a single track)

class TrackState: # 定义了轨迹的三种状态
class Track:
def __init__(self, mean, covariance, track_id, class_id, n_init, max_age,
                 feature=None): # 初始化状态
def to_tlwh(self): # 得到bbox尺寸(左上,宽,高)
def to_tlbr(self):# 得到bbox尺寸(左上,右下)
def get_yolo_pred(self): # 得到yolo 检测框
def increment_age(self): # 预测下一帧轨迹时调用 age+1 time_since_updata+1
def predict(self, kf): # 预测下一帧轨迹信息
def update(self, kf, detection, class_id): # 更新匹配成功的轨迹信息
def mark_missed(self): # 是否将轨迹状态转为删除态
def is_tentative(self): # 状态为在考察期
def is_confirmed(self): # 状态是确认的轨迹
def is_deleted(self): # 轨迹被删除

Tracker.py (stores the status and information of all tracks (sets), is responsible for the initialization of tracks and the prediction and update of all tracks (sets) and encapsulates the data matching module;)

class Tracker: GATING_THRESHOLD = np.sqrt(kalman_filter.chi2inv95[4])# 设置马氏距离门限
def __init__(self, metric, max_iou_distance=0.9, max_age=30, n_init=3, _lambda=0): # 初始化
def predict(self): # 预测轨迹集合中所有轨迹的下一帧信息
def increment_ages(self): # 视频帧中未检测到目标时调用 因无法进行卡尔曼滤波更新,故不再额外进行卡尔曼滤波预测,但需要更新相应轨迹信息
def update(self, detections, classes): # 更新轨迹集合中所有的轨迹状态和信息
def _full_cost_metric(self, tracks, dets, track_indices, detection_indices): # 使用外观特征和运动特征(门控距离)并结合相关阈值计算相似度矩阵
def _match(self, detections): # 进行级联匹配和IOU匹配,获取匹配成功轨迹和目标框、匹配失败轨迹以及匹配失败目标框
def _initiate_track(self, detection, class_id): # 初始化轨迹状态和信息

deep_sort

class DeepSort(object):
def __init__(self, model, device, max_dist=0.2, max_iou_distance=0.7, max_age=70, n_init=3, nn_budget=100): # 定义一些超参数 选择模型和cuda
def update(self, bbox_xywh, confidences, classes, ori_img, use_yolo_preds=False): # 更新每一帧轨迹
def _get_features(self, bbox_xywh, ori_img): # 提取re-id特征

YOLOV5

track.py

(Add the id information of each frame obtained after deepsort to the detection frame of yolo, this step belongs to the detector series)

Guess you like

Origin blog.csdn.net/weixin_46084134/article/details/130290764