Annotate with micro-SAM¶
This is the main tutorial. You will connect to OMERO, choose images, annotate them in napari with micro-SAM, and find the results back in OMERO. It takes about 20 minutes plus the time you spend annotating.
Before you start
- OMERO Annotate AI is installed and Jupyter is running.
- You can log in to an OMERO server and know the ID of a dataset, project, plate or screen with images (visible in OMERO.web next to the name).
- You are on a machine with a screen: napari opens a separate window.
The steps below follow the notebook
omero-annotate-ai-annotation-widget.ipynb.
Download it, or copy the code blocks into a new notebook.
1. Connect to OMERO¶
from omero_annotate_ai import (
TrainingSet,
create_omero_connection_widget,
create_workflow_widget,
)
conn_widget = create_omero_connection_widget()
conn_widget.display()
Fill in the server address, your username and password, and click Connect. Instead of username and password you can paste a session key, e.g. from a session you started in OMERO.web.
With Stay logged in on this computer ticked (the default), the login is saved as a
token: next time choose it under Saved login and click Log in, no password
needed. Tokens are kept per server and user, so you can save logins for several OMERO
servers and still log in to another one with the form. A token ends after 7 days
without use; Forget removes it and ends it on the server. Tokens are in
~/.omero-annotate-ai/tokens.json, readable only by you: anyone who can read that file
can use your OMERO account until the token ends, so do not tick the box on a shared
account. A token made with
omero-user-token set is offered
too.

The widget remembers the server and username for next time (in
~/.omero-annotate-ai/connections.json), but never your password.
Then, in the next cell:
2. Configure the run¶
The widget has one screen, in cards:
Data. Find the data on the server:
- Group switches the session to another of your groups (an administrator sees all groups); Owner shows your containers (Me, the default), everyone's (All members) or one colleague's.
- Container is the type (project, dataset, plate, screen). Search filters the list by name or ID as you type. Go to ID finds a container by its ID in any of your groups and switches to that group.
- Pick one container, or Ctrl/Shift-click several of the same type for one training set
over all of them (
omero.container_ids). Picks stay picked while you change the search.
A line below shows the number of images, a sample image's dimensions and its channel names.
New training set or Open training set. For a first run choose New:

| Setting | What to choose |
|---|---|
| Name | A short, descriptive name such as hela_nuclei_v1; used for the OMERO table and the local folder. |
| Annotate on | The channel you will draw on, by name, e.g. the DAPI channel for nuclei. |
| Timepoints, Z-slices | Which planes to annotate: Specific (a list such as 0, 3, 5-7), All, or Random (n drawn per image with the seed). For 2D images leave them at Specific 0. |
| 3D | Annotate one volume over the chosen z-slices instead of single planes (e.g. z-slices Specific 10-20, or All). |
| Patches | Annotate crops instead of whole images, with patch size and patches per image. Recommended for large images or a CPU-only computer. |
| Training / Validation / Test | How many images to annotate for each split. Start small, e.g. 5 and 2. |
| Seed | Empty picks one at random and stores it, so the selection can be repeated. |
| Storage | OMERO uploads masks and keeps a tracking table; Local folder only writes nothing to OMERO. |
Two folded sections hold the advanced options:
- Channels. One row per channel. Train on: the channels the model gets as input
(
sampling.input_channels; by default only the annotated one). Show and Colour: extra channels napari shows next to the annotated one, e.g. a membrane stain while drawing nuclei (display; never used for training). Below: the intensity normalisation (percentiles, or none, and whether to apply it at export). - Plate wells (plates and screens). Keep or leave out wells by their metadata
(
omero.well_filters). Keys and values come from the wells' key-value annotations; assay conditions added with MIHCSME_py (cell line, treatment, dose, ...) come first. Conditions combine with AND; the number of wells kept updates as you pick.
Open lists the training sets on the container (and local ones). Selecting one shows its progress per split and the notes of earlier sessions; you can add images per split, and choose Annotate pending (the default) or Review annotated (see Correct earlier annotations).

Annotation. The SAM model (vit_b_lm for light microscopy, vit_b_em_organelles for
electron microscopy) and the batch size (images per napari session, 0 = all; the masks of a
batch are saved and uploaded when you close its napari window). Click Create or Open.
Not sure what a setting means?
How it works explains annotation units, patches and the two modes. The configuration reference lists every option.
3. Annotate in napari¶
ts = workflow_widget.training_set()
workflow_widget.annotate(note="nuclei only") # the note is stored with this session
Progress appears in the widget's Progress card. After the session the note box shows
the session's note; edit it and click Save session note to replace it (one note per
session). To change an earlier session's note, open the set and pick the session next to
the note box. Without the widget, TrainingSet.create(config, conn)
and ts.annotate(...) do the same.
Creating the set:
- picks images (reproducibly, from the stored seed) and assigns them to training, validation and test,
- creates the tracking table in OMERO (not for
storage: local) and the set's folder,
and annotate downloads the first batch and opens it in napari with the micro-SAM annotator. To continue
later, open the set again with TrainingSet.open(ts.table_id, conn).
The first time, micro-SAM downloads its model (a few hundred MB). For each image it then computes an embedding; this is what makes the clicks fast afterwards, and takes a few seconds on a GPU or longer on a CPU.
Correct earlier annotations¶
To check or fix masks you already saved, open the set, choose Review annotated and
run the same cell, or call ts.review():
ts.review(note="tightened the outlines") # every annotated unit
ts.review(ids=["4ba674d4_0_0"]) # or only some (annotation IDs)
Each unit opens in napari with its saved mask (downloaded from OMERO if your folder lacks it); correct it with micro-SAM or napari's label tools and go to the next image as usual. A changed mask replaces the old label file and ROI on OMERO; an unchanged one is left alone. The session is recorded as a review, with the number of masks it changed.
Start from Cellpose or StarDist (Segment with model)¶
With napari-multi-segmenter
installed, a Segment with model dock opens (left, below the layer list). It runs Cellpose 4,
Cellpose 3 or StarDist on the image being annotated and puts the result in micro-SAM's
auto_segmentation layer: commit it there, correct the objects with micro-SAM, then go to
the next image (or Accept in the napari plugin). The proposal is cleared when the next
image opens.
pip install git+https://github.com/maartenpaul/napari-multi-segmenter.git
# the models run in their own environments, made by the multi-segmenter's setup:
git clone https://github.com/maartenpaul/napari-multi-segmenter && cd napari-multi-segmenter && ./setup.sh
export SEGMENTER_ENVS=$PWD/envs
For each image saved after a proposal, the tracking table records the model and its
parameters in the column segmented_with, e.g.
stardist:2D_versatile_fluo prob_thresh=0.5 nms_thresh=0 pmin=1 pmax=99.8. A model you
trained yourself (own model under Settings in the dock) is recorded by name and a short checksum of
its file, so a set says which version proposed its masks. The exported set description
lists these models as tools and calls the method semi-automatic.
This also works as a loop: annotate a first batch, train a model on it, then let that model propose the objects of the next batch and correct them.
In napari¶
The micro-SAM image series annotator shows one image at a time.
- With the
point_promptslayer selected, click on an object to place a positive point, or use thepromptslayer to draw a box around it. Press T to switch to negative points, which tell SAM what is not part of the object. - Press S to segment the object from your prompts. Correct it by adding more points and pressing S again.
- Press C to commit the object. It is added to the label layer and gets its own colour. (Shift+C clears the current prompts and segmentation.)
- Repeat for every object. To speed things up, use Automatic Segmentation in the side panel, then fix mistakes with napari's label tools.
- Click Next Image (or press N) to save the mask and go to the next image. In 3D mode, Shift+S extends the segmentation through the volume.
When the last image of the batch is done, close the napari window. The masks are then uploaded and the next batch, if any, opens.
See the micro-SAM documentation for all annotator features and shortcuts.
The notebook cell waits for napari
The cell keeps running until you close the last napari window. That is normal.
4. Check the results¶
In the notebook:
done = sum(row.processed for row in ts.units())
print(f"{done} of {len(ts.units())} units annotated; folder: {ts.bundle_dir}")
In OMERO.web (storage: omero):
- Each annotated image now has ROIs (open it in OMERO.iviewer) and a mask file in its attachments.
- The container has a table named after your training set, listing every unit and whether it is done, plus the set's configuration file as an attachment.
Every set also has a local folder (ts.bundle_dir) with the images and masks in
annotation_input/ and annotation_output/, a tracking_table.csv and its
config.yaml.
5. What next?¶
- Stopped half-way? Resume the run.
- Want to repeat it on another dataset? Save the config and run it from YAML.
- Enough annotations? Prepare training data and fine-tune a model on your own images.
Variations¶
Annotate one channel, train on another
Draw nuclei on a fluorescent channel but train a model that predicts them from
brightfield: set sampling.label_channel to the fluorescent channel and
sampling.input_channels to the brightfield channel. The brightfield images are
saved in model_input/.
Show extra channels for context
display.context_channels are shown in napari next to the channel you annotate
(e.g. a membrane stain while you draw cytoplasm) but never used for training.
Only some wells of a plate
Keep only wells whose key-value pairs match:
3D volumes
Each timepoint becomes one volumetric unit and opens in micro-SAM's 3D annotator.Without the widgets
import getpass
from omero_annotate_ai import TrainingSet, load_config_from_yaml
from omero_annotate_ai.omero.simple_connection import SimpleOMEROConnection
conn = SimpleOMEROConnection().connect(
host="omero.example.org", username="me", password=getpass.getpass()
)
# or, with a login saved earlier ("Stay logged in"): no password
# conn = SimpleOMEROConnection().connect_saved(host="omero.example.org")
config = load_config_from_yaml("annotation_config.yaml")
ts = TrainingSet.create(config, conn)
ts.annotate()