MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / get_label_ccp

Function get_label_ccp

monai/auto3dseg/utils.py:105–145  ·  view source on GitHub ↗

Find all connected components and their bounding shape. Backend can be cuPy/cuCIM or Numpy depending on the hardware. Args: mask_index: a binary mask. use_gpu: a switch to use GPU/CUDA or not. If GPU is unavailable, CPU will be used regardless of this settin

(mask_index: MetaTensor, use_gpu: bool = True)

Source from the content-addressed store, hash-verified

103
104
105def get_label_ccp(mask_index: MetaTensor, use_gpu: bool = True) -> tuple[list[Any], int]:
106 """
107 Find all connected components and their bounding shape. Backend can be cuPy/cuCIM or Numpy
108 depending on the hardware.
109
110 Args:
111 mask_index: a binary mask.
112 use_gpu: a switch to use GPU/CUDA or not. If GPU is unavailable, CPU will be used
113 regardless of this setting.
114
115 """
116 skimage, has_cucim = optional_import("cucim.skimage")
117 shape_list = []
118 if mask_index.device.type == "cuda" and has_cp and has_cucim and use_gpu:
119 mask_cupy = ToCupy()(mask_index.short())
120 labeled = skimage.measure.label(mask_cupy)
121 vals = cp.unique(labeled[cp.nonzero(labeled)])
122
123 for ncomp in vals:
124 comp_idx = cp.argwhere(labeled == ncomp)
125 comp_idx_min = cp.min(comp_idx, axis=0).tolist()
126 comp_idx_max = cp.max(comp_idx, axis=0).tolist()
127 bbox_shape = [comp_idx_max[i] - comp_idx_min[i] + 1 for i in range(len(comp_idx_max))]
128 shape_list.append(bbox_shape)
129 ncomponents = len(vals)
130
131 del mask_cupy, labeled, vals, comp_idx, ncomp
132 cp.get_default_memory_pool().free_all_blocks()
133
134 elif has_measure:
135 labeled, ncomponents = measure_np.label(mask_index.data.cpu().numpy(), background=-1, return_num=True)
136 for ncomp in range(1, ncomponents + 1):
137 comp_idx = np.argwhere(labeled == ncomp)
138 comp_idx_min = np.min(comp_idx, axis=0).tolist()
139 comp_idx_max = np.max(comp_idx, axis=0).tolist()
140 bbox_shape = [comp_idx_max[i] - comp_idx_min[i] + 1 for i in range(len(comp_idx_max))]
141 shape_list.append(bbox_shape)
142 else:
143 raise RuntimeError("Cannot find one of the following required dependencies: {cuPy+cuCIM} or {scikit-image}")
144
145 return shape_list, ncomponents
146
147
148def concat_val_to_np(

Callers 1

__call__Method · 0.90

Calls 3

optional_importFunction · 0.90
ToCupyClass · 0.90
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…