Compute the accuracy. Args: arr: (*b_shape, *d_shape), binary ref: (*b_shape, *d_shape), binary ndim_b: number of dimension of b_shape. If None, = 0. valid_mask: (*b_shape, *d_shape,) Returns: acc: (*b_shape,)
(
arr: torch.Tensor,
ref: torch.Tensor,
ndim_b: int = None,
valid_mask: torch.Tensor = None,
)
| 437 | |
| 438 | |
| 439 | def compute_accuracy( |
| 440 | arr: torch.Tensor, |
| 441 | ref: torch.Tensor, |
| 442 | ndim_b: int = None, |
| 443 | valid_mask: torch.Tensor = None, |
| 444 | ): |
| 445 | """ |
| 446 | Compute the accuracy. |
| 447 | |
| 448 | Args: |
| 449 | arr: (*b_shape, *d_shape), binary |
| 450 | ref: (*b_shape, *d_shape), binary |
| 451 | ndim_b: |
| 452 | number of dimension of b_shape. If None, = 0. |
| 453 | valid_mask: (*b_shape, *d_shape,) |
| 454 | |
| 455 | Returns: |
| 456 | acc: (*b_shape,) |
| 457 | """ |
| 458 | if ndim_b is None: |
| 459 | ndim_b = 0 |
| 460 | |
| 461 | same = (arr > 0.5) == (ref > 0.5) # (*b, *d) |
| 462 | same = same.reshape(*(arr.shape[:ndim_b]), -1) # (*b, numel_d) |
| 463 | if valid_mask is None: |
| 464 | acc = same.float().mean(dim=-1) # (*b,) |
| 465 | else: |
| 466 | valid_mask = valid_mask.view( |
| 467 | *(valid_mask.shape), *([1] * (arr.ndim - valid_mask.ndim))).expand_as(arr) |
| 468 | valid_mask = valid_mask.reshape(*(arr.shape[:ndim_b]), -1) # (*b, numel_d) |
| 469 | acc = (same.float() * valid_mask).sum(dim=-1) / valid_mask.sum(-1) |
| 470 | return acc |