nwojke / deep_sort

Simple Online Realtime Tracking with a Deep Association Metric
GNU General Public License v3.0
5.19k stars 1.46k forks source link

np.linalg.norm() for normalizing the features #233

Closed pandey-anurag closed 3 years ago

pandey-anurag commented 3 years ago

Hello, I am trying to understand the use of

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)

here

I tried using the same approach, but I do not get normalized vectors.

feat = np.random.rand(3,4)
print(feat)
norm = feat/np.linalg.norm(feat, axis=1, keepdims=True)
print(norm)
print(np.sum(norm, axis=1))

Output:

[[0.75011799 0.54083259 0.65699116 0.9437311 ]
 [0.1359358  0.80203965 0.87496547 0.74936549]
 [0.43723459 0.27744972 0.64938242 0.42874583]]

[[0.50834226 0.36651309 0.44523179 0.63955059]
 [0.09638997 0.56871384 0.62042441 0.53136342]
 [0.46777829 0.29683141 0.69474604 0.45869654]]

[1.95963773 1.81689164 1.91805229]

Please advice.

dranaivo commented 3 years ago

@pandey-anurag

Doing np.sum(norm, axis=1) doesn't give the norm of each of norm's row. Do instead np.linalg.norm(norm, axis=1, keepdims=True).

Output:

[[1.]
 [1.]
 [1.]]

Each row vector are normalized.

pandey-anurag commented 3 years ago

@dranaivo I understand, I performed L1 norm(since all values were positive hence sum) instead of L2 right?