Skip to content

Shape mismatch ValueError when using RandGaussianNoised with more than one key #1512

Closed
@LucasFidon

Description

@LucasFidon

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions