Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for treating background as an object #101

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading