Borrar filtros
Borrar filtros

Unexpected image size: All images must have the same size.

35 visualizaciones (últimos 30 días)
I have a problem in classifiying sign languages images.
Error using trainNetwork (line 183)
Unexpected image size: All images must have the same size.
I have tried this solution but it did not work
please find attached the code
imds = imageDatastore('ArASL_Database_6k','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsTest] = splitEachLabel(imds,0.7,'randomized');
[imdsTrain,imdsValidation] = splitEachLabel(imdsTrain,0.7,'randomized');
imdsTrain.ReadFcn = @(filename)preprocess_image(filename);
imdsTest.ReadFcn = @(filename)preprocess_image(filename);
imdsValidation.ReadFcn = @(filename)preprocess_image(filename);
net = alexnet;
inputSize = net.Layers(1).InputSize;
%If the network is a SeriesNetwork object, such as AlexNet, VGG-16,
%or VGG-19, then convert the list of layers in net.Layers to a layer graph.
if isa(net,'SeriesNetwork')
lgraph = layerGraph(net.Layers);
lgraph = layerGraph(net);
[learnableLayer,classLayer] = findLayersToReplace(lgraph);
numClasses = numel(categories(imdsTrain.Labels));
if isa(learnableLayer,'nnet.cnn.layer.FullyConnectedLayer')
newLearnableLayer = fullyConnectedLayer(numClasses, ...
'Name','new_fc', ...
'WeightLearnRateFactor',10, ...
elseif isa(learnableLayer,'nnet.cnn.layer.Convolution2DLayer')
newLearnableLayer = convolution2dLayer(1,numClasses, ...
'Name','new_conv', ...
'WeightLearnRateFactor',10, ...
lgraph = replaceLayer(lgraph,learnableLayer.Name,newLearnableLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,classLayer.Name,newClassLayer);
layers = lgraph.Layers;
connections = lgraph.Connections;
layers(1:10) = freezeWeights(layers(1:10));
lgraph = createLgraphUsingConnections(layers,connections);
pixelRange = [-30 30];
scaleRange = [0.9 1.1];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange, ...
'RandXScale',scaleRange, ...
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain,'ColorPreprocessing', 'gray2rgb','DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation,'ColorPreprocessing', 'gray2rgb');
augimdsTest = augmentedImageDatastore(inputSize(1:2),imdsTest,'ColorPreprocessing', 'gray2rgb');
miniBatchSize = 14;
valFrequency = floor(numel(augimdsTrain.Files)/miniBatchSize);
options = trainingOptions('sgdm', ...
'MiniBatchSize',miniBatchSize, ...
'MaxEpochs',1, ...
'InitialLearnRate',0.0003, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',valFrequency, ...
'Verbose',false, ...
'Plots','training-progress','ExecutionEnvironment', 'cpu');
net = trainNetwork(augimdsTrain,lgraph,options);
[YPred,probs] = classify(net,augimdsTest,'ExecutionEnvironment', 'cpu');
accuracy = mean(YPred == imdsTest.Labels);

Respuesta aceptada

Divya Gaddipati
Divya Gaddipati el 10 de Mayo de 2021
This error could be due to a few different causes.
1. The images you are classifying might not all be the same size.
2. Your images might have different numbers of channels - some images could be RGB while others are grayscale.
3. There could be images with incompatible formats within your dataset.
The following workflow can help illuminate the cause:
imageDS = imageDatastore(imgPath);
imageDS.ReadFcn = @customReadDatstoreImage;
function data = customReadDatastoreImage(filename)
data = imread(filename);
% added lines:
data = data(:,:,min(1:3, end));
data = imresize(data,[224 224]);
1. Write a custom read function for your image datastore.
This doc page describes the 'ReadFcn' for imageDatastore objects:
You can start with the default 'ReadFcn', and make a copy with a different name. Then, set the 'ReadFcn' property of your datastore to this custom function.
2. At the end of the custom ReadFcn, add a line that resizes the images to your desired image size. Example:
data = imresize(data, [224 224]);
Run your script again, and see if the issue is resolved.
3. If you still get the same error message, add another line to the custom ReadFcn that sets all the images to have the same number of channels as the image with the fewest channels. This line should go right before the 'imresize' line. Example:
data = data(:,:,min(1:3, end));
If you are using an augmented image datastore (in R2018b) for your data, you can use the following line to standardize the number of channels in all of your images:
auimds = augmentedImageDatastore(outputSize,imds, 'ColorPreprocessing', 'gray2rgb')
Run your script again, and observe if you still get the same error message.
4. At this point, it is possible you are getting a different more specific error message, referring to incompatible image formats. If this is the case, you will need to remove these incompatible images from your dataset. The error message should point out which images are incompatible. Remove these images, and try classifying again.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by