Trying to classify images with a CNN but getting errors
Show older comments
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?
Accepted Answer
More Answers (0)
Categories
Find more on Deep Learning Toolbox 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!