Skip to content
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

h5 to dict and back does not return same file #1

Open
ajroeth205 opened this issue Apr 24, 2020 · 0 comments
Open

h5 to dict and back does not return same file #1

ajroeth205 opened this issue Apr 24, 2020 · 0 comments

Comments

@ajroeth205
Copy link

ajroeth205 commented Apr 24, 2020

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.

@ajroeth205 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant