-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Describe the bug
When using RandGaussianNoised during training with more than one key the error "ValueError: shape mismatch: objects cannot be broadcast to a single shape" is returned.
To Reproduce
I have encountered the error when using RandGaussianNoised in my training pipeline for BraTS.
I have in my composition of training transforms:
RandGaussianNoised(keys=["flair", "t1", "t1ce", "t2"], mean=0., std=0.1, prob=0.15),
For this example, in
| self.mean = ensure_tuple_size(mean, len(self.keys)) |
we obtain
self.mean = (0.,0,0,0).
This creates a mismatch between the shape of self.mean and im_shape in
| self._noise = self.R.normal(self.mean, self.R.uniform(0, self.std), size=im_shape) |
where
im_shape=(1,128,128,128).
A Simple Solution
By replacing
| self.mean = ensure_tuple_size(mean, len(self.keys)) |
by
self.mean = mean the error does not appear anymore.
Expected behavior
The reason why the tests pass with the current version of the code is that the test https://github.com/Project-MONAI/MONAI/blob/master/tests/test_rand_gaussian_noised.py only contains examples with len(keys)==1.
In addition, in
MONAI/monai/transforms/intensity/dictionary.py
Lines 131 to 133 in 8207e1e
| for key in self.keys: | |
| dtype = dtype_torch_to_numpy(d[key].dtype) if isinstance(d[key], torch.Tensor) else d[key].dtype | |
| d[key] = d[key] + self._noise.astype(dtype) |
the same sample of Gaussian noise is used for all the keys.
I would have expected that different noise samples would be used with different keys.
Is it the intended behavior for this transformation?
I can work on a pull request if you can confirm which behavior you would like to see here.
Thank you,
Lucas