CellposeModel#

class cellpose_omni.models.CellposeModel(gpu=False, pretrained_model=False, model_type=None, net_avg=True, use_torch=True, diam_mean=30.0, device=None, residual_on=True, style_on=True, concatenation=False, nchan=1, nclasses=2, dim=2, omni=True, checkpoint=False, dropout=False, kernel_size=2)[source]#

Bases: UnetModel

Parameters
  • gpu (bool (optional, default False)) -- whether or not to save model to GPU, will check if GPU available

  • pretrained_model (str or list of strings (optional, default False)) -- path to pretrained cellpose model(s), if None or False, no model loaded

  • model_type (str (optional, default None)) -- 'cyto'=cytoplasm model; 'nuclei'=nucleus model; if None, pretrained_model used

  • net_avg (bool (optional, default True)) -- loads the 4 built-in networks and averages them if True, loads one network if False

  • torch (bool (optional, default True)) -- use torch nn rather than mxnet

  • diam_mean (float (optional, default 27.)) -- mean 'diameter', 27. is built in value for 'cyto' model

  • device (mxnet device (optional, default None)) -- where model is saved (mx.gpu() or mx.cpu()), overrides gpu input, recommended if you want to use a specific GPU (e.g. mx.gpu(4))

  • model_dir (str (optional, default None)) -- overwrite the built in model directory where cellpose looks for models

  • omni (use omnipose model (optional, default False)) --

Methods Summary

eval(x[, batch_size, indices, channels, ...])

Evaluation for CellposeModel.

loss_fn(lbl, y)

loss function between true labels lbl and prediction y This is the one used to train the instance segmentation network.

train(train_data, train_labels[, ...])

train network with images train_data

Methods Documentation

eval(x, batch_size=8, indices=None, channels=None, channel_axis=None, z_axis=None, normalize=True, invert=False, rescale=None, diameter=None, do_3D=False, anisotropy=None, net_avg=True, augment=False, tile=True, tile_overlap=0.1, bsize=224, num_workers=8, resample=True, interp=True, cluster=False, suppress=None, boundary_seg=False, affinity_seg=False, despur=True, flow_threshold=0.4, mask_threshold=0.0, diam_threshold=12.0, niter=None, cellprob_threshold=None, dist_threshold=None, flow_factor=5.0, compute_masks=True, min_size=15, max_size=None, stitch_threshold=0.0, progress=None, show_progress=True, omni=False, calc_trace=False, verbose=False, transparency=False, loop_run=False, model_loaded=False, hysteresis=True)[source]#

Evaluation for CellposeModel. Segment list of images x, or 4D array - Z x nchan x Y x X

Parameters
  • x (list or array of images) -- can be list of 2D/3D/4D images, or array of 2D/3D/4D images

  • batch_size (int (optional, default 8)) -- number of 224x224 patches to run simultaneously on the GPU (can make smaller or bigger depending on GPU memory usage)

  • channels (list (optional, default None)) -- list of channels, either of length 2 or of length number of images by 2. First element of list is the channel to segment (0=grayscale, 1=red, 2=green, 3=blue). Second element of list is the optional nuclear channel (0=none, 1=red, 2=green, 3=blue). For instance, to segment grayscale images, input [0,0]. To segment images with cells in green and nuclei in blue, input [2,3]. To segment one grayscale image and one image with cells in green and nuclei in blue, input [[0,0], [2,3]].

  • channel_axis (int (optional, default None)) -- if None, channels dimension is attempted to be automatically determined

  • z_axis (int (optional, default None)) -- if None, z dimension is attempted to be automatically determined

  • normalize (bool (default, True)) -- normalize data so 0.0=1st percentile and 1.0=99th percentile of image intensities in each channel

  • invert (bool (optional, default False)) -- invert image pixel intensity before running network

  • rescale (float (optional, default None)) -- resize factor for each image, if None, set to 1.0

  • diameter (float (optional, default None)) -- diameter for each image (only used if rescale is None), if diameter is None, set to diam_mean

  • do_3D (bool (optional, default False)) -- set to True to run 3D segmentation on 4D image input

  • anisotropy (float (optional, default None)) -- for 3D segmentation, optional rescaling factor (e.g. set to 2.0 if Z is sampled half as dense as X or Y)

  • net_avg (bool (optional, default True)) -- runs the 4 built-in networks and averages them if True, runs one network if False

  • augment (bool (optional, default False)) -- tiles image with overlapping tiles and flips overlapped regions to augment

  • tile (bool (optional, default True)) -- tiles image to ensure GPU/CPU memory usage limited (recommended)

  • tile_overlap (float (optional, default 0.1)) -- fraction of overlap of tiles when computing flows

  • resample (bool (optional, default True)) -- run dynamics at original image size (will be slower but create more accurate boundaries)

  • interp (bool (optional, default True)) -- interpolate during 2D dynamics (not available in 3D) (in previous versions it was False)

  • flow_threshold (float (optional, default 0.4)) -- flow error threshold (all cells with errors below threshold are kept) (not used for 3D)

  • mask_threshold (float (optional, default 0.0)) -- all pixels with value above threshold kept for masks, decrease to find more and larger masks

  • dist_threshold (float (optional, default None) DEPRECATED) -- use mask_threshold instead

  • cellprob_threshold (float (optional, default None) DEPRECATED) -- use mask_threshold instead

  • compute_masks (bool (optional, default True)) -- Whether or not to compute dynamics and return masks. This is set to False when retrieving the styles for the size model.

  • min_size (int (optional, default 15)) -- minimum number of pixels per mask, can turn off with -1

  • stitch_threshold (float (optional, default 0.0)) -- if stitch_threshold>0.0 and not do_3D, masks are stitched in 3D to return volume segmentation

  • progress (pyqt progress bar (optional, default None)) -- to return progress bar status to GUI

  • omni (bool (optional, default False)) -- use omnipose mask reconstruction features

  • calc_trace (bool (optional, default False)) -- calculate pixel traces and return as part of the flow

  • verbose (bool (optional, default False)) -- turn on additional output to logs for debugging

  • transparency (bool (optional, default False)) -- modulate flow opacity by magnitude instead of brightness (can use flows on any color background)

  • loop_run (bool (optional, default False)) -- internal variable for determining if model has been loaded, stops model loading in loop over images

  • model_loaded (bool (optional, default False)) -- internal variable for determining if model has been loaded, used in __main__.py

Returns

  • masks (list of 2D arrays, or single 3D array (if do_3D=True)) -- labelled image, where 0=no masks; 1,2,...=mask labels

  • flows (list of lists 2D arrays, or list of 3D arrays (if do_3D=True)) -- flows[k][0] = 8-bit RGb phase plot of flow field flows[k][1] = flows at each pixel flows[k][2] = scalar cell probability (Cellpose) or distance transform (Omnipose) flows[k][3] = boundary output (nonempty for Omnipose) flows[k][4] = final pixel locations after Euler integration flows[k][5] = pixel traces (nonempty for calc_trace=True)

  • styles (list of 1D arrays of length 64, or single 1D array (if do_3D=True)) -- style vector summarizing each image, also used to estimate size of objects in image

loss_fn(lbl, y)[source]#

loss function between true labels lbl and prediction y This is the one used to train the instance segmentation network.

train(train_data, train_labels, train_links=None, train_files=None, test_data=None, test_labels=None, test_links=None, test_files=None, channels=None, channel_axis=0, normalize=True, save_path=None, save_every=100, save_each=False, learning_rate=0.2, n_epochs=500, momentum=0.9, SGD=True, weight_decay=1e-05, batch_size=8, dataloader=False, num_workers=0, nimg_per_epoch=None, rescale=True, min_train_masks=5, netstr=None, tyx=None, timing=False, do_autocast=False, affinity_field=False)[source]#

train network with images train_data

Parameters
  • train_data (list of arrays (2D or 3D)) -- images for training

  • train_labels (list of arrays (2D or 3D)) -- labels for train_data, where 0=no masks; 1,2,...=mask labels can include flows as additional images

  • train_links (list of label links) -- These lists of label pairs define which labels are "linked", i.e. should be treated as part of the same object. This is how Omnipose handles internal/self-contact boundaries during training.

  • train_files (list of strings) -- file names for images in train_data (to save flows for future runs)

  • test_data (list of arrays (2D or 3D)) -- images for testing

  • test_labels (list of arrays (2D or 3D)) -- See train_labels.

  • test_links (list of label links) -- See train_links.

  • test_files (list of strings) -- file names for images in test_data (to save flows for future runs)

  • channels (list of ints (default, None)) -- channels to use for training

  • normalize (bool (default, True)) -- normalize data so 0.0=1st percentile and 1.0=99th percentile of image intensities in each channel

  • save_path (string (default, None)) -- where to save trained model, if None it is not saved

  • save_every (int (default, 100)) -- save network every [save_every] epochs

  • learning_rate (float or list/np.ndarray (default, 0.2)) -- learning rate for training, if list, must be same length as n_epochs

  • n_epochs (int (default, 500)) -- how many times to go through whole training set during training

  • weight_decay (float (default, 0.00001)) --

  • SGD (bool (default, True)) -- use SGD as optimization instead of RAdam

  • batch_size (int (optional, default 8)) -- number of tyx-sized patches to run simultaneously on the GPU (can make smaller or bigger depending on GPU memory usage)

  • nimg_per_epoch (int (optional, default None)) -- minimum number of images to train on per epoch, with a small training set (< 8 images) it may help to set to 8

  • rescale (bool (default, True)) -- whether or not to rescale images to diam_mean during training, if True it assumes you will fit a size model after training or resize your images accordingly, if False it will try to train the model to be scale-invariant (works worse)

  • min_train_masks (int (default, 5)) -- minimum number of masks an image must have to use in training set

  • netstr (str (default, None)) -- name of network, otherwise saved with name as params + training start time

  • tyx (int, tuple (default, 224x224 in 2D)) -- size of image patches used for training