使用余弦相似性找到前五名相似的图像
我有一个具有长度n的图像的功能列表。
feature_list -> [array[img1], array[img2]....n]
我可以使用 sklearn.neighbors.nearestneighbors 找到前5名。通过遵循
neighbors = NearestNeighbors(n_neighbors=5, algorithm='brute',metric='euclidean').fit(feature_list)
test_img_feature = extract_features('test.jpg', model_after_tuning)
distances, indices = neighbors.kneighbors([test_img_feature])
此操作很好。但是我想使用余弦相似性而不是欧几里得。 到目前为止,我做了以下操作。
def calculate_similarity(vector1, vector2):
sim_cos = 1-spatial.distance.cosine(vector1, vector2)
return sim_cos
fea_vec_img1 = extract_features('./test.jpg', model_after_tuning)
fea_vec_img2 = extract_features('./test1.jpg', model_after_tuning)
calculate_similarity(fea_vec_img1 , fea_vec_img2)
它的工作正常,但是我想从包含整个数据的整个功能列表中搜索前五名。
当我传递以下误差时,
calculate_similarity(feature_list, fea_vec_img2)
对于整个功能_list,例如最近的neighbors ,余弦相似性的任何拟合函数吗?
I have a feature list of images with length n.
feature_list -> [array[img1], array[img2]....n]
I can find top 5 using sklearn.neighbors.NearestNeighbors. by following
neighbors = NearestNeighbors(n_neighbors=5, algorithm='brute',metric='euclidean').fit(feature_list)
test_img_feature = extract_features('test.jpg', model_after_tuning)
distances, indices = neighbors.kneighbors([test_img_feature])
This is working fine. But I want to use cosine similarity instead of euclidean.
so far I did the following ..
def calculate_similarity(vector1, vector2):
sim_cos = 1-spatial.distance.cosine(vector1, vector2)
return sim_cos
fea_vec_img1 = extract_features('./test.jpg', model_after_tuning)
fea_vec_img2 = extract_features('./test1.jpg', model_after_tuning)
calculate_similarity(fea_vec_img1 , fea_vec_img2)
Its working fine but I want to search top 5 from the whole feature_list which containing the whole data.
when I pass the following its getting error
calculate_similarity(feature_list, fea_vec_img2)
Is there any fit function of cosine similarity for whole feature_list like NearestNeighbors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论