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?
Walter Roberson
Walter Roberson 2023 年 5 月 14 日
net = alexnet('Weights','none');
lys = net;
lys(end-3:end)
and then whatever it was you were going to do with lys
Tan
Tan 2023 年 5 月 14 日
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?
Walter Roberson
Walter Roberson 2023 年 5 月 14 日
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 ExchangeDeep 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