IoU and Dice for a Binary Mask

~10 mincode completion

Implement segmentation_overlap(pred, target) returning a length-2 array [iou, dice].

Examples

One shared pixel, union of three: IoU 1/3, Dice 1/2

Input
segmentation_overlap([[1, 1], [0, 0]], [[1, 0], [1, 0]])
Output
[0.33333, 0.5]

Identical masks score 1 on both

Input
segmentation_overlap([[1, 0], [1, 1]], [[1, 0], [1, 1]])
Output
[1, 1]

Disjoint masks score 0 on both

Input
segmentation_overlap([[1, 0]], [[0, 1]])
Output
[0, 0]

Hints

Hint 1

is the natural log, which is what this formula wants.

Hint 2

Watch for this: used sum of sizes as the union.

Requirements

  • pred: 2D array of 0/1

  • target: 2D array of 0/1, same shape

  • Return array [iou, dice]

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~10 min

8 employers weight this skill

4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 81.

Python
import numpy as np


def segmentation_overlap(pred, target):
    """
    Intersection over union and Dice coefficient of two binary masks.

    Args:
        pred:   2D array of 0/1
        target: 2D array of 0/1, same shape

    Returns:
        array [iou, dice]
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.