PIXELBANKv9.1.0
Menu

You are given a predicted segmentation mask and ground truth mask, and need to calculate the pixel-wise accuracy.

Pixel accuracy is the simplest segmentation metric:

Accuracy=correctly classified pixelstotal pixelsAccuracy = \frac{\text{correctly classified pixels}}{\text{total pixels}}

While easy to compute, pixel accuracy can be misleading when classes are imbalanced. For example, if 90% of pixels are background, a trivial "all background" prediction achieves 90% accuracy.

Compare each pixel's predicted class to its ground truth class and count matches.

Example:

Input:
pred = [[0, 1], [1, 0]]
gt = [[0, 1], [0, 0]]
Output:
0.75
Reasoning:

Comparing pixel by pixel:

  • (0,0): pred=0, gt=0 → correct
  • (0,1): pred=1, gt=1 → correct
  • (1,0): pred=1, gt=0 → wrong
  • (1,1): pred=0, gt=0 → correct

Correct = 3, Total = 4 Accuracy = 3/4 = 0.75

Constraints:

  • pred and gt are 2D lists (masks) of the same dimensions
  • Each value is a class index (integer)
  • Return accuracy rounded to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.