How to convert 3-axis data to 2DCNN input like image data?

2 views (last 30 days)
I have 1921×3 3-axis data side by side and want to assign them to a Imageinputlayer(2DCNN).
However, I get the following error.
The data to be entered is a cell (1500 x 1) with each element containing 1921 x 3 data.
I get the following error.
Error using: trainNetwork (line 191)
Invalid training data. Predictor must be a numeric array, datastore, or table. For networks with sequence input, the predictor can also be a sequence cell array.
Please teach me Solution.
Xdata = FFT_cell;
%Tdata is categorical data.
Tdata
% Split
p = 0.25;
c = cvpartition(Tdata,'Holdout',p)
%%
Train_index = training(c);
Test_index = test(c);
XTrain = Xdata(Train_index);
TTrain = Tdata(Train_index);
XValidation = Xdata(Test_index);
TValidation = Tdata(Test_index);
%%
% numFeatures = size(XTrain{1},1);
classes = categories(TTrain);
numClasses = numel(classes)
%%
% Define CNN
filterSize = 3;
numFilters = 32;
layers = [
imageInputLayer([1921 3 1],"Name","imageinput")
convolution2dLayer([3 3],32,"Name","conv","Padding","same")
reluLayer("Name","relu")
convolution2dLayer([3 3],32,"Name","conv_1","Padding","same")
reluLayer("Name","relu_1")
convolution2dLayer([3 3],32,"Name","conv_2","Padding","same")
reluLayer("Name","relu_2")
globalAveragePooling2dLayer("Name","gapool")
fullyConnectedLayer(5,"Name","fc")
softmaxLayer("Name","softmax")
classificationLayer("Name","classification")];
miniBatchSize = 2;
options = trainingOptions("sgdm", ...
MiniBatchSize=miniBatchSize, ...
MaxEpochs=40, ...
SequencePaddingDirection="left", ...
ValidationData={XValidation,TValidation}, ...
Plots="training-progress", ...
Verbose=0);
net = trainNetwork(XTrain,TTrain,layers,options);
YPred = classify(net,XValidation, ...
MiniBatchSize=miniBatchSize, ...
SequencePaddingDirection="left");
acc = mean(YPred == TValidation)
confusionchart(TValidation,YPred)

Answers (1)

Venu
Venu on 14 Apr 2024
Given that each cell of your Xdata contains a 1921x3 numeric array, and you're attempting to feed this into a 2D CNN with an imageInputLayer expecting input of size [1921 3 1], you need to convert your cell array into a 4D numeric array where each "image" is of size 1921x3 and the fourth dimension indexes each example in your dataset.
Here's how you can convert your cell array Xdata into the required 4D array format:
% Initialize a 4D array with zeros. Assuming the depth is 1 for grayscale images.
X_4D = zeros(1921, 3, 1, numel(Xdata));
for i = 1:numel(Xdata)
X_4D(:, :, 1, i) = Xdata{i};
end
Make sure your training and validation splits (XTrain, XValidation) are also converted to the correct format
XTrain_4D = X_4D(:, :, :, Train_index);
TTrain = Tdata(Train_index);
XValidation_4D = X_4D(:, :, :, Test_index);
TValidation = Tdata(Test_index);
The "SequencePaddingDirection" option in "trainingOptions" and "classify" is not necessary unless you are dealing with sequence data, which does not seem to be the case here.
options = trainingOptions("sgdm", ...
MiniBatchSize=miniBatchSize, ...
MaxEpochs=40, ...
ValidationData={XValidation_4D, TValidation}, ...
Plots="training-progress", ...
Verbose=0);
net = trainNetwork(XTrain_4D, TTrain, layers, options);
Hope it works after these modifications!
  1 Comment
颯一郎 中坂
颯一郎 中坂 on 15 Apr 2024
Hi, Venu!
I can successfully activate trainnetwork.
I learned something new.
Thank you for helping me!

Sign in to comment.

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!