multilabel_dice_coefficient: Dice function for multilabel segmentation problems

View source: R/customMetrics.R

multilabel_dice_coefficientR Documentation

Dice function for multilabel segmentation problems

Description

Note: Assumption is that y_true is a one-hot representation of the segmentation batch. The background (label 0) should be included but is not used in the calculation.

Usage

multilabel_dice_coefficient(
  y_true,
  y_pred,
  dimensionality = 3L,
  smoothingFactor = 0
)

Arguments

y_true

True labels (Tensor)

y_pred

Predictions (Tensor of the same shape as y_true)

dimensionality

image dimension.

smoothingFactor

parameter for smoothing the metric.

Value

Dice value (negative)

Author(s)

Tustison NJ

Examples


library( ANTsR )
library( ANTsRNet )
library( keras )

model <- createUnetModel2D( c( 64, 64, 1 ) )

dice_loss <- multilabel_dice_coefficient( smoothingFactor = 0.1 )

model %>% compile( loss = dice_loss,
  optimizer = optimizer_adam( lr = 0.0001 ) )

########################################
#
# Run in isolation
#

library( ANTsR )

r16 <- antsImageRead( getANTsRData( "r16" ) )
r16seg <- kmeansSegmentation( r16, 3 )$segmentation
r16array <- array( data = as.array( r16seg ), dim = c( 1, dim( r16seg ) ) )
r16tensor <- tensorflow::tf$convert_to_tensor( encodeUnet( r16array, c( 0, 1, 2, 3 ) ) )

r64 <- antsImageRead( getANTsRData( "r64" ) )
r64seg <- kmeansSegmentation( r64, 3 )$segmentation
r64array <- array( data = as.array( r64seg ), dim = c( 1, dim( r64seg ) ) )
r64tensor <- tensorflow::tf$convert_to_tensor( encodeUnet( r64array, c( 0, 1, 2, 3 ) ) )

dice_loss <- multilabel_dice_coefficient( r16tensor, r64tensor, dimensionality = 2L )
loss_value <- dice_loss( r16tensor, r64tensor )$numpy()

# Compare with
# overlap_value <- labelOverlapMeasures( r16seg, r64seg )$MeanOverlap[1]

rm(model); gc()

ANTsX/ANTsRNet documentation built on March 27, 2024, 4:26 a.m.