how to fix the warning message always displayed when I want to classify images after augmentation process, the worning message is about preallocating

3 visualizaciones (últimos 30 días)
I have dataset of x-ray images, and I want to classify the dataset to normal and not_normal. I have used the augmentation process as shown in the following script:
augmenter = imageDataAugmenter( ...
'RandRotation',[-5 5],'RandXReflection',1,...
'RandYReflection',1,'RandXShear',[-0.05 0.05],'RandYShear',[-0.05 0.05]);
auimds = augmentedImageDatastore([224 224],imdsTrain,'DataAugmentation',augmenter);
netTransfer=trainNetwork(auimds,lgraph,options);
augtestimds=augmentedImageDatastore([224 224],imdsTest);
[predicted_labels(test_idx),posterior(test_idx,:)] = classify(netTransfer,augtestimds);
But every time there is a warning message appeare on the last statement, which is: [predicted_labels(test_idx),posterior(test_idx,:)] = classify(netTransfer,augtestimds);
The warning message said that the variable 'posterior' appears to change size on every loop iteration (within a script). consider preallocating for speed.
How can fix this issue?
Thanks in advance

Respuestas (1)

Nithin
Nithin el 26 de Mzo. de 2025
The warning message you are seeing is due to the dynamic resizing of the "posterior" variable in each iteration of the loop. This can slow down the code because MATLAB needs to allocate new memory every time the size changes. Pre-allocating the “posterior” variable before the loop resolves the issue. Kindly refer to the following documentation to know more about pre-allocation: https://www.mathworks.com/help/releases/R2021b/matlab/matlab_prog/preallocating-arrays.html?searchHighlight=preallocation&searchResultIndex=1
A general approach to pre-allocate the "posterior" variable is to start by determining the number of test samples and the number of classes. Then, pre-allocate the "posterior" array with zeros or another placeholder value. Refer to the below sample script which implements the pre-allocation:
numTestSamples = numel(imdsTest.Files); % Get the number of test samples
numClasses = numel(categories(imdsTest.Labels)); % Get the number of classes
% Preallocate the posterior array
posterior = zeros(numTestSamples, numClasses);
% Preallocate the predicted_labels array
predicted_labels = categorical(zeros(numTestSamples, 1));
% Loop through each test sample
for test_idx = 1:numTestSamples [predicted_labels(test_idx), posterior(test_idx, :)] = classify(netTransfer, augtestimds.read());
end

Categorías

Más información sobre Deep Learning Toolbox en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by