aksg87
(Akshay Goel)
August 22, 2019, 5:23am
1
I tried checking the dice function on a single mask with itself and it’s not returning the expected value 1.0. Please let me know if I’m missing something? You can see the output below. Thanks!
1 Like
jcatanza
(Joseph Catanzarite)
August 24, 2019, 2:15am
2
It’s not clear to me that fastai implements the dice and iou metrics the way they are defined…
dice = \frac{2(TP)}{2(TP) + FP + FN}
iou = \frac{ intersection}{union}
def foreground_acc(input, target, void_code):
"Computes non-background accuracy, e.g. camvid for multiclass segmentation"
target = target.squeeze(1)
mask = target != void_code
return (input.argmax(dim=1)[mask]==target[mask]).float().mean()
def error_rate(input:Tensor, targs:Tensor)->Rank0Tensor:
"1 - `accuracy`"
return 1 - accuracy(input, targs)
def dice(input:Tensor, targs:Tensor, iou:bool=False, eps:float=1e-8)->Rank0Tensor:
"Dice coefficient metric for binary target. If iou=True, returns iou metric, classic for segmentation problems."
n = targs.shape[0]
input = input.argmax(dim=1).view(n,-1)
targs = targs.view(n,-1)
intersect = (input * targs).sum(dim=1).float()
union = (input+targs).sum(dim=1).float()
if not iou: l = 2. * intersect / union
else: l = intersect / (union-intersect+eps)
l[union == 0.] = 1.
return l.mean()
1 Like