KerasFileEditor
classkeras.saving.KerasFileEditor(filepath)
Utility to inspect, edit, and resave Keras weights files.
You will find this class useful when adapting an old saved weights file after having made architecture changes to a model.
Arguments
Examples
editor = KerasFileEditor("my_model.weights.h5")
# Displays current contents
editor.summary()
# Remove the weights of an existing layer
editor.delete_object("layers/dense_2")
# Add the weights of a new layer
editor.add_object("layers/einsum_dense", weights={"0": ..., "1": ...})
# Save the weights of the edited model
editor.resave_weights("edited_model.weights.h5")
summary
methodKerasFileEditor.summary()
Prints the weight structure of the opened file.
compare
methodKerasFileEditor.compare(reference_model)
Compares the opened file to a reference model.
This method will list all mismatches between the currently opened file and the provided reference model.
Arguments
Returns
'status'
, 'error_count'
, 'match_count'
.
Status can be 'success'
or 'error'
.
'error_count'
is the number of mismatches found.
'match_count'
is the number of matching weights found.save
methodKerasFileEditor.save(filepath)
Save the edited weights file.
Arguments
.weights.h5
file.rename_object
methodKerasFileEditor.rename_object(object_name, new_name)
Rename an object in the file (e.g. a layer).
Arguments
"dense_2"
or
"layers/dense_2"
).delete_object
methodKerasFileEditor.delete_object(object_name)
Removes an object from the file (e.g. a layer).
Arguments
"dense_2"
or
"layers/dense_2"
).add_object
methodKerasFileEditor.add_object(object_path, weights)
Add a new object to the file (e.g. a layer).
Arguments
"layers/dense_2"
).{"0": kernel_value, "1": bias_value}
.delete_weight
methodKerasFileEditor.delete_weight(object_name, weight_name)
Removes a weight from an existing object.
Arguments
"dense_2"
or "layers/dense_2"
)."0"
).add_weights
methodKerasFileEditor.add_weights(object_name, weights)
Add one or more new weights to an existing object.
Arguments
"dense_2"
or "layers/dense_2"
).{"0": kernel_value, "1": bias_value}
.