Skip to content

Instantly share code, notes, and snippets.

@BIGBALLON
Created April 9, 2021 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BIGBALLON/51b0ef63309e4ee9e13307168d207007 to your computer and use it in GitHub Desktop.
Save BIGBALLON/51b0ef63309e4ee9e13307168d207007 to your computer and use it in GitHub Desktop.
import numpy as np
def voc_ap(rec, prec):
# correct AP calculation
# first append sentinel values at the end
mrec = np.concatenate(([0.], rec, [1.])) #[0. 0.0666, 0.1333, 0.4 , 0.4666, 1.]
mpre = np.concatenate(([0.], prec, [0.])) #[0. 1., 0.6666, 0.4285, 0.3043, 0.]
for i in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i]) #[1. 1. 0.6666 0.4285 0.3043 0. ]
i = np.where(mrec[1:] != mrec[:-1])[0]
print(" == Debug:")
print(mrec[1:])
print(mrec[:-1])
print(f"i = {i}")
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
return ap
def main():
rec = np.array([0.0666, 0.1333,0.1333, 0.4, 0.4666])
prec = np.array([1., 0.6666, 0.6666, 0.4285, 0.3043])
ap = voc_ap(rec, prec)
print(f"ap = {ap}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment