-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keras file format updates #1401
Changes from all commits
473a118
890dd47
ca0a0d7
a0f947e
76bf172
bd446de
bde687f
a53970c
fa96615
9868c04
7ebd238
17016a3
b58c71b
30d3470
cfd338d
e527e7f
a19926e
72a3b04
7832c07
3f40a24
03d5438
98adbe6
7e4e87e
eb39315
efbe7c7
1113057
0c2775e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
|
||
if omlt_available: | ||
from omlt import OmltBlock, OffsetScaling | ||
from omlt.neuralnet import ( | ||
from omlt.neuralnet.nn_formulation import ( | ||
FullSpaceSmoothNNFormulation, | ||
ReducedSpaceSmoothNNFormulation, | ||
ReluBigMFormulation, | ||
|
@@ -253,7 +253,7 @@ def evaluate_surrogate(self, inputs): | |
y = self._output_scaler.unscale(y) | ||
return y | ||
|
||
def save_to_folder(self, keras_folder_name): | ||
def save_to_folder(self, keras_folder_name, keras_model_name="idaes_keras_model"): | ||
""" | ||
Save the surrogate object to disk by providing the name of the | ||
folder to contain the keras model and additional IDAES metadata | ||
|
@@ -263,7 +263,9 @@ def save_to_folder(self, keras_folder_name): | |
The name of the folder to contain the Keras model and additional | ||
IDAES metadata | ||
""" | ||
self._keras_model.save(keras_folder_name) | ||
self._keras_model.save( | ||
os.path.join(keras_folder_name, keras_model_name + ".keras") | ||
) | ||
info = dict() | ||
info["input_scaler"] = None | ||
if self._input_scaler is not None: | ||
|
@@ -281,7 +283,7 @@ def save_to_folder(self, keras_folder_name): | |
json.dump(info, fd) | ||
|
||
@classmethod | ||
def load_from_folder(cls, keras_folder_name): | ||
def load_from_folder(cls, keras_folder_name, keras_model_name="idaes_keras_model"): | ||
""" | ||
Load the surrogate object from disk by providing the name of the | ||
folder holding the keras model | ||
|
@@ -293,7 +295,11 @@ def load_from_folder(cls, keras_folder_name): | |
Returns: an instance of KerasSurrogate | ||
""" | ||
keras_model = keras.models.load_model(keras_folder_name) | ||
|
||
keras_model = keras.models.load_model( | ||
os.path.join(keras_folder_name, keras_model_name + ".keras") | ||
) | ||
|
||
Comment on lines
+298
to
+302
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comments for using |
||
with open(os.path.join(keras_folder_name, "idaes_info.json")) as fd: | ||
info = json.load(fd) | ||
|
||
|
@@ -319,12 +325,11 @@ def save_keras_json_hd5(nn, path, name): | |
json_model = nn.to_json() | ||
with open(os.path.join(path, "{}.json".format(name)), "w") as json_file: | ||
json_file.write(json_model) | ||
nn.save_weights(os.path.join(path, "{}.h5".format(name))) | ||
nn.save(os.path.join(path, "{}.keras".format(name))) | ||
nn.save_weights(os.path.join(path, "{}.weights.h5".format(name))) | ||
|
||
|
||
def load_keras_json_hd5(path, name): | ||
with open(os.path.join(path, "{}.json".format(name)), "r") as json_file: | ||
json_model = json_file.read() | ||
nn = keras.models.model_from_json(json_model) | ||
nn.load_weights(os.path.join(path, "{}.h5".format(name))) | ||
nn = keras.models.load_model(os.path.join(path, "{}.keras".format(name))) | ||
nn.load_weights(os.path.join(path, "{}.weights.h5".format(name))) | ||
Comment on lines
-322
to
+334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comments above for using |
||
return nn |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"class_name": "Sequential", "config": {"name": "PT_data_2_10_10_2_relu", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 2], "dtype": "float32", "sparse": false, "ragged": false, "name": "dense_9_input"}}, {"class_name": "Dense", "config": {"name": "dense_9", "trainable": true, "batch_input_shape": [null, 2], "dtype": "float32", "units": 10, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_10", "trainable": true, "dtype": "float32", "units": 10, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_11", "trainable": true, "dtype": "float32", "units": 2, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.7.0", "backend": "tensorflow"} | ||
{"module": "keras", "class_name": "Sequential", "config": {"name": "PT_data_2_10_10_2_relu", "trainable": true, "dtype": "float32", "layers": [{"module": "keras.layers", "class_name": "InputLayer", "config": {"batch_shape": [null, 2], "dtype": "float32", "sparse": false, "name": "input_layer_3"}, "registered_name": null}, {"module": "keras.layers", "class_name": "Dense", "config": {"name": "dense_9", "trainable": true, "dtype": "float32", "units": 10, "activation": "relu", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 2]}}, {"module": "keras.layers", "class_name": "Dense", "config": {"name": "dense_10", "trainable": true, "dtype": "float32", "units": 10, "activation": "relu", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 10]}}, {"module": "keras.layers", "class_name": "Dense", "config": {"name": "dense_11", "trainable": true, "dtype": "float32", "units": 2, "activation": "linear", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 10]}}], "build_input_shape": [null, 2]}, "registered_name": null, "build_config": {"input_shape": [null, 2]}, "compile_config": {"optimizer": {"module": "keras.optimizers", "class_name": "Adam", "config": {"name": "adam", "learning_rate": 0.0010000000474974513, "weight_decay": null, "clipnorm": null, "global_clipnorm": null, "clipvalue": null, "use_ema": false, "ema_momentum": 0.99, "ema_overwrite_frequency": null, "loss_scale_factor": null, "gradient_accumulation_steps": null, "beta_1": 0.9, "beta_2": 0.999, "epsilon": 1e-07, "amsgrad": false}, "registered_name": null}, "loss": "mse", "loss_weights": null, "metrics": null, "weighted_metrics": null, "run_eagerly": false, "steps_per_execution": 1, "jit_compile": false}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: since Keras seems to support
pathlib.Path
objects now, this could be rewritten to be slightly more concise/readable (requires replacingimport os.path
withfrom pathlib import Path
at the top of the file):