forked from MingjunZhong/seq2point-nilm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_structure.py
67 lines (48 loc) · 3.08 KB
/
model_structure.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
import tensorflow as tf
import os
def create_model(input_window_length):
"""Specifies the structure of a seq2point model using Keras' functional API.
Returns:
model (tensorflow.keras.Model): The uncompiled seq2point model.
"""
input_layer = tf.keras.layers.Input(shape=(input_window_length,))
reshape_layer = tf.keras.layers.Reshape((1, input_window_length, 1))(input_layer)
conv_layer_1 = tf.keras.layers.Convolution2D(filters=30, kernel_size=(10, 1), strides=(1, 1), padding="same", activation="relu")(reshape_layer)
conv_layer_2 = tf.keras.layers.Convolution2D(filters=30, kernel_size=(8, 1), strides=(1, 1), padding="same", activation="relu")(conv_layer_1)
conv_layer_3 = tf.keras.layers.Convolution2D(filters=40, kernel_size=(6, 1), strides=(1, 1), padding="same", activation="relu")(conv_layer_2)
conv_layer_4 = tf.keras.layers.Convolution2D(filters=50, kernel_size=(5, 1), strides=(1, 1), padding="same", activation="relu")(conv_layer_3)
conv_layer_5 = tf.keras.layers.Convolution2D(filters=50, kernel_size=(5, 1), strides=(1, 1), padding="same", activation="relu")(conv_layer_4)
flatten_layer = tf.keras.layers.Flatten()(conv_layer_5)
label_layer = tf.keras.layers.Dense(1024, activation="relu")(flatten_layer)
output_layer = tf.keras.layers.Dense(1, activation="linear")(label_layer)
model = tf.keras.Model(inputs=input_layer, outputs=output_layer)
return model
def save_model(model, network_type, algorithm, appliance, save_model_dir):
""" Saves a model to a specified location. Models are named using a combination of their
target appliance, architecture, and pruning algorithm.
Parameters:
model (tensorflow.keras.Model): The Keras model to save.
network_type (string): The architecture of the model ('', 'reduced', 'dropout', or 'reduced_dropout').
algorithm (string): The pruning algorithm applied to the model.
appliance (string): The appliance the model was trained with.
"""
#model_path = "saved_models/" + appliance + "_" + algorithm + "_" + network_type + "_model.h5"
model_path = save_model_dir
if not os.path.exists (model_path):
open((model_path), 'a').close()
model.save(model_path)
def load_model(model, network_type, algorithm, appliance, saved_model_dir):
""" Loads a model from a specified location.
Parameters:
model (tensorflow.keras.Model): The Keas model to which the loaded weights will be applied to.
network_type (string): The architecture of the model ('', 'reduced', 'dropout', or 'reduced_dropout').
algorithm (string): The pruning algorithm applied to the model.
appliance (string): The appliance the model was trained with.
"""
#model_name = "saved_models/" + appliance + "_" + algorithm + "_" + network_type + "_model.h5"
model_name = saved_model_dir
print("PATH NAME: ", model_name)
model = tf.keras.models.load_model(model_name)
num_of_weights = model.count_params()
print("Loaded model with ", str(num_of_weights), " weights")
return model