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 ビュー (過去 30 日間)
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 件のコメント
Petrov Daniel
Petrov Daniel 2023 年 12 月 17 日
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!
Stephen23
Stephen23 2023 年 12 月 17 日
@Petrov Daniel: please show us the complete error message. This means all of the red text.

サインインしてコメントする。

回答 (1 件)

Yash
Yash 2023 年 12 月 26 日
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!

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by