Skip to content

Fine tune on your own data

Prepare separate directories of grayscale TIFF images and integer instance masks. Matching files have the same name before .tif or .tiff, and each image and mask must have the same shape. In masks, 0 is background and each positive integer identifies a cell.

data/
  images/sample1.tif
  images/sample2.tif
  masks/sample1.tif
  masks/sample2.tif
morphodeep finetune --images data/images --masks data/masks \
  --output runs/my_cells --mode 2D --epochs 20 \
  --initial-weights /path/to/model.h5

Without separate validation folders, the command needs at least two pairs and splits complete images reproducibly into training and validation groups. For related images, supply independent --validation-images and --validation-masks directories. Use --mode 3D for volumes.

The run writes best.weights.h5, history.csv, and run.json in the output directory. Apply the trained weights with:

morphodeep predict image.tif labels.tif \
  --weights runs/my_cells/best.weights.h5

The equivalent Python entry point is:

from morphodeep.training import fine_tune

result = fine_tune(
    "data/images", "data/masks", "runs/my_cells",
    mode="2D", epochs=20, initial_weights="/path/to/model.h5",
)
print(result.weights_path)

See the fine tuning API for the remaining options.