Darknet: Trailing comma not removed from coco_results.json in Windows

Created on 8 Jun 2019  路  2Comments  路  Source: AlexeyAB/darknet

There appears to be an issue caused by the difference between Windows vs Linux line-endings, when running a COCO evaluation. When running the detector on the COCO dataset, e.g. "detector valid cfg/coco.data ../cfg/yolov3.cfg ../weights/yolov3.weights", a "results/coco_results.json" file is generated. After running on a Linux system, an example of the last few lines of this file is:

{"image_id":284282, "category_id":65, "bbox":[0.000000, 18.000229, 106.680382, 308.656616], "score":0.010804},
{"image_id":284282, "category_id":47, "bbox":[350.594543, 30.112206, 36.254150, 39.351761], "score":0.112114}
]

However, when run under Windows, the output includes a trailing comma after the last entry, e.g.:

{"image_id":284282, "category_id":65, "bbox":[0.000000, 18.000229, 106.680382, 308.656616], "score":0.010804},
{"image_id":284282, "category_id":47, "bbox":[350.594543, 30.112206, 36.254150, 39.351761], "score":0.112114},
]

Because a trailing comma is typically not allowed in a JSON object, the subsequent COCO evaluation python scripts fail when parsing a coco_results.json file generated from a darknet run on Windows.

Currently, detector.c (lines 557-559) and coco.c (lines 221-223) are:

 fseek(fp, -2, SEEK_CUR);
 fprintf(fp, "\n]\n");
 fclose(fp);

After running darknet, an ASCII dump of the end of coco_results.json from Linux is:

162447420   9   .   3   5   1   7   6   1   ]   ,  sp   "   s   c   o   r
162447440   e   "   :   0   .   1   1   2   1   1   4   }  nl   ]  nl

From Windows, it's:

163477420  sp   "   s   c   o   r   e   "   :   0   .   1   3   1   5   5
163477440   7   }   ,  cr  nl   ]  cr  nl

So for coco evaluation to work properly on Windows, it looks like the fseek() offset should be -3 on Windows and -2 on Linux. Another solution may be to pass a flag to print_cocos() so that the comma delimiter is not printed for the last line.

Bug fixed

Most helpful comment

Hi, Thanks for the quick fix! Should the same fix also be made to coco.c (lines 221-223)?

All 2 comments

Hi, Thanks for the quick fix! Should the same fix also be made to coco.c (lines 221-223)?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yongcong1415 picture yongcong1415  路  3Comments

jasleen137 picture jasleen137  路  3Comments

Jacky3213 picture Jacky3213  路  3Comments

rezaabdullah picture rezaabdullah  路  3Comments

PROGRAMMINGENGINEER-NIKI picture PROGRAMMINGENGINEER-NIKI  路  3Comments