Trying to classify images with a CNN but getting errors
4 views (last 30 days)
Show older comments
Teshan Rezel
on 19 Feb 2020
Answered: Kaashyap Pappu
on 20 Feb 2020
Hi all,
Apologies in advance, I'm new to Matlab. I'm trying to pass some images to a CNN for classification but am stuck in resolving a particular error. The error is as follows:
Error using activations
Expected layer to be one of these types:
numeric
Instead its type was nnet.cnn.layer.Layer.
Error in nnet.internal.cnn.util.validateNetworkLayerNameOrIndex (line 26)
validateattributes(layerNameOrIndex, {'numeric'},...
Error in DAGNetwork/activationsSeries (line 263)
layerID = nnet.internal.cnn.util.validateNetworkLayerNameOrIndex(layerID, this.Layers, 'activations');
Error in SeriesNetwork/activations (line 779)
Y = this.UnderlyingDAGNetwork.activationsSeries(X, layerID, varargin{:});
My code is as follows:
AnisotropyDatasetPath = fullfile(matlabroot,'Training', 'Anisotropy');
IsotropyDatasetPath = fullfile(matlabroot,'Training', 'Isotropy');
FillerDatasetPath = fullfile(matlabroot,'Training', 'Filler');
TrainingDatasetPath = fullfile(matlabroot,'Training');
cropDatasetPath = fullfile('C:\Users\ezxtg4\Downloads\JPEG pics', 'crops');
imds = imageDatastore(TrainingDatasetPath, 'IncludeSubfolders',true,...
'LabelSource','foldernames');
labelCount = countEachLabel(imds)
numTrainFiles = 999;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
imageInputLayer([227 227 3])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(3)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation)
testImage = imread('C:\Users\ezxtg4\Downloads\JPEG pics\crops\crop 1.jpeg');
testLabel = imdsValidation.Labels(1)
ds = augmentedImageDatastore([227 227 3], testImage, 'ColorPreprocessing', 'gray2rgb');
imageFeatures = activations(net, ds, layers, 'OutputAs', 'columns');
predictedLabel = predict(classifier, imageFeatures, 'ObservationsIn', 'columns')
Any ideas on how to resolve this please?
0 Comments
Accepted Answer
Kaashyap Pappu
on 20 Feb 2020
The variable ‘net’ already has the information regarding the layers. The function’s third input is expected to a numeric index or a character vector as has been mentioned here. For example, if you want the activation of the fourth layer, the input value should be 4. Alternatively, each layer has a name property and this property value, which is a character vector, can also be passed as an input parameter.
Hope this helps!
0 Comments
More Answers (0)
See Also
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!