How do I make this code exceed 80 percent?

11 visualizzazioni (ultimi 30 giorni)
Youssef
Youssef il 13 Feb 2023
Commentato: Youssef il 13 Feb 2023
%Classify PQD Signals Using Continuous Wavelet Transform and AlexNet
clc;
%clear;
close all;
% This signal is a matrix of size N x 45000. So it means that it carries N Power Quality Des. signals of size 45000 samples each.
% PQD = load('/home/draadel/Desktop/physionet_ECG_data-main/PQD_DATA/Noise_Data.mat'); %load ecg
% data=PQD.DATA_noise.Data;
% labels=PQD.DATA_noise.Labels; %getting labels
%
% % PQD signal to image conversion using cwt
% SAG = data(1:50,:);
% SWELL = data(51:100,:);
% HARMONICS = data(101:150,:);
% signallength = 450;
% Fs = 2500 ;
% %Defining filters for CWT with 48 filtering per octave
%
% fb = cwtfilterbank('SignalLength',signallength,'SamplingFrequency',Fs, ...
% 'VoicesPerOctave',48); %cwtfilterbank
%
% % ecgtype is a cell array that carries the names of the signal by string.
% PQDtype = {'Sag', 'Swell', 'Harmonics'};
%
% %Function to convert ECG to image
% PDQ2cwtscg_cnn(SAG, fb, PQDtype{1});
% PDQ2cwtscg_cnn(SWELL, fb, PQDtype{2});
% PDQ2cwtscg_cnn(HARMONICS, fb, PQDtype{3});
%
% % Training and validation using AlexNet
% DatasetPath = '/MATLAB Drive/CNN';
%
% % reading images from the image database folder
% images = imageDatastore(DatasetPath, "IncludeSubfolders", true, "LabelSource", "foldernames");
%
% % Distributing images in the set of training and testing
% numTrainFiles = 875; % 70% of the data
% [TrainImages, TestImages] = splitEachLabel(images, numTrainFiles, "randomized");
% numTrainFiles_for_training = 700 ; % *0 % of the training data
% [TrainImages_for_training, TrainImages_for_Validation] = splitEachLabel(TrainImages, numTrainFiles_for_training, "randomized");
%% Code for Design and Training - CNN from Scratch
% Resize the images to match the network input layer.
augimdsTrain = augmentedImageDatastore([240 240 3],TrainImages_for_training);
augimdsValidation = augmentedImageDatastore([240 240 3],TrainImages_for_Validation);
%training options
opts = trainingOptions("sgdm",...
"ExecutionEnvironment","auto",...
"InitialLearnRate",0.01,...
"LearnRateSchedule",'piecewise',...
"LearnRateDropFactor",0.1,...
"LearnRateDropPeriod",10,...
"L2Regularization",0.0001,...
"MaxEpochs",20,...
"MiniBatchSize",128,...
"Shuffle","once",...
"ValidationFrequency",50,...
"Plots","training-progress",...
"ValidationData",augimdsValidation);
%create an array of layers
layers = [
imageInputLayer([240 240 3],"Name","imageinput")
convolution2dLayer([3 8],32,"Name","conv_1","Padding","same")
batchNormalizationLayer("Name","batchnorm_1")
reluLayer("Name","relu_1")
maxPooling2dLayer([2 2],"Name","maxpool_1","Padding","same","Stride",[2 2])
convolution2dLayer([3 16],32,"Name","conv_2","Padding","same")
batchNormalizationLayer("Name","batchnorm_2")
reluLayer("Name","relu_2")
maxPooling2dLayer([2 2],"Name","maxpool_2","Padding","same","Stride",[2 2])
convolution2dLayer([3 32],32,"Name","conv_3","Padding","same")
batchNormalizationLayer("Name","batchnorm_3")
reluLayer("Name","relu_3")
maxPooling2dLayer([2 2],"Name","maxpool_3","Padding","same","Stride",[2 2])
convolution2dLayer([3 64],32,"Name","conv_4","Padding","same")
batchNormalizationLayer("Name","batchnorm_4")
reluLayer("Name","relu_4")
maxPooling2dLayer([2 2],"Name","maxpool_4","Padding","same","Stride",[2 2])
convolution2dLayer([3 128],32,"Name","conv_5","Padding","same")
batchNormalizationLayer("Name","batchnorm_5")
reluLayer("Name","relu_5")
fullyConnectedLayer(3,"Name","fc","BiasLearnRateFactor",10, ...
"WeightLearnRateFactor",10)
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")];
%train cnn
[net, traininfo] = trainNetwork(augimdsTrain,layers,opts);
%% Plot Training Performance
%Classifying images
YPred = classify(net, TestImages);
YValidation = TestImages.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation);
%plotting Confusion Matrix
plotconfusion(YValidation, YPred)
Unrecognized function or variable 'TrainImages_for_training'.

Risposte (1)

Youssef
Youssef il 13 Feb 2023
function PDQ2cwtscg_cnn(ecgdata, cwtfb, ecgtype)
nos = 25; %number of signals
no1 = 450; %signal length
colormap = jet(128);
if strcmpi(ecgtype,'Sag') ==1
s1 = 'Yes';
s2 = 'yes';
folderpath = strcat('/MATLAB Drive/CNN/Sag');
findx = 0;
for i = 1:50
indx = 0;
for k = 1:nos
ecgsignal = ecgdata(i, indx+1: indx+no1);
cfs = abs(cwtfb.wt(ecgsignal));
im = ind2rgb(im2uint8(rescale(cfs)), colormap);
filenameindex = findx + k;
filename = strcat(folderpath, sprintf('%d.jpg', filenameindex));
imwrite(imresize(im, [240 240]), filename);
indx = indx + no1;
end
findx = findx + nos;
end
elseif strcmpi(ecgtype,'Swell') == 1
folderpath = strcat('/MATLAB Drive/CNN/Swell');
findx = 0;
for i = 1:50
indx = 0;
for k = 1:nos
ecgsignal = ecgdata(i, indx+1: indx+no1);
cfs = abs(cwtfb.wt(ecgsignal));
im = ind2rgb(im2uint8(rescale(cfs)), colormap);
filenameindex = findx + k;
filename = strcat(folderpath, sprintf('%d.jpg', filenameindex));
imwrite(imresize(im, [240 240]), filename);
indx = indx + no1;
end
findx = findx + nos;
end
else
folderpath = strcat('/MATLAB Drive/CNN/Harmonics');
findx = 0;
for i = 1:50
indx = 0;
for k = 1:nos
ecgsignal = ecgdata(i, indx+1: indx+no1);
cfs = abs(cwtfb.wt(ecgsignal));
im = ind2rgb(im2uint8(rescale(cfs)), colormap);
filenameindex = findx + k;
filename = strcat(folderpath, sprintf('%d.jpg', filenameindex));
imwrite(imresize(im, [240 240]), filename);
indx = indx + no1;
end
findx = findx + nos;
end
end
end

Categorie

Scopri di più su Time-Frequency Analysis in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by