-
Notifications
You must be signed in to change notification settings - Fork 13
/
NILM_test.py
267 lines (211 loc) · 10.8 KB
/
NILM_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import numpy as np
import tensorflow as tf
import tensorflow.keras.backend as K
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import os
import datetime
import argparse
from VAE_functions import *
from NILM_functions import *
import pickle
from scipy.stats import norm
from keras.utils.vis_utils import plot_model
from dtw import *
import logging
import json
logging.getLogger('tensorflow').disabled = True
###############################################################################
# Config
###############################################################################
parser = argparse.ArgumentParser()
parser.add_argument("--gpu", default=0, type=int, help="GPU to use")
parser.add_argument("--config", default="", type=str, help="Path to the config file")
parser.add_argument("--time", default="", type=str, help="Folder name containing runs")
parser.add_argument("--save_pred", default=True, type=bool, help="Save y_pred_all")
parser.add_argument("--previous", default=1, type=int, help="Select previous experiment")
a = parser.parse_args()
# Select GPU
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = str(a.gpu)
thr_house_2 = { "Fridge" : 50,
"WashingMachine" : 20,
"Dishwasher" : 100,
"Kettle" : 100,
"Microwave" : 200}
with open(a.config) as data_file:
nilm = json.load(data_file)
#print(nilm)
np.random.seed(123)
name = "NILM_Disag_{}".format(nilm["appliance"])
previous = a.previous
if a.time == "":
files_and_directories = os.listdir("{}/ukdale/{}/logs/model/House_{}/".format(name, nilm["model"], nilm["dataset"]["test"]["house"][0]))
time = np.sort(files_and_directories)[-previous]
else:
time = a.time
# Get exact configuration for this experiment
with open("{}/ukdale/{}/logs/model/House_{}/{}/config.txt".format(name, nilm["model"], nilm["dataset"]["test"]["house"][0], time)) as data_file:
nilm = json.load(data_file)
print(nilm)
epochs = nilm["training"]["epoch"]
start = nilm["training"]["start_stopping"]
print("###############################################################################")
print("NILM DISAGREGATOR")
print("GPU : {}".format(a.gpu))
print("CONFIG : {}".format(a.config))
print("FOLDER : {}".format(time))
print("###############################################################################")
###############################################################################
# Load history files
###############################################################################
hist = []
for r in range(1, nilm["run"]+1):
#hist.append(np.load("{}/ukdale/{}/logs/model/{}/{}/history_cb_{}.npy".format(name, nilm["model"], time, r, epochs), allow_pickle=True))
try:
hist.append(np.load("{}/ukdale/{}/logs/model/House_{}/{}/{}/history.npy".format(name, nilm["model"], nilm["dataset"]["test"]["house"][0], time, r), allow_pickle=True))
except:
hist.append(np.load("{}/ukdale/{}/logs/model/House_{}/{}/{}/history_{}.npy".format(name, nilm["model"], nilm["dataset"]["test"]["house"][0], time, r, epochs), allow_pickle=True))
MAE_run = []
for r in range(len(hist)):
pos_val_min = np.argmin(hist[r].all()["val_mean_absolute_error"][start:epochs])
MAE_run.append(hist[r].all()["val_mean_absolute_error"][pos_val_min+start])
print("Result : {} ± {}".format(np.mean(MAE_run), np.std(MAE_run)))
###############################################################################
# Create Model
###############################################################################
###############################################################################
# Optimizer
###############################################################################
def get_optimizer(opt):
if opt == "adam":
return tf.keras.optimizers.Adam(0.001)
else:
return tf.keras.optimizers.RMSprop(0.001)
if nilm["model"] == "VAE":
model = create_model(nilm["model"], nilm["config"], nilm["preprocessing"]["width"], optimizer=get_optimizer(nilm["training"]["optimizer"]))
elif nilm["model"] == "DAE":
model = create_model(nilm["model"], nilm["config"], nilm["preprocessing"]["width"], optimizer="adam")
elif nilm["model"] == "S2P":
model = create_model(nilm["model"], nilm["config"], nilm["preprocessing"]["width"], optimizer=tf.keras.optimizers.Adam(learning_rate=nilm["training"]["lr"], beta_1=0.9, beta_2=0.999))
elif nilm["model"] == "S2S":
model = create_model(nilm["model"], nilm["config"], nilm["preprocessing"]["width"], optimizer=tf.keras.optimizers.Adam(learning_rate=nilm["training"]["lr"], beta_1=0.9, beta_2=0.999))
app_list = nilm["appliance"]
width = nilm["preprocessing"]["width"]
stride = nilm["preprocessing"]["strides"]
main_mean = nilm["preprocessing"]["main_mean"]
main_std = nilm["preprocessing"]["main_std"]
app_mean = nilm["preprocessing"]["app_mean"]
app_std = nilm["preprocessing"]["app_std"]
def transform_s2p(x, y, width=199):
x_s2p, y_s2p = [], []
for i in range(x.shape[0]):
for t in range(width):
x_s2p.append(x[i,t:t+width,0])
y_s2p.append(y[i,t+width//2+1,0])
x_s2p = np.array(x_s2p)
y_s2p = np.array(y_s2p)
print("Shape before S2P data transformations : {}, {}".format(x.shape, y.shape))
print("Shape after S2P data transformations : {}, {}".format(x_s2p.shape, y_s2p.shape))
return x_s2p, y_s2p
MAE_run = []
ACC_run =[]
PR_run = []
RE_run = []
F1_run = []
SAE_run = []
#Load Data
if nilm["dataset"]["test"]["ratio"][0] < 0.1:
nilm["dataset"]["test"]["ratio"] = [1]
if "batch" in nilm["dataset"]["test"]:
batch_int = np.arange(0, nilm["dataset"]["test"]["ratio"][0], nilm["dataset"]["test"]["batch"][0])
else:
nilm["dataset"]["test"]["batch"] = nilm["dataset"]["test"]["ratio"]
batch_int = np.arange(0, nilm["dataset"]["test"]["ratio"][0], nilm["dataset"]["test"]["batch"][0])
print(batch_int)
for r in range(1, nilm["run"]+1):
x_total = []
y_total_pred = []
y_total_true = []
for test_from in batch_int:
x_test, y_test = load_data(nilm["model"], nilm["appliance"], nilm["dataset"], nilm["preprocessing"]["width"], nilm["preprocessing"]["strides"], test_from=test_from, set_type="test")
print(x_test.shape, y_test.shape)
pos_val_min = np.argmin(hist[r-1].all()["val_mean_absolute_error"][start:epochs]) + start
if nilm["training"]["save_best"]:
model.load_weights("{}/ukdale/{}/logs/model/House_{}/{}/{}/checkpoint.ckpt".format(name, nilm["model"], nilm["dataset"]["test"]["house"][0], time, r))
else:
model.load_weights("{}/ukdale/{}/logs/model/House_{}/{}/{}/cp-{epoch:04d}.ckpt".format(name, nilm["model"], nilm["dataset"]["test"]["house"][0], time, r, epoch=pos_val_min+1))
if nilm["model"] == "S2P":
x_test_s2p, y_test_s2p = transform_s2p(x_test, y_test, width)
y_pred = model.predict([(x_test_s2p-main_mean)/main_std], verbose=1)
y_all_pred = y_pred.reshape([-1])*app_std+app_mean
x_all = x_test_s2p[:,(width+1)//2].reshape([-1])
y_all_true = y_test_s2p.reshape([-1])
del x_test_s2p
del y_test_s2p
elif nilm["model"] == "VAE":
y_pred = model.predict([(x_test-main_mean)/main_std], verbose=1)
y_all_pred = reconstruct(y_pred[:]*app_std+app_mean, width, stride, "median")
x_all = reconstruct(x_test[:], width, stride, "median")
y_all_true = reconstruct(y_test[:], width, stride, "median")
elif nilm["model"] == "S2S":
y_pred = model.predict([(x_test-main_mean)/main_std], verbose=1)
y_all_pred = reconstruct(y_pred[:]*app_std+app_mean, width, stride)
x_all = reconstruct(x_test[:], width, stride)
y_all_true = reconstruct(y_test[:], width, stride)
elif nilm["model"] == "DAE":
y_pred = model.predict([(x_test-main_mean)/main_std], verbose=1)
y_all_pred = reconstruct(y_pred[:]*app_std+app_mean, width, stride)
x_all = reconstruct(x_test[:], width, stride)
y_all_true = reconstruct(y_test[:], width, stride)
#y_all_true[y_all_true<15] = 0
y_all_pred[y_all_pred<15] = 0
#print(x_all.shape, y_all_pred.shape, y_all_true.shape)
x_all = x_all.reshape([1,-1])
y_all_pred = y_all_pred.reshape([1,-1])
y_all_true = y_all_true.reshape([1,-1])
###############################################################################
# Completed sequence
###############################################################################
for i in range(x_all.shape[-1]):
x_total.append(x_all[0,i])
for i in range(y_all_pred.shape[-1]):
y_total_pred.append(y_all_pred[0,i])
for i in range(y_all_true.shape[-1]):
y_total_true.append(y_all_true[0,i])
#print(len(x_total))
del x_all
del y_all_pred
del y_all_true
###############################################################################
# Transform in array
###############################################################################
x_total = np.array(x_total).reshape([1,-1])
y_total_pred = np.array(y_total_pred).reshape([1,-1])
y_total_true = np.array(y_total_true).reshape([1,-1])
if a.save_pred:
np.save("{}/ukdale/{}/logs/model/House_{}/{}/pred_{}.npy".format(name, nilm["model"], nilm["dataset"]["test"]["house"][0], time, r), [x_total, y_total_pred, y_total_true])
print("Best Epoch : {}".format(pos_val_min+1))
MAE_tot, MAE_app, MAE = MAE_metric(y_total_pred, y_total_true, disaggregation=True, only_power_on=False)
acc_P_tot, acc_P_app, acc_P = acc_Power(y_total_pred, y_total_true, disaggregation=True)
PR_app = PR_metric(y_total_pred, y_total_true, thr=thr_house_2[nilm["appliance"]])
RE_app = RE_metric(y_total_pred, y_total_true, thr=thr_house_2[nilm["appliance"]])
F1_app = F1_metric(y_total_pred, y_total_true, thr=thr_house_2[nilm["appliance"]])
SAE_app = SAE_metric(y_total_pred, y_total_true)
if (np.isnan(acc_P_tot)) or (F1_app[0] == 0):
print("Error Detected")
else:
MAE_run.append(MAE_tot)
ACC_run.append(acc_P_tot)
PR_run.append(PR_app[0])
RE_run.append(RE_app[0])
F1_run.append(F1_app[0])
SAE_run.append(SAE_app[0])
print(np.mean(MAE_run), np.std(MAE_run))
print(np.mean(ACC_run), np.std(ACC_run))
print(np.mean(PR_run), np.std(PR_run))
print(np.mean(RE_run), np.std(RE_run))
print(np.mean(F1_run), np.std(F1_run))
print(np.mean(SAE_run), np.std(SAE_run))
np.save("{}/ukdale/{}/logs/model/House_{}/{}/results_median.npy".format(name, nilm["model"], nilm["dataset"]["test"]["house"][0], time), [MAE_run, ACC_run, PR_run, RE_run, F1_run, SAE_run])