Skip to content

Instantly share code, notes, and snippets.

@alisterburt
Created January 11, 2022 10:07
Show Gist options
  • Select an option

  • Save alisterburt/a732fc84b2c0249c8ab39d1fca95539a to your computer and use it in GitHub Desktop.

Select an option

Save alisterburt/a732fc84b2c0249c8ab39d1fca95539a to your computer and use it in GitHub Desktop.
iou einops
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