Created
January 11, 2022 10:07
-
-
Save alisterburt/a732fc84b2c0249c8ab39d1fca95539a to your computer and use it in GitHub Desktop.
iou einops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def area(a, num_classes): | |
| class_idx = einops.rearrange(torch.arange(num_classes), 'i -> i 1 1 1') | |
| a = a == class_idx | |
| area = einops.reduce(a, 'c ... -> c', reduction='sum') | |
| return area | |
| def intersection(a, b, num_classes) -> float: | |
| class_idx = einops.rearrange(torch.arange(num_classes), 'i -> i 1 1 1') | |
| a = a == class_idx | |
| b = b == class_idx | |
| intersection = torch.logical_and(a, b) | |
| intersection = einops.reduce( | |
| intersection, 'c ... -> c', reduction='sum' | |
| ) | |
| return intersection | |
| def union(a, b, num_classes: int) -> float: | |
| class_idx = einops.rearrange(torch.arange(num_classes), 'i -> i 1 1 1') | |
| a = a == class_idx | |
| b = b == class_idx | |
| union = torch.logical_or(a, b) | |
| union = einops.reduce( | |
| union, 'c ... -> c', reduction='sum' | |
| ) | |
| return union |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment