import numpy as np
import os
import time
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
'''
-----------------------------------------------------------------------------------------
Network Name Inference Latency Time / FPS
-----------------------------------------------------------------------------------------
mobilenet-opencl 24.60 ms 2.89 ms / 345.84 fps
Correctness: PASS, max_error: 1.0642305824148934e-05, max_abs_error: 7.450580596923828e-07, fail_ratio: 0.0
mobilenet-cpu 411.35 ms 408.90 ms / 2.45 fps
Correctness: PASS, max_error: 2.0418785425135866e-05, max_abs_error: 1.5869736671447754e-06, fail_ratio: 0.0
'''
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
import keras
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, losses
from tensorflow.keras.models import Model
df = pd.read_csv('./anoicos_prep.csv')
df['time'] = pd.to_datetime(df['time'], format="%Y-%m-%d %H:%M")
df = pd.pivot_table(df, index=['time']) + 0.000000001
df = df.fillna(df.mean())
Using plaidml.keras.backend backend.
# 컬럼명 생성을 위한 코드테이블 - hard coding
operation_suffix = ['_ST', '_SF_ST', '_RF_ST']
#열원설비
hsource_suffix = ['_RTD','_STD']
hsource = ['M2.CH1', 'M2.CH1_1', 'M2.CH1_2', 'M3.CH2', 'PH_3.CT1', 'PH_2.CT2', 'PH_1.CT3', 'M4.HE']
#공조기
AHU_suffix = ['_SAT', '_RAT', '_RAH', '_EAD', '_OAT', '_OAD', '_MAT', '_MAD', '_CSV', '_CV', '_HV']
AHU = ['S2F.AHU4', 'S3F_1.AHU3F', 'S4F_1.AHU4F', 'S5F_1.AHU5F', 'S6F_1.AHU6F', 'S7F_1.AHU7F', 'S8F_1.AHU8F', 'S9F_1.AHU9F']
# 규칙에 따라 컬럼명을 조합하고, 컬럼명이 데이터프레임에 있는지 확인후 저장
cols = []
operation = []
for prefix in hsource+AHU:
for suffix in hsource_suffix+AHU_suffix:
target = prefix+suffix
if target in df.columns:
cols.append(target)
for suffix in operation_suffix:
opl = prefix+suffix
if opl in df.columns: operation.append(opl)
# 라벨 생성 : 원하는 데이터들이 동작 여부가 모두 참 일경우 참으로 기록
df['label'] = (df[operation].sum(axis=1) >= len(operation)-17)
# 필요한 데이터만 필터링
cols.append('label')
df_prem = df[cols]
raw_data = df_prem.values
# display(df.head(10))
print('테이블 사이즈', df_prem.shape)
테이블 사이즈 (70158, 102)
# The last element contains the labels
labels = raw_data[:, -1]
# The other data points are the electrocadriogram data
data = raw_data[:, :-1]
train_data, test_data, train_labels, test_labels = train_test_split(data, labels, test_size=0.2, random_state=21)
whole = len(labels)
tr = len(train_labels)
te = len(test_labels)
print('총합 {}, 총합-훈련데이터={}, 훈련데이터 {}, 테스트 데이터 {}'.format(whole, whole-tr, tr, te))
print('훈련라벨 True = {}, 테스트 라벨 True = {}'.format(train_labels.sum(), test_labels.sum()))
# keras 업데이트후 텐서와 어레이간에 타입불일치생김. float 으로 명시적으로 변환해줌으로써 해결
train_data = train_data.astype(float)
test_data = test_data.astype(float)
train_labels = train_labels.astype(float)
test_labels = test_labels.astype(float)
총합 70158, 총합-훈련데이터=14032, 훈련데이터 56126, 테스트 데이터 14032 훈련라벨 True = 27022, 테스트 라벨 True = 6658
train_data = tf.cast(train_data, tf.float32)
test_data = tf.cast(test_data, tf.float32)
min_val = tf.reduce_min(train_data)
max_val = tf.reduce_max(train_data)
train_data = (train_data - min_val) / (max_val - min_val)
test_data = (test_data - min_val) / (max_val - min_val)
train_labels = train_labels.astype(bool)
test_labels = test_labels.astype(bool)
print (len(train_labels), len(test_labels))
normal_train_data = train_data[train_labels]
normal_test_data = test_data[test_labels]
print (normal_train_data.shape, normal_test_data.shape)
anomalous_train_data = train_data[~train_labels]
anomalous_test_data = test_data[~test_labels]
print (anomalous_train_data.shape, anomalous_test_data.shape)
56126 14032 (27022, 101) (6658, 101) (29104, 101) (7374, 101)
plt.figure(figsize=(14,8))
plt.grid()
# plt.plot(np.arange(101), normal_train_data[0])
# plt.plot(np.arange(101), anomalous_train_data[0])
plt.plot(np.arange(101), normal_train_data.numpy().mean(axis=0))
plt.plot(np.arange(101), anomalous_train_data.numpy().mean(axis=0))
plt.title("Normal/anomal Data compare")
plt.show()
class AnomalyDetector(Model):
def __init__(self):
super(AnomalyDetector, self).__init__()
self.encoder = tf.keras.Sequential([
layers.Dense(32, activation="relu"),
layers.Dense(16, activation="relu"),
layers.Dense(8, activation="relu")])
self.decoder = tf.keras.Sequential([
layers.Dense(16, activation="relu"),
layers.Dense(32, activation="relu"),
layers.Dense(101, activation="sigmoid")])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
autoencoder = AnomalyDetector()
autoencoder.compile(optimizer='adam', loss='mae')
class CustomCallback(tf.keras.callbacks.Callback):
def on_test_begin(self, logs=None):
keys = list(logs.keys())
print("Start testing; got log keys: {}".format(keys))
def on_test_end(self, logs=None):
keys = list(logs.keys())
print("Stop testing; got log keys: {}".format(keys))
def on_predict_begin(self, logs=None):
keys = list(logs.keys())
print("Start predicting; got log keys: {}".format(keys))
def on_predict_end(self, logs=None):
keys = list(logs.keys())
print("Stop predicting; got log keys: {}".format(keys))
def on_test_batch_begin(self, batch, logs=None):
keys = list(logs.keys())
print("...Evaluating: start of batch {}; got log keys: {}".format(batch, keys))
def on_test_batch_end(self, batch, logs=None):
keys = list(logs.keys())
print("...Evaluating: end of batch {}; got log keys: {}".format(batch, keys))
def on_predict_batch_begin(self, batch, logs=None):
keys = list(logs.keys())
print("...Predicting: start of batch {}; got log keys: {}".format(batch, keys))
def on_predict_batch_end(self, batch, logs=None):
keys = list(logs.keys())
print("...Predicting: end of batch {}; got log keys: {}".format(batch, keys))
history = autoencoder.fit (normal_train_data, normal_train_data,
epochs=50,
batch_size=64,
validation_data=(normal_test_data, normal_test_data),
shuffle=True)
plt.figure(figsize=(14,8))
plt.plot(history.history["loss"], label="Training Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.legend()
<matplotlib.legend.Legend at 0x29e46debf70>
encoded_imgs = autoencoder.encoder(normal_test_data).numpy()
decoded_imgs = autoencoder.decoder(encoded_imgs).numpy()
plt.figure(figsize=(14,8))
plt.plot(normal_test_data.numpy().mean(axis=0), 'b')
plt.plot(decoded_imgs.mean(axis=0), 'r')
plt.fill_between(np.arange(101), decoded_imgs.mean(axis=0), normal_test_data.numpy().mean(axis=0), color='lightcoral')
plt.legend(labels=["Input", "Reconstruction", "Error"])
plt.show()
encoded_imgs = autoencoder.encoder(anomalous_test_data).numpy()
decoded_imgs = autoencoder.decoder(encoded_imgs).numpy()
plt.figure(figsize=(14,8))
plt.plot(anomalous_test_data.numpy().mean(axis=0), 'b')
plt.plot(decoded_imgs.mean(axis=0), 'r')
plt.fill_between(np.arange(101), decoded_imgs.mean(axis=0), anomalous_test_data.numpy().mean(axis=0), color='lightcoral')
plt.legend(labels=["Input", "Reconstruction", "Error"])
plt.show()