Unable to perform assignment because the size of the left side is 1-by-4096 and the size of the right side is 6-by-6-by-256

3 views (last 30 days)
imds = imageDatastore('archive', 'IncludeSubfolders',true, ...
'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
net = alexnet;
inputSize = net.Layers(1).InputSize
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels))
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation)
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
netTransfer = trainNetwork(augimdsTrain,layers,options);
[YPred,scores] = classify(netTransfer,augimdsValidation);
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)
numValidation = numel(imds.Files);
featuresValidation = zeros(numValidation, 4096); % 4096 is the output size of 'pool5' layer in AlexNet
for i = 1:numValidation
img = readimage(imds, i);
img = imresize(img, netTransfer.Layers(1).InputSize(1:2));
features = activations(netTransfer, img, 'pool5');
featuresValidation(i, :) = features;
end
  2 Comments
Petrov Daniel
Petrov Daniel on 17 Dec 2023
Hello,
I try to train an image database using AlexNet and I have to do a clusterisation, but I get this error while trying to extract the information from the last layer before fully connected layers. Can anyone give advice how to solve this, please!

Sign in to comment.

Answers (1)

Yash
Yash on 26 Dec 2023
Hi Petrov,
I understand that you are facing issues while extracting the information from the "pool5" layer of the "AlexNet" network.
According to the MATLAB documentation of the "AlexNet" network, the output size of the "pool5" layer in AlexNet is 6*6*256. The figure below also depicts the same:
The above figure is taken from the MATLAB documentation available at the following link: https://www.mathworks.com/help/deeplearning/ref/alexnet.html
Hence, the assumption that the output size of the "pool5" layer in AlexNet is 4096 is incorrect, and this size mismatch is causing the error you are facing.
If you want a 1D array of features, you can use the "reshape" function. However, the size would still remain 9216 (6*6*256). The documentation of the reshape function is available at the following link: https://www.mathworks.com/help/matlab/ref/reshape.html
Hope this helps!

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!