[Python] Utilizing cocoapi for evaluation in python produces print results in console

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am using the Coco eval script for my project. I have modified the demo provided here. The commands I use are basically those:

    cocoGt = COCO(gt_json)
    cocoDt = cocoGt.loadRes(results_json)

    imgIds = sorted(cocoGt.getImgIds())

    # running evaluation
    cocoEval = COCOeval(cocoGt, cocoDt, annType)
    # cocoEval.params.catIds = [1] # person id : 1
    cocoEval.params.imgIds = imgIds
    cocoEval.evaluate()
    cocoEval.accumulate() # in this step the results are printed
    cocoEval.summarize()


    everything works fine except for a display of the results in the console. The display opccurs when cocoEval.accumulate() is executed. The display is something similar to this one:

    DONE (t=0.52s).
    Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.617
    Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.945
    Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.688
    Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.533
    Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.626
    Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.869
    Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.111
    Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.539
    Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.717
    Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.658
    Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.722
    Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.925


    and it is displayed each time the prompt is used (if on debug or interactive mode that means in EVERY command!). When launched in a script the display is only occurring once but still it shouldn't happen.

    After investigating the code I found that the printing occurs in line 411 of cocoeval.py. The inexplicable issue is that the next line which cause this console printing is just a dictionary definition:

    self.eval = {
    'params': p,
    'counts': [T, R, K, A, M],
    'date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    'precision': precision,
    'recall': recall,
    'scores': scores,
    }


    The even funnier thing is that if the precision field is empty the printing stops from happening! For example the following dict does not produce anything in the console (of course it crushes after a while and the script does not work correctly).

    self.eval = {
    'params': p,
    'counts': [T, R, K, A, M],
    'date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    'recall': recall,
    'scores': scores,
    }


    Similarly, if I include a line like

    cocoEval.eval = {} # this stop the result printing for some reason


    after cocoEval.summarize() the printing also stops (it has occurred once of course already). precision is an ordinary numpy array also.

    The questions I have are:

    a) Why the inclusion of this specific field precision in the cocoEval.eval dict produce this printing change?
    b) How can it be stopped from occurring?

    Continue reading...

Compartilhe esta Página