(model, data_loader, device)
| 74 | |
| 75 | @torch.inference_mode() |
| 76 | def evaluate(model, data_loader, device): |
| 77 | n_threads = torch.get_num_threads() |
| 78 | # FIXME remove this and make paste_masks_in_image run on the GPU |
| 79 | torch.set_num_threads(1) |
| 80 | cpu_device = torch.device("cpu") |
| 81 | model.eval() |
| 82 | metric_logger = utils.MetricLogger(delimiter=" ") |
| 83 | header = "Test:" |
| 84 | |
| 85 | coco = get_coco_api_from_dataset(data_loader.dataset) |
| 86 | iou_types = _get_iou_types(model) |
| 87 | coco_evaluator = CocoEvaluator(coco, iou_types) |
| 88 | |
| 89 | for images, targets in metric_logger.log_every(data_loader, 100, header): |
| 90 | images = list(img.to(device) for img in images) |
| 91 | |
| 92 | if torch.cuda.is_available(): |
| 93 | torch.cuda.synchronize() |
| 94 | model_time = time.time() |
| 95 | outputs = model(images) |
| 96 | |
| 97 | outputs = [{k: v.to(cpu_device) for k, v in t.items()} for t in outputs] |
| 98 | model_time = time.time() - model_time |
| 99 | |
| 100 | res = {target["image_id"]: output for target, output in zip(targets, outputs)} |
| 101 | evaluator_time = time.time() |
| 102 | coco_evaluator.update(res) |
| 103 | evaluator_time = time.time() - evaluator_time |
| 104 | metric_logger.update(model_time=model_time, evaluator_time=evaluator_time) |
| 105 | |
| 106 | # gather the stats from all processes |
| 107 | metric_logger.synchronize_between_processes() |
| 108 | print("Averaged stats:", metric_logger) |
| 109 | coco_evaluator.synchronize_between_processes() |
| 110 | |
| 111 | # accumulate predictions from all images |
| 112 | coco_evaluator.accumulate() |
| 113 | coco_evaluator.summarize() |
| 114 | torch.set_num_threads(n_threads) |
| 115 | return coco_evaluator |
no test coverage detected
searching dependent graphs…