juspice.segmentation_module

Deep-learning segmentation and particle tracking.

The public interface is UNetSegmentationAccessor, available as spice.segmentation. It offers:

  • train_unet: train a U-Net on image/mask folders;

  • run_sam1 / run_sam2: segment with Meta’s Segment Anything models;

  • run_trackpy: track particles across frames with Trackpy;

  • run_micronet: train and apply a NASA MicroNet-pretrained model.

The module also holds the helpers these use: Dataset, UNetModel, training and test loops, and Albumentations pipelines. Deep-learning packages are optional and only needed by the methods that use them.

Examples

Train a U-Net:

spice = load_data(train_images[0])
seg_spice = spice.segmentation.train_unet(
    train_images_dir=x_train_dir,
    train_masks_dir=y_train_dir,
    arch='Unet',
    encoder_name='resnet34',
    epochs=20,
    device=device,
)
# seg_spice.data  # shape (epochs, 2) — [train_loss, val_loss] per row

Segment with SAM-1 from a point prompt:

spice = load_data('image.png')
mask_spice = spice.segmentation.run_sam1(
    model_path='sam_vit_h_4b8939.pth',
    input_point=[[400, 300]],
    device='cpu',
)