Args: use_gt (bool, optional): Whether to use GT as detection results to verify the upper bound of person search performance. Defaults to False. use_cache (bool, optional): Whether to use the cached features. Defaults to False. use_cbgm (b
(
model, gallery_loader, query_loader, device, use_gt=False, use_cache=False, use_cbgm=False, gallery_size=100)
| 83 | |
| 84 | @torch.no_grad() |
| 85 | def evaluate_performance( |
| 86 | model, gallery_loader, query_loader, device, use_gt=False, use_cache=False, use_cbgm=False, gallery_size=100): |
| 87 | """ |
| 88 | Args: |
| 89 | use_gt (bool, optional): Whether to use GT as detection results to verify the upper |
| 90 | bound of person search performance. Defaults to False. |
| 91 | use_cache (bool, optional): Whether to use the cached features. Defaults to False. |
| 92 | use_cbgm (bool, optional): Whether to use Context Bipartite Graph Matching algorithm. |
| 93 | Defaults to False. |
| 94 | """ |
| 95 | model.eval() |
| 96 | if use_cache: |
| 97 | eval_cache = torch.load("data/eval_cache/eval_cache.pth") |
| 98 | gallery_dets = eval_cache["gallery_dets"] |
| 99 | gallery_feats = eval_cache["gallery_feats"] |
| 100 | query_dets = eval_cache["query_dets"] |
| 101 | query_feats = eval_cache["query_feats"] |
| 102 | query_box_feats = eval_cache["query_box_feats"] |
| 103 | else: |
| 104 | gallery_dets, gallery_feats = [], [] |
| 105 | for images, targets in tqdm(gallery_loader, ncols=0): |
| 106 | images, targets = to_device(images, targets, device) |
| 107 | if not use_gt: |
| 108 | outputs = model(images) |
| 109 | else: |
| 110 | boxes = targets[0]["boxes"] |
| 111 | n_boxes = boxes.size(0) |
| 112 | embeddings = model(images, targets) |
| 113 | outputs = [ |
| 114 | { |
| 115 | "boxes": boxes, |
| 116 | "embeddings": torch.cat(embeddings), |
| 117 | "labels": torch.ones(n_boxes).to(device), |
| 118 | "scores": torch.ones(n_boxes).to(device), |
| 119 | } |
| 120 | ] |
| 121 | |
| 122 | for output in outputs: |
| 123 | box_w_scores = torch.cat([output["boxes"], output["scores"].unsqueeze(1)], dim=1) |
| 124 | gallery_dets.append(box_w_scores.cpu().numpy()) |
| 125 | gallery_feats.append(output["embeddings"].cpu().numpy()) |
| 126 | |
| 127 | # regarding query image as gallery to detect all people |
| 128 | # i.e. query person + surrounding people (context information) |
| 129 | query_dets, query_feats = [], [] |
| 130 | for images, targets in tqdm(query_loader, ncols=0): |
| 131 | images, targets = to_device(images, targets, device) |
| 132 | # targets will be modified in the model, so deepcopy it |
| 133 | outputs = model(images, deepcopy(targets), query_img_as_gallery=True) |
| 134 | |
| 135 | # consistency check |
| 136 | gt_box = targets[0]["boxes"].squeeze() |
| 137 | |
| 138 | assert ( |
| 139 | gt_box - outputs[0]["boxes"][0] |
| 140 | ).sum() <= 0.001, "GT box must be the first one in the detected boxes of query image" |
| 141 | |
| 142 | for output in outputs: |
no test coverage detected