| 26 | import torch.utils.data |
| 27 | |
| 28 | class PedAttrEvaluator(DatasetEvaluator): |
| 29 | |
| 30 | def __init__( |
| 31 | self, |
| 32 | dataset_name, |
| 33 | config, |
| 34 | distributed=True, |
| 35 | output_dir=None, |
| 36 | ): |
| 37 | |
| 38 | self._logger = logging.getLogger(__name__) |
| 39 | |
| 40 | self._dataset_name = dataset_name |
| 41 | self._distributed = distributed |
| 42 | self._output_dir = output_dir |
| 43 | |
| 44 | self._cpu_device = torch.device("cpu") |
| 45 | self.threshold = 0.5 |
| 46 | |
| 47 | def reset(self): |
| 48 | self.gt_label = [] |
| 49 | self.preds_probs = [] |
| 50 | |
| 51 | def process(self, inputs, outputs): |
| 52 | gt_label = inputs['label'] |
| 53 | gt_label[gt_label == -1] = 0 |
| 54 | preds_probs = outputs['pred_logits'].squeeze().sigmoid() |
| 55 | self.gt_label.append(gt_label) |
| 56 | self.preds_probs.append(preds_probs) |
| 57 | |
| 58 | @staticmethod |
| 59 | def all_gather(data, group=0): |
| 60 | assert link.get_world_size() == 1, f"distributed eval unsupported yet, uncertain if we can use torch.dist with link jointly" |
| 61 | if link.get_world_size() == 1: |
| 62 | return [data] |
| 63 | |
| 64 | world_size = link.get_world_size() |
| 65 | tensors_gather = [torch.ones_like(data) for _ in range(world_size)] |
| 66 | link.allgather(tensors_gather, data, group=group) |
| 67 | return tensors_gather |
| 68 | |
| 69 | def evaluate(self): |
| 70 | gt_label = torch.cat(self.gt_label, dim=0) |
| 71 | preds_probs = torch.cat(self.preds_probs, dim=0) |
| 72 | |
| 73 | if self._distributed: |
| 74 | link.synchronize() |
| 75 | |
| 76 | gt_label = self.all_gather(gt_label) |
| 77 | preds_probs = self.all_gather(preds_probs) |
| 78 | |
| 79 | if link.get_rank() != 0: |
| 80 | return |
| 81 | |
| 82 | gt_label = torch.cat(gt_label, dim=0) |
| 83 | preds_probs = torch.cat(preds_probs, dim=0) |
| 84 | preds_probs = preds_probs.cpu().numpy() |
| 85 | gt_label = gt_label.cpu().numpy() |