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

Add utils for vista3d #7999

Merged
Merged
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3f91409
update mlp block
yiheng-wang-nv Aug 6, 2024
d241db5
add mypy fix
yiheng-wang-nv Aug 6, 2024
1af03f6
remove gelu approximate
yiheng-wang-nv Aug 7, 2024
ac26c78
Merge branch 'dev' into enhance-mlpblock
yiheng-wang-nv Aug 7, 2024
4dc89e5
free space
KumoLiu Aug 7, 2024
b5400b9
ignore test case type annotation
yiheng-wang-nv Aug 7, 2024
906fd16
Merge branch 'enhance-mlpblock' of github.com:yiheng-wang-nv/MONAI in…
yiheng-wang-nv Aug 7, 2024
c53f038
try to fix
KumoLiu Aug 7, 2024
90c7888
Add utils for vista3d
yiheng-wang-nv Aug 7, 2024
12a1e66
add convert_points_to_disc only and fix type issue
yiheng-wang-nv Aug 7, 2024
0af343d
Merge remote-tracking branch 'origin/enhance-mlpblock' into add-utils…
yiheng-wang-nv Aug 7, 2024
87d7363
Merge branch 'dev' into add-utils-for-vista3d
yiheng-wang-nv Aug 7, 2024
681e875
Update monai/transforms/utils.py
yiheng-wang-nv Aug 8, 2024
e069552
update more functions and change morphological path
yiheng-wang-nv Aug 8, 2024
32f37f7
Merge branch 'add-utils-for-vista3d' of github.com:yiheng-wang-nv/MON…
yiheng-wang-nv Aug 8, 2024
dc004cb
add get_largest_connected_component_mask_point
yiheng-wang-nv Aug 8, 2024
503bdd7
add tests
yiheng-wang-nv Aug 8, 2024
e6991fa
Merge branch 'dev' into add-utils-for-vista3d
yiheng-wang-nv Aug 8, 2024
f34bc42
adjust tests
yiheng-wang-nv Aug 8, 2024
53287bb
Merge branch 'add-utils-for-vista3d' of github.com:yiheng-wang-nv/MON…
yiheng-wang-nv Aug 8, 2024
496e0a9
update doc
yiheng-wang-nv Aug 8, 2024
4a8489a
add tests
yiheng-wang-nv Aug 9, 2024
7ea02e5
Merge branch 'dev' into add-utils-for-vista3d
yiheng-wang-nv Aug 9, 2024
021466f
Merge branch 'dev' into add-utils-for-vista3d
KumoLiu Aug 9, 2024
06a0b84
Update monai/transforms/utils.py
yiheng-wang-nv Aug 9, 2024
b84302e
skip if quick
yiheng-wang-nv Aug 9, 2024
cacbe6c
use to device
yiheng-wang-nv Aug 9, 2024
901ca09
Merge branch 'dev' into add-utils-for-vista3d
yiheng-wang-nv Aug 9, 2024
2ac7917
Merge branch 'dev' into add-utils-for-vista3d
KumoLiu Aug 9, 2024
7d15188
add .float before round
yiheng-wang-nv Aug 9, 2024
ffe2bac
Merge branch 'add-utils-for-vista3d' of github.com:yiheng-wang-nv/MON…
yiheng-wang-nv Aug 9, 2024
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
41 changes: 41 additions & 0 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import numpy as np
import torch
from torch import Tensor

import monai
from monai.config import DtypeLike, IndexSelection
Expand Down Expand Up @@ -65,6 +66,8 @@
min_version,
optional_import,
pytorch_after,
unsqueeze_left,
unsqueeze_right,
)
from monai.utils.enums import TransformBackends
from monai.utils.type_conversion import (
Expand Down Expand Up @@ -103,6 +106,7 @@
"generate_spatial_bounding_box",
"get_extreme_points",
"get_largest_connected_component_mask",
"convert_points_to_disc",
"remove_small_objects",
"img_bounds",
"in_bounds",
Expand Down Expand Up @@ -1172,6 +1176,43 @@ def get_largest_connected_component_mask(
return convert_to_dst_type(out, dst=img, dtype=out.dtype)[0]


def convert_points_to_disc(
image_size: Sequence[int], point: Tensor, point_label: Tensor, radius: int = 2, disc: bool = False
):
"""
Convert a 3D point coordinates into image mask. The returned mask has the same spatial
size as `image_size` while the batch dimension is the same as 'point' batch dimension.
The point is converted to a mask ball with radius defined by `radius`. The output
contains two channels each for negative (first channel) and positive points.

Args:
image_size: The output size of the converted mask. It should be a
point: [b, N, 3]
point_label: [b, N], 0 or 2 means negative points, 1 or 3 means postive points.
radius: disc ball radius size
disc: If true, use regular disc other other use gaussian.
yiheng-wang-nv marked this conversation as resolved.
Show resolved Hide resolved
"""
masks = torch.zeros([point.shape[0], 2, image_size[0], image_size[1], image_size[2]], device=point.device)
_array = [
torch.arange(start=0, end=image_size[i], step=1, dtype=torch.float32, device=point.device) for i in range(3)
]
yiheng-wang-nv marked this conversation as resolved.
Show resolved Hide resolved
coord_rows, coord_cols, coord_z = torch.meshgrid(_array[2], _array[1], _array[0])
# [1, 3, h, w, d] -> [b, 2, 3, h, w, d]
coords = unsqueeze_left(torch.stack((coord_rows, coord_cols, coord_z), dim=0), 6)
coords = coords.repeat(point.shape[0], 2, 1, 1, 1, 1)
for b in range(point.shape[0]):
for n in range(point.shape[1]):
yiheng-wang-nv marked this conversation as resolved.
Show resolved Hide resolved
point_bn = unsqueeze_right(point[b, n], 6)
if point_label[b, n] > -1:
channel = 0 if (point_label[b, n] == 0 or point_label[b, n] == 2) else 1
yiheng-wang-nv marked this conversation as resolved.
Show resolved Hide resolved
pow_diff = torch.pow(coords[b, channel] - point_bn[b, n], 2)
if disc:
masks[b, channel] += pow_diff.sum(0) < radius**2
else:
masks[b, channel] += torch.exp(-pow_diff.sum(0) / (2 * radius**2))
return masks


def remove_small_objects(
img: NdarrayTensor,
min_size: int = 64,
Expand Down
Loading