This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Create pixel classification layer for semantic segmentation


You can use the pixel classification layer to provide a categorical label for each image pixel processed by a convolutional neural network (CNN).



layer = pixelClassificationLayer
layer = pixelClassificationLayer(Name,Value)



layer = pixelClassificationLayer creates a pixel classification output layer for semantic image segmentation networks. The layer outputs the categorical label for each image pixel processed by a CNN. The layer automatically ignores undefined pixel labels during training.


layer = pixelClassificationLayer(Name,Value) returns a pixel classification output layer using Name,Value pair arguments to set the optional ClassNames, ClassWeights, and Name properties by using name-value pairs.


expand all

Class names, specified as 'auto', a string vector, or a cell array of character vectors. When you set ClassNames to 'auto', the class names are automatically set during training.

Class weights, specified as 'none' or as a vector of real scalar. The elements of the vector correspond to the classes in ClassNames. If you specify ClassWeights, then you must specify ClassNames.

Use class weighting to balance classes when there are underrepresented classes in the training data.

Layer name, specified as a character vector. If Name is set to '', then the software automatically assigns a name at training time.

Data Types: char

This property is read-only.

The output size of the layer. The value is 'auto' prior to training, and is specified as a numeric value at training time.

This property is read-only.

Loss function used for training, specified as 'crossentropyex'.


expand all

Predict the categorical label of every pixel in an input image.

layers = [
         imageInputLayer([32 32 3])
layers = 
  5x1 Layer array with layers:

     1   ''   Image Input                  32x32x3 images with 'zerocenter' normalization
     2   ''   Convolution                  16 3x3 convolutions with stride [2  2] and padding [1  1]
     3   ''   ReLU                         ReLU
     4   ''   Transposed Convolution       1 3x3 transposed convolutions with stride [2  2] and output cropping [1  1]
     5   ''   Pixel Classification Layer   Cross-entropy loss 

Balance classes using inverse class frequency weighting when some classes are underrepresented in the training data. First, count class frequencies over the training data using pixelLabelImageSource. Then, set the 'ClassWeights' in pixelClassificationLayer to the computed inverse class frequencies.

Set the location of image and pixel label data.

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

Create datasource using ground truth and pixel labeled images.

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

Tabulate class distribution in dataset.

  tbl = countEachLabel(src)
tbl =

  4x3 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      

Calculate inverse frequency class weights.

  totalNumberOfPixels = sum(tbl.PixelCount);
  frequency = tbl.PixelCount / totalNumberOfPixels;
  inverseFrequency = 1./frequency
inverseFrequency =


Set 'ClassWeights' to the inverse class frequencies.

  layer = pixelClassificationLayer(...
layer = 

  PixelClassificationLayer with properties:

            Name: ''
      ClassNames: {'sky'  'grass'  'building'  'sidewalk'}
    ClassWeights: [4x1 double]
      OutputSize: 'auto'

    LossFunction: 'crossentropyex'

Introduced in R2017b