How to solve the error: The spatial dimension sizes [313 316 3] of the input images to layer 'imageinput' must be greater than or equal to the corresponding minimum input dime

I tried to use evaluatesemanticsegmentation function as follow:
destinationTestLabels = "D:\Matlab pics\matlab practices21\Phd analysis\trial meyhods\GroundTruth\crack\testLabels";
destinationTestImages = "D:\Matlab pics\matlab practices21\Phd analysis\trial meyhods\GroundTruth\crack\testImages";
testImagesDir = fullfile(destinationTestImages,'testImages');
testLabelsDir = fullfile(destinationTestLabels,'testLabels');
pixelLabelID = [1 0];
classNames = ["crack" "background"];
imds = imageDatastore(destinationTestImages);
pxdstest11 = pixelLabelDatastore(destinationTestLabels,classNames,pixelLabelID,"FileExtensions",".png");
data = load('triangleSegmentationNetwork');
net = data.net
pxdsResults = semanticseg(imds,net,"WriteLocation",tempdir);
I got the following error:
Error using SeriesNetwork/activations (line 817)
The spatial dimension sizes [313 316 3] of the input images to layer 'imageinput' must be greater than
or equal to the corresponding minimum input dimension sizes of the layer [32 32 1].
Error in semanticseg>iPredictDAGSeriesNetwork (line 609)
allScores = activations(net, X, layerName, ...
Error in semanticseg>iClassifyImagePixels (line 556)
allScores = iPredictDAGSeriesNetwork(X, net, params);
Error in semanticseg>iProcessImageDatastoreSerially (line 938)
L = iClassifyImagePixels(X{i}, net, params);
Error in semanticseg (line 265)
filenames = iProcessImageDatastoreSerially(ds, net, params);
Error in Gtruth3 (line 11)
pxdsResults = semanticseg(imds,net,"WriteLocation",tempdir);
I followed the same steps mentioned in:
www.mathworks.com/help/vision/ref/evaluatesemanticsegmentation.html
The files mentioned has the same two attached images, can anyone help me to fix this erro to calculate IoU?

6 Comments

See if the following simplified code works. It seems to work here:
A=imread('gT7001_60.png');
net = load('triangleSegmentationNetwork').net;
out=semanticseg(A,net);
whos A out
Name Size Bytes Class Attributes A 312x312x3 292032 uint8 out 312x312 97590 categorical
@Matt J I didnt get it, and still there is an error and how i will be able to evaluate IoU by semantic as shown in
I already hve two files testImages and testLabels and each will contain 100 or more of groundTruth and testImages and all shoud enter semantic segemnation not only one image (gT7001_60.png) , I still have error as shown:
destinationTestLabels = "D:\Matlab pics\matlab practices21\Phd analysis\trial meyhods\GroundTruth\crack\testLabels";
destinationTestImages = "D:\Matlab pics\matlab practices21\Phd analysis\trial meyhods\GroundTruth\crack\testImages";
testImagesDir = fullfile(destinationTestImages,'testImages');
testLabelsDir = fullfile(destinationTestLabels,'testLabels');
pixelLabelID = [1 0];
classNames = ["crack" "background"];
imds = imageDatastore(destinationTestImages);
pxdstest11 = pixelLabelDatastore(destinationTestLabels,classNames,pixelLabelID,"FileExtensions",".png");
data = load('triangleSegmentationNetwork');
net = data.net
pxdsResults = semanticseg(imds,net,"WriteLocation",tempdir);
metrics = evaluateSemanticSegmentation(pxdsResults,pxdsTruth);
metrics
metrics.ClassMetrics
Error using SeriesNetwork/activations (line 817)
The spatial dimension sizes [313 316 3] of the input images to layer 'imageinput' must be greater than
or equal to the corresponding minimum input dimension sizes of the layer [32 32 1].
Error in semanticseg>iPredictDAGSeriesNetwork (line 609)
allScores = activations(net, X, layerName, ...
Error in semanticseg>iClassifyImagePixels (line 556)
allScores = iPredictDAGSeriesNetwork(X, net, params);
Error in semanticseg>iProcessImageDatastoreSerially (line 938)
L = iClassifyImagePixels(X{i}, net, params);
Error in semanticseg (line 265)
filenames = iProcessImageDatastoreSerially(ds, net, params);
Error in Gtruth3 (line 11)
pxdsResults = semanticseg(imds,net,"WriteLocation",tempdir);
How can I fix it?
@Matt J I didnt get it,
What do you mean "didn't get it"? What happened when you ran the simplified code? As you can see, it ran for me with no errors. Did you get a different result?
@Matt J I want to evaluate the IoU as shown in prevoiuse and the code should include many images as shown in Evaluate semantic segmentation data set against ground truth - MATLAB evaluateSemanticSegmentation (mathworks.com)
not only (gT7001_60.png) ,I still have error as shown above, please check my code above and you will understand what I mean and what i want to find
Please stop re-iterating your original post and goals. We all understand you have a bigger task than the simplified code, but the simplified code contains key parts of the original code which you should be checking first. If it runs fine for you, then the problem is somewhere in your imds, because it is then obvious that the network fails only for particular images in the data store.

Sign in to comment.

Answers (1)

The error is occuring because the network ("triangleSegmentationNetwork") expects grayscale images with an input size of [32 32 1], but the images in the Datastore are RGB images with dimensions [313 316 3].
The MATLAB example works because it uses grayscale images that match the network’s input requirements.
Now, as I see in the comments section, where one image was passed to the "semanticseg" function and it worked without throwing any errors. This is because when a single image is passed the underlying code converts the RGB image to a grayscale image as expected by the network. I am explaining with reference to the code below.
A=imread('gT7001_60.png');
net = load('triangleSegmentationNetwork').net;
out=semanticseg(A,net);
whos A out
However, when using an imageDatastore with multiple images, "semanticseg" function does not automatically perform this preprocessing, leading to the dimension mismatch error.
To resolve this, we need to preprocess the images in the imageDatastore by converting them to grayscale and resizing them to [32 32] before passing them to semanticseg. Alternatively, you can use a different network trained to accept RGB images (e.g., with input size [32 32 3] or larger).
Hope this helps!

Asked:

on 6 Oct 2023

Answered:

on 12 Jun 2025

Community Treasure Hunt

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

Start Hunting!