How to run this CNN?

42 views (last 30 days)
NGR MNFD
NGR MNFD on 11 Dec 2024
Edited: Pratyush Swain on 2 Jan 2025 at 13:03
Hello. Good time. According to the Matlab code I wrote, I want to consider the position, velocity, and acceleration features together and feed them to the convolutional network. I get an error. The first column of my data is time and the rest of the columns are features. The data is related to the skeletal position of the 25 joints of the body. I uploaded the workspace and Matlab code. Please help. Error: Invalid training data. Predictors must be a numeric array, a datastore, or a table. For networks with sequence input, predictors can also be a cell array of sequences.
  1 Comment
Pratyush Swain
Pratyush Swain on 2 Jan 2025 at 8:29
Hi NGR,
I am unable to extract the workspace folder zip, it shows error stating the zip file is empty. Can you upload it again or upload any other relevant data files required to run your script ?
Thanks

Sign in to comment.

Answers (1)

Pratyush Swain
Pratyush Swain on 2 Jan 2025 at 13:00
Edited: Pratyush Swain on 2 Jan 2025 at 13:03
Hi NGR,
In your provided script, the dimensions were managed by resizing entire data arrays to match a target row size 'desiredRows'. This approach maintained the data structure class wise which might have led to complications while splitting data for training/testing and reshaping for input into the network.
Alternatively you can try this approach where the data is padded to have a fixed set of rows for each class - 'maxRows' and each row of the padded data is treated as a separate sample, so the final structure of 'XTrainArray' has dimensions [maxRows, desiredCols, channels, total_samples] where:
  • 'maxRows' is the padded row size.
  • 'desiredCols' is 72, the fixed number of features (columns).
  • 'channels' is 3 (position, velocity, acceleration).
  • 'total_samples' is the number of samples from all classes.
Please refer to this example implementation:
% Calculate maxRows (max number of rows in the data under each class)
maxRows = max(cellfun(@(x) size(x, 1), positionData_resized));
% Initialize XTrain and YTrain
XTrain = {};
YTrain = [];
% Prepare data and labels
for i = 1:numCells
% Concatenate position, velocity, acceleration
data = cat(3, positionData_resized{i}, velocityData_resized{i}, accelerationData_resized{i});
% Pad data with zeros to match maxRows
paddedData = padarray(data, [maxRows - size(data, 1), 0, 0], 0, 'post');
% Append each row (slice) of paddedData as a separate sample to XTrain
for j = 1:maxRows
XTrain{end+1} = paddedData(j, :, :); % Each cell contains [1, 72, 3]
YTrain = [YTrain; i]; % Label for each sample
end
end
% Convert XTrain to a 4D array
XTrainArray = cat(4, XTrain{:});
% Convert YTrain to categorical
YTrain = categorical(YTrain);
% Adjusted input size
inputSize = [1, 72, 3];
layers = [
imageInputLayer(inputSize, 'Normalization', 'none', 'Name', 'Input')
convolution2dLayer([3 3], 32, 'Padding', 'same', 'Name', 'Conv1')
batchNormalizationLayer('Name', 'BatchNorm1')
reluLayer('Name', 'ReLU1')
fullyConnectedLayer(numCells, 'Name', 'FC')
softmaxLayer('Name', 'Softmax')
classificationLayer('Name', 'Output')];
% Training options
options = trainingOptions('sgdm', ...
'MaxEpochs', 50, ...
'InitialLearnRate', 0.01, ...
'Verbose', false, ...
'Plots', 'training-progress');
% Train the network
net = trainNetwork(XTrainArray, YTrain, layers, options);
You can prepare the test data similar to the training data in the above implementation.
For more information on 'trainNetwork' function, please refer https://www.mathworks.com/help/deeplearning/ref/trainnetwork.html
Hope this helps.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!