这篇文章是小萌新对西储大学轴承故障进行分析,固定特征为故障直径为0.007,电机转速为1797,12k驱动端故障数据(Drive_End)即DE-time。故障类型y值:滚动体故障,内圈故障,3时,6时,12时外圈故障。

  由于CWRU包无法在python3中直接用,因此首先改写了cwru的代码,直接进行数据处理并划分为训练集和测试集。接着对这些数据进行HHT转换,求出imfs,最后再构建LSTM模型进行分析。

import PyEMD
from PyEMD import *
import scipy
from scipy.io import loadmat
import matplotlib.pyplot as plt
from matplotlib import *
import os
import errno
import urllib.request as urllib
import numpy as np

from scipy.io import loadmat

import random

import pandas as pd

from keras.callbacks import ModelCheckpoint

from keras.models import Model, load_model, Sequential

from keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D, Flatten

from keras.layers import GRU, Bidirectional, BatchNormalization, Reshape

from keras.optimizers import Adam

from keras.utils import to_categorical

from keras.utils import plot_model

'''选取训练集和测试集数据
#选取故障直径为0.007,电机转速为1797,12k驱动端故障数据(Drive_End),
#y值分别为滚动体故障,内圈故障,3时,6时,12时外圈故障'''
## ===================================================选取数据===============================================================
#
class CWRU:

    def __init__(self, path1,length):
        file_list = []
        for root,dirs,files in os.walk(path1):
            for file in files:
                if '12k_Drive_End' in file and '007' in file and '_0_' in file:
                    file_list.append(file)


        self.length = length
        self._load_and_slice_data(path1, file_list)

        # shuffle training and test arrays

    def _load_and_slice_data(self, rdir, infos):
        self.X_train = np.zeros((0, self.length))
        self.X_test = np.zeros((0, self.length))
        self.y_train = []
        self.y_test = []
        for idx, info in enumerate(infos):
#            # directory of this file
            fdir = os.path.join(rdir, info)
            mat_dict = loadmat(fdir)      #载入数据
            fliter_i = filter(lambda x: 'DE_time' in x, mat_dict.keys())           #提取数据中的de-time部分
            fliter_list = [item for item in fliter_i]
            key = fliter_list[0]                                                 #这两步是取key值
#            key = filter(lambda x: 'DE_time' in x, mat_dict.keys())[0]
            time_series1 = mat_dict[key][:, 0]                    #将DE-time的时间序列取出来
            time_series = time_series1[:120001]
            idx_last = -(time_series.shape[0] % self.length)     #算出信号长度整数倍外还有那些数
            clips = time_series[:idx_last].reshape(-1, self.length)        # 将提取的时间序列转换成二维,每一个数据的长度为设置的长度

            n = clips.shape[0]                                               #行数,也就是代表数据量的大小
            n_split =int((3 * n / 4))                                      #设置训练集和测试集的比例
            self.X_train = np.vstack((self.X_train, clips[:n_split]))            #取训练集
            self.X_test = np.vstack((self.X_test, clips[n_split:]))             #取测试集
            self.y_train += [idx] * n_split                                    #给故障类型设立标签
            self.y_test += [idx] * (clips.shape[0] - n_split)                  #给测试的故障类型设立标签

path1 =   r"E:\work\CWRU_analysis\CaseWesternReserveUniversityData-master"

data = CWRU(path1, 400)

X_train,y_train, X_test,y_test = [],[],[],[]

X_test.extend(data.X_test)

y_test.extend(data.y_test)

X_train.extend(data.X_train)

y_train.extend(data.y_train)


''' ===============================================将data进行HHT,求出imf,这会转成三维的数据============================================================================
#对每个 数据去求imf,并作为输入'''
def data_to_imf(Data,t):
    imf = []
    for data in Data:
        emd = EMD()
        imf_ = emd.emd(data,t)[:5]
        imf.append(imf_)
    return np.array(imf).reshape(-1,5,400)

t = np.linspace(0, 1, 12000)[:400]

a = data_to_imf(X_train,t)

X_train_data = np.transpose(a,(0,2,1))

y_train_data = to_categorical(y_train)         #将数据转换成类别矩阵


b= data_to_imf(X_test,t)

X_test_data = np.transpose(b,(0,2,1))

y_test_data = to_categorical(y_test)



'''
# =======================================构建LSTM模型并实验========================================================================================
# '''

def create_model():
    model = Sequential()
    #输入数据的shape为(n_samples, timestamps, features)
    #隐藏层设置为20, input_shape元组第二个参数1意指features为1
    model.add(LSTM(units=20,input_shape=(X_train_data.shape[1], X_train_data.shape[2])))
#    model.add(Dropout(0.2))
    #后接全连接层,直接输出单个值,故units为10

    model.add(Dense(units=5))
    model.add(Activation('softmax'))#选用非线性激活函数,用于分类
    model.compile(loss='categorical_crossentropy',optimizer=Adam(lr=0.001), metrics=['accuracy'])#损失函数为平均均方误差,优化器为Adam,学习率为0.001
    return model

model = create_model()
history =model.fit(X_train_data, y_train_data, epochs=1000, batch_size=225)

#求损失函数
loss, acc = model.evaluate(X_test_data, y_test_data)

#保存模型
model_save_path = "model_file_path.h5"
model.save(model_save_path)
model.summary()

#输出准确率
print("Dev set accuracy = ", acc)

  本文有参考众多故障分析文章,但是忘了保存,没有链接了。。。。。。。。

01-18 09:59