-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindowgrudisaggregatorDot.py
481 lines (391 loc) · 17.9 KB
/
windowgrudisaggregatorDot.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
from __future__ import print_function, division
from warnings import warn, filterwarnings
from matplotlib import rcParams
import matplotlib.pyplot as plt
import random
import sys
import pandas as pd
import numpy as np
import h5py
import tensorflow as tf
from tensorflow.keras import Input,Model
from tensorflow.keras.layers import Attention,Conv1D,Dense,Embedding,Flatten,GlobalAveragePooling1D,GRU, Bidirectional
from tensorflow.keras.models import Sequential,load_model
from tensorflow.keras.utils import plot_model
from nilmtk.timeframe import merge_timeframes, TimeFrame
from datetime import datetime
from nilmtk.utils import find_nearest
from nilmtk.feature_detectors import cluster
from nilmtk.legacy.disaggregate import Disaggregator
from nilmtk.datastore import HDFDataStore
tf.random.set_seed(0)
class WindowGRUDisaggregator(Disaggregator):
'''Attempt to create a RNN Disaggregator
Attributes
----------
model : keras Sequential model
mmax : the maximum value of the aggregate data
MIN_CHUNK_LENGTH : int
the minimum length of an acceptable chunk
'''
def __init__(self, window_size=100):
'''Initialize disaggregator
'''
self.MODEL_NAME = "WindowGRU"
self.mmax = None
self.MIN_CHUNK_LENGTH = window_size
self.window_size = window_size
self.model = self._create_model()
def train(self, mains, meter, epochs=1, batch_size=512, start=None, end=None, **load_kwargs):
'''Train
Parameters
----------
mains : a nilmtk.ElecMeter object for the aggregate data
meter : a nilmtk.ElecMeter object for the meter data
epochs : number of epochs to train
batch_size : size of batch used for training
**load_kwargs : keyword arguments passed to `meter.power_series()`
'''
main_power_series = mains.power_series(**load_kwargs)
meter_power_series = meter.power_series(**load_kwargs)
# Train chunks
run = True
mainchunk = next(main_power_series)
meterchunk = next(meter_power_series)
if self.mmax == None:
self.mmax = mainchunk.max()
while(run):
if (end != None):
mainchunk = mainchunk[:end]
meterchunk = meterchunk[:end]
mainchunk = self._normalize(mainchunk, self.mmax)
meterchunk = self._normalize(meterchunk, self.mmax)
self.train_on_chunk(mainchunk, meterchunk, epochs, batch_size)
try:
mainchunk = next(main_power_series)
meterchunk = next(meter_power_series)
except:
run = False
def train_on_chunk(self, mainchunk, meterchunk, epochs, batch_size):
'''Train using only one chunk
Parameters
----------
mainchunk : chunk of site meter
meterchunk : chunk of appliance
epochs : number of epochs for training
batch_size : size of batch used for training
'''
# Replace NaNs with 0s
mainchunk.fillna(0, inplace=True)
meterchunk.fillna(0, inplace=True)
ix = mainchunk.index.intersection(meterchunk.index)
mainchunk = np.array(mainchunk[ix])
meterchunk = np.array(meterchunk[ix])
indexer = np.arange(self.window_size)[None, :] + np.arange(len(mainchunk)-self.window_size+1)[:, None]
mainchunk = mainchunk[indexer]
meterchunk = meterchunk[self.window_size-1:]
mainchunk = np.reshape(mainchunk, (mainchunk.shape[0], mainchunk.shape[1],1))
self.model.fit(mainchunk, meterchunk, epochs=epochs, batch_size=batch_size, shuffle=True)
def train_across_buildings(self, mainlist, meterlist, epochs=1, batch_size=128, **load_kwargs):
'''Train using data from multiple buildings
Parameters
----------
mainlist : a list of nilmtk.ElecMeter objects for the aggregate data of each building
meterlist : a list of nilmtk.ElecMeter objects for the meter data of each building
batch_size : size of batch used for training
epochs : number of epochs to train
**load_kwargs : keyword arguments passed to `meter.power_series()`
'''
assert len(mainlist) == len(meterlist), "Number of main and meter channels should be equal"
num_meters = len(mainlist)
mainps = [None] * num_meters
meterps = [None] * num_meters
mainchunks = [None] * num_meters
meterchunks = [None] * num_meters
# Get generators of timeseries
for i,m in enumerate(mainlist):
mainps[i] = m.power_series(**load_kwargs)
for i,m in enumerate(meterlist):
meterps[i] = m.power_series(**load_kwargs)
# Get a chunk of data
for i in range(num_meters):
mainchunks[i] = next(mainps[i])
meterchunks[i] = next(meterps[i])
if self.mmax == None:
self.mmax = max([m.max() for m in mainchunks])
run = True
while(run):
# Normalize and train
mainchunks = [self._normalize(m, self.mmax) for m in mainchunks]
meterchunks = [self._normalize(m, self.mmax) for m in meterchunks]
self.train_across_buildings_chunk_CONCAT_method(mainchunks, meterchunks, epochs, batch_size)
# If more chunks, repeat
try:
for i in range(num_meters):
mainchunks[i] = next(mainps[i])
meterchunks[i] = next(meterps[i])
except:
run = False
# Below are different implementations of a function with the same purpose.
# 'train_across_buildings_chunk_CONCAT_method' was actually used for the final experiments
def train_across_buildings_chunk(self, mainchunks, meterchunks, epochs, batch_size):
'''Train using only one chunk of data. This chunk consists of data from
all buildings.
Parameters
----------
mainchunk : chunk of site meter
meterchunk : chunk of appliance
epochs : number of epochs for training
batch_size : size of batch used for training
'''
num_meters = len(mainchunks)
batch_size = int(batch_size/num_meters)
num_of_batches = [None] * num_meters
# Find common parts of timeseries
for i in range(num_meters):
mainchunks[i].fillna(0, inplace=True)
meterchunks[i].fillna(0, inplace=True)
ix = mainchunks[i].index.intersection(meterchunks[i].index)
m1 = mainchunks[i]
m2 = meterchunks[i]
mainchunks[i] = m1[ix]
meterchunks[i] = m2[ix]
indexer = np.arange(self.window_size)[None, :] + np.arange(len(mainchunks[i].values)-self.window_size+1)[:, None]
mainchunks[i] = mainchunks[i].values[indexer]
meterchunks[i] = meterchunks[i].values[self.window_size-1:]
num_of_batches[i] = int(len(ix)/batch_size) - 1
for e in range(epochs): # Iterate for every epoch
print(e)
batch_indexes = range(min(num_of_batches))
random.shuffle(batch_indexes)
for bi, b in enumerate(batch_indexes): # Iterate for every batch
print("Batch {} of {}".format(bi,min(num_of_batches)), end="\r")
sys.stdout.flush()
X_batch = np.empty((batch_size*num_meters, self.window_size, 1))
Y_batch = np.empty((batch_size*num_meters, 1))
# Create a batch out of data from all buildings
for i in range(num_meters):
mainpart = mainchunks[i]
meterpart = meterchunks[i]
mainpart = mainpart[b*batch_size:(b+1)*batch_size]
meterpart = meterpart[b*batch_size:(b+1)*batch_size]
X = np.reshape(mainpart, (batch_size, self.window_size, 1))
Y = np.reshape(meterpart, (batch_size, 1))
X_batch[i*batch_size:(i+1)*batch_size] = np.array(X)
Y_batch[i*batch_size:(i+1)*batch_size] = np.array(Y)
# Shuffle data
p = np.random.permutation(len(X_batch))
X_batch, Y_batch = X_batch[p], Y_batch[p]
# Train model
self.model.train_on_batch(X_batch, Y_batch)
print("\n")
def train_across_buildings_chunk_2(self, mainchunks, meterchunks, epochs, batch_size):
'''Train using only one chunk of data. This chunk consists of data from
all buildings.
Parameters
----------
mainchunk : chunk of site meter
meterchunk : chunk of appliance
epochs : number of epochs for training
batch_size : size of batch used for training
'''
num_meters = len(mainchunks)
batch_size = int(batch_size/num_meters)
num_of_batches = [None] * num_meters
# Find common parts of timeseries
for i in range(num_meters):
mainchunks[i].fillna(0, inplace=True)
meterchunks[i].fillna(0, inplace=True)
ix = mainchunks[i].index.intersection(meterchunks[i].index)
m1 = mainchunks[i]
m2 = meterchunks[i]
mainchunks[i] = m1[ix]
meterchunks[i] = m2[ix]
indexer = np.arange(self.window_size)[None, :] + np.arange(len(mainchunks[i].values)-self.window_size+1)[:, None]
mainchunks[i] = mainchunks[i].values[indexer]
meterchunks[i] = meterchunks[i].values[self.window_size-1:]
num_of_batches[i] = int(len(ix)/batch_size) - 1
for e in range(epochs): # Iterate for every epoch
print(e)
batch_indexes = list(range(min(num_of_batches)))
random.shuffle(batch_indexes)
for bi, b in enumerate(batch_indexes): # Iterate for every batch
print("Batch {} of {}".format(bi,min(num_of_batches)), end="\r")
sys.stdout.flush()
X_batch = np.empty((batch_size*num_meters, self.window_size, 1))
Y_batch = np.empty((batch_size*num_meters, 1))
# Create a batch out of data from all buildings
for i in range(num_meters):
mainpart = mainchunks[i]
meterpart = meterchunks[i]
mainpart = np.array(mainpart[b*batch_size:(b+1)*batch_size])
meterpart = np.array(meterpart[b*batch_size:(b+1)*batch_size])
X = np.reshape(mainpart, (batch_size, self.window_size, 1))
Y = np.reshape(meterpart, (batch_size, 1))
X_batch[i*batch_size:(i+1)*batch_size] = np.array(X)
Y_batch[i*batch_size:(i+1)*batch_size] = np.array(Y)
# Shuffle data
p = np.random.permutation(len(X_batch))
X_batch, Y_batch = X_batch[p], Y_batch[p]
# Train model
self.model.train_on_batch(X_batch, Y_batch)
print("\n")
def train_across_buildings_chunk_CONCAT_method(self, mainchunks, meterchunks, epochs, batch_size):
num_meters = len(mainchunks)
X_batch= None
Y_batch = None
for i in range(num_meters):
mainchunks[i].fillna(0, inplace=True)
meterchunks[i].fillna(0, inplace=True)
ix = mainchunks[i].index.intersection(meterchunks[i].index)
m1 = mainchunks[i]
m2 = meterchunks[i]
mainchunks[i] = np.array(m1[ix])
meterchunks[i] = np.array(m2[ix])
if X_batch is None:
X_batch = mainchunks[i]
Y_batch = meterchunks[i]
else:
X_batch = np.append(X_batch, mainchunks[i], 0)
Y_batch = np.append(Y_batch, meterchunks[i], 0)
# Clear up memory (not sure if it works)
mainchunks[i] = None
meterchunks[i] = None
indexer = np.arange(self.window_size)[None, :] + np.arange(len(X_batch) - self.window_size + 1)[:, None]
X_batch = X_batch[indexer]
Y_batch = Y_batch[self.window_size - 1:]
X_batch = np.reshape(X_batch, (X_batch.shape[0], X_batch.shape[1], 1))
self.model.fit(X_batch, Y_batch, batch_size=batch_size, epochs=epochs, shuffle=True)
def disaggregate(self, mains, output_datastore, meter_metadata, **load_kwargs):
'''Disaggregate mains according to the model learnt previously.
Parameters
----------
mains : a nilmtk.ElecMeter of aggregate data
meter_metadata: a nilmtk.ElecMeter of the observed meter used for storing the metadata
output_datastore : instance of nilmtk.DataStore subclass
For storing power predictions from disaggregation algorithm.
**load_kwargs : key word arguments
Passed to `mains.power_series(**kwargs)`
'''
load_kwargs = self._pre_disaggregation_checks(load_kwargs)
load_kwargs.setdefault('sample_period', 60)
load_kwargs.setdefault('sections', mains.good_sections())
timeframes = []
building_path = '/building{}'.format(mains.building())
mains_data_location = building_path + '/elec/meter1'
data_is_available = False
for chunk in mains.power_series(**load_kwargs):
if len(chunk) < self.MIN_CHUNK_LENGTH:
continue
print("New sensible chunk: {}".format(len(chunk)))
timeframes.append(chunk.timeframe)
measurement = chunk.name
chunk2 = self._normalize(chunk, self.mmax)
appliance_power = self.disaggregate_chunk(chunk2)
appliance_power[appliance_power < 0] = 0
appliance_power = self._denormalize(appliance_power, self.mmax)
# Append prediction to output
data_is_available = True
cols = pd.MultiIndex.from_tuples([chunk.name])
meter_instance = meter_metadata.instance()
df = pd.DataFrame(
appliance_power.values, index=appliance_power.index,
columns=cols, dtype="float32")
key = '{}/elec/meter{}'.format(building_path, meter_instance)
output_datastore.append(key, df)
# Append aggregate data to output
mains_df = pd.DataFrame(chunk, columns=cols, dtype="float32")
output_datastore.append(key=mains_data_location, value=mains_df)
# Save metadata to output
if data_is_available:
self._save_metadata_for_disaggregation(
output_datastore=output_datastore,
sample_period=load_kwargs['sample_period'],
measurement=measurement,
timeframes=timeframes,
building=mains.building(),
meters=[meter_metadata]
)
def disaggregate_chunk(self, mains):
'''In-memory disaggregation.
Parameters
----------
mains : pd.Series of aggregate data
Returns
-------
appliance_powers : pd.DataFrame where each column represents a
disaggregated appliance. Column names are the integer index
into `self.model` for the appliance in question.
'''
up_limit = len(mains)
mains.fillna(0, inplace=True)
X_batch = np.array(mains)
Y_len = len(X_batch)
indexer = np.arange(self.window_size)[None, :] + np.arange(len(X_batch)-self.window_size+1)[:, None]
X_batch = X_batch[indexer]
X_batch = np.reshape(X_batch, (X_batch.shape[0],X_batch.shape[1],1))
pred = self.model.predict(X_batch, batch_size=128)
pred = np.reshape(pred, (len(pred)))
column = pd.Series(pred, index=mains.index[self.window_size-1:Y_len], name=0)
appliance_powers_dict = {}
appliance_powers_dict[0] = column
appliance_powers = pd.DataFrame(appliance_powers_dict)
return appliance_powers
def import_model(self, filename):
'''Loads keras model from h5
Parameters
----------
filename : filename for .h5 file
Returns: Keras model
'''
self.model = load_model(filename)
with h5py.File(filename, 'a') as hf:
ds = hf.get('disaggregator-data').get('mmax')
self.mmax = np.array(ds)[0]
def export_model(self, filename):
'''Saves keras model to h5
Parameters
----------
filename : filename for .h5 file
'''
self.model.save(filename)
with h5py.File(filename, 'a') as hf:
gr = hf.create_group('disaggregator-data')
gr.create_dataset('mmax', data = [self.mmax])
def _normalize(self, chunk, mmax):
'''Normalizes timeseries
Parameters
----------
chunk : the timeseries to normalize
max : max value of the powerseries
Returns: Normalized timeseries
'''
tchunk = chunk / mmax
return tchunk
def _denormalize(self, chunk, mmax):
'''Deormalizes timeseries
Note: This is not entirely correct
Parameters
----------
chunk : the timeseries to denormalize
max : max value used for normalization
Returns: Denormalized timeseries
'''
tchunk = chunk * mmax
return tchunk
def _create_model(self):
'''Creates the GRU architecture described in the paper
'''
model = Sequential()
input_shape=(self.window_size,1)
inputs = Input(input_shape)
cnn_1 = Conv1D(16, 4, activation="relu", padding="same", strides=1)(inputs)
att = Attention(causal='True')([cnn_1,cnn_1])
b1 = Bidirectional(GRU(64, return_sequences=False, stateful=False), merge_mode='concat')(att)
x = Dense(64, activation='relu')(b1)
outputs = Dense(1, activation='linear')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='mse', optimizer='adam')
print(model.summary())
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=False)
return model