Training a CNN model with Numerical Data for Binary Classification
Ältere Kommentare anzeigen
I want to train a CNN Model for Binary classification on numeric datasets extracted from level (1-4) and the approximate coefficient level of Discrete Wavelet Transform decomposition. The data have been partitioned into Training, Validation and Test sets, and stored as seperate CSV file format with corresponding labels.
Input Data reshaped as (4D double):
Train data size is : 5x30660
Test size: 5x6570
Validation size: 5x6570
While the responses(categorical) are:
XTrain label size: 5x30660,
XTest label size:5x6570,
XValidation label size: 5x6570. I used the imageInputLayer as input and Convolution1DLayer as seen in the code provided.
This is the Error message from this code:
"Error in another (line 107)
net = trainNetwork(XTrain, categorical(YTrain), layers, options);
Caused by:
Layer 'Conv_1': Input data must have one spatial dimension only, one temporal dimension only, or one of each. Instead, it
has 2 spatial dimensions and 0 temporal dimensions."
This is the code:
% Initializing empty arrays for data and labels
allTrainData = cell(1, 5);
allTrainLabels = cell(1, 5);
allValidationData = cell(1, 5);
allValidationLabels = cell(1, 5);
allTestData = cell(1, 5);
allTestLabels = cell(1, 5);
% Loading and concatenating the training datasets
trainDataFiles = ["activetrain.csv", "ambienttrain.csv", "generatedtrain.csv", "moduletrain.csv", "radiationtrain.csv"];
trainLabelFiles = ["labeltrainactive.csv", "labeltrainambient.csv", "labeltraingenerated.csv", "labeltrainmodule.csv", "labeltrainradiation.csv"];
for i = 1:5
trainData = load(trainDataFiles(i));
trainLabels = load(trainLabelFiles(i));
% Extracting the numeric arrays from the structure arrays
allTrainData{i} = trainData; % Store the numeric arrays directly
allTrainLabels{i} = trainLabels;
end
% Loading and concatenating the validation datasets
validationDataFiles = ["activevalid.csv", "ambientvalid.csv", "generatedvalid.csv", "modulevalid.csv", "radiationvalid.csv"];
validationLabelFiles = ["labelvalidactive.csv", "labelvalidambient.csv", "labelvalidgenerated.csv", "labelvalidmodule.csv", "labelvalidradiation.csv"];
for i = 1:5
validationData = load(validationDataFiles(i));
validationLabels = load(validationLabelFiles(i));
% Extracting the numeric arrays from the structure arrays
allValidationData{i} = validationData; % Store the numeric arrays directly
allValidationLabels{i} = validationLabels;
end
% Loading and concatenating the test datasets
testDataFiles = ["activetest.csv", "ambienttest.csv", "generatedtest.csv", "moduletest.csv", "radiationtest.csv"];
testLabelFiles = ["labeltestactive.csv", "labeltestambient.csv", "labeltestgenerated.csv", "labeltestmodule.csv", "labeltestradiation.csv"];
for i = 1:5
testData = load(testDataFiles(i));
testLabels = load(testLabelFiles(i));
% Extract the numeric arrays from the structure arrays
allTestData{i} = testData; % Store the numeric arrays directly
allTestLabels{i} = testLabels;
end
% Reshaping the input data to 4D tensor: [height, width, channels, samples]
inputHeight = 1;
inputWidth = 5; %length of your input data
numChannels = 5; % 5 coefficient levels
numTrainSamples = size(allTrainData{i}, 2);
numTestSamples = size(allTestData{i}, 2);
numValidationSamples = size( allValidationData{i}, 2);
XTrain = reshape( allTrainData{i}, inputHeight, inputWidth, numChannels, numTrainSamples);
XTest = reshape( allTestData{i}, inputHeight, inputWidth, numChannels, numTestSamples);
XValidation = reshape( allValidationData{i}, inputHeight, inputWidth, numChannels, numValidationSamples);
% Normalizing the input data
XTrain = normalize(XTrain);
XTest = normalize(XTest);
XValidation = normalize(XValidation);
% Converting the labels to categorical format
YTrain = categorical(cell2mat(allTrainLabels));
YTest = categorical(cell2mat(allTestLabels));
YValidation = categorical(cell2mat(allValidationLabels));
% Defining the CNN architecture
layers = [
imageInputLayer([1 5 5],"Name","Input","Normalization","zscore")
convolution1dLayer(3,8,"Name","Conv_1","Padding","same")
batchNormalizationLayer("Name","Bnorm")
reluLayer("Name","relu_1")
maxPooling1dLayer(2, "Padding", "same", "Stride", 2)
convolution1dLayer(3,16,"Name","Conv_2","Padding","same")
batchNormalizationLayer("Name","Bnorm_2")
reluLayer("Name","relu_2")
maxPooling1dLayer(2,"Padding","same","Stride",2)
convolution1dLayer(3,32,"Name","Conv_3","Padding","same")
batchNormalizationLayer("Name","Bnorn_3")
reluLayer("Name","relu_3")
maxPooling1dLayer(2,"Padding","same","Stride",2)
convolution2dLayer(3,64,"Name","Conv_4","Padding","same")
batchNormalizationLayer("Name","BNorm_4")
reluLayer("Name","relu_4")
fullyConnectedLayer(8,"Name","FC_1","WeightLearnRateFactor",0.01)
reluLayer("Name","relu_5")
fullyConnectedLayer(2,"Name","FC_2","WeightLearnRateFactor",0.01)
softmaxLayer("Name","Softmax_layer")
classificationLayer("Classes","auto")];
plot(layerGraph(layers));
% Setting the training options
options = trainingOptions('adam', ...
'InitialLearnRate', 0.01, ...
'MaxEpochs', 5, ...
'MiniBatchSize', 32, ...
'ValidationData', {XValidation, YValidation}, ...
'ValidationFrequency', 10, ...
'Verbose', true, ...
'Plots', 'training-progress');
% Training the CNN model
net = trainNetwork(XTrain, categorical(YTrain), layers, options);
% Perform anomaly detection on the test dataset
YTestPred = classify(net, XTest);
% Evaluating the performance
accuracy = sum(YTestPred == YTest) / numel(YTest);
disp(['Test Accuracy: ', num2str(accuracy)]);
Kindly share your thought. Thanks
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Predictive Maintenance Toolbox finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
