Skip to content
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

lazy RandAffine has no effect #6773

Closed
function2-llx opened this issue Jul 25, 2023 · 1 comment · Fixed by #6774
Closed

lazy RandAffine has no effect #6773

function2-llx opened this issue Jul 25, 2023 · 1 comment · Fixed by #6774
Assignees
Labels
bug Something isn't working

Comments

@function2-llx
Copy link
Contributor

function2-llx commented Jul 25, 2023

Describe the bug
When using RandAffine with lazy=True, it does not perform any transform at all.

To Reproduce

import torch

from monai import transforms as mt

def main():
    x = torch.randn(1, 32, 32, 32)
    trans = mt.Compose(
        [mt.RandAffine(1., translate_range=[(0, 0), (1, 1), (2, 2)])],
        lazy=False
    )
    print(trans(x).affine)
    lazy_trans = mt.Compose(
        [mt.RandAffine(1., translate_range=[(0, 0), (1, 1), (2, 2)])],
        lazy=True,
    )
    print(lazy_trans(x).affine)

if __name__ == '__main__':
    main()

Actual Result

tensor([[1., 0., 0., 0.],
        [0., 1., 0., 1.],
        [0., 0., 1., 2.],
        [0., 0., 0., 1.]], dtype=torch.float64)
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.],
        [0., 0., 0., 1.]], dtype=torch.float64)
@atbenmurray atbenmurray self-assigned this Jul 25, 2023
@wyli wyli added the bug Something isn't working label Jul 28, 2023
@wyli
Copy link
Contributor

wyli commented Jul 28, 2023

thanks for testing and reporting @function2-llx, that's a good point, the bug was not detected earlier because we used the same transform instance to verify lazy/non-lazy consistency in this case

g = RandAffine(**input_param)
g.set_random_state(123)
result = g(**input_data)
test_resampler_lazy(g, result, input_param, input_data, seed=123)
, your PR #6774 addresses the issue, I'll try to make it backward compatible (in terms of pseudo random states) and merge it.

wyli added a commit that referenced this issue Jul 28, 2023
Fixes #6773.

### Description

Call `rand_affine_grid()` once before call
`rand_affine_grid.get_transformation_matrix()`, since its documented as
"Get the most recently applied transformation matrix", or the `.affine`
attribute will not be set.
Also, set `randomize=False` here since randomization if performed in the
beginning of the function.

### 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).
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.

---------

Signed-off-by: function2 <function2-llx@outlook.com>
Co-authored-by: Wenqi Li <831580+wyli@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants