Count occurrence of pixel or box labels
The output of countEachLabel
can be used to calculate class weights
for class balancing. For example, for labeled pixel data information in
tbl
:
Uniform class balancing weights each class such that each contains a uniform prior probability:
numClasses = height(tbl) prior = 1/numClasses; classWeights = prior./tbl.PixelCount
Inverse frequency balancing weights each class such that underrepresented classes are given higher weight:
totalNumberOfPixels = sum(tbl.PixelCount) frequency = tbl.PixelCount / totalNumberOfPixels; classWeights = 1./frequency
Median frequency balancing weights each class using the median frequency. The weight for each class is defined as median(imageFreq)/imageFreq(c), where imageFreq(c) represents the number of pixels of the class divided by the total number of pixels in images that had an instance of the class (c):
imageFreq = tbl.PixelCount ./ tbl.ImagePixelCount classWeights = median(imageFreq) ./ imageFreq
pixelClassificationLayer
.