Main Content

Export Labeled Data from Signal Labeler for Deep Learning Classification

This example shows how to label and export data using the Signal Labeler app, and then train and classify the labeled data with a deep learning network. The data set consists of 750 signals with 2000 samples each. The signals correspond to realizations of random noise processes belonging to three distributions: white, brown, and pink noise. The brown noise has two additional sinusoidal components at 0.19π radians/sample and 0.33π radians/sample frequencies and the pink noise has two additional sinusoidal components at 0.17π radians/sample and 0.31π radians/sample frequencies. The data set contains 250 signals for each class.

Label Data Set Using Signal Labeler App

Download and Prepare the Data

Download the data files into your temporary directory, whose location is specified by MATLAB®'s tempdir command.

dataURL = "https://ssd.mathworks.com/supportfiles/SPT/data/NoiseSignalsDataSet.zip";
datasetFolder = fullfile(tempdir,"NoiseSignalsDataSet");
zipFile = fullfile(tempdir,"NoiseSignalsDataSet.zip");
if ~exist(datasetFolder,"dir")
     websave(zipFile,dataURL);
     unzip(zipFile,datasetFolder);
end

Import Signals from Files

Open the Signal Labeler app by typing the signalLabeler command. To import signals from multiple files in a folder, on the Labeler tab, click Import and select From Folders in the Members list. In the dialog box, browse to select the NoiseSignalsDataSet folder and click Import.

The imported files appear in the Labeled Signal Set Members browser.

Create Label Definitions

To label data in the Signal Labeler app, first create a label definition. Since each member has one label, create an attribute label definition with categorical data type. To add a signal label definition to your labeled signal set, click Add Definition on the Labeler tab and select Add label definition. In the dialog box, specify these:

  • Label Name — Specify the name in the text box as NoiseType.

  • Label Type — Select Attribute.

  • Data Type — Select categorical.

  • Categories — Enter each category on a new line: white, brown, pink.

Manual Labeling

Plot one signal of each noise type by selecting the check boxes next to the file names in the Labeled Signal Set Members browser. Notice that it is hard to distinguish the signal types by looking at the time-domain plots. However, the distinctive features of each signal are evident by looking in the frequency-domain spectrum plots. To plot the spectral estimates of the signals, on the Display tab, click Spectrum to open a spectrum view.

The spectrum shows sinusoidal components at 0.19π radians/sample and 0.33π radians/sample frequency for a brown noise signal.

The spectrum shows sinusoidal components at 0.17π radians/sample and 0.31π radians/sample frequency for a pink noise signal.

The spectrum shows no sinusoidal component for a white noise signal.

To label interactively, plot a signal and specify the label value, based on spectrum of the signal. In the Set Value section of the Labeler tab. Click the Label Attribute to apply the label value. Do this for the remaining signals in the set.

Export Labeled Data

After manually labeling all the noise signals, export the resulting labeled data set to the MATLAB Workspace. Click Export on the Labeler tab and select To Workspace under Labeled Signal Set. In the dialog box, specify the name lssAll and an optional brief description. The lssAll variable contains a labeledSignalSet object that groups together all the signals, label definitions, and label values that you created in the app.

For convenience, the resulting exported labeledSignalSet object is attached with this example so that you do not have to label all the signals manually and can move on to learning how to use the object to train an AI model.

Load the labeled signal set.

load("lssAll.mat")

Set the AlternateFileSystemRoots to the folder location where the files were downloaded.

if lssAll.Source.Folders ~= datasetFolder
    lssAll.setAlternateFileSystemRoots([lssAll.Source.Folders,datasetFolder]);
end

Training a Deep Learning Network

To train a deep learning classifier model, create a signal datastore and a label datastore from the exported labeled signal set. Combine the two datastores to train the network with pairs of signals and response labels.

Create a combined datastore from the exported labeled signal set using NoiseType labels.

[sdsAll,ldsAll] = lssAll.createDatastores("NoiseType");
cdsAll = combine(sdsAll,ldsAll);

Split the data into two sets: A training set containing 80% of the signals and a testing set with the rest. Use the splitlabels function to ensure that the training and test partitions have the same proportion of labels from each noise type. For the test set the combined datastore is not used because signals and labels need to be used separately when testing the network's classification performance.

% specify UnderlyingDatastoreIndex as 2 so that splitlabels operates on the
% datastore that contains the labels
splitIndices = splitlabels(cdsAll,0.8,"randomized","UnderlyingDatastoreIndex",2);
% Get the training set
cdsTrain = subset(cdsAll,splitIndices{1});
% Get the test set
sdsTest = subset(sdsAll,splitIndices{2});
ldsTest = subset(ldsAll,splitIndices{2});

Define a convolution neural network to classify the signal into the three noise classes. Specify the minimum sequence length in the SequenceInputLayer object as the signal length.

numClasses = 3;
layers = [
    sequenceInputLayer(1,"MinLength",2e3)
    convolution1dLayer(5,1,"Padding","same")
    stftLayer("Window",hamming(256),"OverlapLength",192)
    convolution2dLayer(5,20,"Name","","Padding","same")
    reluLayer("Name", "relu1")
    convolution2dLayer(3,20,"Padding",1,"Name","")
    reluLayer("Name","relu2")
    convolution2dLayer(3,20,"Padding",1,"Name","conv3")
    reluLayer("Name","relu3")
    globalAveragePooling2dLayer()
    fullyConnectedLayer(numClasses,"Name","fc")
    softmaxLayer("Name","softmax")];

Define training options. An Adam optimizer with 10 epochs and mini batch size of 32 is chosen.

options = trainingOptions("adam", ...
    InitialLearnRate = 1e-2, ...
    MaxEpochs=10, ...
    MiniBatchSize=32, ...
    Shuffle="every-epoch", ...
    Plots="none", ...
    ExecutionEnvironment="auto", ...
    Verbose=true);

Train the neural network with the combined datastore, layers, and training options. Specify a cross-entropy loss function.

trainedNet = trainnet(cdsTrain,layers,crossentropy=options);
    Iteration    Epoch    TimeElapsed    LearnRate    TrainingLoss
    _________    _____    ___________    _________    ____________
            1        1       00:00:17         0.01          1.7303
           50        3       00:01:09         0.01         0.63194
          100        6       00:02:04         0.01         0.45514
          150        9       00:02:52         0.01          0.4473
          180       10       00:03:26         0.01         0.49408
Training stopped: Max epochs completed

Measure the performance of the trained network using the test data set. Use the minibatchpredict function to predict the labels for all signals in the test datastore sdsTest. Use the ground truth test labels in the ldsTest datastore to measure the classification performance.

probmatrix = minibatchpredict(trainedNet,sdsTest);
testlabelsCell = ldsTest.readall;
testLabelsArr = [testlabelsCell{:}];
ypred = scores2label(probmatrix,categories(testLabelsArr));
figure
cm = confusionchart(testLabelsArr,ypred, ...
    ColumnSummary="column-normalized",RowSummary="row-normalized");

Figure contains an object of type ConfusionMatrixChart.

The network is able to classify each noise type with 100% accuracy.

Conclusion

This example showed how to label data in Signal Labeler app and use the exported data to train and test a deep learning classifier.

See Also

Functions

Objects

Apps

Related Topics