Why does my code for waveform segmentation yield no result?
Show older comments
Hello, i'm trying to use the code shown in the example "Waveform Segmentation using Deep Learning" available at the link:
My objective is using this code for training a lstm network to classify phases of contact and rupture during needle insertions.
I have a set of 35 .mat files that contain raw force signals (time information given in samples) and tables referring to the contact/rupture ROIs.
The signals are all 7000 samples long and look like this:

And this is the code i use for the training
%%
clear
datasetFolder=fullfile('C:\Users\User\Desktop\Project\ALLTrainingData','RAW');
sds = signalDatastore(datasetFolder,'SignalVariableNames',["force","Events"]);
data=preview(sds)
head(data{2})
%% File preparation
rng default
[trainIdx,~,testIdx]=dividerand(numel(sds.Files),0.7,0,0.3);
trainDs=subset(sds,trainIdx);
testDs=subset(sds,testIdx);
trainDs=transform(trainDs,@getmask2);
testDs=transform(testDs,@getmask2);
%transformedData=preview(trainDs)
trainDs = transform(trainDs,@resizeData2);
testDs = transform(testDs,@resizeData2);
%plot(transformedData{2}(1:15000)) %test
%% Net options
layers = [ ...
sequenceInputLayer(1)
lstmLayer(200,'OutputMode','sequence')
fullyConnectedLayer(3) %(Displacement,Rupture,n/a)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',30, ...
'MiniBatchSize',64, ...
'InitialLearnRate',0.001, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',3, ...
'GradientThreshold',1, ...
'Plots','training-progress',...
'shuffle','every-epoch',...
'Verbose',0,...
'DispatchInBackground',true);
%% tall arrays
tallTrainSet=tall(trainDs);
tallTestSet=tall(testDs);
trainData=gather(tallTrainSet);
testData=gather(tallTestSet);
%% TRAINING
rawNet=trainNetwork(trainData(:,1),trainData(:,2),layers,options);
%% TESTING
predTest = classify(rawNet,testData(:,1),'MiniBatchSize',512);
confusionchart([predTest{:}],[testData{:,2}],'Normalization','column-normalized');
Mtest = signalMask(testData{2,2});
Mtest.SpecifySelectedCategories = true;
Mtest.SelectedCategories = find(Mtest.Categories ~= "n/a");
figure
subplot(2,1,1)
plotsigroi(Mtest,testData{2,1})
title('Ground Truth')
Mpred = signalMask(predTest{2});
Mpred.SpecifySelectedCategories = true;
Mpred.SelectedCategories = find(Mpred.Categories ~= "n/a");
subplot(2,1,2)
plotsigroi(Mpred,testData{2,1})
title('Predicted')
Referring to the example, I had to change getmask in order to accept 2 classes (Contact, Rupture) and i set the "targetlength" in ResizeData to 7000: each signal contains only one pattern, so I thought that breaking them in smaller signals would not be ideal.
The code runs without errors, though i get some unexpected results.
Here are pictures of the training progress, confusion chart, and an attempt of classification:



As you can see, the net doesn't seem to classify anything, and the training progress looks odd, converging at high accuracy and fairly high loss in the first 2-3 epochs.
I tried fine tuning all the options parameters, with particular attention to the learning rate, number of epochs, and minibatchsize but it doesn't seem to make a difference. I also tried classifying the same data i used for training but i get the same result.
What could be the cause of the issue? The code is fairly similar to the tutorial's, so I expected worse results but not this bad.
Could it be that since the signals are far shorter than the ones used in the example, and also contain only one pattern, an lstm net isn't the correct choice?
Or maybe using a datastore for such a small number of small signals could cause issues?
Thank you all for your attention.
1 Comment
Saif Punjwani
on 25 May 2022
Hello, where did you get your resizeData function from?
Answers (0)
Categories
Find more on AI for Signals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!