--- title: Metrics keywords: fastai sidebar: home_sidebar summary: "Definition of the metrics that can be used in training models. See fastai2 docs for more information." description: "Definition of the metrics that can be used in training models. See fastai2 docs for more information." nb_path: "nbs/03_metrics.ipynb" ---
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 = TensorImage(torch.randn(20,2,3,3))
x2 = TensorMask(torch.randint(0, 2, (20, 3, 3)))
pred = x1.argmax(1)
inter = (pred*x2).float().sum().item()
union = (pred+x2).float().sum().item()
test_eq(compute_val(Dice_f1(), x1, x2), 2*inter/union)
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()
test_eq(compute_val(Iou(), x1, x2), inter/(union-inter))