cyto_dl.models.im2im.utils.instance_seg module#

class cyto_dl.models.im2im.utils.instance_seg.InstanceSegCluster(dim: int = 3, anisotropy: float = 2.6, skel_threshold: float = 0, semantic_threshold: float = 0, min_size: int = 1000, distance_threshold: int = 100, progress: bool = True)[source]#

Bases: object

Clustering for InstanceSeg - finds skeletons and assigns semantic points to skeleton based on spatial embedding and nearest neighbor distances.

cluster_object(semantic, skel, embedding)[source]#
kd_clustering(embeddings, skel)[source]#

assign embedded points to closest skeleton.

remove_small_skeletons(skel)[source]#

remove small skeletons below self.min_size that are not touching the edge of the image.

class cyto_dl.models.im2im.utils.instance_seg.InstanceSegLoss(dim: int = 3, weights: Dict[str, float] | None = {})[source]#

Bases: object

Loss function for InstanceSeg.

Parameters:
  • dim (int=3) – Spatial dimension of input images.

  • weights (Optional[Dict[str, float]]={}) – Dictionary of weights for each loss component.

class cyto_dl.models.im2im.utils.instance_seg.InstanceSegPreprocessd(label_keys: Sequence[str] | str, kernel_size: int = 3, thin: int = 5, dim: int = 3, anisotropy: float = 2.6, keep_largest: bool = True, allow_missing_keys: bool = False)[source]#

Bases: Transform

Parameters:
  • label_keys (Union[Sequence[str], str]) – Keys of instance segmentations in input dictionary to convert to Instance Seg ground truth images.

  • kernel_size (int=3) – Size of kernel for gaussian smoothing of flows

  • thin (int=5) – Amount to thin to create psuedo-skeleton

  • dim (int=3) – Spatial dimension of images

  • anisotropy (float=2.6) – Anisotropy of images

  • keep_largest (bool=True) – Whether to keep only the largest connected component of each label

  • allow_missing_keys (bool=False) – Whether to raise error if key in label_keys is not present

embed_from_skel(skel: ndarray, iseg: ndarray)[source]#

Find per-pixel embedding vector to closest point on skeleton.

keep_largest_cc(img)[source]#
label_2d(img)[source]#

dim = 2: return labeled image dim = 3: label each z slice separately

shrink(im)[source]#

Topology-preserving thinning of a binary image.

skeleton_tall(img, max_label)[source]#

Skeletonize 3d image with increased thickness in z.

smooth_embedding(embedding)[source]#

Smooths embedding by convolving with a mean kernel, excluding non-object pixels.

topology_preserving_thinning(bw, min_size=100)[source]#

Topology-preserving thinning of a binary image.

Use skeleton to bridge gaps created by erosion.

class cyto_dl.models.im2im.utils.instance_seg.InstanceSegRandFlipd(spatial_axis: int, label_keys: str | Sequence[str] = [], image_keys: str | Sequence[str] = [], prob: float = 0.5, dim: int = 3, allow_missing_keys: bool = False)[source]#

Bases: RandomizableTransform

Flipping Augmentation for InstanceSeg training.

When flipping ground truths generated by InstanceSegPreprocessD, the sign of gradients have to be changed after flipping.

Parameters:
  • spatial_axis (int) – axis to flip across

  • label_keys (Union[str, Sequence[str]]=[]) – key or list of keys generated by InstanceSegPreprocessD to flip

  • image_keys (Union[str, Sequence[str]]=[]) – key or list of keys NOT generated by InstanceSegPreprocessd to flip

  • prob (float=0.1) – probability of flipping

  • dim (int=3) – spatial dimensions of images

  • allow_missing_keys (bool=False) – Whether to raise error if a provided key is missing

cyto_dl.models.im2im.utils.instance_seg.pad_slice(s, padding, constraints)[source]#