deepcell.utils
Deepcell Utilities Module
backbone_utils
Functions for creating model backbones
- deepcell.utils.backbone_utils.featurenet_3D_backbone(input_tensor=None, input_shape=None, n_filters=32, **kwargs)[source]
Construct the deepcell backbone with five convolutional units
- deepcell.utils.backbone_utils.featurenet_3D_block(x, n_filters)[source]
Add a set of layers that make up one unit of the featurenet 3D backbone
- Parameters:
x (tensorflow.keras.Layer) – Keras layer object to pass to backbone unit
n_filters (int) – Number of filters to use for convolutional layers
- Returns:
Keras layer object
- Return type:
tensorflow.keras.Layer
- deepcell.utils.backbone_utils.featurenet_backbone(input_tensor=None, input_shape=None, n_filters=32, **kwargs)[source]
Construct the deepcell backbone with five convolutional units
- deepcell.utils.backbone_utils.featurenet_block(x, n_filters)[source]
Add a set of layers that make up one unit of the featurenet backbone
- Parameters:
x (tensorflow.keras.Layer) – Keras layer object to pass to backbone unit
n_filters (int) – Number of filters to use for convolutional layers
- Returns:
Keras layer object
- Return type:
tensorflow.keras.Layer
- deepcell.utils.backbone_utils.get_backbone(backbone, input_tensor=None, input_shape=None, use_imagenet=False, return_dict=True, frames_per_batch=1, **kwargs)[source]
Retrieve backbones for the construction of feature pyramid networks.
- Parameters:
backbone (str) – Name of the backbone to be retrieved.
input_tensor (tensor) – The input tensor for the backbone. Should have channel dimension of size 3
use_imagenet (bool) – Load pre-trained weights for the backbone
return_dict (bool) – Whether to return a dictionary of backbone layers, e.g.
{'C1': C1, 'C2': C2, 'C3': C3, 'C4': C4, 'C5': C5}
. If false, the whole model is returned insteadkwargs (dict) – Keyword dictionary for backbone constructions. Relevant keys include
'include_top'
,'weights'
(should beNone
),'input_shape'
, and'pooling'
.
- Returns:
An instantiated backbone
- Return type:
tensorflow.keras.Model
- Raises:
ValueError – bad backbone name
ValueError – featurenet backbone with pre-trained imagenet
data_utils
Functions for making training data
- deepcell.utils.data_utils.get_data(file_name, mode='sample', test_size=0.2, seed=0)[source]
Load data from NPZ file and split into train and test sets
- Parameters:
file_name (str) – path to NPZ file to load
mode (str) – if ‘siamese_daughters’, returns lineage information from .trk file otherwise, returns the same data that was loaded.
test_size (float) – percent of data to leave as testing holdout
seed (int) – seed number for random train/test split repeatability
- Returns:
dict of training data, and a dict of testing data
- Return type:
- deepcell.utils.data_utils.get_max_sample_num_list(y, edge_feature, output_mode='sample', padding='valid', window_size_x=30, window_size_y=30)[source]
For each set of images and each feature, find the maximum number of samples for to be used. This will be used to balance class sampling.
- Parameters:
- Returns:
list of maximum sample size for all classes
- Return type:
- deepcell.utils.data_utils.relabel_movie(y)[source]
Relabels unique instance IDs to be from 1 to N
- Parameters:
y (numpy.array) – tensor of integer labels
- Returns:
relabeled tensor with sequential labels
- Return type:
numpy.array
- deepcell.utils.data_utils.reshape_matrix(X, y, reshape_size=256)[source]
Reshape matrix of dimension 4 to have x and y of size reshape_size. Adds overlapping slices to batches. E.g.
reshape_size
of 256 yields (1, 1024, 1024, 1) -> (16, 256, 256, 1) The input image is divided into subimages of side length reshape_size, with the last row and column of subimages overlapping the one before the last if the original image side lengths are not divisible byreshape_size
.- Parameters:
X (numpy.array) – raw 4D image tensor
y (numpy.array) – label mask of 4D image data
reshape_size (int, list) – size of the output tensor If input is int, output images are square with side length equal reshape_size. If it is a list of 2 ints, then the output images size is reshape_size[0] x reshape_size[1]
- Returns:
reshaped
X
andy
4D tensors inshape[1:3] = (reshape_size, reshape_size)
, ifreshape_size
is anint
, andshape[1:3] = reshape_size
, ifreshape_size
is a list of length 2- Return type:
numpy.array
- Raises:
ValueError –
X.ndim
is not 4ValueError –
y.ndim
is not 4
- deepcell.utils.data_utils.reshape_movie(X, y, reshape_size=256)[source]
Reshape tensor of dimension 5 to have x and y of size
reshape_size
. Adds overlapping slices to batches. E.g.reshape_size
of 256 yields(1, 5, 1024, 1024, 1) -> (16, 5, 256, 256, 1)
- Parameters:
X (numpy.array) – raw 5D image tensor
y (numpy.array) – label mask of 5D image tensor
reshape_size (int) – size of the square output tensor
- Returns:
reshaped
X
andy
tensors in shape(reshape_size, reshape_size)
- Return type:
numpy.array
- Raises:
ValueError –
X.ndim
is not 5ValueError –
y.ndim
is not 5
- deepcell.utils.data_utils.sample_label_matrix(y, window_size=(30, 30), padding='valid', max_training_examples=10000000.0, data_format=None)[source]
Sample a 4D Tensor, creating many small images of shape window_size.
- Parameters:
- Returns:
4 arrays of coordinates of each sampled pixel
- Return type:
- deepcell.utils.data_utils.sample_label_movie(y, window_size=(30, 30, 5), padding='valid', max_training_examples=10000000.0, data_format=None)[source]
Sample a 5D Tensor, creating many small voxels of shape window_size.
- Parameters:
- Returns:
5 arrays of coordinates of each sampled pixel
- Return type:
- deepcell.utils.data_utils.trim_padding(nparr, win_x, win_y, win_z=None)[source]
Trim the boundaries of the numpy array to allow for a sliding window of size (win_x, win_y) to not slide over regions without pixel data
- Parameters:
- Returns:
trimmed numpy array of size
x - 2 * win_x - 1, y - 2 * win_y - 1
- Return type:
numpy.array
- Raises:
ValueError – nparr.ndim is not 4 or 5
export_utils
Save Keras models as a SavedModel for TensorFlow Serving
- deepcell.utils.export_utils.export_model(keras_model, export_path, model_version=0, weights_path=None, include_optimizer=True, overwrite=True, save_format='tf')[source]
Export a model for use with TensorFlow Serving.
DEPRECATED:
tf.keras.models.save_model
is preferred.- Parameters:
keras_model (tensorflow.keras.Model) – Instantiated Keras model.
export_path (str) – Destination to save the exported model files.
model_version (int) – Integer version of the model.
weights_path (str) – Path to a
.h5
or.tf
weights file.include_optimizer (bool) – Whether to export the optimizer.
overwrite (bool) – Whether to overwrite any existing files in
export_path
.save_format (str) – Saved model format, one of
'tf'
or'h5'
.
- deepcell.utils.export_utils.export_model_to_tflite(model_file, export_path, calibration_images, norm=True, location=True, file_name='model.tflite')[source]
Export a saved keras model to tensorflow-lite with int8 precision.
Deprecated since version 0.12.4: The
export_model_to_tflite
function is deprecated and will be removed in 0.13. Usetf.keras.models.save_model
instead.This export function has only been tested with
PanopticNet
models. For the export to be successful, thePanopticNet
model must havenorm_method
set toNone
,location
set toFalse
, and the upsampling layers must usebilinear
interpolation.- Parameters:
model_file (str) – Path to saved model file
export_path (str) – Directory to save the exported tflite model
calibration_images (numpy.array) – Array of images used for calibration during model quantization
norm (bool) – Whether to normalize calibration images.
location (bool) – Whether to append a location image to calibration images.
file_name (str) – File name for the exported model. Defaults to ‘model.tflite’
io_utils
Utilities for reading/writing files
- deepcell.utils.io_utils.get_image(file_name)[source]
DEPRECATED. Use
skimage.io.imread
instead.Read image from file and returns it as a tensor.
- Parameters:
file_name (str) – path to image file
- Returns:
numpy array of image data
- Return type:
numpy.array
misc_utils
Miscellaneous utility functions
plot_utils
Utilities plotting data
- deepcell.utils.plot_utils.cf(x_coord, y_coord, sample_image)[source]
Format x and y coordinates for printing
- deepcell.utils.plot_utils.create_rgb_image(input_data, channel_colors)[source]
Takes a stack of 1- or 2-channel data and converts it to an RGB image
- Parameters:
input_data – 4D stack of images to be converted to RGB
channel_colors – list specifying the color for each channel
- Returns:
transformed version of input data into RGB version
- Return type:
numpy.array
- Raises:
ValueError – if
len(channel_colors)
is not equal to number of channelsValueError – if invalid
channel_colors
providedValueError – if input_data is not 4D, with 1 or 2 channels
- deepcell.utils.plot_utils.get_js_video(images, batch=0, channel=0, cmap='jet', vmin=0, vmax=0, interval=200, repeat_delay=1000)[source]
Create a JavaScript video as HTML for visualizing 3D data as a movie.
- Parameters:
- Returns:
JS HTML to display video
- Return type:
- deepcell.utils.plot_utils.make_outline_overlay(rgb_data, predictions)[source]
Overlay a segmentation mask with image data for easy visualization
- Parameters:
rgb_data – 3 channel array of images, output of
create_rgb_data
predictions – segmentation predictions to be visualized
- Returns:
overlay image of input data and predictions
- Return type:
numpy.array
- Raises:
ValueError – If predictions are not 4D
ValueError – If there is not matching RGB data for each prediction
- deepcell.utils.plot_utils.plot_error(loss_hist_file, saved_direc, plot_name)[source]
Plot the training and validation error from the npz file
tracking_utils
Utilities for tracking cells
train_utils
Utilities for training neural nets
- deepcell.utils.train_utils.count_gpus()[source]
Get the number of available GPUs.
- Returns:
count of GPUs as integer
- Return type:
- deepcell.utils.train_utils.get_callbacks(model_path, save_weights_only=False, lr_sched=None, tensorboard_log_dir=None, reduce_lr_on_plateau=False, monitor='val_loss', verbose=1)[source]
Returns a list of callbacks used for training
- Parameters:
model_path – (str) path for the
h5
model file.save_weights_only – (bool) if True, then only the model’s weights will be saved.
lr_sched (function) – learning rate scheduler per epoch. from
rate_scheduler
.tensorboard_log_dir (str) – log directory for tensorboard.
monitor (str) – quantity to monitor.
verbose (int) – verbosity mode, 0 or 1.
- Returns:
a list of callbacks to be passed to
model.fit()
- Return type:
transform_utils
Utilities for data transformations
- deepcell.utils.transform_utils.inner_distance_transform_2d(mask, bins=None, erosion_width=None, alpha=0.1, beta=1)[source]
Transform a label mask with an inner distance transform.
inner_distance = 1 / (1 + beta * alpha * distance_to_center)
- Parameters:
mask (numpy.array) – A label mask (
y
data).bins (int) – The number of transformed distance classes.
erosion_width (int) – number of pixels to erode edges of each labels
alpha (float, str) – coefficent to reduce the magnitude of the distance value. If “auto”, determines
alpha
for each cell based on the cell area.beta (float) – scale parameter that is used when
alpha
is “auto”.
- Returns:
a mask of same shape as input mask, with each label being a distance class from 1 to
bins
.- Return type:
numpy.array
- Raises:
ValueError –
alpha
is a string but not set to “auto”.
- deepcell.utils.transform_utils.inner_distance_transform_3d(mask, bins=None, erosion_width=None, alpha=0.1, beta=1, sampling=[0.5, 0.217, 0.217])[source]
Transform a label mask for a z-stack with an inner distance transform.
inner_distance = 1 / (1 + beta * alpha * distance_to_center)
- Parameters:
mask (numpy.array) – A label mask (
y
data).bins (int) – The number of transformed distance classes.
erosion_width (int) – Number of pixels to erode edges of each labels
alpha (float, str) – Coefficent to reduce the magnitude of the distance value. If
'auto'
, determines alpha for each cell based on the cell area.beta (float) – Scale parameter that is used when
alpha
is “auto”.sampling (list) – Spacing of pixels along each dimension.
- Returns:
A mask of same shape as input mask, with each label being a distance class from 1 to
bins
.- Return type:
numpy.array
- Raises:
ValueError –
alpha
is a string but not set to “auto”.
- deepcell.utils.transform_utils.inner_distance_transform_movie(mask, bins=None, erosion_width=None, alpha=0.1, beta=1)[source]
Transform a label mask with an inner distance transform. Applies the 2D transform to each frame.
- Parameters:
mask (numpy.array) – A label mask (
y
data).bins (int) – The number of transformed distance classes.
erosion_width (int) – Number of pixels to erode edges of each labels.
alpha (float, str) – Coefficent to reduce the magnitude of the distance value. If “auto”, determines
alpha
for each cell based on the cell area.beta (float) – Scale parameter that is used when
alpha
is “auto”.
- Returns:
A mask of same shape as input mask, with each label being a distance class from 1 to
bins
.- Return type:
numpy.array
- Raises:
ValueError –
alpha
is a string but not set to “auto”.
- deepcell.utils.transform_utils.outer_distance_transform_2d(mask, bins=None, erosion_width=None, normalize=True)[source]
Transform a label mask with an outer distance transform.
- Parameters:
mask (numpy.array) – A label mask (
y
data).bins (int) – The number of transformed distance classes. If
None
, returns the continuous outer transform.erosion_width (int) – Number of pixels to erode edges of each labels
normalize (bool) – Normalize the transform of each cell by that cell’s largest distance.
- Returns:
A mask of same shape as input mask, with each label being a distance class from 1 to
bins
.- Return type:
numpy.array
- deepcell.utils.transform_utils.outer_distance_transform_3d(mask, bins=None, erosion_width=None, normalize=True, sampling=[0.5, 0.217, 0.217])[source]
Transforms a label mask for a z stack with an outer distance transform. Uses scipy’s distance_transform_edt
- Parameters:
mask (numpy.array) – A z-stack of label masks (
y
data).bins (int) – The number of transformed distance classes.
erosion_width (int) – Number of pixels to erode edges of each labels.
normalize (bool) – Normalize the transform of each cell by that cell’s largest distance.
sampling (list) – Spacing of pixels along each dimension.
- Returns:
3D Euclidiean Distance Transform
- Return type:
numpy.array
- deepcell.utils.transform_utils.outer_distance_transform_movie(mask, bins=None, erosion_width=None, normalize=True)[source]
Transform a label mask for a movie with an outer distance transform. Applies the 2D transform to each frame.
- Parameters:
- Returns:
a mask of same shape as input mask, with each label being a distance class from 1 to
bins
- Return type:
numpy.array
- deepcell.utils.transform_utils.pixelwise_transform(mask, dilation_radius=None, data_format=None, separate_edge_classes=False)[source]
Transforms a label mask for a z stack edge, interior, and background
- Parameters:
mask (numpy.array) – tensor of labels
dilation_radius (int) – width to enlarge the edge feature of each instance
data_format (str) – A string, one of
channels_last
(default) orchannels_first
. The ordering of the dimensions in the inputs.channels_last
corresponds to inputs with shape(batch, height, width, channels)
whilechannels_first
corresponds to inputs with shape(batch, channels, height, width)
.separate_edge_classes (bool) – Whether to separate the cell edge class into 2 distinct cell-cell edge and cell-background edge classes.
- Returns:
An array with the same shape as
mask
, except the channel axis will be a one-hot encoded semantic segmentation for 3 main features:[cell_edge, cell_interior, background]
. Ifseparate_edge_classes
isTrue
, thecell_interior
feature is split into 2 features and the resulting channels are:[bg_cell_edge, cell_cell_edge, cell_interior, background]
.- Return type:
numpy.array