You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There seems to be an issue where when converting an H5 file to a dictionary and back using h5_to_dict and dict_to_h5, the H5 file is no longer the same as the original. This happens when an attribute, in this case weight_names, has an empty field, in this case called values. This is the case for dropout layers in Keras models, so you can reproduce the error using a keras model with a dropout layer.
Minimum reproducible example:
from tensorflow import keras
import h5_to_json as h5j
def demo_issue(dropout: bool):
model = keras.Sequential()
model.add(keras.layers.LSTM(
units=2,
input_shape=(1,1)
))
if dropout:
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(units=1))
model.compile(
loss='mean_squared_error',
optimizer=keras.optimizers.Adam(0.001)
)
model.save('./model.h5')
# This succeeds
immediate_reloaded_model = keras.models.load_model('./model.h5')
model_dict = h5j.h5_to_dict('./model.h5', include_datasets=True)
h5j.dict_to_h5(model_dict, './model.h5')
# This fails if the model has a dropout layer
dict_stored_model = keras.models.load_model('./model.h5')
print("Without dropout layer:")
demo_issue(False)
print("With dropout layer:")
demo_issue(True)
The warning given by h5_to_dict is:
no value key in attribute: weight_names
The error this causes when running keras.models.load_model is:
Traceback (most recent call last):
File "/Users/javieralcazar/Javier/MYAPPS/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 171, in load_model_from_hdf5
load_weights_from_hdf5_group(f['model_weights'], model.layers)
File "/Users/javieralcazar/Javier/MYAPPS/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 669, in load_weights_from_hdf5_group
weight_names = load_attributes_from_hdf5_group(g, 'weight_names')
File "/Users/javieralcazar/Javier/MYAPPS/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 842, in load_attributes_from_hdf5_group
data = [n.decode('utf8') for n in group.attrs[name]]
TypeError: 'Empty' object is not iterable
How can we convert this H5 file to a dict and back and get the same H5 file back? I believe this would mean having an empty values field in the weight_names attribute rather than erasing the values field completely. Thank you.
The text was updated successfully, but these errors were encountered:
ajroeth205
changed the title
h5_to_dict has issue saving dropout layer
h5 to dict and back does not return same file
Apr 24, 2020
There seems to be an issue where when converting an H5 file to a dictionary and back using
h5_to_dict
anddict_to_h5
, the H5 file is no longer the same as the original. This happens when an attribute, in this caseweight_names
, has an empty field, in this case calledvalues
. This is the case for dropout layers in Keras models, so you can reproduce the error using a keras model with a dropout layer.Minimum reproducible example:
The warning given by
h5_to_dict
is:The error this causes when running
keras.models.load_model
is:How can we convert this H5 file to a dict and back and get the same H5 file back? I believe this would mean having an empty
values
field in theweight_names
attribute rather than erasing thevalues
field completely. Thank you.The text was updated successfully, but these errors were encountered: