Pixel Accuracy
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=total pixelscorrectly classified 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:
pred = [[0, 1], [1, 0]] gt = [[0, 1], [0, 0]]
0.75
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
You are computing pixel-wise accuracy for a semantic segmentation task: count how many pixels were predicted with the correct class, then divide by the total number of pixels.
1. Background Knowledge
In semantic segmentation, every pixel in an image is assigned a class label (e.g., background, road, car, person), producing a segmentation mask where each pixelâs value is a class index. This is often called dense prediction because the model outputs a classification decision for each pixel rather than a single label per image.
To evaluate such models, we compare the predicted mask to the ground truth mask. One of the simplest metrics is pixel accuracy:
Accuracy = \frac{\text{# correctly classified pixels}}{\text{# total pixels}}This measures the proportion of pixels where prediction and ground truth agree. However, when classes are imbalanced (e.g., mostly background), high pixel accuracy can be achieved even with poor performance on rare classes, which is why more advanced metrics (like IoU/mIoU) are often preferred in practice.
2. Algorithm / General Approach
The pattern is straightforward:
- Align shapes of predicted and ground truth masks (same height, width, and usually same type, e.g., integer class IDs).
- Compare pixels element-wise to create a boolean mask of correct/incorrect predictions.
- Count correct pixels (sum of Trues).
- Count total pixels.
- Compute ratio: correct / total as a float (often between 0 and 1).
This is essentially a vectorized equality check + reduction.
3. Step-by-Step Strategy
Assume you have:
- pred = predicted mask (2D array of shape (H, W) or 3D with batch)
- gt = ground truth mask (same shape)
Step-by-step for a single image:
- Check dimensions
assert pred.shape == gt.shape
- Element-wise comparison
correct_mask = (pred == gt) # boolean array
- Count correct pixels
correct = correct_mask.sum()
- Count total pixels
total = pred.size # or gt.size
- Compute accuracy
accuracy = correct / total
- If there is a batch dimension (B, H, W):
- Either compute per-image accuracy (loop / per-sample reduction),
- Or flatten everything and compute one global accuracy over all pixels.
4. Common Pitfalls
- Shape mismatch: Pred mask might be (H, W) while ground truth is (1, H, W) or (H, W, 1). Ensure shapes are identical before comparison.
- Logits vs class indices: The model may output probabilities or logits (C, H, W); you must first take argmax over the class dimension to get class indices:
pred_classes = logits.argmax(dim=0) # or dim=1 for (N, C, H, W)
- Data type issues: Comparing floats vs ints can be misleading; convert both to integer class IDs if needed.
- Including ignored labels: Sometimes ground truth uses a special label (e.g., -1 or 255) for âignoreâ. Those pixels should be excluded from both numerator and denominator:
mask = (gt != ignore_index)
correct = ((pred == gt) & mask).sum()
total = mask.sum()
- Integer division: In some languages, dividing two integers yields an integer. Cast to float to avoid truncation:
accuracy = correct / float(total)
5. Time & Space Complexity
Let the image (or batch) have N total pixels.
-
Time complexity:
-
Element-wise comparison: O(N)
-
Summation (counting correct / total): O(N)
-
Overall: O(N)
-
Space complexity:
-
Storing prediction and ground truth: O(N) each (given).
-
Temporary boolean mask (if created): O(N).
-
So extra space is O(N), though some frameworks can fuse operations to reduce explicit extra memory.