-
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
RandCropByLabelClasses, internal variable "ratios" is mutated #6109
Comments
that's a good point, I'll review the crop/pad transforms. @yiheng-wang-nv @KumoLiu could you please help review the spatial transforms? for example the user-specified properties such as MONAI/monai/transforms/spatial/array.py Lines 1220 to 1221 in b85e2c6
shouldn't change during self.__call__
but the MONAI/monai/transforms/spatial/array.py Lines 1225 to 1229 in b85e2c6
|
Fixes #6109 ### Description - use tuples for user inputs to avoid changes - enhance the type checks - fixes issue of `ratios` in `RandCropByLabelClasses ` ### 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 <[email protected]>
I don't think this bug is fixed. the variable self.ratios is still a "list" (not tuple) and is being changed here MONAI/monai/transforms/utils.py Line 573 in a8302ec
the variable "ratios_[i]", comes from self.ratios (which is also a list and is being mutated) MONAI/monai/transforms/croppad/array.py Line 1208 in a8302ec
I though we've agreed to change it to "tuple", but I don't see this. I see many other changes in #6127, but nothing related to this particular bug. Please correct me if I'm wrong. @wyli |
Hi @myron, I think the bug is fixed by Line 563, it generates a new list instead of using the original one. And maybe
Please correct me if I'm wrong. :) @wyli |
@KumoLiu , I see, you're right, the ratios_ is converted to tuple and then to list again, so it won't change the self.ratios. all good, thank you I still think we need to make self.ratios of "class RandCropByLabelClasses" to be a "tuple" or converted to tuple in init() - to make it more robust. Otherwise somebody else may accidentally change self.ratios in the future, and we'll be chasing a similar bug again. |
I included a test case in the PR using tuple as input https://github.com/Project-MONAI/MONAI/pull/6127/files#diff-18bbbf3f78e0ba08f6ff6a953e6ff5eef763ace5e1620814e940d4d35590b207 it'll raise an error if we try to make in-place changes to |
I didn't change any of the transform constructors though, it might be useful to add |
In the cropping transform RandCropByLabelClasses, if a user specified "ratios" (a probability of sampling from each class), that variable mutated during training, on this line
MONAI/monai/transforms/utils.py
Lines 570 to 574 in ab800d8
that means, that during training, if some classes are missing , the probability is set to 0, and it's 0 for all next images too..
here ratios_[i] is actually 'self.ratios', so we're updating the internal variable , which we should not touch.
This is the second time (recently), that we detect a internal variable being mutated (e.g. here for pixdim #5950 (comment)). These bugs are very subtle, often don't trigger any errors, just a wrong performance.
Ideally, we need to run some test on all "transforms" and ensure that no internal variables are modified/mutated. Is there an automated analysis tool for this? maybe mypy with some option?
I think, we also should not use List for internal variables (as here) or np.array (as for pixdim), and always use some immutable type (e.g. tuple, frozenlist)..
The text was updated successfully, but these errors were encountered: