Skip to content

Commit ac0f2bf

Browse files
committed
Accelerate pmap-guided removal of instances
To enable the use of my function on label with more instances (30k in my case), it has to be speed up.
1 parent a09d752 commit ac0f2bf

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

plantseg/dataprocessing/functional/advanced_dataprocessing.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,24 @@ def remove_false_positives_by_foreground_probability(segmentation: np.array,
263263
instances, _, _ = relabel_sequential(segmentation)
264264

265265
regions = regionprops(instances)
266-
for region in regions:
266+
to_keep = np.ones(len(regions) + 1)
267+
pixel_count = np.zeros(len(regions) + 1)
268+
pixel_value = np.zeros(len(regions) + 1)
269+
270+
for region in tqdm.tqdm(regions):
267271
bbox = region.bbox
268272
cube = instances[bbox[0]:bbox[3], bbox[1]:bbox[4], bbox[2]:bbox[5]] == region.label # other instances may exist, don't use `> 0`
269273
prob = foreground[bbox[0]:bbox[3], bbox[1]:bbox[4], bbox[2]:bbox[5]]
270-
pixel_count = region.area
271-
pixel_value = (cube * prob).sum()
272-
likelihood = pixel_value / pixel_count
273-
if likelihood < threshold:
274-
instances[instances == region.label] = 0
275-
print(f" Removing instance {region.label}: pixel count: {pixel_count}, pixel value: {pixel_value}, likelihood: {likelihood}")
274+
pixel_count[region.label] = region.area
275+
pixel_value[region.label] = (cube * prob).sum()
276+
277+
likelihood = pixel_value / pixel_count
278+
to_keep[likelihood < threshold] = 0
279+
ids_to_delete = np.argwhere(to_keep == 0)
280+
assert ids_to_delete.shape[1] == 1
281+
ids_to_delete = ids_to_delete.flatten()
282+
# print(f" Removing instance {region.label}: pixel count: {pixel_count}, pixel value: {pixel_value}, likelihood: {likelihood}")
276283

284+
instances[np.isin(instances, ids_to_delete)] = 0
277285
instances, _, _ = relabel_sequential(instances)
278286
return instances

0 commit comments

Comments
 (0)