From 1cf7ba2aa3873ec4b1c8c66bd6a3e5efa42a7c7f Mon Sep 17 00:00:00 2001 From: markilue <745518019@qq.com> Date: Fri, 4 Nov 2022 10:17:20 +0800 Subject: [PATCH] =?UTF-8?q?model=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../condition_monitoring/plot/test_plot.py | 272 +++++++++++++----- .../self_try/Joint_Monitoring_banda.py | 62 ++-- .../self_try/compare/DCConv.py | 118 ++++---- .../self_try/compare/GRU-CNN.py | 148 ++++++---- .../self_try/compare/GRU.py | 35 +-- .../self_try/compare/RNet-C.py | 102 ++++--- .../self_try/compare/RNet-D.py | 21 +- .../self_try/compare/RNet-L.py | 39 ++- .../self_try/compare/RNet-MSE.py | 25 +- .../self_try/compare/RNet-S.py | 34 +-- .../self_try/compare/RNet.py | 115 +++----- .../self_try/compare/resnet_18.py | 51 ++-- 12 files changed, 579 insertions(+), 443 deletions(-) diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/plot/test_plot.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/plot/test_plot.py index 178a3d8..0a5cf31 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/plot/test_plot.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/plot/test_plot.py @@ -25,9 +25,9 @@ from mpl_toolkits.axes_grid1.inset_locator import inset_axes result_file_name = "E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\self_try\compare\mse\RNet_C\RNet_C_timestamp120_feature10_result.csv" -mse_file_name = "E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\self_try\compare\mse\RNet_D\RNet_D_timestamp120_feature10_mse_predict1.csv" +mse_file_name = "E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\self_try\compare\mse\RNet_D/banda\RNet_D_banda_mse_predict1.csv" -max_file_name = "E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\self_try\compare\mse\RNet_D\RNet_D_timestamp120_feature10_max_predict1.csv" +max_file_name = "E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\self_try\compare\mse\RNet_D/banda\RNet_D_banda_max_predict1.csv" source_path = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" @@ -36,10 +36,11 @@ model_name1 = "../hard_model/two_weight/banda_joint_epoch14_9872_9863/weight" file_name = "E:\跑模型\论文写作/DCAU.txt" + # list = [64.16, 65.26, 65.11, 66.6, 67.16, 66.28, 73.86, 75.24, 73.98, 76.7, 98.86, 99.45, 99.97] -def plot_result(result_data): +def plot_result_banda(result_data): parameters = { 'figure.dpi': 600, 'figure.figsize': (2.8, 2), @@ -51,7 +52,7 @@ def plot_result(result_data): 'legend.fontsize': 5, } plt.rcParams.update(parameters) - fig,ax=plt.subplots(1, 1) + fig, ax = plt.subplots(1, 1) plt.rc('font', family='Times New Roman') # 全局字体样式 font1 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 5} # 设置坐标标签的字体大小,字体 plt.scatter(list(range(result_data.shape[0])), result_data, c='black', s=0.01, label="predict") @@ -64,7 +65,7 @@ def plot_result(result_data): # plt.text(0, 0.55, "Threshold", fontsize=5, color='red', # verticalalignment='top') - plt.axvline(result_data.shape[0] * 2 / 3, c='blue', ls='-.', lw=0.5, label='real fault') + plt.axvline(result_data.shape[0] * 2 / 3-50, c='blue', ls='-.', lw=0.5, label='real fault') # plt.axvline(415548, c='blue', ls='-.', lw=0.5, label='real fault') # plt.xticks(range(6), ('06/09/17', '12/09/17', '18/09/17', '24/09/17', '29/09/17')) # 设置x轴的标尺 plt.text(result_data.shape[0] * 5 / 6, 0.4, "Fault", fontsize=5, color='black', verticalalignment='top', @@ -78,7 +79,7 @@ def plot_result(result_data): 'pad': 1.5, 'linewidth': 0.1}, fontdict=font1) indices = [result_data.shape[0] * i / 4 for i in range(5)] - classes = ['01/09/17', '08/09/17', '15/09/17', '22/09/17', '29/09/17'] + classes = ['02/25', '03/01', '03/06', '03/12', '03/18'] indices1 = [i / 4 for i in range(5)] classes1 = ['0', '0.25', 'Threshold', '0.75', '1'] @@ -92,22 +93,21 @@ def plot_result(result_data): plt.xlabel('Time', fontsize=5) plt.tight_layout() # plt.legend(loc='best', edgecolor='black', fontsize=4) - plt.legend(loc='best', frameon=False, fontsize=4) + plt.legend(loc='upper right', frameon=False, fontsize=4.5) # plt.grid() - - #局部方法图 + # 局部方法图 axins = inset_axes(ax, width="40%", height="30%", loc='lower left', bbox_to_anchor=(0.1, 0.1, 1, 1), bbox_transform=ax.transAxes) axins.scatter(list(range(result_data.shape[0])), result_data, c='black', s=0.001, label="predict") - axins.axvline(result_data.shape[0] * 2 / 3, c='blue', ls='-.', lw=0.5, label='real fault') + axins.axvline(result_data.shape[0] * 2 / 3-50, c='blue', ls='-.', lw=0.5, label='real fault') plt.axhline(0.5, c='red', label='Failure threshold', lw=0.5) - #设置放大区间 # 设置放大区间 - zone_left = int(result_data.shape[0]*2/3-100) - zone_right = int(result_data.shape[0]*2/3)+100 - x=list(range(result_data.shape[0])) + # 设置放大区间 + zone_left = int(result_data.shape[0] * 2 / 3 -160) + zone_right = int(result_data.shape[0] * 2 / 3) + 40 + x = list(range(result_data.shape[0])) # 坐标轴的扩展比例(根据实际数据调整) x_ratio = 0.5 # x轴显示范围的扩展比例 @@ -130,9 +130,98 @@ def plot_result(result_data): axins.set_xlim(xlim0, xlim1) # axins.set_ylim(ylim0, ylim1) + plt.show() + pass +def plot_result(result_data): + parameters = { + 'figure.dpi': 600, + 'figure.figsize': (2.8, 2), + 'savefig.dpi': 600, + 'xtick.direction': 'in', + 'ytick.direction': 'in', + 'xtick.labelsize': 5, + 'ytick.labelsize': 5, + 'legend.fontsize': 5, + } + plt.rcParams.update(parameters) + fig, ax = plt.subplots(1, 1) + plt.rc('font', family='Times New Roman') # 全局字体样式 + font1 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 5} # 设置坐标标签的字体大小,字体 + plt.scatter(list(range(result_data.shape[0])), result_data, c='black', s=0.01, label="predict") + # 画出 y=1 这条水平线 + plt.axhline(0.5, c='red', label='Failure threshold', lw=1) + # 箭头指向上面的水平线 + # plt.arrow(result_data.shape[0]*2/3, 0.55, 2000, 0.085, width=0.00001, ec='red',length_includes_head=True) + # plt.text(result_data.shape[0] * 2 / 3 + 1000, 0.7, "real fault", fontsize=5, color='red', + # verticalalignment='top') + # plt.text(0, 0.55, "Threshold", fontsize=5, color='red', + # verticalalignment='top') + plt.axvline(result_data.shape[0] * 2 / 3-50, c='blue', ls='-.', lw=0.5, label='real fault') + # plt.axvline(415548, c='blue', ls='-.', lw=0.5, label='real fault') + # plt.xticks(range(6), ('06/09/17', '12/09/17', '18/09/17', '24/09/17', '29/09/17')) # 设置x轴的标尺 + plt.text(result_data.shape[0] * 5 / 6, 0.4, "Fault", fontsize=5, color='black', verticalalignment='top', + horizontalalignment='center', + bbox={'facecolor': 'grey', + 'pad': 1.5, + 'linewidth': 0.1}, fontdict=font1) + plt.text(result_data.shape[0] * 1 / 3, 0.6, "Norm", fontsize=5, color='black', verticalalignment='top', + horizontalalignment='center', + bbox={'facecolor': 'grey', + 'pad': 1.5, 'linewidth': 0.1}, fontdict=font1) + indices = [result_data.shape[0] * i / 4 for i in range(5)] + classes = ['09/01', '09/08', '09/15', '09/22', '09/29'] + + indices1 = [i / 4 for i in range(5)] + classes1 = ['0', '0.25', 'Threshold', '0.75', '1'] + + # 第一个是迭代对象,表示坐标的显示顺序,第二个参数是坐标轴显示列表 + plt.xticks([index + 0.5 for index in indices], classes, rotation=25) # 设置横坐标方向,rotation=45为45度倾斜 + # pad调整label与坐标轴之间的距离 + plt.tick_params(bottom=True, top=False, left=True, right=False, direction='inout', length=2, width=0.5, pad=1) + # plt.yticks([index for index in indices1], classes1) + plt.ylabel('Confidence', fontsize=5) + plt.xlabel('Time', fontsize=5) + plt.tight_layout() + # plt.legend(loc='best', edgecolor='black', fontsize=4) + plt.legend(loc='best', frameon=False, fontsize=4.5) + # plt.grid() + + # 局部方法图 + axins = inset_axes(ax, width="40%", height="30%", loc='lower left', + bbox_to_anchor=(0.1, 0.1, 1, 1), + bbox_transform=ax.transAxes) + axins.scatter(list(range(result_data.shape[0])), result_data, c='black', s=0.001, label="predict") + axins.axvline(result_data.shape[0] * 2 / 3-50, c='blue', ls='-.', lw=0.5, label='real fault') + plt.axhline(0.5, c='red', label='Failure threshold', lw=0.5) + # 设置放大区间 + # 设置放大区间 + zone_left = int(result_data.shape[0] * 2 / 3 -100) + zone_right = int(result_data.shape[0] * 2 / 3) + 100 + x = list(range(result_data.shape[0])) + + # 坐标轴的扩展比例(根据实际数据调整) + x_ratio = 0.5 # x轴显示范围的扩展比例 + y_ratio = 0.5 # y轴显示范围的扩展比例 + + mark_inset(ax, axins, loc1=4, loc2=2, fc="none", ec='k', lw=0.5) + + # X轴的显示范围 + xlim0 = x[zone_left] - (x[zone_right] - x[zone_left]) * x_ratio + xlim1 = x[zone_right] + (x[zone_right] - x[zone_left]) * x_ratio + # axins.tick_params(bottom=False) + axins.xaxis.set_visible(False) + + # Y轴的显示范围 + # y = np.hstack((y_1[zone_left:zone_right], y_2[zone_left:zone_right], y_3[zone_left:zone_right])) + # ylim0 = np.min(y) - (np.max(y) - np.min(y)) * y_ratio + # ylim1 = np.max(y) + (np.max(y) - np.min(y)) * y_ratio + + # 调整子坐标系的显示范围 + axins.set_xlim(xlim0, xlim1) + # axins.set_ylim(ylim0, ylim1) plt.show() pass @@ -203,7 +292,7 @@ def plot_Corr(data, size: int = 1): person = pd_data.corr() print(person) # 画热点图heatmap - cmap = sns.heatmap(person, annot=True, cmap='Blues',annot_kws={ + cmap = sns.heatmap(person, annot=True, cmap='Blues', annot_kws={ 'fontsize': 11 }) classes = ['Gs', 'Gio', 'Gip', 'Gp', 'Gwt', 'En', 'Gft', 'Grt', 'Gwt', 'Et', 'Rs', 'Ap', 'Ws', 'Dw', 'Ges', 'Gt', @@ -234,9 +323,9 @@ def plot_bar(y_data): 'savefig.dpi': 600, 'xtick.direction': 'in', 'ytick.direction': 'in', - 'xtick.labelsize': 15, - 'ytick.labelsize': 15, - 'legend.fontsize': 12, + 'xtick.labelsize': 18, + 'ytick.labelsize': 20, + 'legend.fontsize': 18, } plt.rcParams.update(parameters) plt.figure() @@ -263,12 +352,13 @@ def plot_bar(y_data): # plt.tick_params(bottom=False, top=False, left=True, right=False, direction='in', pad=1) plt.xticks([]) - plt.ylabel('False Positive Rate(%)', fontsize=18) - # plt.xlabel('Time', fontsize=5) + plt.ylabel('False Positive Rate(%)', fontsize=22) + plt.xlabel('Methods', fontsize=22) # plt.tight_layout() - num1, num2, num3, num4 = 0.1, 1, 3, 0 - plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol=5, frameon=False, markerscale=0.5) + num1, num2, num3, num4 = 0, 1, 3, 0 + plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol=5, frameon=False, handlelength=1, + handletextpad=0.45, columnspacing=1) plt.ylim([-0.01, 5]) plt.show() @@ -282,7 +372,7 @@ def acc(y_data=list): 'ytick.direction': 'in', 'xtick.labelsize': 18, 'ytick.labelsize': 20, - 'legend.fontsize': 14, + 'legend.fontsize': 18, } plt.rcParams.update(parameters) plt.figure() @@ -308,12 +398,12 @@ def acc(y_data=list): # plt.tick_params(bottom=False, top=False, left=True, right=False, direction='in', pad=1) plt.xticks([]) - plt.ylabel('Accuracy(%)', fontsize=20) - plt.xlabel('Methods', fontsize=20) + plt.ylabel('Accuracy(%)', fontsize=22) + plt.xlabel('Methods', fontsize=22) # plt.tight_layout() num1, num2, num3, num4 = 0, 1, 3, 0 - plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol=5, frameon=False, markerscale=0.5) + plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol=5, frameon=False,handlelength=1,handletextpad=0.45,columnspacing=1) plt.ylim([60, 105]) plt.show() @@ -325,9 +415,9 @@ def plot_FNR1(y_data): 'savefig.dpi': 600, 'xtick.direction': 'in', 'ytick.direction': 'in', - 'xtick.labelsize': 15, - 'ytick.labelsize': 15, - 'legend.fontsize': 12, + 'xtick.labelsize': 18, + 'ytick.labelsize': 20, + 'legend.fontsize': 18, } plt.rcParams.update(parameters) plt.figure() @@ -350,12 +440,16 @@ def plot_FNR1(y_data): # plt.tick_params(bottom=False, top=False, left=True, right=False, direction='in', pad=1) plt.xticks([]) - plt.ylabel('False Negative Rate(%)', fontsize=18) - # plt.xlabel('Time', fontsize=5) + plt.ylabel('False Negative Rate(%)', fontsize=22) + # plt.tick_params(bottom=False, top=False, left=True, right=False, direction='in', pad=1) + + plt.xlabel('Methods', fontsize=22) # plt.tight_layout() - num1, num2, num3, num4 = 0.16, 1, 3, 0 - plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol=5, frameon=False, markerscale=0.5) + num1, num2, num3, num4 = 0.05, 1, 3, 0 + plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol=5, frameon=False, handlelength=1, + handletextpad=0.45, columnspacing=1) + plt.ylim([60, 100]) plt.show() @@ -367,9 +461,9 @@ def plot_FNR2(y_data): 'savefig.dpi': 600, 'xtick.direction': 'in', 'ytick.direction': 'in', - 'xtick.labelsize': 15, - 'ytick.labelsize': 15, - 'legend.fontsize': 12, + 'xtick.labelsize': 18, + 'ytick.labelsize': 20, + 'legend.fontsize': 18, } plt.rcParams.update(parameters) plt.figure() @@ -392,12 +486,16 @@ def plot_FNR2(y_data): # plt.tick_params(bottom=False, top=False, left=True, right=False, direction='in', pad=1) plt.xticks([]) - plt.ylabel('False Negative Rate(%)', fontsize=18) + plt.ylabel('False Negative Rate(%)', fontsize=22) # plt.xlabel('Time', fontsize=5) # plt.tight_layout() - num1, num2, num3, num4 = 0.16, 1, 3, 0 - plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol=5, frameon=False, markerscale=0.5) + plt.xlabel('Methods', fontsize=22) + # plt.tight_layout() + + num1, num2, num3, num4 = 0.08, 1, 3, 0 + plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol=5, frameon=False, handlelength=1, + handletextpad=0.45, columnspacing=1) plt.ylim([0, 5]) plt.show() @@ -415,7 +513,7 @@ def plot_hot(data): 'fraction': 0.6, 'pad': 0.00 }) - plt.title("Pred=0,Expc=0", size=18) + plt.title("Pred={},Expc={}".format(i, i), size=18) plt.axis('off') # 去坐标轴 # 调整色带的标签: cbar = cmap.collections[0].colorbar @@ -431,7 +529,7 @@ def plot_hot_one(data): # 画热点图heatmap # plt.figure(1, figsize=(9, 12)) - plt.subplots(figsize=(14,2)) + plt.subplots(figsize=(14, 2)) # cbar_kws设置色带,fraction设置大小,pad设置填充 cmap = sns.heatmap(data, annot=False, cmap='Blues', linecolor='black', linewidths=1, cbar_kws={"orientation": "horizontal", @@ -451,18 +549,21 @@ def plot_hot_one(data): def plot_mse(file_name="../others_idea/mse"): - mse=np.loadtxt(file_name,delimiter=",") - mse= mse[2000:2338] + mse = np.loadtxt(file_name, delimiter=",") + print(mse.shape) + mse.shape[0]*2/3 + # mse = mse[2000:2300] + # mse = mse[1800:2150] parameters = { 'figure.dpi': 600, - 'figure.figsize': (7,5), + 'figure.figsize': (2.8, 2), 'savefig.dpi': 600, 'xtick.direction': 'in', 'ytick.direction': 'in', - 'xtick.labelsize': 15, - 'ytick.labelsize': 15, - 'legend.fontsize': 10, + 'xtick.labelsize': 6, + 'ytick.labelsize': 6, + 'legend.fontsize': 5, } plt.rcParams.update(parameters) plt.figure() @@ -471,27 +572,62 @@ def plot_mse(file_name="../others_idea/mse"): classes = ['13/09/17', '14/09/17', '15/09/17', '16/09/17', '17/09/17'] # plt.xticks([index + 0.5 for index in indices], classes, rotation=25) # 设置横坐标方向,rotation=45为45度倾斜 - plt.ylabel('MSE', fontsize=15) - plt.xlabel('Time', fontsize=15) + plt.ylabel('MSE', fontsize=8) + plt.xlabel('Time', fontsize=8) plt.tight_layout() - plt.plot(mse) + plt.plot(mse, lw=0.5) plt.show() +def plot_3d(): + # 线 + fig = plt.figure() + ax = fig.add_subplot(projection='3d') + data,label = read_data(file_name='G:\data\SCADA数据\jb4q_8_delete_total_zero.csv',isNew=False) + print(data) + print(data.shape) + x=range(data.shape[1]) + y=range(data.shape[0]) + color=[[0.16,0.14,0.13], + [0.89,0.09,0.05], + [0.12,0.56,1.00], + [0.01,0.66,0.62], + [0.63,0.04,0.83], + [0.63,0.32,0.18], + [0.20,0.63,0.79], + [0.50,0.16,0.16], + [0.61,0.4,0.12], + [1.00,0.38,0.00], + [0.53,0.81,0.92], + [0.13,0.55,0.13], + [1.00,0.89,0.52], + [0.44,0.50,0.41], + [0.20,0.63,0.79], + [0.00,0.78,0.55], + [1.00,0.39,0.28], + [0.25,0.41,0.88]] + # c颜色,marker:样式*雪花 + + ax.plot( xs=y,ys=x, zs=data, c=color, marker="*") + + plt.show() + + def test_result(file_name: str = result_file_name): # result_data = np.recfromcsv(file_name) result_data = np.loadtxt(file_name, delimiter=",") result_data = np.array(result_data) print(len(result_data)) - theshold = len(result_data) - 1 + theshold = len(result_data) print(theshold) print(theshold * 2 / 3) + theshold = theshold * 2 / 3-50 # 计算误报率和漏报率 - positive_rate = result_data[:int(theshold * 2 / 3)][result_data[:int(theshold * 2 / 3)] < 0.66].__len__() / ( + positive_rate = result_data[:int(theshold)][result_data[:int(theshold)] < 0.66].__len__() / ( theshold * 2 / 3) - negative_rate = result_data[int(theshold * 2 / 3):][result_data[int(theshold * 2 / 3):] > 0.66].__len__() / ( + negative_rate = result_data[int(theshold):][result_data[int(theshold):] > 0.66].__len__() / ( theshold * 1 / 3) print("误报率:", positive_rate) print("漏报率", negative_rate) @@ -501,7 +637,7 @@ def test_result(file_name: str = result_file_name): # result_data = np.concatenate([result_data, data], axis=0) print(result_data) print(result_data.shape) - plot_result(result_data) + plot_result_banda(result_data) def test_mse(mse_file_name: str = mse_file_name, max_file_name: str = max_file_name): @@ -514,13 +650,12 @@ def test_mse(mse_file_name: str = mse_file_name, max_file_name: str = max_file_n plot_MSE(mse_data, max_data) - def test_corr(file_name=source_path, N=10): # needed_data, label = read_data(file_name=file_name, isNew=False) # print(needed_data) # print(needed_data.shape) # np.save("corr.npy",needed_data) - needed_data=np.load("corr.npy") + needed_data = np.load("corr.npy") # plot_original_data(needed_data) person = plot_Corr(needed_data) person = np.array(person) @@ -563,6 +698,7 @@ def test_model_weight_l(model_name=model_name): pass + def test_model_weight_s(model_name=model_name): print("===============第一次===================") model = Joint_Monitoring_SE() @@ -581,7 +717,7 @@ def test_model_weight_s(model_name=model_name): def test_model_visualization(model_name=file_name): - with open(file_name, 'r', encoding='utf-8') as f: + with open(model_name, 'r', encoding='utf-8') as f: data = np.loadtxt(f, str, delimiter=',') needed_data = data[1:, 1:].astype(dtype=np.float) @@ -592,8 +728,9 @@ def test_model_visualization(model_name=file_name): if __name__ == '__main__': - # test_mse() - # test_result(file_name='E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\self_try\compare\mse\RNet_C\RNet_C_timestamp120_feature10_result2.csv') + test_mse() + # test_result( + # file_name='E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\self_try\compare\mse\ResNet\ResNet_banda_result1.csv') # test_corr() # acc() # list = [3.77, 2.64, 2.35, 2.05, 1.76, 1.09, 0.757, 0.82, 1.1, 0.58, 0, 0.03, 0.02] @@ -601,16 +738,19 @@ if __name__ == '__main__': # list=[99.99,98.95,99.95,96.1,95,99.65,76.25,72.64,75.87,68.74] # plot_FNR1(list) - # + # # # list=[3.43,1.99,1.92,2.17,1.63,1.81,1.78,1.8,0.6] # plot_FNR2(list) # 查看网络某一层的权重 - # test_model_visualization() - file_name = "../self_try\compare\model\weight\RNet_L_banda_epoch17_0.0086_0.0092/weight" - test_model_weight_l(file_name) - # file_name = "../self_try\compare\model\weight\RNet_S_epoch3_2.47_1.63/weight" + # test_model_visualization(model_name = "E:\跑模型\论文写作/SE.txt") + # file_name = "../self_try\compare\model\weight\RNet_L_banda\RNet_L_banda_epoch18_0.01_0.0087/weight" + # test_model_weight_l(file_name) + # file_name = "../self_try\compare\model\weight\RNet_s_banda\RNet_S_banda_epoch4_0.0034067521/weight" # test_model_weight_l(file_name) - #单独预测图 - # plot_mse() + # 单独预测图 + # plot_mse('E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\self_try\compare\mse\RNet_D/banda\RNet_D_banda_mse_predict1.csv') + + #画3d图 + # plot_3d() diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/Joint_Monitoring_banda.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/Joint_Monitoring_banda.py index 8d09ddc..893a23c 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/Joint_Monitoring_banda.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/Joint_Monitoring_banda.py @@ -40,7 +40,7 @@ save_name = "../hard_model/weight/{0}_epoch16_0.0009_0.0014/weight".format(model feature_num, batch_size, EPOCH) -save_step_two_name = "../hard_model/two_weight/{0}/weight".format(model_name, +save_step_two_name = "../hard_model/two_weight/{0}_epoch24_9875_9867/weight".format(model_name, time_stamp, feature_num, batch_size, @@ -56,6 +56,8 @@ save_step_two_name = "../hard_model/two_weight/{0}/weight".format(model_name, # feature_num, # batch_size, # EPOCH) + +save_mse_name=r"./compare/mse/JM_banda/{0}_mse.csv".format(model_name) '''文件名''' file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" @@ -488,7 +490,7 @@ def test(step_one_model, step_two_model, test_data, test_label1, test_label2): print("val_loss:", val_loss) -def showResult(step_two_model: Joint_Monitoring, test_data, isPlot: bool = False): +def showResult(step_two_model: Joint_Monitoring, test_data, isPlot: bool = False,isSave:bool=True): # 获取模型的所有参数的个数 # step_two_model.count_params() total_result = [] @@ -499,25 +501,41 @@ def showResult(step_two_model: Joint_Monitoring, test_data, isPlot: bool = False total_result.append(output4) total_result = np.reshape(total_result, [total_result.__len__(), -1]) total_result = np.reshape(total_result, [-1, ]) + + #误报率,漏报率,准确性的计算 + + + if isSave: + + np.savetxt(save_mse_name, total_result, delimiter=',') if isPlot: + plt.figure(1, figsize=(6.0, 2.68)) + plt.subplots_adjust(left=0.1, right=0.94, bottom=0.2, top=0.9, wspace=None, + hspace=None) + plt.tight_layout() + font1 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 10} # 设置坐标标签的字体大小,字体 + plt.scatter(list(range(total_result.shape[0])), total_result, c='black', s=10) # 画出 y=1 这条水平线 plt.axhline(0.5, c='red', label='Failure threshold') # 箭头指向上面的水平线 # plt.arrow(35000, 0.9, 33000, 0.75, head_width=0.02, head_length=0.1, shape="full", fc='red', ec='red', # alpha=0.9, overhang=0.5) - # plt.text(35000, 0.9, "Truth Fault", fontsize=10, color='black', verticalalignment='top') + plt.text(test_data.shape[0] * 2 / 3+1000, 0.7, "Truth Fault", fontsize=10, color='red', verticalalignment='top') plt.axvline(test_data.shape[0] * 2 / 3, c='blue', ls='-.') - plt.xlabel("time") - plt.ylabel("confience") + plt.xticks(range(6),('06/09/17','12/09/17','18/09/17','24/09/17','29/09/17')) # 设置x轴的标尺 + plt.tick_params() #设置轴显示 + plt.xlabel("time",fontdict=font1) + plt.ylabel("confience",fontdict=font1) plt.text(total_result.shape[0] * 4 / 5, 0.6, "Fault", fontsize=10, color='black', verticalalignment='top', horizontalalignment='center', bbox={'facecolor': 'grey', - 'pad': 10}) + 'pad': 10},fontdict=font1) plt.text(total_result.shape[0] * 1 / 3, 0.4, "Norm", fontsize=10, color='black', verticalalignment='top', horizontalalignment='center', bbox={'facecolor': 'grey', - 'pad': 10}) + 'pad': 10},fontdict=font1) + plt.grid() # plt.ylim(0, 1) # plt.xlim(-50, 1300) @@ -540,29 +558,29 @@ if __name__ == '__main__': # train_step_one(train_data=train_data_healthy, train_label1=train_label1_healthy, train_label2=train_label2_healthy) # 导入第一步已经训练好的模型,一个继续训练,一个只输出结果 - step_one_model = Joint_Monitoring() - step_one_model.load_weights(save_name) - - step_two_model = Joint_Monitoring() - step_two_model.load_weights(save_name) + # step_one_model = Joint_Monitoring() + # step_one_model.load_weights(save_name) + # + # step_two_model = Joint_Monitoring() + # step_two_model.load_weights(save_name) #### TODO 第二步训练 ### healthy_data.shape: (300333,120,10) ### unhealthy_data.shape: (16594,10) healthy_size, _, _ = train_data_healthy.shape unhealthy_size, _, _ = train_data_unhealthy.shape - train_data, train_label1, train_label2, test_data, test_label1, test_label2 = split_test_data( - healthy_data=train_data_healthy[healthy_size - 2 * unhealthy_size:, :, :], - healthy_label1=train_label1_healthy[healthy_size - 2 * unhealthy_size:, :], - healthy_label2=train_label2_healthy[healthy_size - 2 * unhealthy_size:, ], unhealthy_data=train_data_unhealthy, - unhealthy_label1=train_label1_unhealthy, unhealthy_label2=train_label2_unhealthy) - train_step_two(step_one_model=step_one_model, step_two_model=step_two_model, - train_data=train_data, - train_label1=train_label1, train_label2=np.expand_dims(train_label2, axis=-1)) + # train_data, train_label1, train_label2, test_data, test_label1, test_label2 = split_test_data( + # healthy_data=train_data_healthy[healthy_size - 2 * unhealthy_size:, :, :], + # healthy_label1=train_label1_healthy[healthy_size - 2 * unhealthy_size:, :], + # healthy_label2=train_label2_healthy[healthy_size - 2 * unhealthy_size:, ], unhealthy_data=train_data_unhealthy, + # unhealthy_label1=train_label1_unhealthy, unhealthy_label2=train_label2_unhealthy) + # train_step_two(step_one_model=step_one_model, step_two_model=step_two_model, + # train_data=train_data, + # train_label1=train_label1, train_label2=np.expand_dims(train_label2, axis=-1)) ### TODO 测试测试集 - step_one_model = Joint_Monitoring() - step_one_model.load_weights(save_name) + # step_one_model = Joint_Monitoring() + # step_one_model.load_weights(save_name) step_two_model = Joint_Monitoring() step_two_model.load_weights(save_step_two_name) # test(step_one_model=step_one_model, step_two_model=step_two_model, test_data=test_data, test_label1=test_label1, diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/DCConv.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/DCConv.py index 604561c..64dd99e 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/DCConv.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/DCConv.py @@ -2,16 +2,6 @@ # coding: utf-8 -''' -@Author : dingjiawen -@Date : 2022/10/11 18:53 -@Usage : -@Desc : -''' - -# -*- coding: utf-8 -*- - -# coding: utf-8 ''' @Author : dingjiawen @@ -20,9 +10,7 @@ @Desc : ''' -# -*- coding: utf-8 -*- -# coding: utf-8 import tensorflow as tf import tensorflow.keras import numpy as np @@ -30,7 +18,7 @@ import pandas as pd import matplotlib.pyplot as plt from model.DepthwiseCon1D.DepthwiseConv1D import DepthwiseConv1D from model.Dynamic_channelAttention.Dynamic_channelAttention import DynamicChannelAttention -from condition_monitoring.data_deal import loadData +from condition_monitoring.data_deal import loadData_daban as loadData from model.Joint_Monitoring.Joint_Monitoring3 import Joint_Monitoring from model.CommonFunction.CommonFunction import * @@ -42,16 +30,16 @@ import random '''超参数设置''' time_stamp = 120 feature_num = 10 -batch_size = 16 +batch_size = 32 learning_rate = 0.001 EPOCH = 101 -model_name = "DCConv" +model_name = "DCConv_banda" '''EWMA超参数''' K = 18 namuda = 0.01 '''保存名称''' -save_name = "./model/{0}_timestamp{1}_feature{2}.h5".format(model_name, +save_name = "./model/{0}_0.02_0.05.h5".format(model_name, time_stamp, feature_num, batch_size, @@ -61,12 +49,12 @@ save_step_two_name = "../hard_model/two_weight/{0}_timestamp{1}_feature{2}_weigh feature_num, batch_size, EPOCH) -save_mse_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_mse.csv".format(model_name, +save_mse_name = "./mse/DCConv/banda/mse.csv".format(model_name, time_stamp, feature_num, batch_size, EPOCH) -save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_name, +save_max_name = "./mse/DCConv/banda/max.csv".format(model_name, time_stamp, feature_num, batch_size, @@ -83,7 +71,8 @@ save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_nam # batch_size, # EPOCH) '''文件名''' -file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" +'''文件名''' +file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" ''' 文件说明:jb4q_8_delete_total_zero.csv是删除了只删除了全是0的列的文件 @@ -92,9 +81,9 @@ file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" ''' '''文件参数''' # 最后正常的时间点 -healthy_date = 415548 +healthy_date = 96748 # 最后异常的时间点 -unhealthy_date = 432153 +unhealthy_date = 107116 # 异常容忍程度 unhealthy_patience = 5 @@ -207,46 +196,7 @@ def EWMA(data, K=K, namuda=namuda): pass -def get_MSE(data, label, new_model, isStandard: bool = True, isPlot: bool = True): - predicted_data = new_model.predict(data) - temp = np.abs(predicted_data - label) - temp1 = (temp - np.broadcast_to(np.mean(temp, axis=0), shape=predicted_data.shape)) - temp2 = np.broadcast_to(np.sqrt(np.var(temp, axis=0)), shape=predicted_data.shape) - temp3 = temp1 / temp2 - mse = np.sum((temp1 / temp2) ** 2, axis=1) - print("z:", mse) - print(mse.shape) - - # mse=np.mean((predicted_data-label)**2,axis=1) - print("mse", mse) - if isStandard: - dims, = mse.shape - mean = np.mean(mse) - std = np.sqrt(np.var(mse)) - max = mean + 3 * std - print("max:", max) - # min = mean-3*std - max = np.broadcast_to(max, shape=[dims, ]) - # min = np.broadcast_to(min,shape=[dims,]) - mean = np.broadcast_to(mean, shape=[dims, ]) - if isPlot: - plt.figure(random.randint(1,9)) - plt.plot(max) - plt.plot(mse) - plt.plot(mean) - # plt.plot(min) - plt.show() - else: - if isPlot: - plt.figure(random.randint(1, 9)) - plt.plot(mse) - # plt.plot(min) - plt.show() - return mse - - return mse, mean, max - # pass def condition_monitoring_model(): @@ -383,6 +333,48 @@ def DCConv_Model(): pass +def get_MSE(data, label, new_model, isStandard: bool = True, isPlot: bool = True, predictI: int = 1): + predicted_data = new_model.predict(data) + + temp = np.abs(predicted_data - label) + temp1 = (temp - np.broadcast_to(np.mean(temp, axis=0), shape=predicted_data.shape)) + temp2 = np.broadcast_to(np.sqrt(np.var(temp, axis=0)), shape=predicted_data.shape) + temp3 = temp1 / temp2 + mse = np.sum((temp1 / temp2) ** 2, axis=1) + print("z:", mse) + print(mse.shape) + + # mse=np.mean((predicted_data-label)**2,axis=1) + print("mse", mse) + if isStandard: + dims, = mse.shape + mean = np.mean(mse) + std = np.sqrt(np.var(mse)) + max = mean + 3 * std + print("max:", max) + # min = mean-3*std + max = np.broadcast_to(max, shape=[dims, ]) + # min = np.broadcast_to(min,shape=[dims,]) + mean = np.broadcast_to(mean, shape=[dims, ]) + if isPlot: + plt.figure(random.randint(1,9)) + plt.plot(max) + plt.plot(mse) + plt.plot(mean) + # plt.plot(min) + plt.show() + else: + if isPlot: + plt.figure(random.randint(1, 9)) + plt.plot(mse) + # plt.plot(min) + plt.show() + return mse + + return mse, mean, max + # pass + + # healthy_data是健康数据,用于确定阈值,all_data是完整的数据,用于模型出结果 def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data, unhealthy_label, isPlot: bool = False, isSave: bool = True, predictI: int = 1): @@ -454,7 +446,7 @@ if __name__ == '__main__': model.compile(optimizer=tf.optimizers.Adam(0.01), loss=tf.losses.mse) model.summary() - early_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=3, mode='min', verbose=1) + early_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=20, mode='min', verbose=1) checkpoint = tf.keras.callbacks.ModelCheckpoint( filepath=save_name, @@ -463,8 +455,8 @@ if __name__ == '__main__': save_best_only=True, mode='min', period=1) - - # history = model.fit(train_data_healthy[:30000, :, :], train_label1_healthy[:30000, :], epochs=20, + # + # history = model.fit(train_data_healthy[:40000, :, :], train_label1_healthy[:40000, :], epochs=20, # batch_size=32, validation_split=0.2, shuffle=False, verbose=1, # callbacks=[checkpoint, early_stop]) # model.save(save_name) diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/GRU-CNN.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/GRU-CNN.py index 99c2c80..c08af50 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/GRU-CNN.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/GRU-CNN.py @@ -15,7 +15,7 @@ import pandas as pd import matplotlib.pyplot as plt from model.DepthwiseCon1D.DepthwiseConv1D import DepthwiseConv1D from model.Dynamic_channelAttention.Dynamic_channelAttention import DynamicChannelAttention -from condition_monitoring.data_deal import loadData +from condition_monitoring.data_deal import loadData_daban as loadData from model.Joint_Monitoring.Joint_Monitoring3 import Joint_Monitoring from model.CommonFunction.CommonFunction import * @@ -30,13 +30,13 @@ feature_num = 10 batch_size = 16 learning_rate = 0.001 EPOCH = 101 -model_name = "DCNN-GRU" +model_name = "CNN-GRU_banda" '''EWMA超参数''' K = 18 namuda = 0.01 '''保存名称''' -save_name = "./model/{0}_timestamp{1}_feature{2}.h5".format(model_name, +save_name = "./model/{0}_epoch1_0.023_0.04.h5".format(model_name, time_stamp, feature_num, batch_size, @@ -47,6 +47,17 @@ save_step_two_name = "../hard_model/two_weight/{0}_timestamp{1}_feature{2}_weigh batch_size, EPOCH) +save_mse_name = "./mse/CNN_GRU/banda/mse.csv".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) +save_max_name = "./mse/CNN_GRU/banda/max.csv".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) + # save_name = "../model/joint/{0}_timestamp{1}_feature{2}.h5".format(model_name, # time_stamp, # feature_num, @@ -58,7 +69,8 @@ save_step_two_name = "../hard_model/two_weight/{0}_timestamp{1}_feature{2}_weigh # batch_size, # EPOCH) '''文件名''' -file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" +'''文件名''' +file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" ''' 文件说明:jb4q_8_delete_total_zero.csv是删除了只删除了全是0的列的文件 @@ -67,13 +79,14 @@ file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" ''' '''文件参数''' # 最后正常的时间点 -healthy_date = 415548 +healthy_date = 96748 # 最后异常的时间点 -unhealthy_date = 432153 +unhealthy_date = 107116 # 异常容忍程度 unhealthy_patience = 5 + def remove(data, time_stamp=time_stamp): rows, cols = data.shape print("remove_data.shape:", data.shape) @@ -182,46 +195,6 @@ def EWMA(data, K=K, namuda=namuda): pass -def get_MSE(data, label, new_model, isStandard: bool = True, isPlot: bool = True): - predicted_data = new_model.predict(data) - - temp = np.abs(predicted_data - label) - temp1 = (temp - np.broadcast_to(np.mean(temp, axis=0), shape=predicted_data.shape)) - temp2 = np.broadcast_to(np.sqrt(np.var(temp, axis=0)), shape=predicted_data.shape) - temp3 = temp1 / temp2 - mse = np.sum((temp1 / temp2) ** 2, axis=1) - print("z:", mse) - print(mse.shape) - - # mse=np.mean((predicted_data-label)**2,axis=1) - print("mse", mse) - if isStandard: - dims, = mse.shape - mean = np.mean(mse) - std = np.sqrt(np.var(mse)) - max = mean + 3 * std - print("max:", max) - # min = mean-3*std - max = np.broadcast_to(max, shape=[dims, ]) - # min = np.broadcast_to(min,shape=[dims,]) - mean = np.broadcast_to(mean, shape=[dims, ]) - if isPlot: - plt.figure(random.randint(1,9)) - plt.plot(max) - plt.plot(mse) - plt.plot(mean) - # plt.plot(min) - plt.show() - else: - if isPlot: - plt.figure(random.randint(1, 9)) - plt.plot(mse) - # plt.plot(min) - plt.show() - return mse - - return mse, mean, max - # pass def condition_monitoring_model(): @@ -357,9 +330,52 @@ def CNN_GRU_Model(): pass +def get_MSE(data, label, new_model, isStandard: bool = True, isPlot: bool = True, predictI: int = 1): + predicted_data = new_model.predict(data) + + temp = np.abs(predicted_data - label) + temp1 = (temp - np.broadcast_to(np.mean(temp, axis=0), shape=predicted_data.shape)) + temp2 = np.broadcast_to(np.sqrt(np.var(temp, axis=0)), shape=predicted_data.shape) + temp3 = temp1 / temp2 + mse = np.sum((temp1 / temp2) ** 2, axis=1) + print("z:", mse) + print(mse.shape) + + # mse=np.mean((predicted_data-label)**2,axis=1) + print("mse", mse) + if isStandard: + dims, = mse.shape + mean = np.mean(mse) + std = np.sqrt(np.var(mse)) + max = mean + 3 * std + print("max:", max) + # min = mean-3*std + max = np.broadcast_to(max, shape=[dims, ]) + # min = np.broadcast_to(min,shape=[dims,]) + mean = np.broadcast_to(mean, shape=[dims, ]) + if isPlot: + plt.figure(random.randint(1,9)) + plt.plot(max) + plt.plot(mse) + plt.plot(mean) + # plt.plot(min) + plt.show() + else: + if isPlot: + plt.figure(random.randint(1, 9)) + plt.plot(mse) + # plt.plot(min) + plt.show() + return mse + + return mse, mean, max + # pass + + # healthy_data是健康数据,用于确定阈值,all_data是完整的数据,用于模型出结果 def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data, unhealthy_label, isPlot: bool = False, - isSave: bool = False): + isSave: bool = True, predictI: int = 1): + # TODO 计算MSE确定阈值 # TODO 计算MSE确定阈值 mse, mean, max = get_MSE(healthy_data, healthy_label, model) @@ -368,7 +384,7 @@ def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data total, = mse.shape faultNum = 0 faultList = [] - faultNum=mse[mse[:]>max[0]].__len__() + faultNum = mse[mse[:] > max[0]].__len__() # for i in range(total): # if (mse[i] > max[i]): # faultNum += 1 @@ -379,21 +395,37 @@ def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data # 漏报率计算 missNum = 0 - missList = [] mse1 = get_MSE(unhealthy_data, unhealthy_label, model, isStandard=False) + + total_mse = np.concatenate([mse, mse1], axis=0) + total_max = np.broadcast_to(max[0], shape=[total_mse.shape[0], ]) + # min = np.broadcast_to(min,shape=[dims,]) + total_mean = np.broadcast_to(mean[0], shape=[total_mse.shape[0], ]) + if isSave: + save_mse_name1 = save_mse_name + save_max_name1 = save_max_name + + np.savetxt(save_mse_name1, total_mse, delimiter=',') + np.savetxt(save_max_name1, total_max, delimiter=',') + all, = mse1.shape - flag=True - for i in range(all): - if (mse1[i] < max[0] and flag) : - missNum += 1 - missList.append(mse1[i]) - elif(mse1[i]>max[0]): - print(i) - flag=False - print("all:",all) + + missNum = mse1[mse1[:] < max[0]].__len__() + + + print("all:", all) miss_rate = missNum / all print("漏报率:", miss_rate) + + + + plt.figure(random.randint(1, 100)) + plt.plot(total_max) + plt.plot(total_mse) + plt.plot(total_mean) + # plt.plot(min) + plt.show() pass diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/GRU.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/GRU.py index f969f7e..92d2d1c 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/GRU.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/GRU.py @@ -16,7 +16,7 @@ import pandas as pd import matplotlib.pyplot as plt from model.DepthwiseCon1D.DepthwiseConv1D import DepthwiseConv1D from model.Dynamic_channelAttention.Dynamic_channelAttention import DynamicChannelAttention -from condition_monitoring.data_deal import loadData +from condition_monitoring.data_deal import loadData_daban as loadData from model.Joint_Monitoring.Joint_Monitoring3 import Joint_Monitoring from model.CommonFunction.CommonFunction import * @@ -28,16 +28,16 @@ import random '''超参数设置''' time_stamp = 120 feature_num = 10 -batch_size = 16 +batch_size = 32 learning_rate = 0.001 EPOCH = 101 -model_name = "GRU" +model_name = "GRU_banda" '''EWMA超参数''' K = 18 namuda = 0.01 '''保存名称''' -save_name = "./model/{0}_timestamp{1}_feature{2}_epoch1_loss0.005.h5".format(model_name, +save_name = "./model/{0}_epoch3_0.0138_0.0147.h5".format(model_name, time_stamp, feature_num, batch_size, @@ -47,12 +47,12 @@ save_step_two_name = "../hard_model/two_weight/{0}_timestamp{1}_feature{2}_weigh feature_num, batch_size, EPOCH) -save_mse_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_mse.csv".format(model_name, +save_mse_name = "./mse/GRU/banda/mse.csv".format(model_name, time_stamp, feature_num, batch_size, EPOCH) -save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_name, +save_max_name = "./mse/GRU/banda/max.csv".format(model_name, time_stamp, feature_num, batch_size, @@ -70,7 +70,8 @@ save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_nam # batch_size, # EPOCH) '''文件名''' -file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" +'''文件名''' +file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" ''' 文件说明:jb4q_8_delete_total_zero.csv是删除了只删除了全是0的列的文件 @@ -79,9 +80,9 @@ file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" ''' '''文件参数''' # 最后正常的时间点 -healthy_date = 415548 +healthy_date = 96748 # 最后异常的时间点 -unhealthy_date = 432153 +unhealthy_date = 107116 # 异常容忍程度 unhealthy_patience = 5 @@ -437,17 +438,17 @@ if __name__ == '__main__': # 单次测试 model = GRU_Model() - model.compile(optimizer=tf.optimizers.Adam(0.01), loss=tf.losses.mse) + model.compile(optimizer=tf.optimizers.Adam(0.001), loss=tf.losses.mse) model.summary() early_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5, mode='min', verbose=1) - # checkpoint = tf.keras.callbacks.ModelCheckpoint( - # filepath=save_name, - # monitor='val_loss', - # verbose=1, - # save_best_only=True, - # mode='min', - # period=1) + checkpoint = tf.keras.callbacks.ModelCheckpoint( + filepath=save_name, + monitor='val_loss', + verbose=1, + save_best_only=True, + mode='min', + period=1) # history = model.fit(train_data_healthy[:30000,:,:], train_label1_healthy[:30000,:], epochs=10, # batch_size=32,validation_split=0.2,shuffle=False, verbose=1,callbacks=[checkpoint,early_stop]) diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-C.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-C.py index c49af18..aa036de 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-C.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-C.py @@ -19,7 +19,7 @@ import pandas as pd import matplotlib.pyplot as plt from model.DepthwiseCon1D.DepthwiseConv1D import DepthwiseConv1D from model.Dynamic_channelAttention.Dynamic_channelAttention import DynamicChannelAttention -from condition_monitoring.data_deal import loadData +from condition_monitoring.data_deal import loadData_daban as loadData from model.Joint_Monitoring.compare.RNet_C import Joint_Monitoring from model.CommonFunction.CommonFunction import * @@ -33,51 +33,50 @@ feature_num = 10 batch_size = 16 learning_rate = 0.001 EPOCH = 101 -model_name = "RNet_C" +model_name = "RNet_C_banda" '''EWMA超参数''' K = 18 namuda = 0.01 '''保存名称''' save_name = "./model/weight/{0}/weight".format(model_name, - time_stamp, - feature_num, - batch_size, - EPOCH) -save_step_two_name = "./model/two_weight/{0}_weight_epoch5_9991_9995/weight".format(model_name, - time_stamp, - feature_num, - batch_size, - EPOCH) + time_stamp, + feature_num, + batch_size, + EPOCH) +save_step_two_name = "./model/two_weight/{0}_epoch15_986_985/weight".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) -save_mse_name = "./mse/RNet_C/{0}_timestamp{1}_feature{2}_result2.csv".format(model_name, - time_stamp, - feature_num, - batch_size, - EPOCH) -save_max_name = "./mse/RNet_C/{0}_timestamp{1}_feature{2}_max.csv".format(model_name, - time_stamp, - feature_num, - batch_size, - EPOCH) +save_mse_name = "./mse/{0}/{0}_result1.csv".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) +save_max_name = "./mse/{0}/{0}_max.csv".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) '''文件名''' -file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" +file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" ''' 文件说明:jb4q_8_delete_total_zero.csv是删除了只删除了全是0的列的文件 -文件从0:415548行均是正常值(2019/7.30 00:00:00 - 2019/9/18 11:14:00) -从415549:432153行均是异常值(2019/9/18 11:21:01 - 2021/1/18 00:00:00) +文件从0:96748行均是正常值(2019/12.30 00:00:00 - 2020/3/11 05:58:00) +从96748:107116行均是异常值(2020/3/11 05:58:01 - 2021/3/18 11:04:00) ''' '''文件参数''' # 最后正常的时间点 -healthy_date = 415548 +healthy_date = 96748 # 最后异常的时间点 -unhealthy_date = 432153 +unhealthy_date = 107116 # 异常容忍程度 unhealthy_patience = 5 - # 画图相关设置 font1 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 10} # 设置坐标标签的字体大小,字体 @@ -243,9 +242,6 @@ def EWMA(data, K=K, namuda=namuda): pass - - - def condition_monitoring_model(): input = tf.keras.Input(shape=[time_stamp, feature_num]) conv1 = tf.keras.layers.Conv1D(filters=256, kernel_size=1)(input) @@ -461,7 +457,7 @@ def test(step_one_model, step_two_model, test_data, test_label1, test_label2): print("val_loss:", val_loss) -def showResult(step_two_model: Joint_Monitoring, test_data, isPlot: bool = False,isSave:bool=True): +def showResult(step_two_model: Joint_Monitoring, test_data, isPlot: bool = False, isSave: bool = True): # 获取模型的所有参数的个数 # step_two_model.count_params() total_result = [] @@ -473,11 +469,9 @@ def showResult(step_two_model: Joint_Monitoring, test_data, isPlot: bool = False total_result = np.reshape(total_result, [total_result.__len__(), -1]) total_result = np.reshape(total_result, [-1, ]) - #误报率,漏报率,准确性的计算 - + # 误报率,漏报率,准确性的计算 if isSave: - np.savetxt(save_mse_name, total_result, delimiter=',') if isPlot: plt.figure(1, figsize=(6.0, 2.68)) @@ -492,20 +486,21 @@ def showResult(step_two_model: Joint_Monitoring, test_data, isPlot: bool = False # 箭头指向上面的水平线 # plt.arrow(35000, 0.9, 33000, 0.75, head_width=0.02, head_length=0.1, shape="full", fc='red', ec='red', # alpha=0.9, overhang=0.5) - plt.text(test_data.shape[0] * 2 / 3+1000, 0.7, "Truth Fault", fontsize=10, color='red', verticalalignment='top') + plt.text(test_data.shape[0] * 2 / 3 + 1000, 0.7, "Truth Fault", fontsize=10, color='red', + verticalalignment='top') plt.axvline(test_data.shape[0] * 2 / 3, c='blue', ls='-.') - plt.xticks(range(6),('06/09/17','12/09/17','18/09/17','24/09/17','29/09/17')) # 设置x轴的标尺 - plt.tick_params() #设置轴显示 - plt.xlabel("time",fontdict=font1) - plt.ylabel("confience",fontdict=font1) + plt.xticks(range(6), ('06/09/17', '12/09/17', '18/09/17', '24/09/17', '29/09/17')) # 设置x轴的标尺 + plt.tick_params() # 设置轴显示 + plt.xlabel("time", fontdict=font1) + plt.ylabel("confience", fontdict=font1) plt.text(total_result.shape[0] * 4 / 5, 0.6, "Fault", fontsize=10, color='black', verticalalignment='top', horizontalalignment='center', bbox={'facecolor': 'grey', - 'pad': 10},fontdict=font1) + 'pad': 10}, fontdict=font1) plt.text(total_result.shape[0] * 1 / 3, 0.4, "Norm", fontsize=10, color='black', verticalalignment='top', horizontalalignment='center', bbox={'facecolor': 'grey', - 'pad': 10},fontdict=font1) + 'pad': 10}, fontdict=font1) plt.grid() # plt.ylim(0, 1) @@ -598,7 +593,7 @@ def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data mseList, meanList, maxList = get_MSE(healthy_data, healthy_label, model) mse1List, _, _ = get_MSE(unhealthy_data, unhealthy_label, model, isStandard=False) - for mse, mean, max, mse1,j in zip(mseList, meanList, maxList, mse1List,range(3)): + for mse, mean, max, mse1, j in zip(mseList, meanList, maxList, mse1List, range(3)): # 误报率的计算 total, = mse.shape @@ -634,12 +629,11 @@ def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data total_mean = np.broadcast_to(mean[0], shape=[total_mse.shape[0], ]) if isSave: - save_mse_name1=save_mse_name[:-4]+"_predict"+str(j+1)+".csv" - save_max_name1=save_max_name[:-4]+"_predict"+str(j+1)+".csv" - - np.savetxt(save_mse_name1,total_mse, delimiter=',') - np.savetxt(save_max_name1,total_max, delimiter=',') + save_mse_name1 = save_mse_name[:-4] + "_predict" + str(j + 1) + ".csv" + save_max_name1 = save_max_name[:-4] + "_predict" + str(j + 1) + ".csv" + np.savetxt(save_mse_name1, total_mse, delimiter=',') + np.savetxt(save_max_name1, total_max, delimiter=',') plt.figure(random.randint(1, 100)) plt.plot(total_max) @@ -651,8 +645,8 @@ def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data if __name__ == '__main__': - # total_data = loadData.execute(N=feature_num, file_name=file_name) - total_data = np.load("G:\data\SCADA数据\靖边8号处理后的数据\原始10SCADA数据/total_data.npy") + total_data = loadData.execute(N=feature_num, file_name=file_name) + # total_data = np.load("G:\data\SCADA数据\靖边8号处理后的数据\原始10SCADA数据/total_data.npy") total_data = normalization(data=total_data) train_data_healthy, train_label1_healthy, train_label2_healthy = get_training_data_overlapping( total_data[:healthy_date, :], is_Healthy=True) @@ -665,10 +659,10 @@ if __name__ == '__main__': # train_step_one(train_data=train_data_healthy, train_label1=train_label1_healthy, train_label2=train_label2_healthy) # 导入第一步已经训练好的模型,一个继续训练,一个只输出结果 - # step_one_model = Joint_Monitoring() - # # step_one_model.load_weights(save_name) - # # - # step_two_model = Joint_Monitoring() + step_one_model = Joint_Monitoring() + # step_one_model.load_weights(save_name) + # + step_two_model = Joint_Monitoring() # step_two_model.load_weights(save_name) #### TODO 第二步训练 @@ -714,4 +708,4 @@ if __name__ == '__main__': # showResult(step_two_model, test_data=all_data[:32], isPlot=True) showResult(step_two_model, test_data=all_data, isPlot=True) - pass \ No newline at end of file + pass diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-D.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-D.py index fdf88c3..1d6b319 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-D.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-D.py @@ -16,8 +16,8 @@ import pandas as pd import matplotlib.pyplot as plt from model.DepthwiseCon1D.DepthwiseConv1D import DepthwiseConv1D from model.Dynamic_channelAttention.Dynamic_channelAttention import DynamicChannelAttention -from condition_monitoring.data_deal import loadData -from model.Joint_Monitoring.Joint_Monitoring3 import Joint_Monitoring +from condition_monitoring.data_deal import loadData_daban as loadData +from model.Joint_Monitoring.Joint_Monitoring_banda import Joint_Monitoring from model.CommonFunction.CommonFunction import * from sklearn.model_selection import train_test_split @@ -27,15 +27,15 @@ import random '''超参数设置''' time_stamp = 120 feature_num = 10 -batch_size = 16 +batch_size = 32 learning_rate = 0.001 EPOCH = 101 -model_name = "RNet_D" +model_name = "RNet_D_banda" '''EWMA超参数''' K = 18 namuda = 0.01 '''保存名称''' -save_name = "E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\hard_model\weight\joint_timestamp120_feature10_weight_epoch11_0.0077/weight" +save_name = "E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\hard_model\weight/banda_joint_epoch16_0.0009_0.0014/weight" # save_name = "../hard_model/weight/{0}_timestamp{1}_feature{2}_weight_epoch11_0.0077/weight".format(model_name, # time_stamp, # feature_num, @@ -47,12 +47,12 @@ save_step_two_name = "../hard_model/two_weight/{0}_timestamp{1}_feature{2}_weigh batch_size, EPOCH) -save_mse_name = "./mse/RNet_D/{0}_timestamp{1}_feature{2}_mse.csv".format(model_name, +save_mse_name = "./mse/RNet_D/banda/{0}_mse.csv".format(model_name, time_stamp, feature_num, batch_size, EPOCH) -save_max_name = "./mse/RNet_D/{0}_timestamp{1}_feature{2}_max.csv".format(model_name, +save_max_name = "./mse/RNet_D/banda/{0}_max.csv".format(model_name, time_stamp, feature_num, batch_size, @@ -69,7 +69,8 @@ save_max_name = "./mse/RNet_D/{0}_timestamp{1}_feature{2}_max.csv".format(model_ # batch_size, # EPOCH) '''文件名''' -file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" +'''文件名''' +file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" ''' 文件说明:jb4q_8_delete_total_zero.csv是删除了只删除了全是0的列的文件 @@ -78,9 +79,9 @@ file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" ''' '''文件参数''' # 最后正常的时间点 -healthy_date = 415548 +healthy_date = 96748 # 最后异常的时间点 -unhealthy_date = 432153 +unhealthy_date = 107116 # 异常容忍程度 unhealthy_patience = 5 diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-L.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-L.py index 6fbc67a..a50b628 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-L.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-L.py @@ -38,27 +38,27 @@ K = 18 namuda = 0.01 '''保存名称''' -save_name = "./model/weight/{0}/weight".format(model_name, - time_stamp, - feature_num, - batch_size, - EPOCH) +save_name = "./model/weight/{0}/RNet_L_banda_epoch18_0.01_0.0087/weight".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) save_step_two_name = "./model/two_weight/{0}_timestamp{1}_feature{2}_weight/weight".format(model_name, time_stamp, feature_num, batch_size, EPOCH) -save_mse_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_mse.csv".format(model_name, - time_stamp, - feature_num, - batch_size, - EPOCH) -save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_name, - time_stamp, - feature_num, - batch_size, - EPOCH) +save_mse_name = "./mse/RNet_L/banda/{0}_mse.csv".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) +save_max_name = "./mse/RNet_L/banda/{0}_max.csv".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) # save_name = "../model/joint/{0}_timestamp{1}_feature{2}.h5".format(model_name, # time_stamp, @@ -642,7 +642,7 @@ if __name__ == '__main__': # train_step_one(train_data=train_data_healthy[:256, :, :], train_label1=train_label1_healthy[:256, :], # train_label2=train_label2_healthy[:256, ]) #### 模型训练 - train_step_one(train_data=train_data_healthy, train_label1=train_label1_healthy, train_label2=train_label2_healthy) + # train_step_one(train_data=train_data_healthy, train_label1=train_label1_healthy, train_label2=train_label2_healthy) # 导入第一步已经训练好的模型,一个继续训练,一个只输出结果 step_one_model = Joint_Monitoring() @@ -670,12 +670,5 @@ if __name__ == '__main__': healthy_label=train_label1_healthy[healthy_size - 2 * unhealthy_size:, :], unhealthy_data=train_data_unhealthy, unhealthy_label=train_label1_unhealthy, isSave=True) - # ###TODO 展示全部的结果 - # all_data, _, _ = get_training_data_overlapping( - # total_data[healthy_size - 2 * unhealthy_size:unhealthy_date, :], is_Healthy=True) - # # all_data = np.concatenate([]) - # # 单次测试 - # # showResult(step_two_model, test_data=all_data[:32], isPlot=True) - # showResult(step_two_model, test_data=all_data, isPlot=True) pass diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-MSE.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-MSE.py index 0f126c8..138245d 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-MSE.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-MSE.py @@ -16,7 +16,7 @@ import pandas as pd import matplotlib.pyplot as plt from model.DepthwiseCon1D.DepthwiseConv1D import DepthwiseConv1D from model.Dynamic_channelAttention.Dynamic_channelAttention import DynamicChannelAttention -from condition_monitoring.data_deal import loadData +from condition_monitoring.data_deal import loadData_daban as loadData from model.Joint_Monitoring.compare.RNet_MSE import Joint_Monitoring from model.CommonFunction.CommonFunction import * @@ -27,16 +27,16 @@ import random '''超参数设置''' time_stamp = 120 feature_num = 10 -batch_size = 16 +batch_size = 32 learning_rate = 0.001 EPOCH = 101 -model_name = "RNet_MSE" +model_name = "RNet_MSE_banda" '''EWMA超参数''' K = 18 namuda = 0.01 '''保存名称''' -save_name = "./model/weight/{0}_weight/weight".format(model_name, +save_name = "./model/weight/{0}/RNet_MSE_banda_epoch17_0.0030094008/weight".format(model_name, time_stamp, feature_num, batch_size, @@ -48,12 +48,12 @@ save_step_two_name = "./model/two_weight/{0}_timestamp{1}_feature{2}_weight/weig EPOCH) -save_mse_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_mse.csv".format(model_name, +save_mse_name = "./mse/RNet_MSE/banda/{0}_mse.csv".format(model_name, time_stamp, feature_num, batch_size, EPOCH) -save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_name, +save_max_name = "./mse/RNet_MSE/banda/{0}_max.csv".format(model_name, time_stamp, feature_num, batch_size, @@ -69,7 +69,8 @@ save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_nam # batch_size, # EPOCH) '''文件名''' -file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" +'''文件名''' +file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" ''' 文件说明:jb4q_8_delete_total_zero.csv是删除了只删除了全是0的列的文件 @@ -78,9 +79,9 @@ file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" ''' '''文件参数''' # 最后正常的时间点 -healthy_date = 415548 +healthy_date = 96748 # 最后异常的时间点 -unhealthy_date = 432153 +unhealthy_date = 107116 # 异常容忍程度 unhealthy_patience = 5 @@ -348,7 +349,7 @@ def train_step_one(train_data, train_label1, train_label2): k = k + 1 val_loss, val_accuracy = model.get_val_loss(val_data=val_data, val_label1=val_label1, val_label2=val_label2, is_first_time=True) - SaveBestModel(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy()) + SaveBestModel(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy(),epoch=epoch,is_all=True) # SaveBestH5Model(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy()) history_val_loss.append(val_loss) history_loss.append(loss_value.numpy()) @@ -625,7 +626,7 @@ def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data if __name__ == '__main__': total_data = loadData.execute(N=feature_num, file_name=file_name) - np.save("G:\data\SCADA数据/total_data.npy",total_data) + # np.save("G:\data\SCADA数据/total_data.npy",total_data) total_data = normalization(data=total_data) train_data_healthy, train_label1_healthy, train_label2_healthy = get_training_data_overlapping( total_data[:healthy_date, :], is_Healthy=True) @@ -633,7 +634,7 @@ if __name__ == '__main__': total_data[healthy_date - time_stamp + unhealthy_patience:unhealthy_date, :], is_Healthy=False) #### TODO 第一步训练 - # 单次测试 + # # 单次测试 # train_step_one(train_data=train_data_healthy[:256, :, :], train_label1=train_label1_healthy[:256, :], # train_label2=train_label2_healthy[:256, ]) #### 模型训练 diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-S.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-S.py index eb985a6..942adee 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-S.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet-S.py @@ -16,7 +16,7 @@ import pandas as pd import matplotlib.pyplot as plt from model.DepthwiseCon1D.DepthwiseConv1D import DepthwiseConv1D from model.Dynamic_channelAttention.Dynamic_channelAttention import DynamicChannelAttention -from condition_monitoring.data_deal import loadData +from condition_monitoring.data_deal import loadData_daban as loadData from model.Joint_Monitoring.compare.RNet_S import Joint_Monitoring from model.CommonFunction.CommonFunction import * @@ -27,16 +27,16 @@ import random '''超参数设置''' time_stamp = 120 feature_num = 10 -batch_size = 16 +batch_size = 32 learning_rate = 0.001 EPOCH = 101 -model_name = "RNet_S" +model_name = "RNet_S_banda" '''EWMA超参数''' K = 18 namuda = 0.01 '''保存名称''' # save_name = "E:\self_example\TensorFlow_eaxmple\Model_train_test\condition_monitoring\hard_model\weight\joint_timestamp120_feature10_weight_epoch11_0.0077/weight" -save_name = "./model/weight/{0}_epoch3_2.47_1.63/weight".format(model_name, +save_name = "./model/weight/{0}/RNet_S_banda_epoch17_0.0017712703/weight".format(model_name, time_stamp, feature_num, batch_size, @@ -47,12 +47,12 @@ save_step_two_name = "../hard_model/two_weight/{0}_timestamp{1}_feature{2}_weigh batch_size, EPOCH) -save_mse_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_mse.csv".format(model_name, +save_mse_name = "./mse/RNet_S/banda/{0}_mse.csv".format(model_name, time_stamp, feature_num, batch_size, EPOCH) -save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_name, +save_max_name = "./mse/RNet_S/banda/{0}_max.csv".format(model_name, time_stamp, feature_num, batch_size, @@ -69,7 +69,8 @@ save_max_name = "./mse/{0}/{0}_timestamp{1}_feature{2}_max.csv".format(model_nam # batch_size, # EPOCH) '''文件名''' -file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" +'''文件名''' +file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" ''' 文件说明:jb4q_8_delete_total_zero.csv是删除了只删除了全是0的列的文件 @@ -78,9 +79,9 @@ file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" ''' '''文件参数''' # 最后正常的时间点 -healthy_date = 415548 +healthy_date = 96748 # 最后异常的时间点 -unhealthy_date = 432153 +unhealthy_date = 107116 # 异常容忍程度 unhealthy_patience = 5 @@ -360,7 +361,7 @@ def train_step_one(train_data, train_label1, train_label2): k = k + 1 val_loss, val_accuracy = model.get_val_loss(val_data=val_data, val_label1=val_label1, val_label2=val_label2, is_first_time=True) - SaveBestModel(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy()) + SaveBestModel(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy(),epoch=epoch,is_all=True) # SaveBestH5Model(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy()) history_val_loss.append(val_loss) history_loss.append(loss_value.numpy()) @@ -635,8 +636,8 @@ def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data if __name__ == '__main__': - # total_data = loadData.execute(N=feature_num, file_name=file_name) - total_data=np.load("G:\data\SCADA数据\靖边8号处理后的数据\原始10SCADA数据/total_data.npy") + total_data = loadData.execute(N=feature_num, file_name=file_name) + # total_data=np.load("G:\data\SCADA数据\靖边8号处理后的数据\原始10SCADA数据/total_data.npy") total_data = normalization(data=total_data) train_data_healthy, train_label1_healthy, train_label2_healthy = get_training_data_overlapping( total_data[:healthy_date, :], is_Healthy=True) @@ -645,7 +646,7 @@ if __name__ == '__main__': is_Healthy=False) #### TODO 第一步训练 # 单次测试 - # train_step_one(train_data=train_data_healthy[:32, :, :], train_label1=train_label1_healthy[:32, :],train_label2=train_label2_healthy[:32, ]) + # train_step_one(train_data=train_data_healthy[:128, :, :], train_label1=train_label1_healthy[:128, :],train_label2=train_label2_healthy[:128, ]) # train_step_one(train_data=train_data_healthy, train_label1=train_label1_healthy, train_label2=train_label2_healthy) # 导入第一步已经训练好的模型,一个继续训练,一个只输出结果 @@ -672,13 +673,6 @@ if __name__ == '__main__': healthy_label=train_label1_healthy[healthy_size - 2 * unhealthy_size:, :], unhealthy_data=train_data_unhealthy, unhealthy_label=train_label1_unhealthy, isSave=True) - ###TODO 展示全部的结果 - # all_data, _, _ = get_training_data_overlapping( - # total_data[healthy_size - 2 * unhealthy_size:unhealthy_date, :], is_Healthy=True) - # all_data = np.concatenate([]) - # 单次测试 - # showResult(step_two_model, test_data=all_data[:32], isPlot=True) - # showResult(step_two_model, test_data=all_data, isPlot=True) pass diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet.py index 9d55188..93d7827 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/RNet.py @@ -16,7 +16,7 @@ import pandas as pd import matplotlib.pyplot as plt from model.DepthwiseCon1D.DepthwiseConv1D import DepthwiseConv1D from model.Dynamic_channelAttention.Dynamic_channelAttention import DynamicChannelAttention -from condition_monitoring.data_deal import loadData +from condition_monitoring.data_deal import loadData_daban as loadData from model.Joint_Monitoring.compare.RNet import Joint_Monitoring from model.CommonFunction.CommonFunction import * @@ -36,16 +36,26 @@ K = 18 namuda = 0.01 '''保存名称''' -save_name = "./model/weight/{0}/weight".format(model_name, - time_stamp, - feature_num, - batch_size, - EPOCH) +save_name = "./model/weight/{0}/RNet_banda_epoch4_0.0070075933/weight".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) save_step_two_name = "./model/two_weight/{0}_timestamp{1}_feature{2}_weight/weight".format(model_name, time_stamp, feature_num, batch_size, EPOCH) +save_mse_name = "./mse/RNet/banda/{0}_mse.csv".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) +save_max_name = "./mse/RNet/banda/{0}_max.csv".format(model_name, + time_stamp, + feature_num, + batch_size, + EPOCH) # save_name = "../model/joint/{0}_timestamp{1}_feature{2}.h5".format(model_name, # time_stamp, @@ -132,57 +142,7 @@ def get_training_data_overlapping(data, time_stamp: int = time_stamp, is_Healthy return train_data, train_label, train_label2 -# RepConv重参数化卷积 -def RepConv(input_tensor, k=3): - _, _, output_dim = input_tensor.shape - conv1 = tf.keras.layers.Conv1D(filters=output_dim, kernel_size=k, strides=1, padding='SAME')(input_tensor) - b1 = tf.keras.layers.BatchNormalization()(conv1) - conv2 = tf.keras.layers.Conv1D(filters=output_dim, kernel_size=1, strides=1, padding='SAME')(input_tensor) - b2 = tf.keras.layers.BatchNormalization()(conv2) - - b3 = tf.keras.layers.BatchNormalization()(input_tensor) - - out = tf.keras.layers.Add()([b1, b2, b3]) - out = tf.nn.relu(out) - return out - - -# RepBlock模块 -def RepBlock(input_tensor, num: int = 3): - for i in range(num): - input_tensor = RepConv(input_tensor) - return input_tensor - - -# GAP 全局平均池化 -def Global_avg_channelAttention(input_tensor): - _, length, channel = input_tensor.shape - DWC1 = DepthwiseConv1D(kernel_size=1, padding='SAME')(input_tensor) - GAP = tf.keras.layers.GlobalAvgPool1D()(DWC1) - c1 = tf.keras.layers.Conv1D(filters=channel, kernel_size=1, padding='SAME')(GAP) - s1 = tf.nn.sigmoid(c1) - output = tf.multiply(input_tensor, s1) - return output - - -# GDP 全局动态池化 -def Global_Dynamic_channelAttention(input_tensor): - _, length, channel = input_tensor.shape - DWC1 = DepthwiseConv1D(kernel_size=1, padding='SAME')(input_tensor) - - # GAP - GAP = tf.keras.layers.GlobalAvgPool1D()(DWC1) - c1 = tf.keras.layers.Conv1D(filters=channel, kernel_size=1, padding='SAME')(GAP) - s1 = tf.nn.sigmoid(c1) - - # GMP - GMP = tf.keras.layers.GlobalMaxPool1D()(DWC1) - c2 = tf.keras.layers.Conv1D(filters=channel, kernel_size=1, padding='SAME')(GMP) - s3 = tf.nn.sigmoid(c2) - - output = tf.multiply(input_tensor, s1) - return output # 归一化 @@ -338,7 +298,8 @@ def train_step_one(train_data, train_label1, train_label2): k = k + 1 val_loss, val_accuracy = model.get_val_loss(val_data=val_data, val_label1=val_label1, val_label2=val_label2, is_first_time=True) - SaveBestModel(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy()) + SaveBestModel(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy(), + epoch=epoch, is_all=True) # SaveBestH5Model(model=model, save_name=save_name, history_loss=history_val_loss, loss_value=val_loss.numpy()) history_val_loss.append(val_loss) history_loss.append(loss_value.numpy()) @@ -501,12 +462,12 @@ def get_MSE(data, label, new_model, isStandard: bool = True, isPlot: bool = True predict_data = 0 predict_data = predicted_data1 - mseList=[] - meanList=[] - maxList=[] + mseList = [] + meanList = [] + maxList = [] - for i in range(1,4): - print("i:",i) + for i in range(1, 4): + print("i:", i) if i == 1: predict_data = predicted_data1 elif i == 2: @@ -519,7 +480,7 @@ def get_MSE(data, label, new_model, isStandard: bool = True, isPlot: bool = True temp3 = temp1 / temp2 mse = np.sum((temp1 / temp2) ** 2, axis=1) - print("mse.shape:",mse.shape) + print("mse.shape:", mse.shape) # mse=np.mean((predicted_data-label)**2,axis=1) # print("mse", mse) mseList.append(mse) @@ -549,7 +510,6 @@ def get_MSE(data, label, new_model, isStandard: bool = True, isPlot: bool = True # plt.plot(min) plt.show() - return mseList, meanList, maxList # pass @@ -558,11 +518,11 @@ def get_MSE(data, label, new_model, isStandard: bool = True, isPlot: bool = True def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data, unhealthy_label, isPlot: bool = False, isSave: bool = False, predictI: int = 1): # TODO 计算MSE确定阈值 - + # plt.ion() mseList, meanList, maxList = get_MSE(healthy_data, healthy_label, model) - mse1List,_,_ = get_MSE(unhealthy_data, unhealthy_label, model, isStandard=False) + mse1List, _, _ = get_MSE(unhealthy_data, unhealthy_label, model, isStandard=False) - for mse,mean,max,mse1 in zip(mseList, meanList, maxList,mse1List): + for mse, mean, max, mse1, j in zip(mseList, meanList, maxList, mse1List, range(3)): # 误报率的计算 total, = mse.shape @@ -588,16 +548,23 @@ def getResult(model: tf.keras.Model, healthy_data, healthy_label, unhealthy_data miss_rate = missNum / all print("漏报率:", miss_rate) - #总体图 - print("mse:",mse) - print("mse1:",mse1) + # 总体图 + # print("mse:", mse) + # print("mse1:", mse1) print("============================================") total_mse = np.concatenate([mse, mse1], axis=0) total_max = np.broadcast_to(max[0], shape=[total_mse.shape[0], ]) # min = np.broadcast_to(min,shape=[dims,]) total_mean = np.broadcast_to(mean[0], shape=[total_mse.shape[0], ]) - plt.figure(random.randint(1, 9)) + if isSave: + save_mse_name1 = save_mse_name[:-4] + "_predict" + str(j + 1) + ".csv" + save_max_name1 = save_max_name[:-4] + "_predict" + str(j + 1) + ".csv" + + np.savetxt(save_mse_name1, total_mse, delimiter=',') + np.savetxt(save_max_name1, total_max, delimiter=',') + + plt.figure(random.randint(1, 100)) plt.plot(total_max) plt.plot(total_mse) plt.plot(total_mean) @@ -618,7 +585,7 @@ if __name__ == '__main__': # 单次测试 # train_step_one(train_data=train_data_healthy[:256, :, :], train_label1=train_label1_healthy[:256, :], # train_label2=train_label2_healthy[:256, ]) - #### 模型训练 + # #### 模型训练 # train_step_one(train_data=train_data_healthy, train_label1=train_label1_healthy, train_label2=train_label2_healthy) # 导入第一步已经训练好的模型,一个继续训练,一个只输出结果 @@ -640,11 +607,11 @@ if __name__ == '__main__': # :], # healthy_label=train_label1_healthy[ # healthy_size - 2 * unhealthy_size:healthy_size - 2 * unhealthy_size + 200, :], - # unhealthy_data=train_data_unhealthy[:200,:], unhealthy_label=train_label1_unhealthy[:200,:]) + # unhealthy_data=train_data_unhealthy[:200,:], unhealthy_label=train_label1_unhealthy[:200,:], isSave=True) getResult(step_one_model, healthy_data=train_data_healthy[healthy_size - 2 * unhealthy_size:, :], healthy_label=train_label1_healthy[healthy_size - 2 * unhealthy_size:, :], - unhealthy_data=train_data_unhealthy, unhealthy_label=train_label1_unhealthy) + unhealthy_data=train_data_unhealthy, unhealthy_label=train_label1_unhealthy, isSave=True) # ###TODO 展示全部的结果 # all_data, _, _ = get_training_data_overlapping( diff --git a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/resnet_18.py b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/resnet_18.py index 5f3b55c..4d73859 100644 --- a/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/resnet_18.py +++ b/TensorFlow_eaxmple/Model_train_test/condition_monitoring/self_try/compare/resnet_18.py @@ -6,33 +6,33 @@ from sklearn.manifold import TSNE from sklearn.metrics import confusion_matrix import seaborn as sns from sklearn.model_selection import train_test_split -from condition_monitoring.data_deal import loadData +from condition_monitoring.data_deal import loadData_daban as loadData from keras.callbacks import EarlyStopping '''超参数设置''' time_stamp = 120 feature_num = 10 -batch_size = 16 +batch_size = 32 learning_rate = 0.001 EPOCH = 101 -model_name = "ResNet" +model_name = "ResNet_banda" '''EWMA超参数''' K = 18 namuda = 0.01 '''保存名称''' -save_name = "./model/{0}_9990_9998.h5".format(model_name, +save_name = "./model/{0}.h5".format(model_name, time_stamp, feature_num, batch_size, EPOCH) -save_step_two_name = "./model/two_weight/{0}_weight_epoch6_99899_9996/weight".format(model_name, +save_step_two_name = "./model/two_weight/{0}/weight".format(model_name, time_stamp, feature_num, batch_size, EPOCH) -save_mse_name = "./mse/ResNet/{0}_timestamp{1}_feature{2}_result.csv".format(model_name, +save_mse_name = "./mse/ResNet/{0}_result.csv".format(model_name, time_stamp, feature_num, batch_size, @@ -42,13 +42,14 @@ save_max_name = "./mse/ResNet/{0}_timestamp{1}_feature{2}_max.csv".format(model_ feature_num, batch_size, EPOCH) -save_mse_name1 = "./mse/ResNet/{0}_timestamp{1}_feature{2}_totalresult.csv".format(model_name, +save_mse_name1 = "./mse/ResNet/{0}_result1.csv".format(model_name, time_stamp, feature_num, batch_size, EPOCH) '''文件名''' -file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" +'''文件名''' +file_name = "G:\data\SCADA数据\SCADA_已处理_粤水电达坂城2020.1月-5月\风机15.csv" ''' 文件说明:jb4q_8_delete_total_zero.csv是删除了只删除了全是0的列的文件 @@ -57,9 +58,9 @@ file_name = "G:\data\SCADA数据\jb4q_8_delete_total_zero.csv" ''' '''文件参数''' # 最后正常的时间点 -healthy_date = 415548 +healthy_date = 96748 # 最后异常的时间点 -unhealthy_date = 432153 +unhealthy_date = 107116 # 异常容忍程度 unhealthy_patience = 5 @@ -357,8 +358,8 @@ def plot_confusion_matrix_accuracy(cls, true_labels, predict_labels): if __name__ == '__main__': # # 数据读入 # - # total_data = loadData.execute(N=feature_num, file_name=file_name) - total_data = np.load("G:\data\SCADA数据\靖边8号处理后的数据\原始10SCADA数据/total_data.npy") + total_data = loadData.execute(N=feature_num, file_name=file_name) + # total_data = np.load("G:\data\SCADA数据\靖边8号处理后的数据\原始10SCADA数据/total_data.npy") total_data = normalization(data=total_data) train_data_healthy, train_label1_healthy, train_label2_healthy = get_training_data_overlapping( total_data[:healthy_date, :], is_Healthy=True) @@ -381,27 +382,29 @@ if __name__ == '__main__': model.compile(optimizer=tf.optimizers.Adam(), loss=tf.losses.binary_crossentropy, metrics=['acc']) model.summary() - # early_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5, mode='min', verbose=1) - # - # checkpoint = tf.keras.callbacks.ModelCheckpoint( - # filepath=save_name, - # monitor='val_loss', - # verbose=1, - # save_best_only=False, - # mode='min', - # period=1) - # + early_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5, mode='min', verbose=1) + + checkpoint = tf.keras.callbacks.ModelCheckpoint( + filepath=save_name, + monitor='val_loss', + verbose=1, + save_best_only=False, + mode='min', + period=1) + # history = model.fit(train_data, train_label, epochs=20, batch_size=16, validation_data=(test_data, test_label), # callbacks=[checkpoint, early_stop]) # model.save("./model/ResNet.h5") - model = tf.keras.models.load_model("./model/ResNet_2_9906.h5") + model = tf.keras.models.load_model("model/ResNet_banda/ResNet_banda_epoch12_9942.h5") # 结果展示 healthy_size, _, _ = train_data_healthy.shape unhealthy_size, _, _ = train_data_unhealthy.shape + # all_data, _, _ = get_training_data_overlapping( + # total_data[0:, :], is_Healthy=True) all_data, _, _ = get_training_data_overlapping( - total_data[0:, :], is_Healthy=True) + total_data[healthy_size - 2 * unhealthy_size:unhealthy_date, :], is_Healthy=True) showResult(model, test_data=all_data, isPlot=True)