설명 없음

v2.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Csar Fdez, UdL, 2025
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import datetime
  5. import numpy as np
  6. import keras
  7. import os.path
  8. import pickle
  9. from keras import layers
  10. from optparse import OptionParser
  11. # facility type 5. Mural cerrado de congelación (closed freezer). Set point at -18 (we will have two possible setpoints, -18 and -26)
  12. # This code only deals with a given failure type
  13. # Data for abnormal functioning corresponds to Condenser Fan failure
  14. parser = OptionParser()
  15. parser.add_option("-t", "--train", dest="train", help="Trains the models (false)", default=False, action="store_true")
  16. (options, args) = parser.parse_args()
  17. normal_datafiles_list=['2025-01-09_5_','2025-01-10_5_','2025-01-11_5_']
  18. anormal_datafiles_list=['2025-01-04_5_','2025-01-05_5_','2025-01-06_5_','2025-01-07_5_']
  19. # Features suggested by Xavier
  20. features=['r1 s1','r1 s4','r1 s5','pa1 apiii']
  21. NumFeatures=len(features)
  22. df_list=[]
  23. for f in normal_datafiles_list:
  24. #df1 = pd.read_csv('./data/'+f+'.csv', parse_dates=['datetime'], dayfirst=True, index_col='datetime')
  25. df1 = pd.read_csv('./data/'+f+'.csv')
  26. df_list.append(df1)
  27. df=pd.concat(df_list)
  28. datalength=df.shape[0]
  29. # subsampled to 5' = 30 * 10"
  30. # We consider smaples every 5' because in production, we will only have data at this frequency
  31. subsamplingrate=30
  32. subsamplingrate=30
  33. normaldataframe=df.iloc[range(0,datalength,subsamplingrate)][features]
  34. normaldataframe.reset_index(inplace=True,drop=True)
  35. df_list=[]
  36. for f in anormal_datafiles_list:
  37. #df1 = pd.read_csv('./data/'+f+'.csv', parse_dates=['datetime'], dayfirst=True, index_col='datetime')
  38. df1 = pd.read_csv('./data/'+f+'.csv')
  39. df_list.append(df1)
  40. df=pd.concat(df_list)
  41. datalength=df.shape[0]
  42. # subsampled to 5' = 30 * 10"
  43. anormaldataframe=df.iloc[range(0,datalength,subsamplingrate)][features]
  44. anormaldataframe.reset_index(inplace=True,drop=True)
  45. # Train data is first 2/3 of normaldata
  46. # Test data is: last 1/3 of normaldata + anormaldata + last 1/3 of normaldata
  47. dataTrain=normaldataframe.values[0:int(normaldataframe.shape[0]*2/3),:]
  48. dataTest=np.vstack((normaldataframe.values[int(normaldataframe.shape[0]*2/3)+1:,:],anormaldataframe.values, normaldataframe.values[int(normaldataframe.shape[0]*2/3)+1:,:] ))
  49. def normalize2():
  50. # merges train and test
  51. means=[]
  52. stdevs=[]
  53. for i in range(NumFeatures):
  54. means.append(dataTrain[:,i].mean())
  55. stdevs.append(dataTrain[:,i].std())
  56. return( (dataTrain-means)/stdevs, (dataTest-means)/stdevs )
  57. (dataTrainNorm,dataTestNorm)=normalize2()
  58. TIME_STEPS = 24
  59. def create_sequences(values, time_steps=TIME_STEPS):
  60. output = []
  61. for i in range(len(values) - time_steps + 1):
  62. output.append(values[i : (i + time_steps)])
  63. return np.stack(output)
  64. x_train = create_sequences(dataTrainNorm)
  65. model = keras.Sequential(
  66. [
  67. layers.Input(shape=(x_train.shape[1], x_train.shape[2])),
  68. layers.Conv1D(
  69. filters=64,
  70. kernel_size=7,
  71. padding="same",
  72. strides=2,
  73. activation="relu",
  74. ),
  75. layers.Dropout(rate=0.2),
  76. layers.Conv1D(
  77. filters=32,
  78. kernel_size=7,
  79. padding="same",
  80. strides=2,
  81. activation="relu",
  82. ),
  83. layers.Conv1DTranspose(
  84. filters=32,
  85. kernel_size=7,
  86. padding="same",
  87. strides=2,
  88. activation="relu",
  89. ),
  90. layers.Dropout(rate=0.2),
  91. layers.Conv1DTranspose(
  92. filters=64,
  93. kernel_size=7,
  94. padding="same",
  95. strides=2,
  96. activation="relu",
  97. ),
  98. layers.Conv1DTranspose(filters=x_train.shape[2], kernel_size=7, padding="same"),
  99. ]
  100. )
  101. model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
  102. model.summary()
  103. path_checkpoint = "model._checkpoint.weights.h5"
  104. es_callback = keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=15)
  105. modelckpt_callback = keras.callbacks.ModelCheckpoint(
  106. monitor="val_loss",
  107. filepath=path_checkpoint,
  108. verbose=1,
  109. save_weights_only=True,
  110. save_best_only=True,
  111. )
  112. if options.train:
  113. history = model.fit(
  114. x_train,
  115. x_train,
  116. epochs=400,
  117. batch_size=128,
  118. validation_split=0.3,
  119. callbacks=[ es_callback, modelckpt_callback ],
  120. )
  121. plt.plot(history.history["loss"], label="Training Loss")
  122. plt.plot(history.history["val_loss"], label="Validation Loss")
  123. plt.legend()
  124. plt.show()
  125. else:
  126. model.load_weights(path_checkpoint)
  127. x_train_pred = model.predict(x_train)
  128. train_mae_loss = np.mean(np.abs(x_train_pred - x_train), axis=1)
  129. threshold = np.max(train_mae_loss,axis=0)
  130. print("Threshold : ",threshold)
  131. threshold=threshold*2
  132. # Threshold is enlarged because, otherwise, for subsamples at 5' have many false positives
  133. x_test = create_sequences(dataTestNorm)
  134. x_test_pred = model.predict(x_test)
  135. test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
  136. anomalies = test_mae_loss > threshold
  137. anomalous_data_indices = []
  138. for i in range(anomalies.shape[0]):
  139. if anomalies[i][0] or anomalies[i][1]:
  140. anomalous_data_indices.append(i)
  141. #print(anomalous_data_indices)
  142. # Let's plot only a couple of features
  143. def plotData2():
  144. fig, axes = plt.subplots(
  145. nrows=2, ncols=1, figsize=(15, 20), dpi=80, facecolor="w", edgecolor="k",sharex=True
  146. )
  147. axes[0].plot(range(len(x_train)),x_train[:,0,0],label="normal")
  148. axes[0].plot(range(len(x_train),len(x_train)+len(x_test)),x_test[:,0,0],label="abnormal")
  149. axes[0].plot(len(x_train)+np.array(anomalous_data_indices),x_test[anomalous_data_indices,0,0],color='red',marker='.',linewidth=0,label="abnormal detection")
  150. axes[0].legend()
  151. axes[1].plot(range(len(x_train)),x_train[:,0,1],label="normal")
  152. axes[1].plot(range(len(x_train),len(x_train)+len(x_test)),x_test[:,0,1],label="abnormal")
  153. axes[1].plot(len(x_train)+np.array(anomalous_data_indices),x_test[anomalous_data_indices,0,1],color='red',marker='.',linewidth=0,label="abnormal detection")
  154. axes[1].legend()
  155. axes[0].set_ylabel(features[0])
  156. axes[1].set_ylabel(features[1])
  157. plt.show()
  158. plotData2()

Powered by TurnKey Linux.