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

Fix aggregate microbatches bug #711

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions batchflow/models/torch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from .initialization import best_practice_resnet_init
from .losses import CrossEntropyLoss, BinaryLovaszLoss, LovaszLoss, SSIM, MSSIM
from .losses import binary as binary_losses, multiclass as multiclass_losses
from .utils import get_shape
from .utils import get_shape, get_size
from ..base import BaseModel
from ...config import Config

Expand Down Expand Up @@ -1540,7 +1540,7 @@ def aggregate_microbatches(self, outputs, chunked_outputs, chunk_sizes, single_o
for i, _ in enumerate(outputs):
# All tensors for current `output_name`
chunked_output = [chunk_outputs[i] for chunk_outputs in chunked_outputs]
if chunked_output[0].size != 1:
if get_size(chunked_output[0]) != 1:
if len(chunked_output) == 1:
output_ = chunked_output[0][:chunk_sizes[0]]
elif isinstance(chunked_output[0], np.ndarray):
Expand All @@ -1551,7 +1551,10 @@ def aggregate_microbatches(self, outputs, chunked_outputs, chunk_sizes, single_o
for chunk_output, chunk_size in zip(chunked_output, chunk_sizes)], dim=0)
result.append(output_)
else:
result.append(np.mean(chunked_output))
if isinstance(chunked_output[0], np.ndarray):
result.append(np.mean(chunked_output))
else:
result.append(torch.mean(torch.stack(chunked_output)))

if single_output:
result = result[0]
Expand Down
12 changes: 12 additions & 0 deletions batchflow/models/torch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ def get_shape(inputs, default_shape=None):
raise TypeError(f'Inputs can be array, tensor, or sequence, got {type(inputs)} instead!')
return shape

def get_size(inputs):
""" Compute number of elements in tensor or in list of tensors """
if isinstance(inputs, np.ndarray):
size = inputs.size
elif isinstance(inputs, torch.Tensor):
size = inputs.numel()
elif isinstance(inputs, list):
size = [get_size(item) for item in inputs]
else:
raise TypeError(f'Inputs can be array, tensor, or sequence, got {type(inputs)} instead!')
return size

def get_num_channels(inputs):
""" Get number of channels in one tensor. """
return inputs.shape[1]
Expand Down
Loading