pythontensorflowaverage-precision

Mean average precision (mAP) in tensorflow


I need to calculate the mAP described in this question for object detection using Tensorflow.

Average precision(AP) is a typical performance measure used for ranked sets. AveragePrecision is defined as the average of the precision scores after each true positive, TP in the scope S. Given a scope S = 7,and a ranked list (gain vector) G = [1,1,0,1,1,0,0,1,1,0,1,0,0,..] where 1/0 indicate the gains associated to relevant/non-­‐relevant items, respectively:

AP = (1/1 + 2/2 + 3/4 + 4/5) / 4 = 0.8875.

Mean Average Precision (mAP): average of the average precision value for a set of queries.

i got 5 One-Hot tensors with the predictions:

prediction_A 
prediction_B
prediction_C 
prediction_D 
prediction_E 

where a single prediction tensor has this structure (for example prediction_A):

00100
01000
00001
00010
00010

Then i've got the correct labels (one-hot) tensors, with the same structure:

y_A
y_B
y_C
y_D
y_E

i want compute mAP using tensorflow, cause i want summarize that, how i can do it?

i found this function but i can't use it, cause i have a multidimensional vector.

I also write a python function that compute AP but it doesn't use Tensorflow

def compute_av_precision(match_list):
    n = len(match_list)
    tp_counter = 0

    cumulate_precision = 0
    for i in range(0,n):
        if match_list[i] == True:

            tp_counter += 1

            cumulate_precision += (float(tp_counter)/float(i+1))


    if tp_counter != 0:
        av_precision = cumulate_precision/float(tp_counter)
        return av_precision
    return 0

Solution

  • I think you may need this one:

    tf.metrics.average_precision_at_k
    

    this method takes labels and prediction to calcualte the AP@K you mentioned

    below are the referenced links

    https://www.tensorflow.org/api_docs/python/tf/metrics/average_precision_at_k

    which implemented AP@K metric defined here:

    https://en.wikipedia.org/wiki/Evaluation_measures_(information_retrieval)#Average_precision

    BTW, if you need a metric in Tensorflow, firstly you should search inside their official documents. Here is a list of all implemented metrics

    https://www.tensorflow.org/api_docs/python/tf/metrics

    cheers