xuepengqiang
2020-05-26 bb5cb224c9abe4216aaa49a8287b06d9f05dab60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
3
<ñ»]ã@s>ddlZdd„Zd dd„Zdd„Zd    d
„ZGd d „d eƒZdS)éNcCs´tj|ƒtj|ƒ}}t|ƒdks.t|ƒdkrDtjt|ƒt|ƒfƒStj|ƒjddtj|ƒjdd}}dtj||jƒ|dd…df|ddd…f}tj|dt    tj
ƒƒ}|S)a¬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]`.
 
    ré)Úaxisg@NggÀ) ÚnpÚasarrayÚlenÚzerosÚsquareÚsumÚdotÚTÚclipÚfloatÚinf)ÚaÚbÚa2Úb2Zr2©rúLD:\project\07_tracking_Re-id\04_s_yolo_tracking\lib\deep_sort\nn_matching.pyÚ_pdist    s&2rFcCsN|s<tj|ƒtjj|ddd}tj|ƒtjj|ddd}dtj||jƒS)aWCompute 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]`.
 
    rT)rÚkeepdimsgð?)rrÚlinalgÚnormr
r )rrZdata_is_normalizedrrrÚ_cosine_distance#srcCst||ƒ}tjd|jddƒS)aŽ 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`.
 
    gr)r)rrÚmaximumÚmin)ÚxÚyÚ    distancesrrrÚ_nn_euclidean_distance<s
rcCst||ƒ}|jddS)aˆ 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`.
 
    r)r)rr)rrrrrrÚ_nn_cosine_distanceQs
r c@s*eZdZdZd    dd„Zdd„Zdd„ZdS)
ÚNearestNeighborDistanceMetricaÈ
    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.
 
    NcCs>|dkrt|_n|dkr t|_ntdƒ‚||_||_i|_dS)NZ    euclideanÚcosinez6Invalid metric; must be either 'euclidean' or 'cosine')rÚ_metricr Ú
ValueErrorÚmatching_thresholdÚbudgetÚsamples)ÚselfÚmetricr%r&rrrÚ__init__~sz&NearestNeighborDistanceMetric.__init__csjxPt||ƒD]B\}}ˆjj|gƒj|ƒˆjdk    r ˆj|ˆj d…ˆj|<q W‡fdd„|Dƒˆ_dS)atUpdate 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.
 
        Ncsi|]}ˆj||“qSr)r')Ú.0Úk)r(rrú
<dictcomp>sz=NearestNeighborDistanceMetric.partial_fit.<locals>.<dictcomp>)Úzipr'Ú
setdefaultÚappendr&)r(ÚfeaturesÚtargetsZactive_targetsÚfeatureÚtargetr)r(rÚ partial_fitŒs
 
 z)NearestNeighborDistanceMetric.partial_fitcCsNtjt|ƒt|ƒfƒ}x2t|ƒD]&\}}|j|j||ƒ||dd…f<q W|S)a 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]`.
 
        N)rrrÚ    enumerater#r')r(r1r2Z cost_matrixÚir4rrrÚdistanceŸs"z&NearestNeighborDistanceMetric.distance)N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__r*r5r8rrrrr!fs
r!)F)Únumpyrrrrr Úobjectr!rrrrÚ<module>s