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.
@delegates()
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):
met.reset()
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]],)
met.accumulate(learn)
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)
test_eq(compute_val(Iou(), x1, x2), inter/(union-inter))