0
0
Computer Visionml~10 mins

Segmentation evaluation (IoU, Dice) in Computer Vision - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the Intersection over Union (IoU) between two binary masks.

Computer Vision
def iou_score(mask1, mask2):
    intersection = (mask1 & mask2).sum()
    union = (mask1 | mask2).sum()
    return intersection [1] union
Drag options to blanks, or click blank then click option'
A+
B-
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of division
Multiplying intersection and union
2fill in blank
medium

Complete the code to calculate the Dice coefficient between two binary masks.

Computer Vision
def dice_score(mask1, mask2):
    intersection = (mask1 & mask2).sum()
    return (2 * intersection) [1] (mask1.sum() + mask2.sum())
Drag options to blanks, or click blank then click option'
A+
B/
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division
Forgetting to multiply intersection by 2
3fill in blank
hard

Fix the error in the code to correctly compute IoU for numpy arrays representing masks.

Computer Vision
import numpy as np

def compute_iou(pred_mask, true_mask):
    intersection = np.logical_and(pred_mask, true_mask).sum()
    union = np.logical_or(pred_mask, true_mask).sum()
    iou = intersection [1] union
    return iou
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or addition instead of division
Not using logical operations for intersection and union
4fill in blank
hard

Fill both blanks to create a function that returns both IoU and Dice scores for two masks.

Computer Vision
def evaluate_segmentation(mask_a, mask_b):
    intersection = (mask_a & mask_b).sum()
    union = (mask_a | mask_b).sum()
    iou = intersection [1] union
    dice = (2 * intersection) [2] (mask_a.sum() + mask_b.sum())
    return iou, dice
Drag options to blanks, or click blank then click option'
A/
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication instead of division
Mixing operators between blanks
5fill in blank
hard

Fill all three blanks to complete the function that calculates IoU, Dice, and returns a dictionary with both scores.

Computer Vision
def segmentation_metrics(pred, truth):
    inter = (pred & truth).sum()
    union = (pred | truth).sum()
    iou = inter [1] union
    dice = (2 * inter) [2] (pred.sum() [3] truth.sum())
    return {"IoU": iou, "Dice": dice}
Drag options to blanks, or click blank then click option'
A/
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition in denominator
Using multiplication instead of division