How to resolve Error using trainNetwork Invalid training or validation response data.

The following is my code:
Load training images and pixel labels into the workspace.
dataSetDir = fullfile(toolboxdir('images'),'imdata','OuterBearing');
imageDir = fullfile(dataSetDir,'images');
labelDir = fullfile(dataSetDir,'labels');
Create an imageDatastore object to store the training images.
imds = imageDatastore(imageDir);
Error using imageDatastore
Cannot find files or folders matching: '/MATLAB/toolbox/images/imdata/OuterBearing/images'.
Define the class names and their associated label IDs.
classNames = ["ScrewHoles", "background"];
labelIDs = [255 0];
pxds = pixelLabelDatastore(labelDir,classNames,labelIDs);
Create the U-Net network.
imageSize =[2048 1536 3];
numClasses = 2;
lgraph = unetLayers(imageSize, numClasses)
Create a datastore for training the network.
ds = combine(imds,pxds);
Set training options.
opts = trainingOptions('adam', ...
'InitialLearnRate',1e-3, ...
'MaxEpochs',10, ...
'MiniBatchSize',100,...
'Plots','training-progress',...
'ExecutionEnvironment','cpu',...
'Verbose',true);
Train the network.
net = trainNetwork(ds,lgraph,opts)
The error I'm getting is:
Error using trainNetwork
Invalid training or validation response data. Categorical responses must either be a vector or a 2-D matrix or a
single-channel 2-D or 3-D image.

Answers (1)

Hi Bowen,
I understand that you are trying to follow the "Train U-NET Network for Semantic Segmentation" example from 'unetLayers' to perform semantic segmentation but facing some issues.
The error is mainly due to the abscence of training images in 'imds' to train unlike the pixel labels.
As you can see, the 'imageDatastore' is not able to load any data as it is not able to find the directory specified.
Hence, please check if the path given is correct and ensure that there are images present in the folder specified.
Please go through the 'imageDatastore' Documentation to know more about Image Datastores and how to use them.

5 Comments

Hi Sairam,
Thank you so much for your reply, however, there isn't such error in my MATLAB application, only in MATLAB Answers. I have also check the specified folder and all the training images are there. May I know how to troubleshoot this error in MATLAB Answers?
OuterBearing is not a folder provided with any toolbox or Support package or File Exchange package that I can see. What leads you to expect that such a folder exists at that location ?
Hi sir, I created a file inside that location and named it OuterBearing. I also added my own training images and training labels.
Hi Bowen,
You are right, as you are placing your data in the required location, MATLAB application will not give any error. However, in MATLAB Answers there is no option to upload the data.
There is no such way as to troubleshoot the error in MATLAB answers, it is suggested to work on your MATLAB Desktop application or MATLAB Online (after uploading your data) when you are working on data that is not available in MATLAB Example Data Sets.
Please follow the MATLAB Answer - List of built-in data sets, please to know about the built-in Datasets that can be used directly without any uploading in MATLAB Application/Answers/Online.
It is possible to attach a .zip here and have code that unzip's it into tempdir() and then use it.
Not really convenient, but possible

Sign in to comment.

Products

Release

R2022b

Asked:

on 2 Dec 2022

Commented:

on 7 Dec 2022

Community Treasure Hunt

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

Start Hunting!