In binary image segmentation, for given a set of images, it's true mask and predicted mask. How to compute dice score?, should I compute dice score for each image separately and then find mean across all images. or compute dice score for all images at once by flattening tensor? which is the correct way?
1 Answer
Usually - Compute dice score for each image separately and then calculate mean across all images.
Rationale
The above suggestion assumes that images may have truth masks of different size - and that they are equally important.
Example: Suppose 80% of your data have truth masks of size 1000px, while 20% have truth masks of size 10px. Now lets assume that all large masks are segmented perfectly, while the small ones are totally off (predicted with correct size, but no overlap).
Compute per image => dice = 0.8 * 1.0 + 0.2 * 0.0 = 0.8
Compute globally => dice = (0.8 * N) * 1000 / ((0.8 * N) * 1000 + (0.2 * N) * 10) = 0.9975
The small masks have negligible effect on the total dice score (although 80% of your images failed).
At some cases, it might make sense to measure a global dice score. You can look at it this way - what corresponds to an independent "test case"? an image or a pixel?