MATLAB Answers

Testing a trained neural network using semantic segmentation (segnet layers)

4 views (last 30 days)
Atreya Danturthi
Atreya Danturthi on 9 Aug 2021
Commented: Amanjit Dulai on 19 Aug 2021
I have built a semantic segmentation network using the segnet layers to identify circular and psuedo-circular objects in a series of grayscale images.
I have trained the model with the training dataset stored as an imageDatastore (imds), and would now like to test it with the testdata stored as an imds as well.
Could anyone tell me how do I do that?
  1 Comment
Amanjit Dulai
Amanjit Dulai on 19 Aug 2021
You can do this with the semanticseg function. Because the prediction results for semantic segmentation can be very large, the semanticseg function will write the outputs to disk. Here is a short example.
% Load a pretrained segmentation network.
data = load('triangleSegmentationNetwork');
net =;
% Specify the location of the test data and load the images.
dataDir = fullfile(toolboxdir('vision'),'visiondata','triangleImages');
testImageDir = fullfile(dataDir,'testImages');
imds = imageDatastore(testImageDir);
% Predict on the test data. The results are returned as a datastore with
% image files stored in a temporary directory.
pxdsResults = semanticseg( imds, net, ...
'MiniBatchSize', 4, ...
'WriteLocation', tempdir );
If you have a the ground truth labels as well, you can use semanticseg with evaluateSemanticSegmentation to compute metrics. For example, you can run this code after the previous code:
% Load the labels for the data. Note that class names and pixel labels
% depend on your data.
testLabelDir = fullfile(dataDir,'testLabels');
classNames = ["triangle" "background"];
pixelLabelID = [255 0];
pxdsTruth = pixelLabelDatastore(testLabelDir,classNames,pixelLabelID);
% Compute metrics on the predictions using the ground truth.
metrics = evaluateSemanticSegmentation(pxdsResults,pxdsTruth);

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!