Main Content

pixelLabelTrainingData

Create training data for semantic segmentation from ground truth

Description

example

[imds,pxds] = pixelLabelTrainingData(gTruth)creates image datastoreimdsand pixel label datastorepxdsfrom the specified ground truth. You can combine the returned datastores into apixelLabelImageDatastoreand use thetrainNetwork(Deep Learning Toolbox)function to train deep learning segmentation networks. You can also use these datastores with theevaluateSemanticSegmentationfunction to evaluate the result from deep learning or classical segmentation methods.

This function supports parallel computing using multiple MATLAB®workers. Enable parallel computing using theComputer Vision Toolbox Preferencesdialog box.

[imds,pxds] = pixelLabelTrainingData(gTruth,Name,Value)returns image and pixel label datastores with additional options specified by one or more name-value pair arguments.

  • If thegroundTruthobjects ingTruthwere created using a video file, a custom data source, or animageDatastorewith different custom read functions, then you can specify any combination of name-value pair arguments.

  • If thegroundTruthobjects were created from an image collection or image sequence data source, then you can specify only theSamplingFactorname-value pair argument

Examples

collapse all

Load agroundTruthobject namedgTruth. The ground truth contains pixel labels for triangles and background, annotated on a video with 100 frames.

visiondataPath = fullfile(matlabroot,'toolbox','vision','visiondata'); addpath(fullfile(visiondataPath,'triangleImages')); addpath(fullfile(visiondataPath,'triangleImages','testLabels')); loadedData = load(fullfile(visiondataPath,'triangleImages','triangleGroundTruth.mat')); gTruth = loadedData.gTruth;

Create a folder in the current directory.

foldername = fullfile(tempdir,"videoFrames"); mkdir(foldername)

Create animageDatastoreand apixelLabelDatastorefrom the video file and corresponding pixel labels. Write every fifth image to disk.

[imds,pxdsTruth] = pixelLabelTrainingData(gTruth,...'SamplingFactor',5,'WriteLocation',foldername);
Write images extracted for training to folder: /tmp/videoFrames Writing 20 images extracted from triangleVideo.avi...Completed.

Confirm that the temporary folder contains every fifth image.

imds.Files
ans =20×1细胞{'/tmp/videoFrames/triangleVideo01.png'} {'/tmp/videoFrames/triangleVideo06.png'} {'/tmp/videoFrames/triangleVideo11.png'} {'/tmp/videoFrames/triangleVideo16.png'} {'/tmp/videoFrames/triangleVideo21.png'} {'/tmp/videoFrames/triangleVideo26.png'} {'/tmp/videoFrames/triangleVideo31.png'} {'/tmp/videoFrames/triangleVideo36.png'} {'/tmp/videoFrames/triangleVideo41.png'} {'/tmp/videoFrames/triangleVideo46.png'} {'/tmp/videoFrames/triangleVideo51.png'} {'/tmp/videoFrames/triangleVideo56.png'} {'/tmp/videoFrames/triangleVideo61.png'} {'/tmp/videoFrames/triangleVideo66.png'} {'/tmp/videoFrames/triangleVideo71.png'} {'/tmp/videoFrames/triangleVideo76.png'} {'/tmp/videoFrames/triangleVideo81.png'} {'/tmp/videoFrames/triangleVideo86.png'} {'/tmp/videoFrames/triangleVideo91.png'} {'/tmp/videoFrames/triangleVideo96.png'}

Remove the video and images from the path.

rmpath (fullfile (visiondataPath,'triangleImages')); rmpath(fullfile(visiondataPath,'triangleImages','testLabels'));

Input Arguments

collapse all

Ground truth data, specified as a scalargroundTruthobject or an array ofgroundTruthobjects. WhengTruthis an array ofgroundTruthobjects, theLabelDefinitionsproperty of each object must contain the same pixel label names.

If you use custom data sources ingTruthwith parallel computing enabled, then the reader function is expected to work with a pool of MATLAB workers to read images from the data source in parallel.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:'SamplingFactor',5

Factor for subsampling images in the ground truth data source, specified an integer or a vector of integers. For a sampling factor ofN, the returned training data includes everyNth image in the ground truth data source. The function ignores ground truth images with empty label data

Use sampled data to reduce repeated data, such as a sequence of images with the same scene and labels. It can also help in reducing training time.

Value Sampling Factor
Integer Manually set the sampling factor to apply to all data.
Vector of integers When you input a vector of ground truth objects, the function uses the sampling factor specified by the corresponding vector element.

Image file format, specified as the comma-separated pair consisting of'ImageFormat'and a string scalar or character vector. File formats must be supported byimwrite. This argument applies only forgroundTruthobjects created using a video file or a custom data source.

Folder name to write extracted images to, specified as a string scalar or character vector. The specified folder must exist and have write permissions.

This argument applies only for:

The function ignores this argument when:

  • The inputgroundTruthobject was created from an image sequence data source.

  • The array of inputgroundTruthobjects all contain image datastores using the same customreadfunction.

  • Any of the inputgroundTruthobjects containing datastores, use the defaultreadfunctions.

Image file format, specified as a string scalar or character vector. File formats must be supported byimwrite.

This argument applies only for:

The function ignores this argument when:

  • The inputgroundTruthobject was created from an image sequence data source.

  • The array of inputgroundTruthobjects all contain image datastores using the same customreadfunction.

  • Any of the inputgroundTruthobjects containing datastores, use the defaultreadfunctions.

前缀output image file names, specified as a string scalar or character vector. The image files are named as:

_.

The default value uses the name of the data source that the images were extracted from,strcat(sourceName,'_')for video and custom data source, or'datastore'for image datastore.

This argument applies only for:

The function ignores this argument when:

  • The inputgroundTruthobject was created from an image sequence data source.

  • The array of inputgroundTruthobjects all contain image datastores using the same customreadfunction.

  • Any of the inputgroundTruthobjects containing datastores, use the defaultreadfunctions.

Display training progress on the MATLAB command line, specified as the comma-separated pair consisting of'Verbose'andtrueorfalse. This argument applies only forgroundTruthobjects created using a video file or a custom data source.

Output Arguments

collapse all

Collection of images extracted from the ground truth,gTruth, returned as anImageDatastoreobject. Each image inimdshas annotations with at least one class of pixel labels.imdsignores images that with no annotations.

Collection of pixel-labeled data extracted from the ground truth,gTruth, returned as aPixelLabelDatastoreobject. The object contains a categorical matrix of pixel labels for each image contained in the image datastore,imds. Labels that do not correspond to pixel labels are ignored.

Version History

Introduced in R2018a