-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Parse callables (functions, methods) similarly to objects #5852
Comments
thanks for reporting, it's possible to use from monai.bundle import ConfigParser
config = {"import statements": "$import math",
"calculate": "$math.isclose(@a, @b)",
"a": 0.001,
"b": 0.005}
parser = ConfigParser(config)
print(parser.get_parsed_content("calculate")) # False
parser["b"] = 0.001
print(parser.get_parsed_content("calculate")) # True |
Hi, yes, thank you, but you still have to refer the arguments manually in Do you think you're interested in adding support for calling functions/methods in the same fashion as object instantiation? |
I took another look, actually it's a supported use case, but there's bug in the code. I'm create a pull request to fix it. |
Awesome, thank you so much. I love what you did; I switched from OmegaConf/Hydra, Monai Bundle Config solves quite a few problems I had there, and the error messages are way better. Super simple to integrate it too. Looking forward to learning more about Bundle Specification! |
Hi, so I just installed monai from your branch and tried this: config.yaml
Command: And it raises:
Am I doing something wrong, or is referencing a method of an object from the config not supported? |
that's a good question, at the moment the usage would be imports: $import torch
x: "$torch.rand(1, 3, 256, 256)"
forward: "$@model().forward(@x)"
model:
_target_: monai.networks.nets.resnet.resnet18
pretrained: False
spatial_dims: 2 if you want to delay the forward call, it's also possible to add the imports:
- $import torch
- $import functools
x: "$torch.rand(1, 3, 256, 256)"
forward: "$functools.partial(@model().forward, @x)"
model:
_target_: monai.networks.nets.resnet.resnet18
pretrained: False
spatial_dims: 2
|
Alright, thanks! Do you think that's something that the bundle config will support? |
The way that we do searching when referring to something by name should perhaps be revisited. Currently our code traverses all known libraries looking for names, this a problem if multiple definitions use the same name in different modules as well as also forcing a lot of things to get loaded that don't need to be. The solution to your specific use case above works but there's a number of extra syntax that we've introduce with the "$@" sigils that isn't obvious. I feel we need a bit of a smarter name searching system that will look at some target like "X.Y.Z" and search for "X" first in currently known objects and imported modules, then for "X.Y" if not found, etc. It would then resolve the rest of the name looking for definitions or members of definitions, so a target "model.forward" should find the object "model" and that "forward" is a callable member and not try to instantiate it. |
I've updated #5854 for this. MONAI/monai/bundle/config_item.py Lines 237 to 239 in 33d41d7
please see the new test cases in PR #5854 for the newly supported cases. |
Signed-off-by: Wenqi Li <wenqil@nvidia.com> Fixes #5852 ### Description 1. the following partial instantiate doesn't work ```py config = {"import statements": "$import math", "calc": {"_target_": "math.isclose", "a": 0.001, "b": 0.001}} print(ConfigParser(config).calc()) ``` with an error message: ``` Component to instantiate must represent a valid class or function, but got math.isclose. Traceback (most recent call last): File "test.py", line 4, in <module> print(ConfigParser(config).calc()) TypeError: isclose() missing required argument 'a' (pos 1) ``` because `math.isclose` is a builtin type but not a function: ```py import inspect inspect.isfunction(math.isclose) # False inspect.isbuiltin(math.isclose) # True ``` the `partial` should support `callable` including builtin functions 2. also this PR supports `_target_` of reference object's methods such as partial init: ```py "forward": {"_target_": "$@model().forward", "x": "$torch.rand(1, 3, 256, 256)"} ``` ### 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). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [x] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Wenqi Li <wenqil@nvidia.com>
Is your feature request related to a problem? Please describe.
Support for functions/methods in config by making Monai call them instead of attempting to instantiate them.
Describe the solution you'd like
A solution like Hydra's (https://hydra.cc/docs/advanced/instantiate_objects/overview/) that can both instantiate or call would be perfect.
Describe alternatives you've considered
None
Additional context
While you could do
calculate: "$math.isclose(0.001, 0.002)"
, it does not allow argument overriding from CLI.The text was updated successfully, but these errors were encountered: