基于自编码器的滚动轴承失效异常检测(Python)

B站影视 2025-01-24 06:17 2

摘要:# importing packagesimport pandas as pdimport numpy as npfrom sklearn.preprocessing import StandardScalerimport seaborn as snsimpo

# importing packagesimport pandas as pdimport numpy as npfrom sklearn.preprocessing import StandardScalerimport seaborn as snsimport matplotlib.pyplot as pltimport matplotlib.image as mpimg%matplotlib inlinesns.set(color_codes=True)import globimport osimport pickleimport joblibfrom keras.layers import Densefrom keras.models import Model, Sequential, load_modelfrom keras import regularizersdata_dir = '2nd_test'merged_data = pd.DataFrame# Looping over all files from 12th Feb to 19th Febfor filename in os.listdir(data_dir):dataset=pd.read_csv(os.path.join(data_dir, filename), sep='\t')dataset_mean_abs = np.array(dataset.abs.mean)dataset_mean_abs = pd.DataFrame(dataset_mean_abs.reshape(1,4))dataset_mean_abs.index = [filename]merged_data = pd.concat([merged_data, dataset_mean_abs], ignore_index=False)# Renaming columnsmerged_data.columns = ['Bearing 1','Bearing 2','Bearing 3','Bearing 4']# Identifying index as dateTime formatmerged_data.index = pd.to_datetime(merged_data.index, format='%Y.%m.%d.%H.%M.%S')merged_data = merged_data.sort_indexmerged_data.to_csv('merged_dataset_BearingTest_2.csv')df_filename = "merged_dataset_BearingTest_2.csv"df = pd.read_csv(df_filename, index_col="Unnamed: 0")df# total missing value each columnsfor col in df.columns.tolist:print('{} column missing values: {}'.format(col, df[col].isnull.sum))Bearing 1 column missing values: 0Bearing 2 column missing values: 0Bearing 3 column missing values: 0Bearing 4 column missing values: 0# Visualisation Dataax = df.plot(figsize = (14,5), title="Vibration Activity" , legend = True)ax.set(xlabel="Time", ylabel="Vibration")plt.axvline(x=df.index[-1], linewidth=4, color='b')df['2004-02-15 14:52:39':'2004-02-16 14:52:39'].plot(figsize = (10,5), title="Zoom Vibration Activity" , legend = True)ax.set(xlabel="Time", ylabel="Vibration")plt.xticks(rotation=25);# split datasettrain_df = df[:'2004-02-15 20:52:39'] # represent normal operating conditionstest_df = df['2004-02-15 20:52:39':] # leading up to the bearing failuretrain_df.plot(figsize = (10,5), title="Train dataset" , legend = True)plt.xticks(rotation=25);# Standardize the data.scaler = StandardScalerscaler = scaler.fit(train_df)x_train = pd.DataFrame(scaler.transform(train_df),columns=train_df.columns, index=train_df.index)x_test = pd.DataFrame(scaler.transform(test_df),columns=test_df.columns, index=test_df.index)## modellingfor i, col_name in enumerate(df.columns):print('================= TRAINING %s col_name)curr_xtrain = np.array(x_train[[col_name]])autoencoder = Sequentialautoencoder.add(Dense(9,activation='elu',kernel_initializer='glorot_uniform',kernel_regularizer=regularizers.l2(0.0),input_shape=(curr_xtrain.shape[1],)))autoencoder.add(Dense(3,activation='elu',kernel_initializer='glorot_uniform'))autoencoder.add(Dense(9,activation='elu',kernel_initializer='glorot_uniform'))autoencoder.add(Dense(curr_xtrain.shape[1],kernel_initializer='glorot_uniform'))autoencoder.compile(optimizer='adam', loss="mse")# fitting the modelhist = autoencoder.fit(curr_xtrain, curr_xtrain, batch_size=10,epochs=30,validation_split=0.05,verbose = 1)# save model to model pathautoencoder.save("model/model_%s.h5"%col_name)# visualize training-validation lossplt.plot(hist.history['loss'],'r',label='Training loss')plt.plot(hist.history['val_loss'],'b',label='Validation loss')plt.title("Training-Validation Loss %s"%col_name)plt.legend(loc='upper right')plt.xlabel('Epochs')plt.ylabel('Loss [mse]')plt.ylim([0,.1])plt.savefig('/content/drive/MyDrive/Machine life detection/Image_Auto_Encoder_Anamoly_Detection/plot_training-validation loss_%s.png'%col_name)plt.closeprint('\n================== DONE Epoch 23/3047/47 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0159 - val_loss: 0.0212Epoch 24/3047/47 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0110 - val_loss: 0.0223Epoch 25/3047/47 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0121 - val_loss: 0.0199Epoch 26/3047/47 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0107 - val_loss: 0.0183Epoch 27/3047/47 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0109 - val_loss: 0.0177Epoch 28/3047/47 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0089 - val_loss: 0.0161Epoch 29/3047/47 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0081 - val_loss: 0.0155Epoch 30/3047/47 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0091 - val_loss: 0.0144from keras.models import load_modelfrom keras.losses import MeanSquaredErrormodels = {}for col_name in df.columns:models[col_name] = load_model(f"/content/model/model_{col_name}.h5", custom_objects={'mse': MeanSquaredError})# visualization x_test prediction# leading up to the bearing failurefor col in df.columns:x_test_array = np.array(x_test[col])x_test_pred = models[col].predict(x_test_array)df_test_pred = pd.DataFrame(x_test_pred)df_test_pred["Predicted"] = x_test_preddf_test_pred["Actual"] = x_test_arraydf_test_pred.index = x_test[col].indexdf_test_pred.plot(figsize = (10,5), title=f"Actual vs Predicted {col}" , legend = True)plt.xticks(rotation=15)plt.show# Get train MAE loss.list_threshold = for col in df.columns:x_train_pred = models[col].predict(np.array(x_train[col]))train_mae_loss = np.mean(np.abs(x_train_pred - np.array(x_train[col])),axis=1)plt.hist(train_mae_loss, bins=50)plt.xlabel("Train MAE loss")plt.ylabel("No of samples")plt.title(col)plt.show# Get reconstruction loss thresholdthreshold = np.max(train_mae_loss)list_threshold.append(threshold)print(f"Reconstruction error threshold {col}: ", threshold)for col in df.columns:window_percentage = 20k = int(len(x_test[col]) * (window_percentage/100))N = len(x_test[col])get_bands = lambda data : (np.mean(x_train[col]) + 3*np.std(x_train[col]),np.mean(x_train[col]) - 3*np.std(x_train[col]))bands = [get_bands(x_test[col][range(0 if i - k upper) | (x_test[col]

知乎学术咨询:

https://www.zhihu.com/consult/people/792359672131756032?isMe=1

担任《Mechanical System and Signal Processing》《中国电机工程学报》等期刊审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。

分割线分割线分割线分割线分割线分割线分割线分割线分割线分割线基于迭代小波阈值方法的非平稳信号降噪-以心电信号为例(Python)完整代码可通过知乎付费咨询获得:https://www.zhihu.com/consult/people/79235967213175603

基于脉冲小波的旋转机械故障诊断(MATLAB R2018a)

基于优化Morlet小波的一维信号瞬态特征提取方法(MATLAB)

来源:科技透视仪

相关推荐