Clear Filters
Clear Filters

i just build a rnn network layer and i got this error

5 views (last 30 days)
clc; clear; close all;
image_folder = 'extraksi';
filenames = fullfile(image_folder);
imds = imageDatastore(filenames, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
numTrainFiles = 18;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
lgraph = layerGraph();
tempLayers = [
sequenceInputLayer([150 50 1],"Name","sequence")
sequenceFoldingLayer("Name","seqfold")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],8,"Name","conv","Padding","same","Stride",[2 2])
batchNormalizationLayer("Name","batchnorm")
reluLayer("Name","relu")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
sequenceUnfoldingLayer("Name","sequnfold")
flattenLayer("Name","flatten")
lstmLayer(128,"Name","lstm")
fullyConnectedLayer(123,"Name","fc")
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")];
lgraph = addLayers(lgraph,tempLayers);
lgraph = connectLayers(lgraph,"seqfold/out","conv");
lgraph = connectLayers(lgraph,"seqfold/miniBatchSize","sequnfold/miniBatchSize");
lgraph = connectLayers(lgraph,"relu","sequnfold/in");
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',10, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',1, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,lgraph,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
save netcnn.mat net;
accuracy = sum(YPred == YValidation)/numel(YValidation)
Error using trainNetwork (line 165)
Unexpected data format: The datastore read method must return a cell array or table.
Error in cnn (line 46)
net = trainNetwork(imdsTrain,lgraph,options);

Answers (1)

Jayanti
Jayanti on 22 Aug 2024
I can see you are performing image classification task here. This issue is occurring because "sequenceInputLayer" function expects a different data format than what the "imageDatastore" delivered. The code is intended to process sequence data using "sequenceInputLayer" which expects sequential data as input.
You can refer below link for more details about "sequenceInputLayer":
However, since the task involves image data we can directly use "imageInputLayer" which is designed to handle images as input.
Refer below link for more details about "imageInputLayer" :
So, you can replace "sequenceInputLayer" with "imageInputLayer". Also, since it is working on the images, we do not need sequence folding/unfolding layers. These layers are used for processing sequences, not images. I am also attaching the changes which I made in the code. I have tested it on the publicly available dataset “Pistachio_Image_Dataset” and it is working perfectly fine.
Also, you can refer to the dataset I have used from the below link:
inputSize = [600 600 3]; % Adjust based on your image size
numClasses = numel(categories(imds.Labels));
lgraph = layerGraph();
tempLayers = [
%Replace "sequenceInputLayer" with "imageInputLayer"
imageInputLayer(inputSize, "Name", "input")
convolution2dLayer([3 3], 8, "Name", "conv", "Padding", "same", "Stride", [2 2])
batchNormalizationLayer("Name", "batchnorm")
reluLayer("Name", "relu")
flattenLayer("Name", "flatten")
lstmLayer(128, "Name", "lstm")
fullyConnectedLayer(numClasses, "Name", "fc")
softmaxLayer("Name", "softmax")
classificationLayer("Name", "classoutput")];
lgraph = addLayers(lgraph, tempLayers);
Let me know if you have any further query.

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!