Getting error while running alexnet

net =alexnet('Weights','none');
lys = net.Layers();
lys(end-3:end)
getting error:
Unrecognized method, property, or field 'Layers' for class 'nnet.cnn.layer.Layer'.

답변 (1개)

Walter Roberson
Walter Roberson 2023년 5월 14일
편집: Walter Roberson 2023년 5월 14일

0 개 추천

When you call alexnet() with 'Weights', 'none' then the result you get back is a Layer array, not a DagNetwork or LayerGraph. The list of layers that you are trying to get is just the same as net itself in this case.
You need the .Layers() if you load in the regular pre-trained alexnet

댓글 수: 11

Tan
Tan 2023년 5월 14일
so what should i do?
net = alexnet('Weights','none');
lys = net;
lys(end-3:end)
and then whatever it was you were going to do with lys
net =alexnet('Weights','none');
analyzeNetwork(net)
lys = net;
lys(end-3:end)
numClasses = numel(categories(imdsTrain.Labels));
lgraph = layerGraph(lys);
%Replace the classification layers for new task
newFCLayer = fullyConnectedLayer(3,'Name','new_fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10);
lgraph = replaceLayer(lgraph,'fc8',newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'output',newClassLayer);
%Resize the image
imageSize=lys(1).InputSize;
is it correct?
Walter Roberson
Walter Roberson 2023년 5월 14일
I do not know whether it is correct. For one thing, you have not described to us what you are trying to do.
Tan
Tan 2023년 5월 14일
trying to perform image classification based on transfer learning using alexnet.
Tan
Tan 2023년 5월 14일
편집: Walter Roberson 2023년 5월 14일
here is my full coding:
clc
clear all;
close all;
outputFolder=fullfile('recycle101');
rootFolder=fullfile(outputFolder,'project');
categories={'aluminiumcan','petbottle','drinkcartonbox'};
imds=imageDatastore(fullfile(rootFolder,categories),'LabelSource','foldernames');
tbl=countEachLabel(imds)
minSetCount=min(tbl{:,2});
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
countEachLabel(imds);
%randomly choose file for aluminium can, PET bottles, and drink carton box
AluminiumCan=find(imds.Labels=='aluminiumcan',1);
PETBottles=find(imds.Labels=='petbottle',1);
DrinkCartonBox=find(imds.Labels=='drinkcartonbox',1);
%plot image that was pick randomly
figure
subplot(2,2,1);
imshow(readimage(imds,AluminiumCan));
subplot(2,2,2);
imshow(readimage(imds,PETBottles));
subplot(2,2,3);
imshow(readimage(imds,DrinkCartonBox));
%Load pre-trained network
%net=alexnet;
net =alexnet('Weights','none');
analyzeNetwork(net)
lys = net;
lys(end-3:end)
numClasses = numel(categories(imdsTrain.Labels));
lgraph = layerGraph(lys);
%Replace the classification layers for new task
newFCLayer = fullyConnectedLayer(3,'Name','new_fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10);
lgraph = replaceLayer(lgraph,'fc8',newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'output',newClassLayer);
%Resize the image
imageSize=lys(1).InputSize;
augmentedTrainingSet=augmentedImageDatastore(imageSize,...
imdsTrain,'ColorPreprocessing','gray2rgb');
augmentedValidateSet=augmentedImageDatastore(imageSize,...
imdsValidation,'ColorPreprocessing','gray2rgb');
options = trainingOptions('sgdm', ...
'MiniBatchSize',4, ...
'MaxEpochs',8, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augmentedValidateSet, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'ExecutionEnvironment','cpu', ...
'Plots','training-progress');
trainedNet = trainNetwork(augmentedTrainingSet,lgraph,options);
[YPred,probs] = classify(trainedNet,augmentedValidateSet,'ExecutionEnvironment', 'cpu');
YValidation = imdsValidation.Labels;
%Calculate accuracy,error,pecision and recall
accuracy = sum(YPred == YValidation)/numel(YValidation)
error=1-accuracy
confMat=confusionmat(YValidation ,YPred);
confMat=bsxfun(@rdivide,confMat,sum(confMat,2));
z=mean(diag(confMat))
cmt=confMat
sum_of_row= sum(cmt,2)
z=(diag(cmt));
precision = z./sum_of_row ;
overallprecision=mean(precision);
sum_of_column= sum(cmt,1)
recall=z./sum_of_column'
totalrecall=mean(recall);
f1=2*(overallprecision*totalrecall)/(overallprecision+totalrecall)
categories = {'aluminium can','PET bottle','drink carton box'}
label = categorical(categories)
cm = confusionchart(cmt,label)
cm.RowSummary = 'row-normalized';
cm.ColumnSummary = 'column-normalized';
%%save Network
save simpleDL.mat trainedNet lgraph
%% Testing process
I = imread('plastic73.jpg');
ds=augmentedImageDatastore(imageSize,...
I,'ColorPreprocessing','gray2rgb');
predictedLabel = trainedNet.classify(ds);
sprintf('The loaded image belongs to %s class', predictedLabel)
Walter Roberson
Walter Roberson 2023년 5월 14일
If I understand correctly, to do transfer learning, you have to use the trained network, not the untrained network. The untrained network has not learned anything yet, so there is nothing to transfer.
Tan
Tan 2023년 5월 14일
So what should I need to do correction for my coding? Can provide suggestion?
net =alexnet('Weights','none');
That asks for an untrained network. You need to work with a trained network.
mukthi
mukthi 2024년 5월 2일
Even though showing error as unrecognised function or variable 'alexnet'
Walter Roberson
Walter Roberson 2024년 5월 2일
"This function requires Deep Learning Toolbox™ Model for AlexNet Network support package. If this support package is not installed, the function provides a download link. Alternatively, see Deep Learning Toolbox Model for AlexNet Network."

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

Tan
2023년 5월 14일

댓글:

2024년 5월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by