Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support regular expression in the mapping arg of
copy_model_state
(#…
…6917) Part of #6552. ### Description After PR #6835, we have added `copy_model_args` in the `load` API which can help us update the state_dict flexibly. https://github.com/KumoLiu/MONAI/blob/93a149a611b66153cf804b31a7b36a939e2e593a/monai/bundle/scripts.py#L397 Given this [issue](#6552), we need to be able to filter the model's weights flexibly. In `copy_model_state`, we already have a "mapping" arg, the filter will be more flexible if we can support regular expression in the mapping. This PR mainly added the support for regular expression for "mapping" arg. In the [example](#6552 (comment)) in this [issue](#6552), after this PR, we can do something like: ``` exclude_vars = "encoder.mask_token|encoder.norm.weight|encoder.norm.bias|out.conv.conv.weight|out.conv.conv.bias" mapping={"encoder.layers(.*).0.0.": "swinViT.layers(.*).0."} dst_dict, updated_keys, unchanged_keys = copy_model_state( model, ssl_weights, exclude_vars=exclude_vars, mapping=mapping ) ``` Additionally, based on the comments of Eric [here](#6552 (comment)), I totally agree, we could add a handler to make the pipeline easier to implement, but perhaps this task is no need to set as a "BundleTodo" for MONAIv1.3 but as an enhancement for MONAI near future. What do you think? @ericspod @wyli @Nic-Ma ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: KumoLiu <yunl@nvidia.com>
- Loading branch information