Skip to content
DeepForgeby svx
All problems
MediumComputer Visioncv-333

Class-Weighted Segmentation Loss

Problem

Compute the mean class-weighted cross-entropy over a segmentation probability map.

Each pixel with target class c contributes -weights[c] * log(probs[i][j][c]), with the probability clamped to at least 1e-12. Return the mean over all pixels rounded to 4 decimal places.

Test cases

case 1
in: [[[[0.8,0.2],[0.1,0.9]]],[[0,1]],[1,2]]
expected: 0.2169
case 2
in: [[[[1,0]]],[[0]],[3,1]]
expected: 0
case 3
in: [[[[0.5,0.5],[0.5,0.5]]],[[0,0]],[2,0]]
expected: 1.3863
Starter codePython
def weighted_seg_loss(probs, target, weights):
    # Your code here
    pass

3 test cases · run your code against them in the editor