Prepare training data¶
Once you have annotations, turn them into a training dataset: pairs of images and label masks, split into training, validation and (if you annotated one) test folders. The same layout works for micro-SAM, BiaPy, DL4MicEverywhere and most other segmentation tools.
my_training_data/
├── train_input/ 4ba674d4_0_0.tif ← image
├── train_label/ 4ba674d4_0_0.tif ← label mask with the same name
├── val_input/
├── val_label/
├── test_input/ ← only when the set has test units
└── test_label/
Labels are instance masks: 0 is background, and each object has its own number.
Export a training set¶
One call exports any training set: by table ID for a set on OMERO (the data is downloaded, from any computer that can reach the server, no display needed), or by folder for a local set (offline).
from omero_annotate_ai import export_training_set
result = export_training_set(table_id, "my_training_data", conn) # OMERO set
result = export_training_set(ts.bundle_dir, "my_training_data") # local set
print(result["stats"])
Only finished units with a mask are used. Each unit goes to the split recorded in the
table's category column (training, validation or test), the split chosen when the
training set was created. The output folder must be outside the set's folder.
Channels and normalisation come from the set's config. For an OMERO set you can train on
another channel than the annotated one with training_channels=[c] (one channel) and
label_channel=; a local set exports the channels it was annotated with. An output
folder inside the set's own folder is refused. apply_normalization=True writes
normalised float32 images instead of the native bit depth.
In Jupyter: the training-data widget¶
from omero_annotate_ai import (
create_omero_connection_widget,
create_training_data_widget,
)
conn_widget = create_omero_connection_widget()
conn_widget.display()
conn = conn_widget.get_connection()
training_widget = create_training_data_widget(connection=conn)
training_widget.display()
Pick the container and the training set (local sets are listed too); its progress per
split is shown. Check the output folder (default ~/omero_annotate_ai/exports/<name>_<table id>, or
the set's folder name for a local set),
the channels and the normalisation, then click Export. The result is in
training_widget.result. The marimo training notebook offers the same choices.
Things to know
- Images keep their original bit depth unless you apply the normalisation at
export.
normalization.jsonrecords how to scale them; micro-SAM training applies it when loading (see normalisation settings). - A label that no longer matches its recorded pixel digest stops the export, so a mask changed after upload is never trained on unnoticed.
- For an OMERO set a copy of the tracking table is saved alongside (
table_<id>.csv). export_training_setcallsprepare_training_data_from_table(OMERO) orreorganize_local_data_for_training(local); use those directly for more options, such as copying, moving or symlinking local files.
Train a model¶
micro-SAM¶
Fine-tune micro-SAM on your annotations so its suggestions fit your images better. A CUDA GPU is strongly recommended.
from omero_annotate_ai import setup_training, run_training
training_config = setup_training(
result, # from export_training_set
model_name="hela_nuclei_v1",
model_type="vit_b_lm", # start from this pretrained model
epochs=50,
batch_size=2,
patch_shape=(512, 512),
)
training_results = run_training(training_config, framework="microsam")
epochs sets the training length. Pass n_iterations= instead for a fixed number of
steps, for example n_iterations=10 for a quick check that everything runs; it
overrides epochs.
The trained model is saved as <output_dir>/<model_name>_final.pt, with checkpoints in
<output_dir>/checkpoints/. Load it in the micro-SAM napari plugin by choosing the
checkpoint as a custom model. Export to the BioImage Model Zoo format with micro-SAM's own tools.
Notebook: micro-SAM training (marimo)
BiaPy¶
BiaPy trains instance segmentation models (U-Net and others) from a YAML file. Point its data paths at the prepared folders:
DATA:
TRAIN:
PATH: my_training_data/train_input
GT_PATH: my_training_data/train_label
VAL:
FROM_TRAIN: False
PATH: my_training_data/val_input
GT_PATH: my_training_data/val_label
Notebook: export for BiaPy or DL4MicEverywhere
DL4MicEverywhere¶
DL4MicEverywhere runs ZeroCostDL4Mic notebooks (StarDist, Cellpose, U-Net, …) in containers. Give it the prepared folder as its data directory.
Notebook: export for BiaPy or DL4MicEverywhere
Tips¶
- Keep at least one validation image (
validate_n: 1or more); micro-SAM training fails with an empty validation folder. - A few well-annotated images go a long way when fine-tuning micro-SAM. Start with 5 to 10 and add more where the model still fails.