Last active
January 20, 2021 08:08
-
-
Save deshwalmahesh/9dceec02fcf017db08e307528209c8cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get top-K accuracy from results. Given a query and its related results, it'll find if any of the Ground Truth are in results. | |
# Or How many of the results are in Ground Truth | |
import numpy as np | |
def get_relative_acc(test_result_indices:[np.ndarray,list],similar_image_indices:[np.ndarray,list])->float: | |
''' | |
Check HOW MANY similar images are in returned results for single test image | |
''' | |
# because then only it'll be relative. If an image has 2,3,4 similar images, it won't matter. It'll normalize relative acc | |
divide_by = min(len(test_result_indices),len(similar_image_indices)) | |
return len(set(similar_image_indices).intersection(set(test_result_indices)))/divide_by | |
def get_top_k_acc(test_result_indices:[np.ndarray,list],similar_image_indices:[np.ndarray,list])->int: | |
''' | |
Check if ATLEAST 1 similar image is in results. K = len(test_result_indices) | |
''' | |
for i in similar_image_indices: | |
if i in test_result_indices: # if there exists atleast one single common image, return True | |
return 1 | |
return 0 # if there are no common images in 5 results, return False | |
#---------------------------------------------# | |
# Case 1 | |
test_result_indices = [1,3,5,7,11,13] | |
similar_image_indices = [1,2,3,4,5,6,7,8,9] | |
print(get_relative_acc(test_result_indices,similar_image_indices)) | |
print(get_top_k_acc(test_result_indices,similar_image_indices)) | |
# Case 2 | |
test_result_indices = [1,2,3,4,5,6,7,8,9] | |
similar_image_indices = [1,3,11,13] | |
print(get_relative_acc(test_result_indices,similar_image_indices)) | |
print(get_top_k_acc(test_result_indices,similar_image_indices)) | |
#---------------------------------------------------------------# | |
# Check for all images | |
# acc = [] | |
# for image in test_images: # get the indices of its similar image | |
# top_5_result_indices = apply_cosine_get_indices(image) | |
# result = calculate_relative_acc(top_5_result_indices,similar_indices) | |
# acc.append(result) | |
# print(sum(acc)/len(acc)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment