countEachLabel

Count occurrence of pixel label for data source images

Syntax

tbl = countEachLabel(datastore)

Description

example

tbl = countEachLabel(datastore) returns a table containing information about the pixel labels and count for the datastore.

Examples

collapse all

Set the location of image and pixel label data.

dataDir = fullfile(toolboxdir('vision'),'visiondata');
imDir = fullfile(dataDir,'building');
pxDir = fullfile(dataDir,'buildingPixelLabels');

Create a pixel label image datastore using the ground truth images in imds and the pixel labeled images in pxds.

imds = imageDatastore(imDir);
classNames = ["sky" "grass" "building" "sidewalk"];
pixelLabelID = [1 2 3 4];
pxds = pixelLabelDatastore(pxDir,classNames,pixelLabelID);
pximds = pixelLabelImageDatastore(imds,pxds);

Tabulate pixel label counts in dataset.

tbl = countEachLabel(pximds)
tbl=4×3 table
       Name       PixelCount    ImagePixelCount
    __________    __________    _______________

    'sky'         3.1485e+05       1.536e+06   
    'grass'       1.5979e+05       1.536e+06   
    'building'    1.0312e+06       1.536e+06   
    'sidewalk'         25313       9.216e+05   

Balance classes using uniform prior weighting.

prior = 1/numel(classNames);
uniformClassWeights = prior./tbl.PixelCount
uniformClassWeights = 4×1
10-5 ×

    0.0794
    0.1565
    0.0242
    0.9876

Balance classes using inverse frequency weighting.

totalNumberOfPixels = sum(tbl.PixelCount);
frequency = tbl.PixelCount / totalNumberOfPixels;
invFreqClassWeights = 1./frequency
invFreqClassWeights = 4×1

    4.8632
    9.5827
    1.4848
   60.4900

Balance classes using median frequency weighting.

freq = tbl.PixelCount ./ tbl.ImagePixelCount
freq = 4×1

    0.2050
    0.1040
    0.6714
    0.0275

medFreqClassWeights = median(freq) ./ freq
medFreqClassWeights = 4×1

    0.7538
    1.4852
    0.2301
    5.6252

Pass the class weights using median frequency weighting to the pixel classification layer.

layer = pixelClassificationLayer('Classes',tbl.Name, ...
  'ClassWeights', medFreqClassWeights)
layer = 
  PixelClassificationLayer with properties:

            Name: ''
         Classes: [sky    grass    building    sidewalk]
    ClassWeights: [4x1 double]
      OutputSize: 'auto'

   Hyperparameters
    LossFunction: 'crossentropyex'

Input Arguments

collapse all

Datastore for training a semantic segmentation network, specified as a pixelLabelImageDatastore or pixelLabelDatastore object.

Output Arguments

collapse all

Pixel label information, returned as a table. The table contains three variables:

VariableDescription
NamePixel label class name
PixelCountNumber of pixels in class
ImagePixelCountTotal number of pixels in images that had an instance of a class

Tips

The output of countEachLabel can be used to calculate class weights for class balancing. For example:

  • 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
    
    The calculated class weights can be passed to the pixelClassificationLayer

Introduced in R2017b