Skip to content

Commit

Permalink
Merge ef5fdcb into 34dcb1b
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvalen committed Apr 21, 2020
2 parents 34dcb1b + ef5fdcb commit ce95acb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 5 additions & 1 deletion deepcell/image_generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def _transform_masks(y, transform, data_format=None, **kwargs):
elif transform == 'centroid':
erosion = kwargs.pop('erosion_width', 0)
disk_size = kwargs.pop('disk_size', 4)
alpha = kwargs.pop('alpha', 0.1)
auto_alpha = kwargs.pop('auto_alpha', False)

if data_format == 'channels_first':
y_transform = np.zeros(tuple([y.shape[0]] + list(y.shape[2:])))
else:
Expand All @@ -190,7 +193,8 @@ def _transform_masks(y, transform, data_format=None, **kwargs):
else:
mask = y[batch, ..., 0]

y_transform[batch] = _transform(mask, erosion)
y_transform[batch] = _transform(mask, erosion_width=erosion,
alpha=alpha, auto_alpha=auto_alpha)

y_transform = np.expand_dims(y_transform, axis=-1)

Expand Down
22 changes: 19 additions & 3 deletions deepcell/utils/transform_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,20 @@ def centroid_transform_continuous_2d(mask, erosion_width=None, alpha=0.1):
Args:
mask (numpy.array): a label mask (y data)
erosion_width (int): number of pixels to erode edges of each labels
alpha (float): coefficent to reduce the magnitude of the distance value.
alpha (float, str): coefficent to reduce the magnitude of the distance
value. If 'auto', determines alpha for each cell based on the cell
area. Defaults to 0.1.
Returns:
numpy.array: a mask of same shape as input mask,
with each label being a distance class from 1 to bins
"""

# Check input to alpha
if isinstance(alpha, str):
if alpha.lower() != 'auto':
raise ValueError('alpha must be set to "auto"')

mask = np.squeeze(mask)
mask = erode_edges(mask, erosion_width)

Expand All @@ -253,7 +261,14 @@ def centroid_transform_continuous_2d(mask, erosion_width=None, alpha=0.1):
coords = prop.coords
center = prop.weighted_centroid
distance_to_center = np.sum((coords - center) ** 2, axis=1)
center_transform = 1 / (1 + alpha * distance_to_center)

# Determine alpha to use
if str(alpha).lower() == 'auto':
_alpha = 1 / np.sqrt(prop.area)
else:
_alpha = float(alpha)

center_transform = 1 / (1 + _alpha * distance_to_center)
coords_x = coords[:, 0]
coords_y = coords[:, 1]
inner_distance[coords_x, coords_y] = center_transform
Expand All @@ -267,7 +282,8 @@ def centroid_transform_continuous_movie(mask, erosion_width=None, alpha=0.1):
Args:
mask (numpy.array): a label mask (y data)
erosion_width (int): number of pixels to erode edges of each labels
alpha (float): coefficent to reduce the magnitude of the distance value.
alpha (float): coefficent to reduce the magnitude of the distance
value.
Returns:
numpy.array: a mask of same shape as input mask,
Expand Down

0 comments on commit ce95acb

Please sign in to comment.