Error using trainNetwork (line 191) Layers argument must be an array of layers or a layer graph in my R2024b version
5 views (last 30 days)
Show older comments
Hello,
I m currently training the U-net model in the matlab I m encoutering with same kind of error again and again when i run my mainscript file in command window the error usig trainnetwork, and layers argument must be an array of layers ora layer graph. R2024b is the current version of the matlab im using is that true that trainnetwork is supported in this version if not what is the alternative way to fix this problem, and
net = trainNetwork(dstrain, lgraph, options)
is the another type of error I m facing.
I will share the mainscript file,
% Set paths
imageDir = 'C:\Users\nt01095\Documents\Dataset\Images';
maskDir = 'C:\Users\nt01095\Documents\Dataset\Masks';
% Create datastores
imds = imageDatastore(imageDir);
classNames = ["background", "roof"];
labelIDs = [0, 1];
pxds = pixelLabelDatastore(maskDir, classNames, labelIDs);
% Combine image and pixel label datastores
dsTrain = combine(imds, pxds);
% Preprocess data
dsTrain = transform(dsTrain, @(data) preprocessData(data, [256 256]));
% Define U-Net architecture using the new unet function
inputSize = [256 256 3];
numClasses = 2;
lgraph = unet(inputSize, numClasses); % Use the built-in unet function
% Visualize the network to check its structure
analyzeNetwork(lgraph);
% Training options
options = trainingOptions('adam', ...
'InitialLearnRate', 1e-4, ...
'MaxEpochs', 20, ...
'MiniBatchSize', 8, ...
'Verbose', true, ...
'Plots', 'training-progress');
% Train the network
net = trainNetwork(dsTrain, lgraph, options);
% Save the trained network
save('trainedRoofSegmentationNet.mat', 'net');
% Supporting function for preprocessing
function dataOut = preprocessData(data, targetSize)
% Resize image and label to the target size
dataOut = data;
dataOut{1} = imresize(data{1}, targetSize); % Resize image
dataOut{2} = imresize(data{2}, targetSize, 'nearest'); % Resize label (nearest neighbor)
end
0 Comments
Answers (1)
Gayathri
on 14 Feb 2025
The error you are getting is because "unet(inputSize, numClasses)" returns a "dlnetwork", but "trainNetwork" expects a "layergraph" network. Whenever, you have a "dlnetwork", you can use the "trainnet" function to train the network as shown below.
net=trainnet(dsTrain,lgraph,"crossentropy",options)
If you want to use the "trainNetwork" function, you will have to convert the "dlnetwork" object to a "layergraph" network using the "layerGraph" function. Even though, this also works, priortize using the "trainnet" function as it is recommended.
Please use the below command in the command window to open an example, which is very similar to the code you have provided. You can see the usage of "trainnet" function here.
openExample('vision/TrainUNetExample')
Also look into the documentation below for a better understanding of "layerGraph" function.
0 Comments
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!