-
Notifications
You must be signed in to change notification settings - Fork 35
/
seq2point_test.py
203 lines (152 loc) · 8.59 KB
/
seq2point_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
import os
import logging
import numpy as np
import keras
import pandas as pd
import tensorflow as tf
import time
from model_structure import create_model, load_model
from data_feeder import TestSlidingWindowGenerator
from appliance_data import appliance_data, mains_data
import matplotlib.pyplot as plt
class Tester():
""" Used to test and evaluate a pre-trained seq2point model with or without pruning applied.
Parameters:
__appliance (string): The target appliance.
__algorithm (string): The (pruning) algorithm the model was trained with.
__network_type (string): The architecture of the model.
__crop (int): The maximum number of rows of data to evaluate the model with.
__batch_size (int): The number of rows per testing batch.
__window_size (int): The size of eaech sliding window
__window_offset (int): The offset of the inferred value from the sliding window.
__test_directory (string): The directory of the test file for the model.
"""
def __init__(self, appliance, algorithm, crop, batch_size, network_type,
test_directory, saved_model_dir, log_file_dir,
input_window_length):
self.__appliance = appliance
self.__algorithm = algorithm
self.__network_type = network_type
self.__crop = crop
self.__batch_size = batch_size
self._input_window_length = input_window_length
self.__window_size = self._input_window_length + 2
self.__window_offset = int(0.5 * self.__window_size - 1)
self.__number_of_windows = 100
self.__test_directory = test_directory
self.__saved_model_dir = saved_model_dir
self.__log_file = log_file_dir
logging.basicConfig(filename=self.__log_file,level=logging.INFO)
def test_model(self):
""" Tests a fully-trained model using a sliding window generator as an input. Measures inference time, gathers, and
plots evaluationg metrics. """
test_input, test_target = self.load_dataset(self.__test_directory)
model = create_model(self._input_window_length)
model = load_model(model, self.__network_type, self.__algorithm,
self.__appliance, self.__saved_model_dir)
test_generator = TestSlidingWindowGenerator(number_of_windows=self.__number_of_windows, inputs=test_input, targets=test_target, offset=self.__window_offset)
# Calculate the optimum steps per epoch.
steps_per_test_epoch = np.round(int(test_generator.total_size / self.__batch_size), decimals=0)
# Test the model.
start_time = time.time()
testing_history = model.predict(x=test_generator.load_dataset(), steps=steps_per_test_epoch, verbose=2)
end_time = time.time()
test_time = end_time - start_time
evaluation_metrics = model.evaluate(x=test_generator.load_dataset(), steps=steps_per_test_epoch)
self.log_results(model, test_time, evaluation_metrics)
self.plot_results(testing_history, test_input, test_target)
def load_dataset(self, directory):
"""Loads the testing dataset from the location specified by file_name.
Parameters:
directory (string): The location at which the dataset is stored, concatenated with the file name.
Returns:
test_input (numpy.array): The first n (crop) features of the test dataset.
test_target (numpy.array): The first n (crop) targets of the test dataset.
"""
data_frame = pd.read_csv(directory, nrows=self.__crop, skiprows=0, header=0)
test_input = np.round(np.array(data_frame.iloc[:, 0], float), 6)
test_target = np.round(np.array(data_frame.iloc[self.__window_offset: -self.__window_offset, 1], float), 6)
del data_frame
return test_input, test_target
def log_results(self, model, test_time, evaluation_metrics):
"""Logs the inference time, MAE and MSE of an evaluated model.
Parameters:
model (tf.keras.Model): The evaluated model.
test_time (float): The time taken by the model to infer all required values.
evaluation metrics (list): The MSE, MAE, and various compression ratios of the model.
"""
inference_log = "Inference Time: " + str(test_time)
logging.info(inference_log)
metric_string = "MSE: ", str(evaluation_metrics[0]), " MAE: ", str(evaluation_metrics[3])
logging.info(metric_string)
self.count_pruned_weights(model)
def count_pruned_weights(self, model):
""" Counts the total number of weights, pruned weights, and weights in convolutional
layers. Calculates the sparsity ratio of different layer types and logs these values.
Parameters:
model (tf.keras.Model): The evaluated model.
"""
num_total_zeros = 0
num_dense_zeros = 0
num_dense_weights = 0
num_conv_zeros = 0
num_conv_weights = 0
for layer in model.layers:
if np.shape(layer.get_weights())[0] != 0:
layer_weights = layer.get_weights()[0].flatten()
if "conv" in layer.name:
num_conv_weights += np.size(layer_weights)
num_conv_zeros += np.count_nonzero(layer_weights==0)
num_total_zeros += np.size(layer_weights)
else:
num_dense_weights += np.size(layer_weights)
num_dense_zeros += np.count_nonzero(layer_weights==0)
conv_zeros_string = "CONV. ZEROS: " + str(num_conv_zeros)
conv_weights_string = "CONV. WEIGHTS: " + str(num_conv_weights)
conv_sparsity_ratio = "CONV. RATIO: " + str(num_conv_zeros / num_conv_weights)
dense_weights_string = "DENSE WEIGHTS: " + str(num_dense_weights)
dense_zeros_string = "DENSE ZEROS: " + str(num_dense_zeros)
dense_sparsity_ratio = "DENSE RATIO: " + str(num_dense_zeros / num_dense_weights)
total_zeros_string = "TOTAL ZEROS: " + str(num_total_zeros)
total_weights_string = "TOTAL WEIGHTS: " + str(model.count_params())
total_sparsity_ratio = "TOTAL RATIO: " + str(num_total_zeros / model.count_params())
print("LOGGING PATH: ", self.__log_file)
logging.info(conv_zeros_string)
logging.info(conv_weights_string)
logging.info(conv_sparsity_ratio)
logging.info("")
logging.info(dense_zeros_string)
logging.info(dense_weights_string)
logging.info(dense_sparsity_ratio)
logging.info("")
logging.info(total_zeros_string)
logging.info(total_weights_string)
logging.info(total_sparsity_ratio)
def plot_results(self, testing_history, test_input, test_target):
""" Generates and saves a plot of the testing history of the model against the (actual)
aggregate energy values and the true appliance values.
Parameters:
testing_history (numpy.ndarray): The series of values inferred by the model.
test_input (numpy.ndarray): The aggregate energy data.
test_target (numpy.ndarray): The true energy values of the appliance.
"""
testing_history = ((testing_history * appliance_data[self.__appliance]["std"]) + appliance_data[self.__appliance]["mean"])
test_target = ((test_target * appliance_data[self.__appliance]["std"]) + appliance_data[self.__appliance]["mean"])
test_agg = (test_input.flatten() * mains_data["std"]) + mains_data["mean"]
test_agg = test_agg[:testing_history.size]
# Can't have negative energy readings - set any results below 0 to 0.
test_target[test_target < 0] = 0
testing_history[testing_history < 0] = 0
test_input[test_input < 0] = 0
# Plot testing outcomes against ground truth.
plt.figure(1)
plt.plot(test_agg[self.__window_offset: -self.__window_offset], label="Aggregate")
plt.plot(test_target[:test_agg.size - (2 * self.__window_offset)], label="Ground Truth")
plt.plot(testing_history[:test_agg.size - (2 * self.__window_offset)], label="Predicted")
plt.title(self.__appliance + " " + self.__network_type + "(" + self.__algorithm + ")")
plt.ylabel("Power Value (Watts)")
plt.xlabel("Testing Window")
plt.legend()
#file_path = "./" + self.__appliance + "/saved_models/" + self.__appliance + "_" + self.__algorithm + "_" + self.__network_type + "_test_figure.png"
#plt.savefig(fname=file_path)
plt.show()