Matlab Deep learning using CNN for Classification Parkinson disease Patients from their voices

6 vues (au cours des 30 derniers jours)
I wrote a script in Matlab 2021b for Deep learning using CNN for Classification Parkinson disease Patients from their voices data which located in three folders contains (mild, moderate, and severe) respectively of Parkinson patients' audio files of .wav format, all audio files in these folders are of sample rate of 22050 khz and 2.2 seconds length. The code below after Run giving error as folllows:
File: imageInputLayer.m Line: 1 Column: 34
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To
construct matrices, use brackets instead of parentheses.
Error in mm1 (line 39)
imageInputLayer([48510, 1, 1], 'Normalization', 'zscore', 'Name', 'input')
Below the code of classification Parkinson patients from their voices ensuring normalizing audio data and ensuring audioData matrix and labels are appropriately shaped for the CNN model.
% Set the paths to your data folders
dataFolder = fullfile(tempdir, "DataSet");
% Load the dataset
mildFolder = fullfile(dataFolder, 'MDN');
moderateFolder = fullfile(dataFolder, 'MEN');
severeFolder = fullfile(dataFolder, 'SEN');
% Load data from folders
mildData = loadAudioData(mildFolder);
moderateData = loadAudioData(moderateFolder);
severeData = loadAudioData(severeFolder);
% Concatenate data and labels
audioData = cat(4, mildData, moderateData, severeData);
labels = categorical([repmat({'mild'}, 1, numel(mildData)), ...
repmat({'moderate'}, 1, numel(moderateData)), ...
repmat({'severe'}, 1, numel(severeData))]);
% Shuffle the data
idx = randperm(size(audioData, 4));
audioData = audioData(:, :, :, idx);
labels = labels(idx);
% Split data into training and testing sets
splitRatio = 0.8;
splitIdx = round(splitRatio * numel(labels));
% Assuming your audio data is a sequence of length 48510 with one channel
trainData = reshape(audioData(:, :, :, 1:splitIdx), [48510, 1, 1, splitIdx]);
trainLabels = labels(1:splitIdx);
testData = reshape(audioData(:, :, :, splitIdx+1:end), [48510, 1, 1, numel(labels)-splitIdx]);
testLabels = labels(splitIdx+1:end);
% Define the CNN model
layers = [
imageInputLayer([48510, 1, 1], 'Normalization', 'zscore', 'Name', 'input')
convolution2dLayer(3, 32, 'Padding', 'same')
reluLayer()
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 64, 'Padding', 'same')
reluLayer()
maxPooling2dLayer(2, 'Stride', 2)
fullyConnectedLayer(64)
reluLayer()
fullyConnectedLayer(3)
softmaxLayer()
classificationLayer()
];
% Specify training options
options = trainingOptions('adam', ...
'MaxEpochs', 10, ...
'MiniBatchSize', 10, ...
'ValidationData', {testData, testLabels}, ...
'Plots', 'training-progress');
disp('Size of training data just before training:');
disp(size(trainData));
% Train the network
net = trainNetwork(trainData, trainLabels, layers, options);
% Function to load audio data from a folder
function audioData = loadAudioData(folderPath)
audioFiles = dir(fullfile(folderPath, '*.wav'));
numFiles = numel(audioFiles);
% Initialize audioData as a cell array
audioData = cell(1, numFiles);
for i = 1:numFiles
filePath = fullfile(folderPath, audioFiles(i).name);
disp(['Loading audio file: ', filePath]);
[audio, ~] = audioread(filePath);
% Normalize audio data
audio = audio / max(abs(audio));
% Store audio data in a cell array
audioData{i} = audio;
end
% Find the maximum number of samples among all files
maxSamples = max(cellfun(@numel, audioData));
% Preallocate audioData as a cell array of 2D matrices
audioDataPadded = cell(1, numFiles);
% Populate audioDataPadded with padded/truncated audio data
for i = 1:numFiles
currentAudio = audioData{i};
% Pad or truncate to match the maximum number of samples
currentAudioPadded = padarray(currentAudio, [maxSamples - numel(currentAudio), 0], 0, 'post');
% Assign the padded/truncated audio to audioDataPadded
audioDataPadded{i} = currentAudioPadded;
end
% Convert audioDataPadded to a 4D array
audioDataArray = cat(4, audioDataPadded{:});
% Convert audioDataArray to single precision (if needed)
audioData = single(audioDataArray);
end

Réponses (1)

Drishti
Drishti le 4 Sep 2024
Hi Ahmad,
On reproducing the error in MATLAB R2021b with sample data, I found that the error is related to dimension discrepancy in the convolutional layers of the deep learning network.
The convolution2dLayer(3, ...) and maxPooling2dLayer(2, ...) implies a square filter and pooling window of size [3, 3] and [2, 2], respectively which creates a mismatch with imageInputLayer([48510, 1, 1], …).
This can be resolved by modifying the convolutional layers dimension as per the imageInputLayer.
For better understanding, you can refer to the provided implementation:
layers = [
imageInputLayer([48510, 1, 1], 'Normalization', 'zscore', 'Name', 'input')
convolution2dLayer([3, 1], 32, 'Padding', 'same', 'Name', 'conv1')
reluLayer('Name', 'relu1')
maxPooling2dLayer([2, 1], 'Stride', [2, 1], 'Name', 'pool1')
convolution2dLayer([3, 1], 64, 'Padding', 'same', 'Name', 'conv2')
reluLayer('Name', 'relu2')
maxPooling2dLayer([2, 1], 'Stride', [2, 1], 'Name', 'pool2')
fullyConnectedLayer(64, 'Name', 'fc1')
reluLayer('Name', 'relu3')
fullyConnectedLayer(3, 'Name', 'fc2')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'output')
];
I hope this solves the error.

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by