xiph / LPCNet

Efficient neural speech synthesis
BSD 3-Clause "New" or "Revised" License
1.12k stars 295 forks source link

Bug: MDense state restore crash with missing argument #191

Open tarepan opened 2 years ago

tarepan commented 2 years ago

Summary

Broken MDense.get_config crash model loading in Python.
Estimated cause is wrong attribute name.
With hacking, it can be fixed.

Phenomena

I tried to resume training from saved checkpoint using keras.models.load_model.
When load the model state from checkpoint, MDense loading raise below error.

Traceback (most recent call last):
  File "training_tf2/train_lpcnet.py", line 112, in <module>
    model = keras.models.load_model(args.resume_model, custom_objects=custom_objs)
...
  File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 796, in from_config
    return cls(**config)
TypeError: __init__() missing 1 required positional argument: 'outputs'

Estimated cause

The error says that default .from_config crash with missing positional argument outputs of MDense.__init__.

As error suggested, during serialization, outputs is not exported, instead, units is exported.

https://github.com/xiph/LPCNet/blob/2159b4f85d9839d9627f1843a1cb5ea298c63159/training_tf2/mdense.py#L81-L83

This will cause above error.

How to Fix

I patch like below, and the bug disappear.

    @classmethod
    def from_config(cls, config):
        units = config.pop("units", None)
        if units is not None:
            # Hack for old broken checkpoints
            return cls(outputs=units, **config)
        else:
            return cls(**config)

I do not know backward compatibility policy of this repository, so this is just an idea.

Thanks for your great LPCNet library, I love this! I am happy if this help this repo.