monai.networks.utils module

Utilities and types for defining networks, these depend on PyTorch.

one_hot(labels, num_classes)[source]

For a tensor labels of dimensions B1[spatial_dims], return a tensor of dimensions BN[spatial_dims] for num_classes N number of classes.

Example

For every value v = labels[b,1,h,w], the value in the result at [b,v,h,w] will be 1 and all others 0. Note that this will include the background label, thus a binary mask should be treated as having 2 classes.

predict_segmentation(logits)[source]

Given the logits from a network, computing the segmentation by thresholding all values above 0 if logits has one channel, or computing the argmax along the channel axis otherwise, logits has shape BCHW[D]

slice_channels(tensor, *slicevals)[source]