Issue with Image sizes when doing transfer learning on Alexnet
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I get the following error:
Error using nnet.internal.cnn.MiniBatchDatasourceDispatcher>iCellTo4DArray (line 328)
Unexpected image size: All images must have the same size.
Error in nnet.internal.cnn.MiniBatchDatasourceDispatcher/readObservations (line 253)
observations = iCellTo4DArray(X);
Error in nnet.internal.cnn.MiniBatchDatasourceDispatcher/readData (line 245)
miniBatchData = this.readObservations(X);
Error in nnet.internal.cnn.MiniBatchDatasourceDispatcher/next (line 144)
[miniBatchData, miniBatchResponse] = readData(this);
Error in SeriesNetwork/predict (line 337)
[X, ~, i] = dispatcher.next();
Error in SeriesNetwork/classify (line 559)
scores = this.predict( X, varargin{:} );
When trying to retrain alexnet with my own imagesets. The directory contains images that is not in 227 by 227 size, so Im resizing them. It fails on testpreds = classify(XXnet,testImgs); I've tried the current code with the ReadFcn, but I've also tried a augmentedImageSource for testing. I cant get this to work.
Since I try to use the same code for resizing both training and testing images, I dont really understand this at all.
XXds = imageDatastore('ImageDir','IncludeSubfolders',true,'LabelSource','foldernames');
testImgs.ReadFcn=@(images_set)imresize(imread(images_set),[227 227]);
[trainImgs,testImgs] = splitEachLabel(XXds,0.6);
trainImgs.ReadFcn=@(trainImgs)imresize(imread(trainImgs),[227 227]);
XXTrain = augmentedImageSource([227 227], trainImgs)
testImgs.ReadFcn = @(testImgs)imresize(imread(testImgs),[227 227])
testImgsBatch = imresize(testImgs.read(), [227 227]);
numClasses = numel(categories(XXds.Labels));
net = alexnet;
layers = net.Layers;
layers(end-2) = fullyConnectedLayer(numClasses);
layers(end) = classificationLayer;
options = trainingOptions('sgdm','InitialLearnRate', 0.001,'MaxEpochs', 1);
[XXnet,info] = trainNetwork(XXTrain, layers, options);
testpreds = classify(XXnet,testImgs);
nnz(testpreds == testImgs.Labels)/numel(testpreds)
[XXconf,XXnames] = confusionmat(testImgs.Labels,testpreds);
heatmap(XXnames,XXnames,XXconf);
Can anyone see what Im doing wrong here?
0 comentarios
Respuestas (1)
Sourabh
el 22 de Abr. de 2025
Hey @Emil Alne
As per my understanding, AlexNet requires RGB images. Using “augmentedImageDatastore” for training ensures correct size but not using it for testing may be the reason why the size check fails during classify.
Kindly refer the following code snippet to fix this issue:
XXds = imageDatastore('ImageDir', ...
'IncludeSubfolders', true, ...
'LabelSource', 'foldernames');
% Split dataset
[trainImgs, testImgs] = splitEachLabel(XXds, 0.6);
% Use augmentedImageDatastore to resize and ensure RGB format
XXTrain = augmentedImageDatastore([227 227 3], trainImgs);
XXTest = augmentedImageDatastore([227 227 3], testImgs);
% Setup AlexNet for transfer learning
net = alexnet;
layers = net.Layers;
numClasses = numel(categories(XXds.Labels));
layers(end-2) = fullyConnectedLayer(numClasses);
layers(end) = classificationLayer;
% Training options
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.001, ...
'MaxEpochs', 1);
% Train
[XXnet, info] = trainNetwork(XXTrain, layers, options);
% Classify test images
testpreds = classify(XXnet, XXTest);
% Evaluate
accuracy = nnz(testpreds == testImgs.Labels) / numel(testpreds);
disp(['Accuracy: ' num2str(accuracy)]);
[XXconf, XXnames] = confusionmat(testImgs.Labels, testpreds);
heatmap(XXnames, XXnames, XXconf);
For more information on “augmentedImageDatastore” and “alexnet”, kindly refer the following MATLAB documentation:
0 comentarios
Ver también
Categorías
Más información sobre Deep Learning Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!