Skip to content
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
6 changes: 3 additions & 3 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ def generate_pos_neg_label_crop_centers(
raise ValueError("No sampling location available.")

if len(fg_indices) == 0 or len(bg_indices) == 0:
pos_ratio = 0 if len(fg_indices) == 0 else 1
warnings.warn(
f"N foreground {len(fg_indices)}, N background {len(bg_indices)},"
"unable to generate class balanced samples."
f"Num foregrounds {len(fg_indices)}, Num backgrounds {len(bg_indices)}, "
f"unable to generate class balanced samples, setting `pos_ratio` to {pos_ratio}."
)
pos_ratio = 0 if fg_indices.size == 0 else 1

for _ in range(num_samples):
indices_to_use = fg_indices if rand_state.rand() < pos_ratio else bg_indices
Expand Down
15 changes: 14 additions & 1 deletion tests/test_generate_pos_neg_label_crop_centers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@
list,
2,
3,
]
],
[
{
"spatial_size": [2, 2, 2],
"num_samples": 2,
"pos_ratio": 0.0,
"label_spatial_shape": [3, 3, 3],
"fg_indices": [],
"bg_indices": [3, 12, 21],
},
list,
2,
3,
],
]


Expand Down