Definition of the metrics that can be used in training models. See fastai2 docs for more information.

Dice coefficient

see here for F1/Dice score discussion.

class Dice[source]

Dice(axis=1) :: Dice

Dice coefficient metric for binary target in segmentation

class TstLearner(Learner):
    def __init__(self,dls=None,model=None,**kwargs): self.pred,self.xb,self.yb = None,None,None    
def compute_val(met, x1, x2):
    vals = [0,6,15,20]
    learn = TstLearner()
    for i in range(3): 
        learn.pred,learn.yb = x1[vals[i]:vals[i+1]],(x2[vals[i]:vals[i+1]],)
    return met.value
x1 = torch.randn(20,2,3,3)
x2 = torch.randint(0, 2, (20, 3, 3))
pred = x1.argmax(1)
inter = (pred*x2).float().sum().item()
union = (pred+x2).float().sum().item()
x1 = TensorImage(x1)
x2 = TensorMask(x2)
test_eq(compute_val(Dice(), x1, x2), 2*inter/union)


class Iou[source]

Iou(axis=1) :: Dice

Implemetation of the IoU (jaccard coefficient) that is lighter in RAM

test_eq(compute_val(Iou(), x1, x2), inter/(union-inter))

Patch to show metrics


Recorder.plot_metrics(nrows=None, ncols=None, figsize=None, imsize=3, suptitle=None, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None)