Skip to content

Commit

Permalink
improve speed of ConvertSegmentationToRegionsTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianIsensee committed Apr 25, 2024
1 parent 65bad9d commit f14188b
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ def __init__(self, regions: Union[List, Tuple],

def __call__(self, **data_dict):
seg = data_dict.get(self.seg_key)
num_regions = len(self.regions)
if seg is not None:
seg_shp = seg.shape
output_shape = list(seg_shp)
output_shape[1] = num_regions
region_output = np.zeros(output_shape, dtype=seg.dtype)
for b in range(seg_shp[0]):
for region_id, region_source_labels in enumerate(self.regions):
if not isinstance(region_source_labels, (list, tuple)):
region_source_labels = (region_source_labels, )
for label_value in region_source_labels:
region_output[b, region_id][seg[b, self.seg_channel] == label_value] = 1
data_dict[self.output_key] = region_output
b, c, *shape = seg.shape
region_output = np.zeros((b, len(self.regions), *shape), dtype=bool)
for region_id, region_labels in enumerate(self.regions):
if not isinstance(region_labels, (list, tuple)):
region_labels = (region_labels, )
for label_value in region_labels:
region_output[:, region_id] |= (seg[:, self.seg_channel] == label_value)
data_dict[self.output_key] = region_output.astype(np.uint8, copy=False)
return data_dict

0 comments on commit f14188b

Please sign in to comment.