一种结合双阶段注意力循环神经网络(DA-RNN)和卷积块注意力模块(CBAM)的滚动轴承故障诊断方法
目录
研究背景
研究方法
核心代码实现(Python)
1. 数据预处理:振动信号→RGB图像
2. DA-RNN模型实现(双阶段注意力机制)
3. CBAM-CNN模型实现(带残差连接)
4. 联合训练流程(DA-RNN+CBAM-CNN)
关键代码解析
实验设计
结果与分析
总体结论
研究背景
- 研究问题:这篇文章要解决的问题是如何在数据不平衡条件下对滚动轴承进行智能故障诊断。传统的故障诊断方法通常假设故障样本和正常样本的数量相近,但在实际应用中,正常样本的数量通常远多于故障样本,导致模型的准确性和稳定性下降。
- 研究难点:该问题的研究难点包括:如何在数据严重不平衡的情况下有效地扩展少数类样本,如何提高模型对少数类样本的识别能力,以及如何在保持模型复杂度较低的情况下实现高精度的故障诊断。
- 相关工作:近年来,基于数据驱动的智能故障诊断方法得到了广泛应用。Chen等人提出了基于深度卷积神经网络的数据融合方法用于健康状态识别;Tang设计了基于融合单元的卷积结构用于多传感器数据的特征提取;Gong提出了一种改进的卷积神经网络方法用于故障分类;Liu提出了基于集成卷积神经网络的轴承故障诊断模型。然而,这些方法大多假设数据分布平衡,难以处理实际中常见的不平衡数据问题。
研究方法
提出了一种结合双阶段注意力循环神经网络(DA-RNN)和卷积块注意力模块(CBAM)的滚动轴承故障诊断方法。具体来说,
- DA-RNN模型:首先,使用DA-RNN模型扩展不平衡数据集中的少数类样本。DA-RNN模型通过输入注意力机制和时间注意力机制,分别在特征维度和时间维度上选择重要的特征和隐藏状态,从而提高模型的预测能力。模型的输入为n个时间序列,输出为当前时间步的预测值。具体公式如下:
yT=F(y1,y2,⋯,yT−1,x1,x2,⋯,xT)
其中,F是通过DA-RNN模型学习到的非线性映射函数。
Fig. 2. The structure of a dual-stage attention-based recurrent neural network model.
2. 信号预处理方法:其次,设计了一种将振动信号转换为图像的方法。该方法利用振动加速度信号及其对应的积分速度和位移信号,通过傅里叶变换和积分运算,将振动信号转换为RGB图像。具体公式如下:a(t)=Aejωtν(t)=jωAejωtx(t)=−ω2Aejωt
其中,a(t)、ν(t)和x(t)分别为加速度、速度和位移信号的傅里叶分量。
Fig. 3. The structure of the designed signal preprocessing method based on vibration acceleration signal, velocity, and displacement signals.
3. CBAM-CNN模型:最后,设计了一种嵌入CBAM结构的卷积神经网络(CNN)模型用于故障分类。CBAM模块在通道维度和空间维度上分别引入注意力机制,增强模型对重要特征的关注能力。具体公式如下:F∗=Mc(F)⊗FF′′=Ms(F′)⊗F′
其中,Mc和Ms分别为通道维度和空间维度的注意力权重。
Fig. 5. The structure of ResNet-18 model combined with CBAM.
核心代码实现(Python)
1. 数据预处理:振动信号→RGB图像
python
import numpy as np
import cv2def signal_to_rgb_image(signal, sr=12000, window_size=32, overlap=0.5):"""将振动信号转换为RGB图像:param signal: 输入一维振动信号:param sr: 采样率:param window_size: 图像尺寸(正方形):param overlap: 重叠率:return: RGB图像数组 (height, width, 3)"""# 参数计算hop_length = int(window_size * (1 - overlap))n_frames = 1 + int((len(signal) - window_size) / hop_length)# 初始化RGB通道r_channel = np.zeros((window_size, n_frames))g_channel = np.zeros_like(r_channel)b_channel = np.zeros_like(r_channel)# 分帧处理for i in range(n_frames):start = i * hop_lengthend = start + window_sizeframe = signal[start:end]# FFT计算频域特征dft = np.fft.fft(frame)freq = np.fft.fftfreq(len(frame), 1/sr)freq = freq[:len(dft)//2]power = np.abs(dft[:len(dft)//2])**2# 积分运算获取速度/位移信号vel = np.cumsum(frame) * (1/sr) # 简化的速度积分disp = np.cumsum(vel) * (1/sr) # 简化的位移积分# 归一化到[0,255]r = cv2.normalize(power, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)g = cv2.normalize(vel, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)b = cv2.normalize(disp, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)r_channel[:, i] = rg_channel[:, i] = gb_channel[:, i] = b# 合并通道并调整尺寸rgb_img = np.stack([r_channel, g_channel, b_channel], axis=-1)rgb_img = cv2.resize(rgb_img, (window_size, window_size))return rgb_img# 示例用法
signal = np.random.randn(12000) # 模拟1秒振动信号
img = signal_to_rgb_image(signal)
cv2.imwrite("sample.png", img)
2. DA-RNN模型实现(双阶段注意力机制)
python
from tensorflow.keras.layers import (Input, LSTM, Dense, Multiply, Reshape, Lambda,GlobalAveragePooling1D, Conv1D, Activation
)
from tensorflow.keras.models import Modeldef darnn_model(input_shape):"""双阶段注意力循环神经网络(DA-RNN):param input_shape: 输入形状 (time_steps, features):return: Keras模型实例"""inputs = Input(shape=input_shape)# 编码器阶段(输入注意力)encoder = LSTM(128, return_sequences=True)(inputs)attention = Dense(1, activation='tanh')(encoder)attention = GlobalAveragePooling1D()(attention)attention = Activation('softmax')(attention)encoder_attended = Multiply()([encoder, attention])# 解码器阶段(时间注意力)decoder = LSTM(128, return_sequences=True)(encoder_attended)time_attention = Dense(decoder.shape[1], activation='softmax')(decoder)context = Multiply()([decoder, time_attention])context = GlobalAveragePooling1D()(context)# 输出层outputs = Dense(1)(context)model = Model(inputs=inputs, outputs=outputs)return model# 示例用法
input_shape = (100, 1) # 时间步长=100,单通道
darnn = darnn_model(input_shape)
darnn.summary()
3. CBAM-CNN模型实现(带残差连接)
python
def cbam_block(x, ratio=16):"""卷积块注意力模块(CBAM):param x: 输入特征图:param ratio: 通道注意力压缩比率:return: 增强后的特征图"""# 通道注意力channel_avg = GlobalAveragePooling2D()(x)channel_max = GlobalMaxPooling2D()(x)channel_att = Concatenate()([channel_avg, channel_max])channel_att = Dense(channel_att.shape[-1]//ratio, activation='relu')(channel_att)channel_att = Dense(x.shape[-1], activation='sigmoid')(channel_att)x = Multiply()([x, channel_att])# 空间注意力spatial_avg = GlobalAveragePooling2D()(x)spatial_max = GlobalMaxPooling2D()(x)spatial_att = Concatenate()([spatial_avg, spatial_max])spatial_att = Reshape((1, 1, -1))(spatial_att)spatial_att = Conv2D(1, (3,3), padding='same', activation='sigmoid')(spatial_att)x = Multiply()([x, spatial_att])return xdef cbam_cnn_model(input_shape, num_classes):"""基于CBAM的改进ResNet-18模型:param input_shape: 输入形状 (height, width, channels):param num_classes: 类别数:return: Keras模型实例"""inputs = Input(shape=input_shape)# 初始卷积层x = Conv2D(64, (7,7), strides=2, padding='same')(inputs)x = Activation('relu')(x)x = MaxPooling2D((3,3), strides=2, padding='same')(x)# 残差块(含CBAM)shortcut = xx = Conv2D(64, (3,3), padding='same')(x)x = Activation('relu')(x)x = Conv2D(64, (3,3), padding='same')(x)x = cbam_block(x) # 插入CBAM模块x = Add()([x, shortcut])x = Activation('relu')(x)# 全局平均池化+分类头x = GlobalAveragePooling2D()(x)outputs = Dense(num_classes, activation='softmax')(x)model = Model(inputs=inputs, outputs=outputs)return model# 示例用法
input_shape = (32, 32, 3) # RGB图像
num_classes = 4
cbam_cnn = cbam_cnn_model(input_shape, num_classes)
cbam_cnn.summary()
4. 联合训练流程(DA-RNN+CBAM-CNN)
python
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split# 加载数据(假设已预处理好)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)# 构建DA-RNN模型(用于数据扩展)
darnn = darnn_model((100, 1)) # 输入形状根据实际调整
darnn.compile(optimizer=Adam(1e-3), loss='mse')# 使用DA-RNN扩展少数类样本(伪代码)
# minority_samples = ...
# expanded_data = darnn.predict(minority_samples)# 构建CBAM-CNN模型
cbam_cnn = cbam_cnn_model((32,32,3), 4)
cbam_cnn.compile(optimizer=Adam(1e-3),loss='categorical_crossentropy',metrics=['accuracy', tf.keras.metrics.F1Score(num_classes=4)]
)# 数据增强(可选)
data_augmentation = tf.keras.Sequential([tf.keras.layers.RandomFlip("horizontal"),tf.keras.layers.RandomRotation(0.1),tf.keras.layers.RandomContrast(0.2)
])# 训练流程
history = cbam_cnn.fit(data_augmentation(X_train),y_train,validation_data=(X_test, y_test),epochs=100,batch_size=32,callbacks=[tf.keras.callbacks.EarlyStopping(patience=10),tf.keras.callbacks.ReduceLROnPlateau(factor=0.5, patience=5)]
)# 评估结果
test_loss, test_acc, test_f1 = cbam_cnn.evaluate(X_test, y_test)
print(f"Test Accuracy: {test_acc:.4f}, F1-score: {test_f1:.4f}")
关键代码解析
-
DA-RNN核心机制
- 输入注意力:通过全连接层动态调整不同时间步的输入权重
- 时间注意力:自适应选择历史状态中的关键时序特征
- 应用场景:解决传统RNN对长序列依赖性不足的问题
-
CBAM创新点
- 通道注意力:建模通道间的相互依赖关系
- 空间注意力:聚焦局部关键区域
- 优势:轻量化设计,可直接嵌入现有CNN架构
-
工程优化技巧
- 混合精度训练:使用
tf.keras.mixed_precision
加速训练 - 分布式训练:通过
tf.distribute.MirroredStrategy
扩展GPU资源 - 模型剪枝:对DA-RNN的LSTM层进行权重剪枝减少计算
- 混合精度训练:使用
实验设计
- 数据收集:实验使用了两个公开的滚动轴承数据集:CWRU轴承数据中心的数据集和SpectraQuest机械故障模拟试验台的数据集。CWRU数据集包含了不同转速和不同缺陷直径下的振动信号数据,SpectraQuest数据集则通过电火花加工(EDM)人工引入了内圈、外圈和滚动体的故障。
- 实验设置:在CWRU数据集上,实验设置了四个不同的不平衡比例(100:50, 100:25, 100:20, 100:10),每个数据集包含1500个训练样本和150个测试样本。在SpectraQuest数据集上,实验设置了四个不同的不平衡比例(100:50, 100:25, 100:20, 100:10),每个数据集包含2000个训练样本和200个测试样本。
- 参数配置:DA-RNN模型的时间步长设置为10,编码器和解码器的隐藏层大小均设置为128。CBAM-CNN模型的初始学习率设置为0.001,使用Adam优化算法更新网络参数,训练迭代次数设置为120,批量大小设置为32。
结果与分析
- DA-RNN模型预测结果:DA-RNN模型在不同不平衡比例的数据集上的预测误差(MAE和RMSE)随着不平衡比例的增加而增加。具体结果如下:
- 数据集A(100:50):内圈故障的MAE为0.0830,RMSE为0.1153;外圈故障的MAE为0.1071,RMSE为0.1670;滚动体故障的MAE为0.0425,RMSE为0.0535。
- 数据集B(100:25):内圈故障的MAE为0.0987,RMSE为0.1345;外圈故障的MAE为0.1321,RMSE为0.2195;滚动体故障的MAE为0.0452,RMSE为0.0578。
- 数据集C(100:20):内圈故障的MAE为0.0991,RMSE为0.1366;外圈故障的MAE为0.1459,RMSE为0.2421;滚动体故障的MAE为0.0467,RMSE为0.0589。
- 数据集D(100:10):内圈故障的MAE为0.1000,RMSE为0.1378;外圈故障的MAE为0.1547,RMSE为0.2456;滚动体故障的MAE为0.0468,RMSE为0.0590。
Fig. 7. The predictions results of DA-RNN model for three types of fault using dataset A, B, C, D. Prediction results are compared to experimental test true values.(a1),(a2),and(a3) are the results of inner race fault, out race fault, and rolling element fault test on dataset A.(b1),(b2),and(b3) are the results of inner race fault,out race fault, and rolling element fault test on dataset B.(c1),(c2), and(c3) are the results of inner race fault, out race fault, and rolling element fault test on dataset C.
- CBAM-CNN模型分类结果:CBAM-CNN模型在不同不平衡比例的数据集上的分类准确率、F1值和G-mean值如下:
- 数据集A(100:50):准确率为97.69%,F1值为97.73%,G-mean值为97.71%。
- 数据集B(100:25):准确率为92.03%,F1值为92.12%,G-mean值为92.04%。
- 数据集C(100:20):准确率为82.02%,F1值为82.43%,G-mean值为82.16%。
- 数据集D(100:10):准确率为70.24%,F1值为75.66%,G-mean值为78.21%。
Fig.10. Confusion matrix of classification results for the proposed DARNN-CBAM-CNN method testing on(a) dataset A,(b) dataset B,(c) dataset C, and(d)dataset D.
- 与其他方法的对比:与BP-SMOTE、SVM-SMOTE、CNN-SMOTE、DARNN-CBAM-VGG16、DARNN-CBAM-VGG19和DARNN-ResNet18等方法相比,DARNN-CBAM-CNN方法在所有数据集上的分类准确率均有显著提升。例如,在数据集A(100:50)上,DARNN-CBAM-CNN方法的准确率比BP-SMOTE方法提高了10.90%。
总体结论
本文提出了一种结合DA-RNN和CBAM-CNN的滚动轴承故障诊断方法,能够在数据不平衡条件下实现高精度的故障诊断。实验结果表明,该方法在不同不平衡比例的数据集上均表现出色,分类准确率显著高于其他方法。该方法不仅能够有效扩展少数类样本,还能通过注意力机制增强模型对重要特征的关注能力,具有较强的实用性和推广价值。