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'.

Answers (1)

Walter Roberson
Walter Roberson on 14 May 2023
Edited: Walter Roberson on 14 May 2023
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 Comments

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?
I do not know whether it is correct. For one thing, you have not described to us what you are trying to do.
trying to perform image classification based on transfer learning using alexnet.
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)
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.
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.
Even though showing error as unrecognised function or variable 'alexnet'
"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."

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2020b

Asked:

Tan
on 14 May 2023

Commented:

on 2 May 2024

Community Treasure Hunt

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

Start Hunting!