Runs official COCO evaluation. dataset: A Dataset object with valiadtion data eval_type: "bbox" or "segm" for bounding box or segmentation evaluation limit: if not 0, it's the number of images to use for evaluation
(model, dataset, coco, eval_type="bbox", limit=0, image_ids=None)
| 337 | |
| 338 | |
| 339 | def evaluate_coco(model, dataset, coco, eval_type="bbox", limit=0, image_ids=None): |
| 340 | """Runs official COCO evaluation. |
| 341 | dataset: A Dataset object with valiadtion data |
| 342 | eval_type: "bbox" or "segm" for bounding box or segmentation evaluation |
| 343 | limit: if not 0, it's the number of images to use for evaluation |
| 344 | """ |
| 345 | # Pick COCO images from the dataset |
| 346 | image_ids = image_ids or dataset.image_ids |
| 347 | |
| 348 | # Limit to a subset |
| 349 | if limit: |
| 350 | image_ids = image_ids[:limit] |
| 351 | |
| 352 | # Get corresponding COCO image IDs. |
| 353 | coco_image_ids = [dataset.image_info[id]["id"] for id in image_ids] |
| 354 | |
| 355 | t_prediction = 0 |
| 356 | t_start = time.time() |
| 357 | |
| 358 | results = [] |
| 359 | for i, image_id in enumerate(image_ids): |
| 360 | # Load image |
| 361 | image = dataset.load_image(image_id) |
| 362 | |
| 363 | # Run detection |
| 364 | t = time.time() |
| 365 | r = model.detect([image], verbose=0)[0] |
| 366 | t_prediction += (time.time() - t) |
| 367 | |
| 368 | # Convert results to COCO format |
| 369 | image_results = build_coco_results(dataset, coco_image_ids[i:i + 1], |
| 370 | r["rois"], r["class_ids"], |
| 371 | r["scores"], r["masks"]) |
| 372 | results.extend(image_results) |
| 373 | |
| 374 | # Load results. This modifies results with additional attributes. |
| 375 | coco_results = coco.loadRes(results) |
| 376 | |
| 377 | # Evaluate |
| 378 | cocoEval = COCOeval(coco, coco_results, eval_type) |
| 379 | cocoEval.params.imgIds = coco_image_ids |
| 380 | cocoEval.evaluate() |
| 381 | cocoEval.accumulate() |
| 382 | cocoEval.summarize() |
| 383 | |
| 384 | print("Prediction time: {}. Average {}/image".format( |
| 385 | t_prediction, t_prediction / len(image_ids))) |
| 386 | print("Total time: ", time.time() - t_start) |
| 387 | |
| 388 | |
| 389 | ############################################################ |
no test coverage detected