dcp_server package

The dcp_server package is structured to handle various server-side functionalities related model serving for segmentation and training.

dcp_server.models

Defines various models for cell classification and segmentation, currently CustomCellposeModel. These models handle tasks such as evaluation, forward pass, and updating configurations.

dcp_server.segmentationclasses

Defines segmentation classes for specific projects, such as GFPProjectSegmentation, GeneralSegmentation, and MitoProjectSegmentation. These classes contain methods for segmenting images and training models on images and masks.

dcp_server.serviceclasses

Defines service classes, such as CustomBentoService and CustomRunnable, for serving the models with BentoML and handling computation on remote Python workers.

dcp_server.utils

Provides various utility functions for dealing with image storage, image processing, feature extraction, file handling, configuration reading, and path manipulation.

Submodules

dcp_server.models module

class dcp_server.models.CustomCellpose(model_name: str, model_config: dict, data_config: dict, eval_config: dict)

Bases: CellposeModel, Model

Custom cellpose model inheriting the attributes and functions from the original CellposeModel and implementing additional attributes and methods needed for this project.

compute_masks_flows(imgs: List[ndarray], masks: List[ndarray]) tuple

Computes instance, binary mask and flows in x and y - needed for loss and metric computations

Parameters:
  • imgs (List[np.ndarray]) – images to process

  • masks (List[np.ndarray]) – masks of the given images

Returns:

A tuple containing the following elements: - pred_masks List [np.ndarray]: A list of predicted instance masks - pred_flows (torch.Tensor): A tensor holding the stacked predicted cell probability map, horizontal and vertical flows for all images - true_lbl (np.ndarray): A numpy array holding the stacked true binary mask, horizontal and vertical flows for all images

Return type:

tuple

eval(img: ndarray) ndarray

Evaluate the model - find mask of the given image Calls the original eval function.

Parameters:

img (np.ndarray) – image to evaluate on

Returns:

mask of the image, list of 2D arrays, or single 3D array (if do_3D=True) labelled image.

Return type:

np.ndarray

eval_all_outputs(img: ndarray) tuple

Get all outputs of the model when running eval.

Parameters:

img (numpy.ndarray) – Input image for segmentation.

Returns:

mask, flows, styles etc. Returns the same as cellpose.models.CellposeModel.eval - see Cellpose API Guide for more details.

Return type:

tuple

masks_to_outlines(mask: ndarray) ndarray

get outlines of masks as a 0-1 array Calls the original cellpose.utils.masks_to_outlines function

Parameters:

mask (ndarray) – int, 2D or 3D array, mask of an image

Returns:

outlines

Return type:

ndarray

dcp_server.segmentationclasses module

class dcp_server.segmentationclasses.GeneralSegmentation(imagestorage: FilesystemImageStorage, runner, model: CustomCellpose)

Bases: object

Segmentation class. Defining the main functions needed for this project and served by service - segment image and train on images.

async segment_image(image: ndarray[Any, dtype[_ScalarType_co]]) ndarray[Any, dtype[_ScalarType_co]]

Segments a single pre-loaded image.

Parameters:

image (NDArray) – Pre-loaded image as numpy array

Returns:

Segmentation mask

Return type:

NDArray

dcp_server.serviceclasses module

class dcp_server.serviceclasses.CustomRunnable(name: str, model: CustomCellpose)

Bases: object

BentoML, Runner represents a unit of computation that can be executed on a remote Python worker and scales independently. CustomRunnable is a custom runner defined to meet all the requirements needed for this project.

SUPPORTED_RESOURCES = ('cpu', 'nvidia.com/gpu')
SUPPORTS_CPU_MULTI_THREADING = False
async evaluate(img: ndarray) ndarray

Evaluate the model - find mask of the given image

Parameters:
  • img (np.ndarray) – image to evaluate on

  • z_axis (int) – z dimension (optional, default is None)

Returns:

mask of the image, list of 2D arrays, or single 3D array (if do_3D=True) labelled image.

Return type:

np.ndarray

dcp_server.utils module