-
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
Modify MeanEnsemble to accumulate results in-place. #8141
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -679,17 +679,21 @@ def __init__(self, weights: Sequence[float] | NdarrayOrTensor | None = None) -> | |
self.weights = torch.as_tensor(weights, dtype=torch.float) if weights is not None else None | ||
|
||
def __call__(self, img: Sequence[NdarrayOrTensor] | NdarrayOrTensor) -> NdarrayOrTensor: | ||
img_ = self.get_stacked_torch(img) | ||
if self.weights is not None: | ||
self.weights = self.weights.to(img_.device) | ||
shape = tuple(self.weights.shape) | ||
for _ in range(img_.ndimension() - self.weights.ndimension()): | ||
shape += (1,) | ||
weights = self.weights.reshape(*shape) | ||
out_pt = None | ||
total_weight = 0.0 | ||
|
||
for i, pred in enumerate(img): | ||
pred = torch.as_tensor(pred) | ||
if out_pt is None: | ||
out_pt = torch.zeros_like(pred) | ||
|
||
if self.weights is not None: | ||
weight = self.weights[i].to(pred.device) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you handle whether |
||
|
||
img_ = img_ * weights / weights.mean(dim=0, keepdim=True) | ||
out_pt += pred * weight | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
total_weight += weight | ||
|
||
out_pt = torch.mean(img_, dim=0) | ||
out_pt /= total_weight | ||
return self.post_convert(out_pt, img) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may need check the the type for img what if it's a single array. Something like the change here:
MONAI/monai/transforms/post/array.py
Lines 639 to 644 in bae8f59