spyrit.core.train.rename_model_attributes

spyrit.core.train.rename_model_attributes(source, old_name, new_name, target=None)[source]

Rename the name of the attributes of a saved model (nn.module)

Parameters:
  • source (str) – Path to the saved model.

  • old_name (str) – source pattern for the attributes of the model to be renamed.

  • new_name (str) – destination pattern for the attributes of the model to be renamed.

  • target (str, optional) – Path to model with remaned attributes. The default is source.

Returns:

None.

Example

Rename the key Denoi.layer.0.weight and Denoi.layer.0.weight as denoi.layer.0.weight and Denoi.layer.0.weight and save the resulting model as target.pth

>>> rename_model_attributes('model.pth', 'Denoi.', 'denoi.', 'target.pth')

Adapted from https://gist.github.com/the-bass/0bf8aaa302f9ba0d26798b11e4dd73e3