Class CellposeBuilder
Uwe Schmidt, Martin Weigert, Coleman Broaddus, and Gene Myers. "Cell Detection with Star-convex Polygons." International Conference on Medical Image Computing and Computer-Assisted Intervention (MICCAI), Granada, Spain, September 2018.See the main repo at ...
Very much inspired by stardist-imagej at ... but re-written from scratch to use OpenCV and adapt the method of converting predictions to contours (very slightly) to be more QuPath-friendly.
Models are expected in the same format as required by the Fiji plugin, or converted to a frozen .pb file for use with OpenCV.
-
Constructor Summary
ModifierConstructorDescriptionprotected
CellposeBuilder
(File builderFile) can create a cellpose builder from a serialized JSON version of this builder.protected
CellposeBuilder
(String modelPath) Build a cellpose model by providing a string which can be the name of a pretrained model or a path to a custom model -
Method Summary
Modifier and TypeMethodDescriptionaddParameter
(String flagName) Generic means of adding a cellpose parameteraddParameter
(String flagName, String flagValue) Generic means of adding a cellpose parameterDefines the batch size for trainingbuild()
Create aCellpose2D
, all ready for detection.cellConstrainScale
(double scale) Constrain any cell expansion defined usingcellExpansion(double)
based upon the nucleus size.cellExpansion
(double distance) Amount by which to expand detected nuclei to approximate the cell area.cellposeChannels
(Integer channel1, Integer channel2) Explicitly set the cellpose channels manually.cellprobThreshold
(Double threshold) /** cellprob threshold, default is 0, decrease to find more and larger maskschannels
(int... channels) Specify channels.Specify channels by name.channels
(qupath.lib.images.servers.ColorTransforms.ColorTransform... channels) Define the channels (or color transformers) to apply to the input image.Request that a classification is applied to all created objects.classify
(qupath.lib.objects.classes.PathClass pathClass) Request that a classification is applied to all created objects.compartments
(qupath.lib.analysis.features.ObjectMeasurements.Compartments... compartments) Specify the compartments within which intensity measurements are made.constrainToParent
(boolean constrainToParent) If true, constrain nuclei and cells to any parent annotation (default is true).Create annotations rather than detections (the default).The estimated diameter of the objects to detect.Defines the number of epochs for trainingExclude on edges.extendChannelOp
(qupath.opencv.ops.ImageOp extendChannelOp) flowThreshold
(Double threshold) Flow error threshold, 0 turns off this optional QC step.groundTruthDirectory
(File groundTruthDirectory) Specify the training directoryignoreCellOverlaps
(boolean ignore) If true, ignore overlaps when computing cell expansion.inputAdd
(double... values) Add an offset as a preprocessing step.inputScale
(double... values) Multiply by a scale factor as a preprocessing step.inputSubtract
(double... values) Subtract an offset as a preprocessing step.learningRate
(Double learningRate) Defines the learning ratemaskThreshold
(Double threshold) Deprecated.Request default intensity measurements are made for all available cell compartments.measureIntensity
(Collection<qupath.lib.analysis.features.ObjectMeasurements.Measurements> measurements) Request specified intensity measurements are made for all available cell compartments.Request shape measurements are made for the detected cell or nucleus.Excludes training data with less than n training masks (n labels)modelDirectory
(File modelDir) Define the directory where the newly trained model should be savedconvenience method? to deactivate cellpose normalization.normalizePercentiles
(double min, double max) Apply percentile normalization separately to the input image channels.normalizePercentiles
(double min, double max, boolean perChannel, double eps) Apply percentile normalization to the input image channels, or across all channels jointly.normalizePercentilesGlobal
(double percentileMin, double percentileMax, double normDownsample) Convenience method to call global normalization for the datasetnThreads
(int nThreads) Specify the number of threads to use for processing.pixelSize
(double pixelSize) Resolution at which the cell detection should be run.preprocess
(OpCreators.TileOpCreator global) Add anOpCreators.TileOpCreator
to generate preprocessing operations based upon the entire image, rather than per tile.preprocess
(qupath.opencv.ops.ImageOp... ops) Add preprocessing operations, if required.Use an asynchronous method to read the results from the cellpose as it writes files Can result in faster processing.saveBuilder
(String name) Save this builder as a JSON file in order to be able to reuse it in placesetOutputModelName
(String outputName) Set the final model name.setOverlap
(int overlap) Set the overlap (in pixels) between tiles.simplify
(double distance) Customize the extent to which contours are simplified.tempDirectory
(File trainingDirectory) Specify the temporary directorytileSize
(int tileSize) Size in pixels of a tile used for detection.tileSize
(int tileWidth, int tileHeight) Size in pixels of a tile used for detection.useGPU
(boolean useGPU) overwrite use GPUUse Omnipose implementation: Adds --omni flag to command
-
Constructor Details
-
CellposeBuilder
can create a cellpose builder from a serialized JSON version of this builder.- Parameters:
builderFile
- the path to a serialized JSON builder made withsaveBuilder(String)
-
CellposeBuilder
Build a cellpose model by providing a string which can be the name of a pretrained model or a path to a custom model- Parameters:
modelPath
- the model name or path
-
-
Method Details
-
extendChannelOp
-
useGPU
overwrite use GPU- Parameters:
useGPU
- add or remove the option- Returns:
- this builder
-
groundTruthDirectory
Specify the training directory -
tempDirectory
Specify the temporary directory -
nThreads
Specify the number of threads to use for processing. If you encounter problems, setting this to 1 may help to resolve them by preventing multithreading.- Parameters:
nThreads
- the number of threads to use- Returns:
- this builder
-
readResultsAsynchronously
Use an asynchronous method to read the results from the cellpose as it writes files Can result in faster processing. !!EXPERIMENTAL!! -
pixelSize
Resolution at which the cell detection should be run. The units depend upon thePixelCalibration
of the input image.The default is to use the full resolution of the input image.
For an image calibrated in microns, the recommended default is approximately 0.5.
- Parameters:
pixelSize
- Pixel size in microns for the analysis- Returns:
- this builder
-
preprocess
Add preprocessing operations, if required.- Parameters:
ops
- a series of ImageOps to apply to the input image- Returns:
- this builder
-
preprocess
Add anOpCreators.TileOpCreator
to generate preprocessing operations based upon the entire image, rather than per tile.Note that only a single such operation is permitted, which is applied after channel extraction but before any other preprocessing.
The intended use is with
OpCreators.imageNormalizationBuilder()
to perform normalization based upon percentiles computed across the image, rather than per tile.- Parameters:
global
- preprocessing operation- Returns:
- this builder
-
simplify
Customize the extent to which contours are simplified. Simplification reduces the number of vertices, which in turn can reduce memory requirements and improve performance.Implementation note: this currently uses the Visvalingam-Whyatt algorithm.
- Parameters:
distance
- simplify distance threshold; set ≤ 0 to turn off additional simplification- Returns:
- this builder
-
channels
Specify channels. Useful for detecting nuclei for one channel within a multi-channel image, or potentially for trained models that support multi-channel input.- Parameters:
channels
- 0-based indices of the channels to use- Returns:
- this builder
-
channels
Specify channels by name. Useful for detecting nuclei for one channel within a multi-channel image, or potentially for trained models that support multi-channel input.- Parameters:
channels
- 0-based indices of the channels to use- Returns:
- this builder
-
channels
public CellposeBuilder channels(qupath.lib.images.servers.ColorTransforms.ColorTransform... channels) Define the channels (or color transformers) to apply to the input image.This makes it possible to supply color deconvolved channels, for example.
- Parameters:
channels
- the channels to use- Returns:
- this builder
-
cellExpansion
Amount by which to expand detected nuclei to approximate the cell area. Units are the same as for thePixelCalibration
of the input image.Warning! This is rather experimental, relying heavily on JTS and a convoluted method of resolving overlaps using a Voronoi tessellation.
In short, be wary.
- Parameters:
distance
- expansion distance in microns- Returns:
- this builder
-
cellConstrainScale
Constrain any cell expansion defined usingcellExpansion(double)
based upon the nucleus size. Only meaningful for values > 1; the nucleus is expanded according to the scale factor, and used to define the maximum permitted cell expansion.- Parameters:
scale
-- Returns:
- this builder
-
createAnnotations
Create annotations rather than detections (the default). If cell expansion is not zero, the nucleus will be included as a child object.- Returns:
- this builder
-
classify
Request that a classification is applied to all created objects.- Parameters:
pathClass
- the classification to give to all detected PathObjects- Returns:
- this builder
-
classify
Request that a classification is applied to all created objects. This is a convenience method that get aPathClass
from a String representation.- Parameters:
pathClassName
- the classification to give to all detected PathObjects as a String- Returns:
- this builder
-
ignoreCellOverlaps
If true, ignore overlaps when computing cell expansion.- Parameters:
ignore
-- Returns:
- this builder
-
constrainToParent
If true, constrain nuclei and cells to any parent annotation (default is true).- Parameters:
constrainToParent
-- Returns:
- this builder
-
measureIntensity
Request default intensity measurements are made for all available cell compartments.- Returns:
- this builder
-
measureIntensity
public CellposeBuilder measureIntensity(Collection<qupath.lib.analysis.features.ObjectMeasurements.Measurements> measurements) Request specified intensity measurements are made for all available cell compartments.- Parameters:
measurements
- the measurements to make- Returns:
- this builder
-
measureShape
Request shape measurements are made for the detected cell or nucleus.- Returns:
- this builder
-
compartments
public CellposeBuilder compartments(qupath.lib.analysis.features.ObjectMeasurements.Compartments... compartments) Specify the compartments within which intensity measurements are made. Only effective ifmeasureIntensity()
andcellExpansion(double)
have been selected.- Parameters:
compartments
- cell compartments for intensity measurements- Returns:
- this builder
-
tileSize
Size in pixels of a tile used for detection. Note that tiles are independently normalized, and therefore tiling can impact the results. Default is 1024.- Parameters:
tileSize
- the width and height of each tile for exporting images- Returns:
- this builder
-
tileSize
Size in pixels of a tile used for detection. Note that tiles are independently normalized, and therefore tiling can impact the results. Default is 1024.- Parameters:
tileWidth
- the width of each tile for exporting imagestileHeight
- the height of each tile for exporting images- Returns:
- this builder
-
normalizePercentiles
Apply percentile normalization separately to the input image channels.Note that this can be used in combination with
preprocess(ImageOp...)
, in which case the order in which the operations are applied depends upon the order in which the methods of the builder are called.Warning! This is applied on a per-tile basis. This can result in artifacts and false detections without background/constant regions. Consider using
inputAdd(double...)
andinputScale(double...)
as alternative normalization strategies, if appropriate constants can be determined to apply globally.- Parameters:
min
- minimum percentilemax
- maximum percentile- Returns:
- this builder
- See Also:
-
normalizePercentiles
Apply percentile normalization to the input image channels, or across all channels jointly.Note that this can be used in combination with
preprocess(ImageOp...)
, in which case the order in which the operations are applied depends upon the order in which the methods of the builder are called.Warning! This is applied on a per-tile basis. This can result in artifacts and false detections without background/constant regions. Consider using
inputAdd(double...)
andinputScale(double...)
as alternative normalization strategies, if appropriate constants can be determined to apply globally.- Parameters:
min
- minimum percentilemax
- maximum percentileperChannel
- if true, normalize each channel separately; if false, normalize channels jointlyeps
- small constant to apply- Returns:
- this builder
- Since:
- v0.4.0
-
inputAdd
Add an offset as a preprocessing step. Usually the value will be negative. Along withinputScale(double...)
this can be used as an alternative (global) normalization.Note that this can be used in combination with
preprocess(ImageOp...)
, in which case the order in which the operations are applied depends upon the order in which the methods of the builder are called.- Parameters:
values
- either a single value to add to all channels, or an array of values equal to the number of channels- Returns:
- this builder
- See Also:
-
inputSubtract
Subtract an offset as a preprocessing step.Note that this can be used in combination with
preprocess(ImageOp...)
, in which case the order in which the operations are applied depends upon the order in which the methods of the builder are called.- Parameters:
values
- either a single value to subtract from all channels, or an array of values equal to the number of channels- Returns:
- this builder
- Since:
- v0.4.0
- See Also:
-
inputScale
Multiply by a scale factor as a preprocessing step. Along withinputAdd(double...)
this can be used as an alternative (global) normalization.Note that this can be used in combination with
preprocess(ImageOp...)
, in which case the order in which the operations are applied depends upon the order in which the methods of the builder are called.- Parameters:
values
- either a single value to add to all channels, or an array of values equal to the number of channels- Returns:
- this builder
- See Also:
-
addParameter
Generic means of adding a cellpose parameter- Parameters:
flagName
- the name of the flag, eg. "save_every"flagValue
- the value that is linked to the flag, eg. "20". Can be an empty string or null if it is not needed- Returns:
- this builder
- See Also:
-
- the cellpose documentation for a list of available flags
-
addParameter
Generic means of adding a cellpose parameter- Parameters:
flagName
- the name of the flag, eg. "save_every" * @param flagName the name of the flag, eg. "save_every"- Returns:
- See Also:
-
- the cellpose documentation for a list of available flags
-
useOmnipose
Use Omnipose implementation: Adds --omni flag to command- Returns:
- this builder
-
excludeEdges
Exclude on edges. Adds --exclude_on_edges flag to cellpose command- Returns:
- this builder
-
cellposeChannels
Explicitly set the cellpose channels manually. This corresponds to --chan and --chan2- Parameters:
channel1
- --chan value passed to cellpose/omniposechannel2
- --chan2 value passed to cellpose/omnipose- Returns:
- this builder
-
maskThreshold
Deprecated.cellprob threshold, default is 0, decrease to find more and larger masks- Parameters:
threshold
- cell/nuclei masks threshold, between -6 and +6- Returns:
- this builder
-
cellprobThreshold
/** cellprob threshold, default is 0, decrease to find more and larger masks- Parameters:
threshold
- cell/nuclei masks threshold, between -6 and +6- Returns:
- this builder
-
flowThreshold
Flow error threshold, 0 turns off this optional QC step. Default: 0.- Parameters:
threshold
- flow threshold (default 0.0)- Returns:
- this builder
-
diameter
The estimated diameter of the objects to detect. Cellpose will further downsample (or upsample) the images in order to match the diamteter corresponding to the model being used- Parameters:
diameter
- in pixels- Returns:
- this builder
-
modelDirectory
Define the directory where the newly trained model should be saved- Parameters:
modelDir
- a directory (does not need to exist yet) where to save the cellpose model- Returns:
- this Builder
-
epochs
Defines the number of epochs for training- Parameters:
nEpochs
- number of epochs for training- Returns:
- this Builder
-
learningRate
Defines the learning rate- Parameters:
learningRate
- learning rate per epoch- Returns:
- this Builder
-
batchSize
Defines the batch size for training- Parameters:
batchSize
- batch size for training- Returns:
- this Builder
-
minTrainMasks
Excludes training data with less than n training masks (n labels)- Parameters:
n
- minimum number of labels per training image- Returns:
- this builder
-
saveBuilder
Save this builder as a JSON file in order to be able to reuse it in place- Parameters:
name
- // A name to append to the JSON file. Keep it meaningful for your needs- Returns:
- this builder
-
setOverlap
Set the overlap (in pixels) between tiles. This overlap should be larger than 2x the largest object you are trying to segment- Parameters:
overlap
- the overlap, in pixels- Returns:
- this builder
-
normalizePercentilesGlobal
public CellposeBuilder normalizePercentilesGlobal(double percentileMin, double percentileMax, double normDownsample) Convenience method to call global normalization for the dataset- Parameters:
percentileMin
- the min percentile 0-100percentileMax
- the max percentile 0-100normDownsample
- a large downsample for the computation to be efficient over the whole image- Returns:
- this builder
-
noCellposeNormalization
convenience method? to deactivate cellpose normalization.- Returns:
- this builder
-
setOutputModelName
Set the final model name. Setting this to "My Model" would lead to the final cellpose file being called "My Model_yyyy-MM-dd_HH_mm.cpm" with the current timestamp and .cpm meaning Cellpose Model- Parameters:
outputName
- the prefix os the cellpose model name- Returns:
- this builder
-
build
Create aCellpose2D
, all ready for detection.- Returns:
- a new
Cellpose2D
instance
-
cellprobThreshold(Double)