필터 지우기
필터 지우기

Invalid training data. For image, sequence-to-label, and feature classification tasks, responses must be categorical.

조회 수: 4 (최근 30일)
This is my code
imds = imageDatastore("Train\", ...
'IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7);
targetSize = [224,224];
% Resize the images in the training and validation sets
imdsTrainResized = transform(imdsTrain, @(x) imresize(x, targetSize));
imdsValidationResized = transform(imdsValidation, @(x) imresize(x, targetSize));
% Convert labels to categorical for each underlying ImageDatastore
imdsTrain.Labels = categorical(imdsTrain.Labels);
imdsTrainResized.UnderlyingDatastores{1,1}.Labels = categorical(imdsTrainResized.UnderlyingDatastores{1,1}.Labels);
imdsValidation.Labels = categorical(imdsValidation.Labels);
imdsValidationResized.UnderlyingDatastores{1,1}.Labels = categorical(imdsValidationResized.UnderlyingDatastores{1,1}.Labels);
% Combine the resized datastores with the original datastores
imdsTrainCombined = combine(imdsTrain, imdsTrainResized);
imdsValidationCombined = combine(imdsValidation, imdsValidationResized);
% Train the network
net = mobilenetv2('Weights','none');
miniBatchSize = 10;
valFrequency = floor(numel(imdsTrain.Files)/miniBatchSize);
options = trainingOptions('sgdm', ...
'MiniBatchSize',miniBatchSize, ...
'MaxEpochs',6, ...
'InitialLearnRate',3e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidationCombined, ...
'ValidationFrequency',valFrequency, ...
'Verbose',false, ...
'Plots','training-progress',....
'ExecutionEnvironment', "auto");
Trained_net = trainNetwork(imdsTrainCombined, net, options);

답변 (1개)

Saarthak Gupta
Saarthak Gupta 2023년 12월 27일
Hi Susitra,
I understand you are getting the error: “Invalid training data. For image, sequence-to-label, and feature classification tasks, responses must be categorical”.
when trying to train a MobileNetV2 network over your data.
Since the original data is not provided, I have used the “Flowers” dataset from Kaggle to reproduce the error.
It appears that there is a problem with the augmentation of the datastore, causing the responses/labels to be formatted incorrectly. To fix this problem, consider using the `augmentedImageDatastore` function from the Deep Learning Toolbox, which is designed to transform and augment the data properly.
Refer to the following code:
imds = imageDatastore('flowers\', ...
'IncludeSubfolders', true, ...
'LabelSource', 'foldernames');
% Define output image size for the network. MobileNetV2 takes inputs of
% size [224 224 3]
inputSize = [224 224];
% Split the datastore into training and validation sets.
[imdsTrain, imdsValidation] = splitEachLabel(imds, 0.7, 'randomized');
augmenter = imageDataAugmenter( ...
'RandXReflection', true, ...
'RandXTranslation', [-10 10], ...
'RandYTranslation', [-10 10], ...
'RandRotation', [-20 20], ...
'RandScale', [0.8 1.2]);
% Apply the same transformations to the training and validation sets.
augimdsTrain = augmentedImageDatastore(inputSize(1:2), imdsTrain, 'DataAugmentation', augmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2), imdsValidation);
net = mobilenetv2('Weights','none');
% Set training options.
miniBatchSize = 10;
valFrequency = floor(numel(augimdsTrain.Files) / miniBatchSize);
options = trainingOptions('sgdm', ...
'MiniBatchSize', miniBatchSize, ...
'MaxEpochs', 6, ...
'InitialLearnRate', 3e-4, ...
'Shuffle', 'every-epoch', ...
'ValidationData', augimdsValidation, ...
'ValidationFrequency', valFrequency, ...
'Verbose', false, ...
'Plots', 'training-progress', ...
'ExecutionEnvironment', "auto");
Trained_net = trainNetwork(augimdsTrain, lgraph, options);
Refer to the following MATLAB documentation for further reference:

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by