简体   繁体   中英

what's the difference between np.linalg.norm(a-x) and np.linalg.norm(a) - np.linalg.norm(x)?

I am trying to do extract features from an image and then compare these features to an array of features from thousands of images.

image = Image.open('C:/Users/Timmi/Desktop/test_image.jpg')

query = fe.extract(image)
print(np.shape(query))
print(np.linalg.norm(query))

dists = np.linalg.norm(features - query, axis=1)
print(dists)
print(np.shape(features))
dists2 = (np.linalg.norm(features, axis=1)) - (np.linalg.norm(query, 0))
print(dists2)

ids = np.argsort(dists)[:3]
scores = [(dists[id], img_paths[id]) for id in ids]
print(scores)

This is what gets printed

(4096,)
0.99999994
[0.96851873 1.0867099  1.054868   ... 1.2182273  1.2591194  1.2556202 ]
(31303, 4096)
[-1158. -1158. -1158. ... -1158. -1158. -1158.]
[(0.0, WindowsPath('static/img/image1.jpg')), (0.0, WindowsPath('static/img/image2.jpg')), (0.08249867, WindowsPath('static/img/image3.jpg'))]

I trying to find the difference between these two lines:

dists1 = np.linalg.norm(features - query, axis=1)

dists2 = (np.linalg.norm(features, axis=1)) - np.array(np.linalg.norm(query, 0))

For a vector x = (x1, x2, ..., xn) , its Euclidean norm (in numpy the linalg.norm ), is defined as:

sqrt(x1 ** 2 + x2 ** 2 + ... + xn ** 2)

So the norm of the features - query vector is:

sqrt((f1 - q1) ** 2 + (f2 - q2) ** 2 + ... + (fn - qn) ** 2)

While the norm of features minus the norm of query is:

sqrt(f1 ** 2 + f2 ** 2 + ... + fn ** 2) - sqrt(q1 ** 2 + q2 ** 2 + ... + qn ** 2)

Which are different quantities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM