1

I'm trying PlaidML, which requires keras==2.2.4. And I'm having difficulty loading the Saved Model format.

OSError: Unable to open file (unable to open file: name = '<my model path here>', errno = 13, error message = 'Permission denied', flags = 0, o_flags = 0)

I'm sure the model path exists, and files in it are readable.
I think Keras v2.2.4 isn't able to load Saved Model format.

Here is my keras\engine\saving.py:

def load_model(filepath, custom_objects=None, compile=True):
    """Loads a model saved via `save_model`.

    # Arguments
        filepath: one of the following:
            - string, path to the saved model, or
            - h5py.File or h5py.Group object from which to load the model
        custom_objects: Optional dictionary mapping names
            (strings) to custom classes or functions to be
            considered during deserialization.
        compile: Boolean, whether to compile the model
            after loading.

    # Returns
        A Keras model instance. If an optimizer was found
        as part of the saved model, the model is already
        compiled. Otherwise, the model is uncompiled and
        a warning will be displayed. When `compile` is set
        to False, the compilation is omitted without any
        warning.

    # Raises
        ImportError: if h5py is not available.
        ValueError: In case of an invalid savefile.
    """
    if h5py is None:
        raise ImportError('`load_model` requires h5py.')
    model = None
    opened_new_file = not isinstance(filepath, h5py.Group)
    f = h5dict(filepath, 'r')
    try:
        model = _deserialize_model(f, custom_objects, compile)
    finally:
        if opened_new_file:
            f.close()
    return model

It seems that it just loads the path as a HDF5 file?

I also tried to convert the saved model to a .h5 file.
Then load it in keras v2.2.4, still not working:

Traceback (most recent call last):
  File "test_plaidml.py", line 95, in <module>        
    model = keras.models.load_model(model_path)
  File "keras\engine\saving.py", line 419, in load_model
    model = _deserialize_model(f, custom_objects, compile)
  File "keras\engine\saving.py", line 224, in _deserialize_model
    model_config = json.loads(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'

Any way to find out what's wrong without v2.2.4 documentation?
(I can't find it online)

Thanks.

Update

I think I known what the problem is, but I don't know how to solve it for now.
I'll try to find a solution.

  1. So Keras of this version is unable to load saved model (a TF2 feature).
    It seems that v2.3.0 started to support TF2 partially.
  2. Incompatible h5py version seems to be the cause of failing to load .h5 file.
    Solved it by installing h5py==2.10.0 using Python 3.8.
  3. However, I still got TypeError: init() got an unexpected keyword argument 'ragged', probably because I was using tf.keras, and switching to keras now.

I'm wondering whether it's possible to run a tf2 model on PlaidML / old Keras.

Kirk
  • 446
  • 4
  • 18
  • Keras 2.2.4 does not support TF2, and by extension the **Saved Model** format. – Lescurel Mar 02 '21 at 15:52
  • Thanks a lot! Then the problem would be to convert the format to a v2.2.4 compatible `.h5` file. – Kirk Mar 02 '21 at 16:00
  • Maybe you can use a newer version of tensorflow to load the saved model, and then save that as an hdf5 keras format model. – jkr Mar 02 '21 at 16:18
  • I just tried that with `import keras`, `model = keras.models.load_model()`, `keras.models.save_model(model, ".h5", save_format="h5")`. But I got [TypeError: init() got an unexpected keyword argument 'ragged'](https://stackoverflow.com/questions/58878421/unexpected-keyword-argument-ragged-in-keras) when loading using the old keras. – Kirk Mar 02 '21 at 16:24
  • Does this [Tensorflow save and load keras](https://www.tensorflow.org/tutorials/keras/save_and_load) model tutorial helps. Thanks! –  Mar 16 '21 at 11:20
  • I don't think so. I've already tried `save_model()` & `load_model()`. I already gave up on this. This is like loading new file format using old code. It's normal that this doesn't work. Even the `.h5` file content may differ. [Tensorflow version compatibility](https://www.tensorflow.org/guide/versions) – Kirk Mar 16 '21 at 12:49
  • Try to use latest Tensorflow and Keras version. Thank you –  Jul 13 '22 at 02:46

0 Answers0