Main Content

使用石灰研究频谱图分类

此示例显示了如何使用局部解释的模型不足解释(LIME)来研究经过对频谱图进行分类的深卷积神经网络的鲁棒性。石灰是一种可视化观察结果哪些部分有助于网络的分类决策的技术。此示例使用成像时function to understand which features in the spectrogram data are most important for classification.

在此示例中,您创建并训练神经网络以对四种模拟时间序列数据进行分类:

  • 单个频率的正弦波

  • 三个正弦波的叠加

  • Broad Gaussian peaks in the time series

  • 时间序列中的高斯脉冲

To make this problem more realistic, the time series include added confounding signals: a constant low-frequency background sinusoid and a large amount of high-frequency noise. Noisy time series data is a challenging sequence classification problem. You can approach the problem by first converting the time series data into a time-frequency spectrogram to reveal the underlying features in the time series data. You can then input the spectrograms to an image classification network.

生成波形和频谱图

Generate time series data for the four classes. This example uses the helper function生成光谱图至generate the time series and the corresponding spectrogram data. The helper functions used in this example are attached as supporting files.

NumObsperClass = 500;class =分类([“单频”,,,,“三频”,,,,“高斯”,,,,"Pulse");numClasses =长度(class);[NoisyTimeseries,频谱图,标签] = GeneratesPectRogramData(NumoBsperClass,class);

Compute the size of the spectrogram images and the number of observations.

inputSize = size(spectrograms, [1 2]); numObs = size(spectrograms,4);

绘图生成的数据

绘制添加噪声的时间序列数据子集。由于噪声与信号具有可比的幅度,因此数据在时域中显得嘈杂。此功能使分类成为一个具有挑战性的问题。

图NumPlots = 12;为了i = 1:numplots子图(3,4,i)绘图(noisyTimeseries(i,:))title(labels(i))结尾

Plot the time-frequency spectrograms of the noisy data, in the same order as the time series plots. The horizontal axis is time and the vertical axis is frequency.

数字为了i=1:12 subplot(3,4,i) imshow(spectrograms(:,:,1,i)) holdoncolormap戒律title(labels(i)) holdoff结尾

Features from each class are clearly visible, demonstrating why converting from the time domain to spectrogram images can be beneficial for this type of problem. For example, the单频率class has a single peak at the fundamental frequency, visible as a horizontal bar in the spectrogram. For theThreeFrequencyclass, the three frequencies are visible.

All classes display a faint band at low frequency (near the top of the image), corresponding to the background sinusoid.

拆分数据

使用拆分标签功能将数据分为培训和验证数据。使用80%的数据进行培训,验证20%。

splitIndices = splitlabels(标签,0.8);trainlabels =标签(SplitIndices {1});trainspectRograms = spectrograms(:, :,:,splitIndices {1});vallabels =标签(SplitIndices {2});valspectRograms = spectrograms(:, :,:,splitIndices {2});

定义神经网络体系结构

创建一个具有卷积,批处理归一化和偏移层的卷积神经网络。

dropoutProb = 0.2; numFilters = 8; layers = [ imageInputLayer(inputSize) convolution2dLayer(3,numFilters,'Padding',,,,'same')batchnormalizationlayer relulayer maxpooling2dlayer(3,“大步”,,,,2,'Padding',,,,'same')convolution2dLayer(3,2*numFilters,'Padding',,,,'same')batchNormalizationLayer convolution2dLayer(3,4*numFilters,'Padding',,,,'same') batchNormalizationLayer reluLayer globalMaxPooling2dLayer dropoutLayer(dropoutProb) fullyConnectedLayer(numClasses) softmaxLayer classificationLayer];

定义培训选项

Define options for training using the SGDM optimizer. Shuffle the data every epoch by setting the'Shuffle'option to'every-epoch'。Monitor the training progress by setting the“绘图”option to“训练过程”。要抑制详细的输出,请设置'Verbose'false

options = trainingOptions('sgdm',,,,...'Shuffle',,,,'every-epoch',,,,...“绘图”,,,,“训练过程”,,,,...'Verbose',,,,false,...'ValidationData',,,,{valSpectrograms,valLabels});

火车网络

训练网络以对频谱图进行分类。

net = trainNetwork(trainSpectrograms,trainLabels,layers,options);

Accuracy

Classify the validation observations using the trained network.

predLabels = classify(net,valSpectrograms);

通过绘制混乱矩阵来调查网络性能混乱

数字混乱(valLabels,predLabels,'Normalization',,,,'row-normalized'

该网络准确地对验证谱图进行了分类,大多数类别具有接近100%的精度。

Investigate Network Predictions

使用成像时了解图像数据中哪些功能对于分类最重要。

这LIME technique segments an image into several features and generates synthetic observations by randomly including or excluding features. Each pixel in an excluded feature is replaced with the value of the average image pixel. The network classifies these synthetic observations, and uses the resulting scores for the predicted class, along with the presence or absence of a feature, as responses and predictors to train a regression problem with a simpler model—in this example, a regression tree. The regression tree tries to approximate the behavior of the network on a single observation. It learns which features are important and significantly impact the class score.

定义自定义细分图

By default,成像时uses superpixel segmentation to divide the image into features. This option works well for natural images, but is less effective for spectrogram data. You can specify a custom segmentation map by setting the'分割'如果名称参数数值数组相同ze as the image, where each element is an integer corresponding to the index of the feature that pixel is in.

对于频谱图数据,频谱图图像在Y维(频率)中具有比X-dimension(Time)更优质的特征。在40 x 6个网格中生成具有240个段的分割图,以提供更高的频率分辨率。通过使用imresizefunction, specifying the upsampling method as'nearest'

featureIdx = 1:240; segmentationMap = reshape(featureIdx,6,40)'; segmentationMap = imresize(segmentationMap,inputSize,'nearest');

Compute LIME Map

Plot the spectrogram and compute the LIME map for two observations from each class.

subboshowperclass = 2;为了j = 1:sovtoshowperclass图为了i = 1:长度(类)idx = find(vallabels == class(i),sovtoshowperclass);%读取测试图像和标签。testSpectrogram = valSpectrograms(:,:,:,idx(j)); testLabel = valLabels(idx(j));% Compute the LIME importance map.map = imagelime(net,testsspectrogram,testlabel,...'数字样本',4096,...'分割',分段图);% Rescale the map to the size of the image.mapRescale = uint8(255*recale(map));% Plot the spectrogram image next to the LIME map.子图(2,2,i)imshow(imtile({testspectrogram,mapRescale}))title(string(testlabel))colormap戒律结尾结尾

这LIME maps demonstrate that for most classes, the network is focused on the relevant features for classification. For example, for the单频率class, the network focuses on the frequency corresponding to the power spectrum of the sine wave and not on spurious background details or noise.

为了单频率类,网络使用频率进行分类。为了PulseandGaussian类,网络还集中在频谱图的正确频率部分上。对于这三个类别,网络并不与所有频谱图顶部附近可见的背景频率混淆。此信息对于区分这些类别无助于这些类(如所有类中所存在),因此网络忽略了它。相反,对于ThreeFrequencyclass, the constant background frequency is relevant to the classification decision of the network. For this class, the network does not ignore this frequency, but treats it with similar importance to the three actual frequencies.

成像时results demonstrate that the network is correctly using peaks in the time-frequency spectrograms and is not confused by the spurious background sinusoid for all classes except for theThreeFrequency类,网络没有区分信号中的三个频率和低频背景。

也可以看看

|(Signal Processing Toolbox)|

Related Topics