Error during training: Invalid training data. Y must be a vector of categorical responses.

am using single image as input for cnn for segmentation above error am getting please help me to resolve

3 Commenti

It would be helpful if you could provide the code or example you're working on, so I can assist you more effectively.
% Load image and label mask
img = imread('2a.png'); % Replace with your actual image path
labelMask = imread('2a.png'); % Replace with your actual mask path
% Ensure the mask is 2D if it has more than 1 channel
if size(labelMask, 3) > 1
labelMask = rgb2gray(labelMask); % Convert RGB mask to grayscale if needed
end
% Convert image to single precision
XTrain = im2single(img);
% Check dimensions of the image
[height, width, numChannels] = size(XTrain);
% Reshape XTrain
XTrain = reshape(XTrain, [height, width, numChannels, 1]); % Adjust dimensions
% Ensure the mask is uint8 for class labeling
labelMask = uint8(labelMask);
% Check unique values in the mask for debugging
uniqueValues = unique(labelMask);
disp('Unique values in labelMask:');
disp(uniqueValues);
% Define number of classes (for VOC dataset, it is 21 classes including background)
numClasses = 21;
% Ensure all values in the mask are within the range [0, numClasses-1]
if any(labelMask(:) >= numClasses)
disp('Found values outside valid class range. Clipping them to valid range.');
labelMask(labelMask >= numClasses) = numClasses - 1; % Clip invalid values to the maximum class
end
% Convert the mask to categorical
YTrain = categorical(labelMask, 0:numClasses-1); % Convert to categorical
% Flatten YTrain to a vector
YTrain = reshape(YTrain, [], 1);
% Debugging: Display sizes of XTrain and YTrain
disp('Debugging Information:');
disp(['XTrain size: ', mat2str(size(XTrain))]);
disp(['YTrain size: ', mat2str(size(YTrain))]);
% Define CNN Architecture (same as before)
layers = [
imageInputLayer([height width numChannels], 'Name', 'input', 'Normalization', 'none')
convolution2dLayer(3, 16, 'Padding', 'same', 'Name', 'conv1')
reluLayer('Name', 'relu1')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'maxpool1')
convolution2dLayer(3, 32, 'Padding', 'same', 'Name', 'conv3')
reluLayer('Name', 'relu3')
transposedConv2dLayer(2, 32, 'Stride', 2, 'Name', 'upconv1')
reluLayer('Name', 'relu7')
convolution2dLayer(1, numClasses, 'Name', 'conv7')
softmaxLayer('Name', 'softmax')
pixelClassificationLayer('Name', 'pixelClassification')
];
% Set training options
options = trainingOptions('sgdm', ...
'InitialLearnRate', 1e-4, ...
'MaxEpochs', 10, ...
'Verbose', true, ...
'Plots', 'training-progress');
% Train the network
try
net = trainNetwork(XTrain, YTrain, layers, options);
disp('Training completed successfully.');
catch ME
disp('Error during training:');
disp(ME.message);
end
% Load image and label mask
img = imread('2a.png'); % Replace with your actual image path
labelMask = imread('2a.png'); % Replace with your actual mask path
% Ensure the mask is 2D if it has more than 1 channel
if size(labelMask, 3) > 1
labelMask = rgb2gray(labelMask); % Convert RGB mask to grayscale if needed
end
% Convert image to single precision
XTrain = im2single(img);
% Check dimensions of the image
[height, width, numChannels] = size(XTrain);
% Ensure all values in the mask are within the range [0, numClasses-1]
uniqueValues = unique(labelMask);
disp('Unique values in labelMask:');
disp(uniqueValues);
% Define number of classes (for VOC dataset, it is 21 classes including background)
numClasses = 21;
% Ensure all values in the mask are within the range [0, numClasses-1]
labelMask(labelMask >= numClasses) = numClasses - 1; % Clip invalid values to the maximum class
% Convert the mask to categorical
YTrain = categorical(labelMask, 0:numClasses-1); % Convert to categorical
% Debugging: Display sizes of XTrain and YTrain before any reshaping
disp('Debugging Information:');
disp(['XTrain size: ', mat2str(size(XTrain))]);
disp(['YTrain size: ', mat2str(size(YTrain))]);
% Flatten YTrain only if needed to match the network's output shape
YTrain = reshape(YTrain, [height, width]); % Match the 2D structure of the image
% Debugging: Check the final sizes of XTrain and YTrain
disp(['Final XTrain size: ', mat2str(size(XTrain))]);
disp(['Final YTrain size: ', mat2str(size(YTrain))]);
% Define CNN Architecture (same as before)
layers = [
imageInputLayer([height width numChannels], 'Name', 'input', 'Normalization', 'none')
convolution2dLayer(3, 16, 'Padding', 'same', 'Name', 'conv1')
reluLayer('Name', 'relu1')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'maxpool1')
convolution2dLayer(3, 32, 'Padding', 'same', 'Name', 'conv3')
reluLayer('Name', 'relu3')
transposedConv2dLayer(2, 32, 'Stride', 2, 'Name', 'upconv1')
reluLayer('Name', 'relu7')
convolution2dLayer(1, numClasses, 'Name', 'conv7')
softmaxLayer('Name', 'softmax')
pixelClassificationLayer('Name', 'pixelClassification')
];
% Set training options
options = trainingOptions('sgdm', ...
'InitialLearnRate', 1e-4, ...
'MaxEpochs', 10, ...
'Verbose', true, ...
'Plots', 'training-progress');
% Train the network
try
net = trainNetwork(XTrain, YTrain, layers, options);
disp('Training completed successfully.');
catch ME
disp('Error during training:');
disp(ME.message);
end

Accedi per commentare.

Risposte (1)

It looks like you are trying to train a network for image segmentation using only a single image as training data. This will not be effective. I would recommend reading through some of the documentation to determine the best approach based on the images you are trying to segment.

Richiesto:

il 13 Set 2024

Modificato:

il 13 Set 2024

Community Treasure Hunt

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

Start Hunting!

Translated by