Skip to content

Commit 4cc9d0d

Browse files
Merge pull request #18 from computational-cell-analytics/prompt-bg
Add Condition for Background Points
2 parents ebfc4be + 56844fb commit 4cc9d0d

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

micro_sam/prompt_generators.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ def __call__(self, gt, gt_id, center_coordinates, bbox_coordinates):
4343
# ([x1, x2, ...], [y1, y2, ...])
4444
n_coordinates = len(object_coordinates[0])
4545

46-
if n_coordinates > n_positive_remaining: # for some cases, there aren't any forground object_coordinates
47-
# randomly sampling n_positive_remaining_points from these coordinates
48-
positive_indices = np.random.choice(n_coordinates, replace=False, size=n_positive_remaining)
49-
for positive_index in positive_indices:
50-
positive_coordinates = int(object_coordinates[0][positive_index]), \
51-
int(object_coordinates[1][positive_index])
52-
53-
coord_list.append(positive_coordinates)
54-
label_list.append(1)
55-
else:
56-
print(f"{n_coordinates} fg spotted..")
46+
# randomly sampling n_positive_remaining_points from these coordinates
47+
positive_indices = np.random.choice(n_coordinates, replace=False,
48+
size=min(n_positive_remaining, n_coordinates) # handles the cases with insufficient fg pixels
49+
)
50+
for positive_index in positive_indices:
51+
positive_coordinates = int(object_coordinates[0][positive_index]), \
52+
int(object_coordinates[1][positive_index])
53+
54+
coord_list.append(positive_coordinates)
55+
label_list.append(1)
5756

5857
# getting the negative points
5958
# for this we do the opposite and we set the mask to the bounding box - the object mask
@@ -75,7 +74,9 @@ def __call__(self, gt, gt_id, center_coordinates, bbox_coordinates):
7574
n_coordinates = len(background_coordinates[0])
7675

7776
# randomly sample n_positive_remaining_points from these coordinates
78-
negative_indices = np.random.choice(n_coordinates, replace=False, size=n_negative_remaining)
77+
negative_indices = np.random.choice(n_coordinates, replace=False,
78+
size=min(n_negative_remaining, n_coordinates) # handles the cases with insufficient bg pixels
79+
)
7980
for negative_index in negative_indices:
8081
negative_coordinates = int(background_coordinates[0][negative_index]), \
8182
int(background_coordinates[1][negative_index])

0 commit comments

Comments
 (0)