Chained affine transforms #4198
Replies: 4 comments 13 replies
-
Hi @EdvardGrodem, we've been keen on figuring out a way to concatenate certain transforms so as to accelerate things and also reduce the number of resampling steps (as we lose original information each time we do that). Unfortunately we haven't implemented a solution to that yet. In your case this problem would be solved if class RandAffineGrid(Randomizable, Transform):
def __init__(
self,
rotate_range: RandRange = None,
shear_range: RandRange = None,
translate_range: RandRange = None,
scale_range: RandRange = None,
as_tensor_output: bool = True,
device: Optional[torch.device] = None,
rotate_independent_dims: bool = True, # new
shear_independent_dims: bool = True, # new
translate_independent_dims: bool = True, # new
scale_independent_dims: bool = True, # new
) -> None: Looks a little clunky to me, has anyone got any better ideas? cc @atbenmurray (interested in concatenating multiple affine transformations). |
Beta Was this translation helpful? Give feedback.
-
Thanks @rijobro for bringing this to my attention. @EdvardGrodem your thinking is good, and a solution that requires at most one resample is actively under discussion at present. I'll keep you informed of any progress. In the meantime, I might suggest the following, provided you are doing your preprocessing on CPU:
Happy to discuss further if you think this approach might be helpful to you. |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot, @atbenmurray for your help. I've made a small git repo that implements your suggestion. For others that might be interested, here is a link: https://github.com/CRAI-OUS/MonaiMatrixTransforms |
Beta Was this translation helpful? Give feedback.
-
with the MetaTensor API, all the spatial transforms are tracked, and the affines updated, e.g. MONAI/monai/transforms/spatial/array.py Lines 1013 to 1031 in e1edc99 perhaps we can leverage those APIs to implement lazy resampling -- when meta_tensor.lazy_resample is True, the transforms will only update meta_tensor.applied_operations attributes to track the transforms. and when the user set meta_tensor.lazy_resample=False , actual voxels values will be updated...
class MetaTensor:
@property
def lazy_resample() -> bool:
return self._lazy_resample
@lazy_resample.setter
def lazy_resample(self, flag: bool):
if not flag and self._lazy_resample: # previously lazy_resample=True, and now
self.do_spatial_resampling(self.applied_operations)
return |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to augment my data by doing a random rotation combined with a random translation and a random isotropic scaling. My best effort at this so far has been this:
The issue is that this is quite slow compared to a single RandAffined. I assume that the transforms are done in stages, however, since the two transforms are affine these can theoretically be done with one stage by using their combined affine matrix. Is there a way to increase the speed by doing this in one step?
Beta Was this translation helpful? Give feedback.
All reactions