Skip to content
Merged
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
5 changes: 4 additions & 1 deletion monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ def map_classes_to_indices(
if img_flat is not None:
label_flat = img_flat & label_flat
# no need to save the indices in GPU, otherwise, still need to move to CPU at runtime when crop by indices
cls_indices: NdarrayOrTensor = convert_data_type(nonzero(label_flat), device=torch.device("cpu"))[0]
output_type = torch.Tensor if isinstance(label, monai.data.MetaTensor) else None
cls_indices: NdarrayOrTensor = convert_data_type(
nonzero(label_flat), output_type=output_type, device=torch.device("cpu")
)[0]
if max_samples_per_class and len(cls_indices) > max_samples_per_class and len(cls_indices) > 1:
sample_id = np.round(np.linspace(0, len(cls_indices) - 1, max_samples_per_class)).astype(int)
indices.append(cls_indices[sample_id])
Expand Down