Skip to content

Commit

Permalink
add support for treating background as an object (#101)
Browse files Browse the repository at this point in the history
add option to avoid predicting all 0s for affs and lsds in background.
  • Loading branch information
rhoadesScholar authored Feb 14, 2024
2 parents 06fbb68 + 45ccfd4 commit 57084e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions dacapo/experiments/tasks/affinities_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, task_config):
affs_weight_clipmax=task_config.affs_weight_clipmax,
lsd_weight_clipmin=task_config.lsd_weight_clipmin,
lsd_weight_clipmax=task_config.lsd_weight_clipmax,
background_as_object=task_config.background_as_object,
)
self.loss = AffinitiesLoss(
len(task_config.neighborhood), task_config.lsds_to_affs_weight_ratio
Expand Down
10 changes: 10 additions & 0 deletions dacapo/experiments/tasks/affinities_task_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ class AffinitiesTaskConfig(TaskConfig):
default=0.95,
metadata={"help_text": "The maximum value for lsds weights."},
)
background_as_object: bool = attr.ib(
default=False,
metadata={
"help_text": (
"Whether to treat the background as a separate object. "
"If set to false background should get an affinity near 0. If "
"set to true, the background should also have high affinity with other background."
)
},
)
13 changes: 10 additions & 3 deletions dacapo/experiments/tasks/predictors/affinities_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
affs_weight_clipmax: float = 0.95,
lsd_weight_clipmin: float = 0.05,
lsd_weight_clipmax: float = 0.95,
background_as_object: bool = False,
):
self.neighborhood = neighborhood
self.lsds = lsds
Expand All @@ -51,6 +52,8 @@ def __init__(
self.lsd_weight_clipmin = lsd_weight_clipmin
self.lsd_weight_clipmax = lsd_weight_clipmax

self.background_as_object = background_as_object

def extractor(self, voxel_size):
if self._extractor is None:
self._extractor = LsdExtractor(
Expand Down Expand Up @@ -105,10 +108,12 @@ def create_target(self, gt):
label_data = label_data[0]
else:
axes = ["c"] + axes
affinities = seg_to_affgraph(label_data, self.neighborhood).astype(np.float32)
affinities = seg_to_affgraph(
label_data + int(self.background_as_object), self.neighborhood
).astype(np.float32)
if self.lsds:
descriptors = self.extractor(gt.voxel_size).get_descriptors(
segmentation=label_data,
segmentation=label_data + int(self.background_as_object),
voxel_size=gt.voxel_size,
)
return NumpyArray.from_np_array(
Expand Down Expand Up @@ -208,7 +213,9 @@ def gt_region_for_roi(self, target_spec):
for a, b in zip(pad_pos, self.lsd_pad(target_spec.voxel_size))
]
)
gt_spec.roi = gt_spec.roi.grow(pad_neg, pad_pos)
gt_spec.roi = gt_spec.roi.grow(pad_neg, pad_pos).snap_to_grid(
target_spec.voxel_size
)
gt_spec.dtype = None
return gt_spec

Expand Down

0 comments on commit 57084e7

Please sign in to comment.