hi i want to build a rnn network for train and validation image classification(finger vein) then i got this error "Unexpected data format: The datastore read method must return a cell array or table."

6 visualizaciones (últimos 30 días)
clc; clear; close all;
image_folder = 'extraksii';
filenames = fullfile(image_folder);
imds = imageDatastore(filenames, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
numTrainFiles = 18;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
sequenceInputLayer([150 50 1])
flattenLayer('Name','flatten')
lstmLayer(125,'OutputMode','sequence')
dropoutLayer(0.2)
flattenLayer('Name','flatten1')
lstmLayer(100,'OutputMode','last')
dropoutLayer(0.2)
fullyConnectedLayer(123)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',10, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',1, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
save net.mat net;
accuracy = sum(YPred == YValidation)/numel(YValidation)

Respuestas (1)

Jayanti
Jayanti el 2 de Jul. de 2025
Hi Andito,
This error occurs because RNNs expects input data in the form of sequences typically a cell array where each cell contains a 2D matrix representing input. However, "imageDatastore" returns images in a format suitable for convolutional networks , not RNNs.
To train the RNN for image data preprocess your images into sequence and store them in cell array.
Please refer to the below code for more details.
It converts images into sequences for RNN training by resizing each image to 150×50, converting it to grayscale, normalizing it, and reshaping it so each row becomes a time step with 50 features. The sequences and their labels are stored in cell arrays and then randomly split into training and validation sets using "dividerand" .
data = readall(imds);
numImages = numel(data);
X = cell(numImages, 1);
Y = imds.Labels;
for i = 1:numImages
img = imresize(data{i}, inputSize);
img = im2double(rgb2gray(img));
X{i} = reshape(img, [150,50]);
end
% Split into Training and Validation
numTrain = 18;
[trainIdx, valIdx] = dividerand(numImages, numTrain/numImages, 1 - numTrain/numImages);
XTrain = X(trainIdx);
YTrain = Y(trainIdx);
XVal = X(valIdx);
YVal = Y(valIdx);
I am also attaching the Mathworks offical documentation on "dividerand" for your reference:

Categorías

Más información sobre Deep Learning Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by